@qntm-code/utils
Version:
A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.
186 lines (185 loc) • 6.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const moment = require("moment");
const type_predicates_1 = require("../type-predicates");
const clone_1 = require("./clone");
describe('clone', () => {
it('should clone a date', () => {
const value = new Date();
const cloned = (0, clone_1.clone)(value);
expect(Object.is(value, cloned)).toBeFalse();
});
it('should clone an array', () => {
const value = ['a', 10, new Date()];
const cloned = (0, clone_1.clone)(value);
expect(Object.is(value, cloned)).toBeFalse();
});
it('should clone an object', () => {
const value = {
a: 'a',
b: 10,
c: new Date(),
};
const cloned = (0, clone_1.clone)(value);
expect(Object.is(value, cloned)).toBeFalse();
});
it('should deeply clone an array', () => {
const childArray = ['a', 10, new Date()];
const childObject = {
a: 'a',
b: 10,
c: new Date(),
};
const value = [childArray, childObject, 'a', 10, new Date()];
const cloned = (0, clone_1.clone)(value);
const clonedArray = cloned[0];
const clonedObject = cloned[1];
expect(Object.is(value, cloned)).toBeFalse();
expect(Object.is(childArray, clonedArray)).toBeFalse();
expect(Object.is(childObject, clonedObject)).toBeFalse();
});
it('should deeply clone an object', () => {
const childArray = ['a', 10, new Date()];
const childObject = {
a: 'a',
b: 10,
c: new Date(),
};
const value = {
childArray,
childObject,
a: 'a',
b: 10,
c: new Date(),
};
const cloned = (0, clone_1.clone)(value);
const clonedArray = cloned.childArray;
const clonedObject = cloned.childObject;
expect(Object.is(value, cloned)).toBeFalse();
expect(Object.is(childArray, clonedArray)).toBeFalse();
expect(Object.is(childObject, clonedObject)).toBeFalse();
});
it(`should deeply clone a Map`, () => {
const nested = { x: 1 };
const a = new Map([['k', nested]]);
const b = (0, clone_1.clone)(a);
nested.x = 2;
expect(a.get('k')?.x).toBe(2);
expect(b.get('k')?.x).toBe(1);
});
it(`should deeply clone a Set`, () => {
const nested = { x: 1 };
const a = new Set([nested]);
const b = (0, clone_1.clone)(a);
nested.x = 2;
const clonedItem = Array.from(b)[0];
expect(clonedItem.x).toBe(1);
});
it(`should return primitives`, () => {
expect((0, type_predicates_1.isEqual)((0, clone_1.clone)(0), 0)).toBeTrue();
});
it(`should use moment's own clone method`, () => {
const source = moment();
const cloned = (0, clone_1.clone)(source).add(1, 'day');
expect(source.isSame(cloned)).toBeFalse();
});
describe(`RegExp`, () => {
it('should clone a regex with flags', () => {
expect((0, clone_1.clone)(/foo/g)).toEqual(/foo/g);
});
it('should clone a regex without any flags', () => {
expect((0, clone_1.clone)(/foo/)).toEqual(/foo/);
});
it('should clone a regex with flags (constructor)', () => {
expect((0, clone_1.clone)(new RegExp('foo', 'g'))).toEqual(new RegExp('foo', 'g'));
});
it('should clone a regex without any flags (constructor)', () => {
expect((0, clone_1.clone)(new RegExp('foo', undefined))).toEqual(new RegExp('foo', undefined));
});
});
it(`Should clone a buffer`, () => {
const a = Buffer.from('a');
const b = (0, clone_1.clone)(a);
a.set([1]);
expect(a).not.toEqual(b);
});
it(`should clone a symbol`, () => {
const a = Symbol('a');
const b = (0, clone_1.clone)(a);
expect(typeof b).toBe('symbol');
expect((0, type_predicates_1.isEqual)(a, b)).toBeFalse();
});
it(`should clone an Error`, () => {
const a = new Error('a');
const b = (0, clone_1.clone)(a);
expect(a).toEqual(b);
});
describe(`Typed Arrays`, () => {
it(`should clone a Uint8Array`, () => {
const a = new Uint8Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Uint8ClampedArray`, () => {
const a = new Uint8ClampedArray([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Uint16Array`, () => {
const a = new Uint16Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Uint32Array`, () => {
const a = new Uint32Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Int8Array`, () => {
const a = new Int8Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Int16Array`, () => {
const a = new Int16Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Int32Array`, () => {
const a = new Int32Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Float32Array`, () => {
const a = new Float32Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a Float64Array`, () => {
const a = new Float64Array([1, 2, 3]);
const b = (0, clone_1.clone)(a);
a.set([4, 5, 6]);
expect(a).not.toEqual(b);
});
it(`should clone a BigInt64Array`, () => {
const a = new BigInt64Array([1n, 2n, 3n]);
const b = (0, clone_1.clone)(a);
a.set([4n, 5n, 6n]);
expect(a).not.toEqual(b);
});
it(`should clone a BigUint64Array`, () => {
const a = new BigUint64Array([1n, 2n, 3n]);
const b = (0, clone_1.clone)(a);
a.set([4n, 5n, 6n]);
expect(a).not.toEqual(b);
});
});
});