lodash-es#toPairs TypeScript Examples
The following examples show how to use
lodash-es#toPairs.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: clone-deep.spec.ts From s-libs with MIT License | 5 votes |
describe('cloneDeep()', () => {
it('clones deeply', () => {
const object = {
a: { b: [{ c: 3 }, [{ d: 4 }]] as [{ c: 3 }, [{ d: 4 }]] },
};
const cloned = cloneDeep(object);
expect(cloned).toEqual(object);
expect(cloned.a).not.toBe(object.a);
expect(cloned.a.b).not.toBe(object.a.b);
expect(cloned.a.b[1]).not.toBe(object.a.b[1]);
expect(cloned.a.b[1][0].d).toBe(4);
});
//
// stolen from https://github.com/lodash/lodash
//
const clonable = {
arrays: ['a', ''],
'array-like objects': { '0': 'a', length: 1 },
booleans: false,
'null values': null,
numbers: 0,
objects: { a: 0, b: 1, c: 2 },
strings: 'a',
'undefined values': undefined,
'objects with object values': { b: ['B'], c: { C: 1 } },
};
for (const [kind, object] of toPairs(clonable)) {
it('should clone ' + kind, () => {
const actual = cloneDeep(object);
expect(actual).toEqual(object);
if (isObject(object)) {
expect(actual).not.toBe(object);
} else {
expect(actual).toBe(object);
}
});
}
it('clones properties that shadow those on `Object.prototype`', () => {
const object = {
constructor: 1,
hasOwnProperty: 2,
isPrototypeOf: 3,
propertyIsEnumerable: 4,
toLocaleString: 5,
toString: 6,
valueOf: 7,
};
const actual = cloneDeep(object);
expect(actual).toEqual(object);
expect(actual).not.toBe(object);
});
it('works for methods like `map`', () => {
const expected: any[] = [{ a: [0] }, { b: [1] }];
const actual = expected.map(cloneDeep);
expect(actual).toEqual(expected);
expect(actual[0]).not.toBe(expected[0]);
expect(actual[0].a).not.toBe(expected[0].a);
expect(actual[1].b).not.toBe(expected[1].b);
});
});
Example #2
Source File: clone.spec.ts From s-libs with MIT License | 4 votes |
describe('clone()', () => {
//
// stolen from https://github.com/healthiers/mini-dash
//
it('should return an empty object if empty given', () => {
expect(clone({})).toEqual({});
});
it('should return an object with the same properties', () => {
expect(clone({ a: 1, b: 2, c: 3 })).toEqual({ a: 1, b: 2, c: 3 });
});
it('should return an object with nested properties', () => {
expect(clone({ a: 1, b: { foo: 'bar' }, c: { bar: 'foo' } })).toEqual({
a: 1,
b: { foo: 'bar' },
c: { bar: 'foo' },
});
});
it('should not mutate the original object', () => {
const object = { a: 1, b: 2, c: 3 };
const result = clone(object);
expect(result).toEqual(object);
expect(result).not.toBe(object);
});
it('should shallow clone', () => {
const object = { a: 1, b: { foo: 'bar' }, c: { bar: 'foo' } };
const result = clone(object);
expect(result).toEqual(object);
expect(result).not.toBe(object);
expect(result.b).toBe(object.b);
expect(result.c).toBe(object.c);
});
//
// stolen from https://github.com/lodash/lodash
//
it('should perform a shallow clone', () => {
const array = [{ a: 0 }, { b: 1 }];
const actual = clone(array);
expect(actual).toEqual(array);
expect(actual).not.toBe(array);
expect(actual[0]).toBe(array[0]);
});
const clonable = {
arrays: ['a', ''],
'array-like objects': { '0': 'a', length: 1 },
booleans: false,
'null values': null,
numbers: 0,
objects: { a: 0, b: 1, c: 2 },
strings: 'a',
'undefined values': undefined,
'objects with object values': { b: ['B'], c: { C: 1 } },
};
for (const [kind, object] of toPairs(clonable)) {
it('should clone ' + kind, () => {
const actual = clone(object);
expect(actual).toEqual(object);
if (isObject(object)) {
expect(actual).not.toBe(object);
} else {
expect(actual).toBe(object);
}
});
}
it('clones properties that shadow those on `Object.prototype`', () => {
const object = {
constructor: 1,
hasOwnProperty: 2,
isPrototypeOf: 3,
propertyIsEnumerable: 4,
toLocaleString: 5,
toString: 6,
valueOf: 7,
};
const actual = clone(object);
expect(actual).toEqual(object);
expect(actual).not.toBe(object);
});
it('works for methods like `map`', () => {
const expected: any[] = [{ a: [0] }, { b: [1] }];
const actual = expected.map(clone);
expect(actual).toEqual(expected);
expect(actual[0]).not.toBe(expected[0]);
expect(actual[0].a).toBe(expected[0].a);
expect(actual[1].b).toBe(expected[1].b);
});
});