From b0824e2d38adf8c510f1ab0d66f8403eff34e622 Mon Sep 17 00:00:00 2001 From: David Guijarro Date: Tue, 23 Jun 2020 11:06:05 +0200 Subject: [PATCH] refactor!: use more abstract generic types BREAKING CHANGE: the `GenericObject` type is deprecated. --- src/interfaces/generics.ts | 3 --- src/is-equal.ts | 10 +++++++--- src/omit.spec.ts | 8 ++++++-- src/omit.ts | 8 +++----- 4 files changed, 16 insertions(+), 13 deletions(-) delete mode 100644 src/interfaces/generics.ts diff --git a/src/interfaces/generics.ts b/src/interfaces/generics.ts deleted file mode 100644 index 31f1ff3..0000000 --- a/src/interfaces/generics.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface GenericObject { - [key: string]: any; -} diff --git a/src/is-equal.ts b/src/is-equal.ts index db2184f..fc879b4 100644 --- a/src/is-equal.ts +++ b/src/is-equal.ts @@ -1,4 +1,3 @@ -import { GenericObject } from './interfaces/generics'; import { isDate } from './is-date'; /** @@ -8,7 +7,7 @@ import { isDate } from './is-date'; * @param first The first object to compare * @param second The second object to compare */ -export function isEqual(first: T, second: T): boolean { +export function isEqual(first: T, second: T): boolean { for (const key in first) { if (second.hasOwnProperty(key) === false) { return false; @@ -20,7 +19,12 @@ export function isEqual(first: T, second: T): boolean { } else if (typeof e1 === 'object') { // Dates are also "objects", so we need to check first if elements are date // and compare their values if they are - if (isDate(e1) && isDate(e2) && e1.getTime() !== e2.getTime()) { + if ( + isDate(e1) && + isDate(e2) && + ((e1 as unknown) as Date).getTime() !== + ((e2 as unknown) as Date).getTime() + ) { return false; } else if (isEqual(e1, e2) === false) { return false; diff --git a/src/omit.spec.ts b/src/omit.spec.ts index 2033825..1c8dbb8 100644 --- a/src/omit.spec.ts +++ b/src/omit.spec.ts @@ -2,7 +2,7 @@ import { omit } from './omit'; describe('omit', () => { it('should return a new object', () => { - const o = {}; + const o = { key: true }; expect(omit(o, ['key'])).not.toBe(o); }); @@ -20,7 +20,11 @@ describe('omit', () => { }); it('should do nothing if the passed property is not found in the source', () => { - const o = { prop1: 'stays', prop2: 'stays', prop3: 'stays' }; + const o: { [key: string]: any } = { + prop1: 'stays', + prop2: 'stays', + prop3: 'stays', + }; const e = { ...o }; const result = omit(o, ['notFoundProp']); expect(result).toEqual(e); diff --git a/src/omit.ts b/src/omit.ts index f173f8c..11f102e 100644 --- a/src/omit.ts +++ b/src/omit.ts @@ -1,5 +1,3 @@ -import { GenericObject } from './interfaces/generics'; - /** * Takes an object and a list of properties to remove, and returns a new object equal to the original one but the listed properties removed. * @@ -10,12 +8,12 @@ import { GenericObject } from './interfaces/generics'; * @param properties An array of property names to be removed. */ -export function omit(source: T, properties: string[]) { +export function omit(source: T, properties: K[]) { // @ts-ignore let omitted: any; - let rest: GenericObject = { ...source }; + let rest = { ...source }; for (const property of properties) { - ({ [property]: omitted, ...rest } = rest); + ({ [property]: omitted, ...rest as Omit } = rest); } return rest; }