Update snippet, add extra tests
This commit is contained in:
@ -191,6 +191,7 @@ const debounce = (fn, ms = 0) => {
|
||||
const decapitalize = ([first, ...rest], upperRest = false) =>
|
||||
first.toLowerCase() + (upperRest ? rest.join('').toUpperCase() : rest.join(''));
|
||||
const deepClone = obj => {
|
||||
if (obj === null) return null;
|
||||
let clone = Object.assign({}, obj);
|
||||
Object.keys(clone).forEach(
|
||||
key => (clone[key] = typeof obj[key] === 'object' ? deepClone(obj[key]) : obj[key])
|
||||
|
||||
@ -10,7 +10,8 @@ const data = {
|
||||
d: {},
|
||||
e: { a: 'a', b: 'b', c: 'c', d: ['a', 'b', 'c'] },
|
||||
f: 1,
|
||||
g: true
|
||||
g: true,
|
||||
h: null
|
||||
};
|
||||
const dupe = deepClone(data);
|
||||
test('Shallow cloning works', () => {
|
||||
@ -33,3 +34,6 @@ test('Cloning primitives works', () => {
|
||||
expect(data.f).toBe(dupe.f);
|
||||
expect(data.g).toBe(dupe.g);
|
||||
});
|
||||
test('Cloning null works', () => {
|
||||
expect(data.h).toBe(dupe.h);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user