Skip to content

Commit

Permalink
feat: added isTypeof method
Browse files Browse the repository at this point in the history
it asserts that a target object is of a given type if all the given props are
defined in the target object
  • Loading branch information
teclone committed Jun 8, 2019
1 parent 17d5f24 commit 29e767f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ export const CASE_STYLES = {
SNAKE_CASE: 2
};

/**
* picks the given value from the object, supressing any error that may occur while trying
* to access key
*
* @param key
* @param object
*/
const _pickValue = (key: string, object: object) => {
try {
const value = object[key];
return value;
}
catch(ex) {
return undefined;
}
};

/**
* tests if argument is null
*/
Expand Down Expand Up @@ -127,7 +144,6 @@ export const isParameter = (arg: any, isNullable: boolean = true) => {

return true;
};

/**
* puts argument into an array if it is not an array,
*/
Expand Down Expand Up @@ -155,6 +171,16 @@ export function makeObject<T>(arg: any): T {
return {} as T;
};

/**
* asserts that target is T if the given property is defined in target
* @param prop property to check
* @param target the target object
*/
export const isTypeOf = <T extends O, O extends object = any>(props: string | string[], target: O): target is T => {
return makeArray(props).every(prop => typeof _pickValue(prop, target) !== 'undefined');
}


/**
* returns true if key is not set in the given object or key is set and its value is truthy
*/
Expand All @@ -169,23 +195,6 @@ export const keySetAndTrue = (key: string, object: object): boolean => {
return typeof object[key] !== 'undefined' && !!object[key];
};

/**
* picks the given value from the object, supressing any error that may occur while trying
* to access key
*
* @param key
* @param object
*/
const _pickValue = (key: string, object: object) => {
try {
const value = object[key];
return value;
}
catch(ex) {
return undefined;
}
};

/**
* returns the value for the first key that exists in the object otherwise,
* it returns the default value
Expand Down
12 changes: 12 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ describe('Utils', function() {
});
});

describe(`.isTypeof<T extends O, O extends object = any>(props: string | string, target: O): target is T`, function() {
it(`should assert that target is of type T if all the given props are defined in the
target object`, function() {
expect(Utils.isTypeOf<Array<number>>('length', [])).toEqual(true);
});

it(`should assert that target is not of type T if any of the given props is not defined in
the target object`, function() {
expect(Utils.isTypeOf<Array<number>>('len', [])).toEqual(false);
});
});

describe('.deleteProperty(key: string, target: object): boolean', function() {
it(`should delete the given property from the target object`, function() {
const target = {name: 'name'};
Expand Down

0 comments on commit 29e767f

Please sign in to comment.