cx.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. */
  9. /**
  10. * This function is used to mark string literals representing CSS class names
  11. * so that they can be transformed statically. This allows for modularization
  12. * and minification of CSS class names.
  13. *
  14. * In static_upstream, this function is actually implemented, but it should
  15. * eventually be replaced with something more descriptive, and the transform
  16. * that is used in the main stack should be ported for use elsewhere.
  17. *
  18. * @param string|object className to modularize, or an object of key/values.
  19. * In the object case, the values are conditions that
  20. * determine if the className keys should be included.
  21. * @param [string ...] Variable list of classNames in the string case.
  22. * @return string Renderable space-separated CSS className.
  23. */
  24. function cx(classNames) {
  25. if (typeof classNames == 'object') {
  26. return Object.keys(classNames).filter(function (className) {
  27. return classNames[className];
  28. }).map(replace).join(' ');
  29. }
  30. return Array.prototype.map.call(arguments, replace).join(' ');
  31. }
  32. function replace(str) {
  33. return str.replace(/\//g, '-');
  34. }
  35. module.exports = cx;