test.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. var assert = require('assert');
  2. var humps = require('../humps');
  3. describe('humps', function() {
  4. 'use strict';
  5. var actual;
  6. // =========
  7. // = Setup =
  8. // =========
  9. beforeEach(function() {
  10. this.simple_obj = {
  11. attr_one: 'foo',
  12. attr_two: 'bar'
  13. };
  14. this.simpleCamelObj = {
  15. attrOne: 'foo',
  16. attrTwo: 'bar'
  17. };
  18. this.simplePascalObj = {
  19. AttrOne: 'foo',
  20. AttrTwo: 'bar'
  21. };
  22. this.complex_obj = {
  23. attr_one: 'foo',
  24. attr_two: {
  25. nested_attr1: 'bar'
  26. },
  27. attr_three: {
  28. nested_attr2: {
  29. nested_attr3: [{
  30. nested_in_array1: 'baz'
  31. }, {
  32. nested_in_array2: 'hello'
  33. }, {
  34. nested_in_array3: ['world', 'boo']
  35. }]
  36. }
  37. }
  38. };
  39. this.complexCamelObj = {
  40. attrOne: 'foo',
  41. attrTwo: {
  42. nestedAttr1: 'bar'
  43. },
  44. attrThree: {
  45. nestedAttr2: {
  46. nestedAttr3: [{
  47. nestedInArray1: 'baz'
  48. }, {
  49. nestedInArray2: 'hello'
  50. }, {
  51. nestedInArray3: ['world', 'boo']
  52. }]
  53. }
  54. }
  55. };
  56. this.complexPascalObj = {
  57. AttrOne: 'foo',
  58. AttrTwo: {
  59. NestedAttr1: 'bar'
  60. },
  61. AttrThree: {
  62. NestedAttr2: {
  63. NestedAttr3: [{
  64. NestedInArray1: 'baz'
  65. }, {
  66. NestedInArray2: 'hello'
  67. }, {
  68. NestedInArray3: ['world', 'boo']
  69. }]
  70. }
  71. }
  72. };
  73. this.complexIgnoringNumbersObj = {
  74. attr_one: 'foo',
  75. attr_two: {
  76. nested_attr1: 'bar'
  77. },
  78. attr_three: {
  79. nested_attr2: {
  80. nested_attr3: [{
  81. nested_in_array1: 'baz'
  82. }, {
  83. nested_in_array2: 'hello'
  84. }, {
  85. nested_in_array3: ['world', 'boo']
  86. }]
  87. }
  88. }
  89. };
  90. this.complexCustomObj = {
  91. 'attr-one': 'foo',
  92. 'attr-two': {
  93. 'nested-attr1': 'bar'
  94. },
  95. 'attr-three': {
  96. 'nested-attr2': {
  97. 'nested-attr3': [{
  98. 'nested-in-array1': 'baz'
  99. }, {
  100. 'nested-in-array2': 'hello'
  101. }, {
  102. 'nested-in-array3': ['world', 'boo']
  103. }]
  104. }
  105. }
  106. };
  107. });
  108. // =========
  109. // = Specs =
  110. // =========
  111. describe('.camelizeKeys', function() {
  112. it('converts simple object keys to camelcase', function() {
  113. assert.deepEqual(humps.camelizeKeys(this.simple_obj), this.simpleCamelObj);
  114. });
  115. it('converts complex object keys to camelcase', function() {
  116. assert.deepEqual(humps.camelizeKeys(this.complex_obj), this.complexCamelObj);
  117. });
  118. it('does not attempt to process dates', function() {
  119. 'work in progress';
  120. var date = new Date();
  121. var _object = {
  122. a_date: date
  123. };
  124. var convertedObject = {
  125. aDate: date
  126. };
  127. assert.deepEqual(humps.camelizeKeys(_object), convertedObject);
  128. });
  129. it('converts keys within arrays of objects', function() {
  130. var array = [{first_name: 'Sam'}, {first_name: 'Jenna'}],
  131. convertedArray = [{firstName: 'Sam'}, {firstName: 'Jenna'}],
  132. result = humps.camelizeKeys(array);
  133. assert.deepEqual(result, convertedArray);
  134. // Ensure it’s an array, and not an object with numeric keys
  135. assert.deepEqual(toString.call(result), '[object Array]');
  136. });
  137. describe("when the value is a function", function() {
  138. it('converts the key and assigns the function', function() {
  139. function myFunction() {
  140. }
  141. var _object = {
  142. a_function: myFunction
  143. };
  144. var result = humps.camelizeKeys(_object);
  145. // deepEqual treats functions as the same as an empty object
  146. assert.strictEqual(result.aFunction, myFunction);
  147. });
  148. });
  149. it('uses a custom convertion callback', function() {
  150. actual = humps.camelizeKeys(this.simple_obj, function(key, convert) {
  151. return key === 'attr_one' ? key : convert(key);
  152. });
  153. assert.deepEqual(actual, { attr_one: 'foo', attrTwo: 'bar' });
  154. });
  155. });
  156. describe('.decamelizeKeys', function() {
  157. it('converts simple objects with camelcased keys to underscored', function() {
  158. assert.deepEqual(humps.decamelizeKeys(this.simpleCamelObj), this.simple_obj);
  159. });
  160. it('converts complex objects with camelcased keys to underscored', function() {
  161. assert.deepEqual(humps.decamelizeKeys(this.complexCamelObj), this.complex_obj);
  162. });
  163. it('decamelizes keys with a custom separator', function() {
  164. actual = humps.decamelizeKeys(this.complexCamelObj, { separator: '-' });
  165. assert.deepEqual(actual, this.complexCustomObj);
  166. });
  167. it('uses a custom split regexp', function() {
  168. actual = humps.decamelizeKeys({ attr1: 'foo' }, { split: /(?=[A-Z0-9])/ });
  169. assert.deepEqual(actual, { attr_1: 'foo' });
  170. });
  171. it('uses a custom convertion callback', function() {
  172. actual = humps.decamelizeKeys(this.simpleCamelObj, function(key, convert, options) {
  173. return key === 'attrOne' ? key : convert(key, options);
  174. });
  175. assert.deepEqual(actual, { attrOne: 'foo', attr_two: 'bar' });
  176. });
  177. describe("when the value is a function", function() {
  178. it('converts the key and assigns the function', function() {
  179. function myFunction() {
  180. }
  181. var _object = {
  182. aFunction: myFunction
  183. };
  184. var result = humps.decamelizeKeys(_object);
  185. // deepEqual treats functions as the same as an empty object
  186. assert.strictEqual(result.a_function, myFunction);
  187. });
  188. });
  189. it('uses a custom convertion callback as an option', function() {
  190. actual = humps.decamelizeKeys(this.simpleCamelObj, {
  191. process: function(key, convert, options) {
  192. return key === 'attrOne' ? key : convert(key, options);
  193. }
  194. });
  195. assert.deepEqual(actual, { attrOne: 'foo', attr_two: 'bar' });
  196. });
  197. });
  198. describe('.pascalizeKeys', function() {
  199. it('converts simple object keys to PascalCase', function() {
  200. assert.deepEqual(humps.pascalizeKeys(this.simple_obj), this.simplePascalObj);
  201. });
  202. it('converts complex object keys to PascalCase', function() {
  203. assert.deepEqual(humps.pascalizeKeys(this.complex_obj), this.complexPascalObj);
  204. });
  205. it('does not attempt to process dates', function() {
  206. 'work in progress';
  207. var date = new Date();
  208. var _object = {
  209. a_date: date
  210. };
  211. var convertedObject = {
  212. ADate: date
  213. };
  214. assert.deepEqual(humps.pascalizeKeys(_object), convertedObject);
  215. });
  216. it('uses a custom convertion callback', function() {
  217. actual = humps.pascalizeKeys(this.simple_obj, function(key, convert) {
  218. return key === 'attr_one' ? key : convert(key);
  219. });
  220. assert.deepEqual(actual, { attr_one: 'foo', AttrTwo: 'bar' });
  221. });
  222. });
  223. describe('.depascalizeKeys', function() {
  224. it('converts simple object with PascalCase keys to underscored', function() {
  225. assert.deepEqual(humps.depascalizeKeys(this.simplePascalObj), this.simple_obj);
  226. });
  227. it('converts complex object with PascalCase keys to underscored', function() {
  228. assert.deepEqual(humps.depascalizeKeys(this.complexPascalObj), this.complex_obj);
  229. });
  230. it('depascalizes keys with a custom separator', function() {
  231. actual = humps.depascalizeKeys(this.complexPascalObj, { separator: '-' });
  232. assert.deepEqual(actual, this.complexCustomObj);
  233. });
  234. });
  235. describe('.camelize', function() {
  236. it('converts underscored strings to camelcase', function() {
  237. assert.equal(humps.camelize('hello_world'), 'helloWorld');
  238. });
  239. it('converts hyphenated strings to camelcase', function() {
  240. assert.equal(humps.camelize('hello-world'), 'helloWorld');
  241. assert.equal(humps.camelize('hello-world-1'), 'helloWorld1');
  242. });
  243. it('converts space-separated strings to camelcase', function() {
  244. assert.equal(humps.camelize('hello world'), 'helloWorld');
  245. });
  246. it('converts PascalCased strings to camelcase', function() {
  247. assert.equal(humps.camelize('HelloWorld'), 'helloWorld');
  248. });
  249. it('keeps numbers unchanged', function() {
  250. assert.equal(humps.camelize('-1'), '-1');
  251. assert.equal(humps.camelize('1'), '1');
  252. });
  253. });
  254. describe('.decamelize', function() {
  255. it('converts camelcased strings to underscored', function() {
  256. assert.equal(humps.decamelize('helloWorld'), 'hello_world');
  257. });
  258. it('decamelizes strings with custom separator', function() {
  259. actual = humps.decamelize('helloWorld', { separator: '-' });
  260. assert.equal(actual, 'hello-world');
  261. });
  262. it('does not separate on digits', function() {
  263. assert.equal(humps.decamelize('helloWorld1'), 'hello_world1');
  264. });
  265. it('uses a custom split regexp', function() {
  266. assert.equal(humps.decamelize('helloWorld1', { split: /(?=[A-Z0-9])/ }),
  267. 'hello_world_1');
  268. });
  269. });
  270. describe('.pascalize', function() {
  271. it('converts underscored strings to PascalCase', function() {
  272. assert.equal(humps.pascalize('hello_world'), 'HelloWorld');
  273. });
  274. it('converts hyphenated strings to PascalCase', function() {
  275. assert.equal(humps.pascalize('hello-world'), 'HelloWorld');
  276. });
  277. it('converts space-separated strings to PascalCase', function() {
  278. assert.equal(humps.pascalize('hello world'), 'HelloWorld');
  279. });
  280. });
  281. });