|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* eslint-env mocha */ |
| 4 | + |
| 5 | +const assert = require('assert') |
| 6 | +const { transformKeysToSnake } = require('../index') |
| 7 | +const { itEach } = require('mocha-it-each') |
| 8 | + |
| 9 | +describe('transformKeysToSnake', () => { |
| 10 | + describe('non recursive (default)', () => { |
| 11 | + itEach('should return input when it is not an object', [new Date(), null, false], (input) => { |
| 12 | + assert.equal(transformKeysToSnake(input), input) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should return input with snake case props recursively', () => { |
| 16 | + const input = { |
| 17 | + camelProp: 'true', |
| 18 | + nestedProp: { |
| 19 | + first_name: 'john', |
| 20 | + lastName: 'doe', |
| 21 | + tags: ['keepCamel'] |
| 22 | + }, |
| 23 | + someObjs: [ |
| 24 | + { someProp: 'value' }, |
| 25 | + { foo_bar: 'value', foo: 'value' } |
| 26 | + ] |
| 27 | + } |
| 28 | + const output = { |
| 29 | + camel_prop: 'true', |
| 30 | + nested_prop: { |
| 31 | + first_name: 'john', |
| 32 | + lastName: 'doe', |
| 33 | + tags: ['keepCamel'] |
| 34 | + }, |
| 35 | + some_objs: [ |
| 36 | + { someProp: 'value' }, |
| 37 | + { foo_bar: 'value', foo: 'value' } |
| 38 | + ] |
| 39 | + } |
| 40 | + assert.deepEqual(transformKeysToSnake(input), output) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should work with array mix of object and nested arrays', () => { |
| 44 | + const input = { |
| 45 | + mixedArray: [123, { someKey: 'value' }, [{ nestedArrayItem: 'test' }]] |
| 46 | + } |
| 47 | + const output = { |
| 48 | + mixed_array: [123, { someKey: 'value' }, [{ nestedArrayItem: 'test' }]] |
| 49 | + } |
| 50 | + assert.deepEqual(transformKeysToSnake(input), output) |
| 51 | + }) |
| 52 | + }) |
| 53 | + describe('recursive', () => { |
| 54 | + itEach('should return input when it is not an object', [new Date(), null, false], (input) => { |
| 55 | + assert.equal(transformKeysToSnake(input, { recursive: true }), input) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should return input with snake case props recursively', () => { |
| 59 | + const input = { |
| 60 | + camelProp: 'true', |
| 61 | + nestedProp: { |
| 62 | + first_name: 'john', |
| 63 | + lastName: 'doe', |
| 64 | + level2: { |
| 65 | + fooBar: 'x' |
| 66 | + }, |
| 67 | + tags: ['keepCamel'] |
| 68 | + }, |
| 69 | + someObjs: [ |
| 70 | + { someProp: 'value' }, |
| 71 | + { foo_bar: 'value', foo: 'value' } |
| 72 | + ] |
| 73 | + } |
| 74 | + const output = { |
| 75 | + camel_prop: 'true', |
| 76 | + nested_prop: { |
| 77 | + first_name: 'john', |
| 78 | + last_name: 'doe', |
| 79 | + level2: { |
| 80 | + foo_bar: 'x' |
| 81 | + }, |
| 82 | + tags: ['keepCamel'] |
| 83 | + }, |
| 84 | + some_objs: [ |
| 85 | + { some_prop: 'value' }, |
| 86 | + { foo_bar: 'value', foo: 'value' } |
| 87 | + ] |
| 88 | + } |
| 89 | + assert.deepEqual(transformKeysToSnake(input, { recursive: true }), output) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should work with array mix of object and nested arrays', () => { |
| 93 | + const input = { |
| 94 | + mixedArray: [123, { someKey: 'value' }, [{ nestedArrayItem: 'test' }]] |
| 95 | + } |
| 96 | + const output = { |
| 97 | + mixed_array: [123, { some_key: 'value' }, [{ nested_array_item: 'test' }]] |
| 98 | + } |
| 99 | + assert.deepEqual(transformKeysToSnake(input, { recursive: true }), output) |
| 100 | + }) |
| 101 | + }) |
| 102 | +}) |
0 commit comments