Utility.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Generated by CoffeeScript 1.10.0
  2. (function() {
  3. var assign, camelCase, capitalize, isArray, isEmpty, isFunction, isObject, isPlainObject, kebabCase, snakeCase, titleCase, words,
  4. slice = [].slice,
  5. hasProp = {}.hasOwnProperty;
  6. assign = function() {
  7. var i, key, len, source, sources, target;
  8. target = arguments[0], sources = 2 <= arguments.length ? slice.call(arguments, 1) : [];
  9. if (isFunction(Object.assign)) {
  10. Object.assign.apply(null, arguments);
  11. } else {
  12. for (i = 0, len = sources.length; i < len; i++) {
  13. source = sources[i];
  14. if (source != null) {
  15. for (key in source) {
  16. if (!hasProp.call(source, key)) continue;
  17. target[key] = source[key];
  18. }
  19. }
  20. }
  21. }
  22. return target;
  23. };
  24. isFunction = function(val) {
  25. return !!val && Object.prototype.toString.call(val) === '[object Function]';
  26. };
  27. isObject = function(val) {
  28. var ref;
  29. return !!val && ((ref = typeof val) === 'function' || ref === 'object');
  30. };
  31. isArray = function(val) {
  32. if (isFunction(Array.isArray)) {
  33. return Array.isArray(val);
  34. } else {
  35. return Object.prototype.toString.call(val) === '[object Array]';
  36. }
  37. };
  38. isEmpty = function(val) {
  39. var key;
  40. if (isArray(val)) {
  41. return !val.length;
  42. } else {
  43. for (key in val) {
  44. if (!hasProp.call(val, key)) continue;
  45. return false;
  46. }
  47. return true;
  48. }
  49. };
  50. isPlainObject = function(val) {
  51. var ctor, proto;
  52. return isObject(val) && (proto = Object.getPrototypeOf(val)) && (ctor = proto.constructor) && (typeof ctor === 'function') && (ctor instanceof ctor) && (Function.prototype.toString.call(ctor) === Function.prototype.toString.call(Object));
  53. };
  54. words = function(val) {
  55. return (val.split(/[-_\s]+|(?=[A-Z][a-z])/) || []).filter(function(n) {
  56. return !!n;
  57. });
  58. };
  59. camelCase = function(val) {
  60. var i, index, len, r, ref, word;
  61. r = '';
  62. ref = words(val);
  63. for (index = i = 0, len = ref.length; i < len; index = ++i) {
  64. word = ref[index];
  65. r += index ? capitalize(word.toLowerCase()) : word.toLowerCase();
  66. }
  67. return r;
  68. };
  69. titleCase = function(val) {
  70. var i, index, len, r, ref, word;
  71. r = '';
  72. ref = words(val);
  73. for (index = i = 0, len = ref.length; i < len; index = ++i) {
  74. word = ref[index];
  75. r += capitalize(word.toLowerCase());
  76. }
  77. return r;
  78. };
  79. kebabCase = function(val) {
  80. var i, index, len, r, ref, word;
  81. r = '';
  82. ref = words(val);
  83. for (index = i = 0, len = ref.length; i < len; index = ++i) {
  84. word = ref[index];
  85. r += (index ? '-' : '') + word.toLowerCase();
  86. }
  87. return r;
  88. };
  89. snakeCase = function(val) {
  90. var i, index, len, r, ref, word;
  91. r = '';
  92. ref = words(val);
  93. for (index = i = 0, len = ref.length; i < len; index = ++i) {
  94. word = ref[index];
  95. r += (index ? '_' : '') + word.toLowerCase();
  96. }
  97. return r;
  98. };
  99. capitalize = function(val) {
  100. return val.charAt(0).toUpperCase() + val.slice(1);
  101. };
  102. module.exports.assign = assign;
  103. module.exports.isFunction = isFunction;
  104. module.exports.isObject = isObject;
  105. module.exports.isArray = isArray;
  106. module.exports.isEmpty = isEmpty;
  107. module.exports.isPlainObject = isPlainObject;
  108. module.exports.camelCase = camelCase;
  109. module.exports.titleCase = titleCase;
  110. module.exports.kebabCase = kebabCase;
  111. module.exports.snakeCase = snakeCase;
  112. module.exports.capitalize = capitalize;
  113. module.exports.words = words;
  114. }).call(this);