Skip to content

Commit

Permalink
refactor!: use more abstract generic types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `GenericObject` type is deprecated.
  • Loading branch information
davguij committed Jun 23, 2020
1 parent 0638b46 commit b0824e2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
3 changes: 0 additions & 3 deletions src/interfaces/generics.ts

This file was deleted.

10 changes: 7 additions & 3 deletions src/is-equal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { GenericObject } from './interfaces/generics';
import { isDate } from './is-date';

/**
Expand All @@ -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<T extends GenericObject>(first: T, second: T): boolean {
export function isEqual<T extends {}>(first: T, second: T): boolean {
for (const key in first) {
if (second.hasOwnProperty(key) === false) {
return false;
Expand All @@ -20,7 +19,12 @@ export function isEqual<T extends GenericObject>(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;
Expand Down
8 changes: 6 additions & 2 deletions src/omit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions src/omit.ts
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -10,12 +8,12 @@ import { GenericObject } from './interfaces/generics';
* @param properties An array of property names to be removed.
*/

export function omit<T extends GenericObject>(source: T, properties: string[]) {
export function omit<T, K extends keyof T>(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<T, K> } = rest);
}
return rest;
}

0 comments on commit b0824e2

Please sign in to comment.