humps.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // =========
  2. // = humps =
  3. // =========
  4. // Underscore-to-camelCase converter (and vice versa)
  5. // for strings and object keys
  6. // humps is copyright © 2012+ Dom Christie
  7. // Released under the MIT license.
  8. ;(function(global) {
  9. var _processKeys = function(convert, obj, options) {
  10. if(!_isObject(obj) || _isDate(obj) || _isRegExp(obj) || _isBoolean(obj) || _isFunction(obj)) {
  11. return obj;
  12. }
  13. var output,
  14. i = 0,
  15. l = 0;
  16. if(_isArray(obj)) {
  17. output = [];
  18. for(l=obj.length; i<l; i++) {
  19. output.push(_processKeys(convert, obj[i], options));
  20. }
  21. }
  22. else {
  23. output = {};
  24. for(var key in obj) {
  25. if(Object.prototype.hasOwnProperty.call(obj, key)) {
  26. output[convert(key, options)] = _processKeys(convert, obj[key], options);
  27. }
  28. }
  29. }
  30. return output;
  31. };
  32. // String conversion methods
  33. var separateWords = function(string, options) {
  34. options = options || {};
  35. var separator = options.separator || '_';
  36. var split = options.split || /(?=[A-Z])/;
  37. return string.split(split).join(separator);
  38. };
  39. var camelize = function(string) {
  40. if (_isNumerical(string)) {
  41. return string;
  42. }
  43. string = string.replace(/[\-_\s]+(.)?/g, function(match, chr) {
  44. return chr ? chr.toUpperCase() : '';
  45. });
  46. // Ensure 1st char is always lowercase
  47. return string.substr(0, 1).toLowerCase() + string.substr(1);
  48. };
  49. var pascalize = function(string) {
  50. var camelized = camelize(string);
  51. // Ensure 1st char is always uppercase
  52. return camelized.substr(0, 1).toUpperCase() + camelized.substr(1);
  53. };
  54. var decamelize = function(string, options) {
  55. return separateWords(string, options).toLowerCase();
  56. };
  57. // Utilities
  58. // Taken from Underscore.js
  59. var toString = Object.prototype.toString;
  60. var _isFunction = function(obj) {
  61. return typeof(obj) === 'function';
  62. };
  63. var _isObject = function(obj) {
  64. return obj === Object(obj);
  65. };
  66. var _isArray = function(obj) {
  67. return toString.call(obj) == '[object Array]';
  68. };
  69. var _isDate = function(obj) {
  70. return toString.call(obj) == '[object Date]';
  71. };
  72. var _isRegExp = function(obj) {
  73. return toString.call(obj) == '[object RegExp]';
  74. };
  75. var _isBoolean = function(obj) {
  76. return toString.call(obj) == '[object Boolean]';
  77. };
  78. // Performant way to determine if obj coerces to a number
  79. var _isNumerical = function(obj) {
  80. obj = obj - 0;
  81. return obj === obj;
  82. };
  83. // Sets up function which handles processing keys
  84. // allowing the convert function to be modified by a callback
  85. var _processor = function(convert, options) {
  86. var callback = options && 'process' in options ? options.process : options;
  87. if(typeof(callback) !== 'function') {
  88. return convert;
  89. }
  90. return function(string, options) {
  91. return callback(string, convert, options);
  92. }
  93. };
  94. var humps = {
  95. camelize: camelize,
  96. decamelize: decamelize,
  97. pascalize: pascalize,
  98. depascalize: decamelize,
  99. camelizeKeys: function(object, options) {
  100. return _processKeys(_processor(camelize, options), object);
  101. },
  102. decamelizeKeys: function(object, options) {
  103. return _processKeys(_processor(decamelize, options), object, options);
  104. },
  105. pascalizeKeys: function(object, options) {
  106. return _processKeys(_processor(pascalize, options), object);
  107. },
  108. depascalizeKeys: function () {
  109. return this.decamelizeKeys.apply(this, arguments);
  110. }
  111. };
  112. if (typeof define === 'function' && define.amd) {
  113. define(humps);
  114. } else if (typeof module !== 'undefined' && module.exports) {
  115. module.exports = humps;
  116. } else {
  117. global.humps = humps;
  118. }
  119. })(this);