-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { convertSubObjectsIntoPaths } from './convertSubObjectsIntoPaths'; | ||
|
||
describe('convertSubObjectsIntoPaths', () => { | ||
it('should flatten a simple object with no nested structure', () => { | ||
const input = { a: 1, b: 2, c: 3 }; | ||
const expected = { a: 1, b: 2, c: 3 }; | ||
|
||
expect(convertSubObjectsIntoPaths(input)).to.deep.equal(expected); | ||
}); | ||
|
||
it('should flatten a nested object into paths', () => { | ||
const input = { | ||
a: 1, | ||
b: { | ||
c: 2, | ||
d: { | ||
e: 3, | ||
}, | ||
}, | ||
}; | ||
const expected = { | ||
'a': 1, | ||
'b.c': 2, | ||
'b.d.e': 3, | ||
}; | ||
|
||
expect(convertSubObjectsIntoPaths(input)).to.deep.equal(expected); | ||
}); | ||
|
||
it('should handle objects with array values', () => { | ||
const input = { | ||
a: [1, 2, 3], | ||
b: { | ||
c: [4, 5], | ||
}, | ||
}; | ||
const expected = { | ||
'a': [1, 2, 3], | ||
'b.c': [4, 5], | ||
}; | ||
|
||
expect(convertSubObjectsIntoPaths(input)).to.deep.equal(expected); | ||
}); | ||
|
||
it('should handle deeply nested objects', () => { | ||
const input = { | ||
a: { | ||
b: { | ||
c: { | ||
d: { | ||
e: { | ||
f: 6, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
const expected = { | ||
'a.b.c.d.e.f': 6, | ||
}; | ||
|
||
expect(convertSubObjectsIntoPaths(input)).to.deep.equal(expected); | ||
}); | ||
|
||
it('should handle an empty object', () => { | ||
const input = {}; | ||
const expected = {}; | ||
|
||
expect(convertSubObjectsIntoPaths(input)).to.deep.equal(expected); | ||
}); | ||
|
||
it('should handle objects with mixed types of values', () => { | ||
const input = { | ||
a: 1, | ||
b: 'string', | ||
c: true, | ||
d: { | ||
e: null, | ||
f: undefined, | ||
g: { | ||
h: 2, | ||
}, | ||
}, | ||
}; | ||
const expected = { | ||
'a': 1, | ||
|
||
'b': 'string', | ||
|
||
'c': true, | ||
'd.e': null, | ||
'd.f': undefined, | ||
'd.g.h': 2, | ||
}; | ||
|
||
expect(convertSubObjectsIntoPaths(input)).to.deep.equal(expected); | ||
}); | ||
|
||
it('should respect the parentPath parameter', () => { | ||
const input = { | ||
a: 1, | ||
b: { | ||
c: 2, | ||
}, | ||
}; | ||
const parentPath = 'root'; | ||
const expected = { | ||
'root.a': 1, | ||
'root.b.c': 2, | ||
}; | ||
|
||
expect(convertSubObjectsIntoPaths(input, parentPath)).to.deep.equal(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { objectMap } from './objectMap'; | ||
|
||
describe('objectMap', () => { | ||
it('should map a simple object non-recursively', () => { | ||
const input = { a: 1, b: 2, c: 3 }; | ||
const callback = ({ key, value }) => ({ key: key.toUpperCase(), value: value * 2 }); | ||
const expected = { A: 2, B: 4, C: 6 }; | ||
expect(objectMap(input, callback)).to.deep.equal(expected); | ||
}); | ||
it('should filter out undefined results from callback', () => { | ||
const input = { a: 1, b: 2, c: 3 }; | ||
const callback = ({ key, value }) => (value > 1 ? { key, value } : undefined); | ||
const expected = { b: 2, c: 3 }; | ||
expect(objectMap(input, callback)).to.deep.equal(expected); | ||
}); | ||
it('should map a nested object recursively', () => { | ||
const input = { | ||
a: 1, | ||
b: { | ||
c: 2, | ||
d: { | ||
e: 3, | ||
}, | ||
}, | ||
}; | ||
const callback = ({ key, value }) => ({ key: `mapped_${key}`, value: typeof value === 'number' ? value * 10 : value }); | ||
const expected = { | ||
mapped_a: 10, | ||
mapped_b: { | ||
mapped_c: 20, | ||
mapped_d: { | ||
mapped_e: 30, | ||
}, | ||
}, | ||
}; | ||
expect(objectMap(input, callback, true)).to.deep.equal(expected); | ||
}); | ||
it('should handle an empty object', () => { | ||
const input = {}; | ||
const callback = ({ key, value }) => ({ key: `mapped_${key}`, value }); | ||
const expected = {}; | ||
expect(objectMap(input, callback)).to.deep.equal(expected); | ||
}); | ||
it('should handle mixed value types in non-recursive mode', () => { | ||
const input = { | ||
a: 1, | ||
b: 'string', | ||
c: true, | ||
d: null, | ||
}; | ||
const callback = ({ key, value }) => ({ key: key.toUpperCase(), value: typeof value === 'number' ? value * 2 : value }); | ||
const expected = { | ||
A: 2, | ||
B: 'string', | ||
C: true, | ||
D: null, | ||
}; | ||
expect(objectMap(input, callback)).to.deep.equal(expected); | ||
}); | ||
it('should handle nested objects with mixed types recursively', () => { | ||
const input = { | ||
a: 1, | ||
b: { | ||
c: 'string', | ||
d: { | ||
e: true, | ||
f: null, | ||
}, | ||
}, | ||
}; | ||
const callback = ({ key, value }) => ({ key: key.toUpperCase(), value }); | ||
const expected = { | ||
A: 1, | ||
B: { | ||
C: 'string', | ||
D: { | ||
E: true, | ||
F: null, | ||
}, | ||
}, | ||
}; | ||
expect(objectMap(input, callback, true)).to.deep.equal(expected); | ||
}); | ||
it('should not modify the original object', () => { | ||
const input = { a: 1, b: 2 }; | ||
const original = { ...input }; | ||
const callback = ({ key, value }) => ({ key, value: value * 2 }); | ||
objectMap(input, callback); | ||
expect(input).to.deep.equal(original); | ||
}); | ||
}); |