Skip to content

Commit

Permalink
fix: fixed incorrect object type check in isObject method
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Apr 12, 2019
1 parent 8d54bf2 commit affb6aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ export const isRegex = (arg: any): arg is RegExp => {
};

/**
* test if argument is a javascript object
* test if argument is a javascript object, but not an array, Regex, function or null
*/
export const isObject = <T=object>(arg: any): arg is T => {
return typeof arg === 'object' && arg !== null;
return typeof arg === 'object' && arg !== null && !isCallable(arg) && !isArray(arg)
&& !isRegex(arg);
};

/**
Expand Down Expand Up @@ -133,13 +134,15 @@ export function makeObject<T extends object>(arg: T): T;
export function makeObject(arg: any): object;

/**
* returns argument if it is an object, otherwise, returns an empty plain object
* returns argument if it is an object, otherwise returns an empty plain object,
* an object is a javascript entity whose typeof returns 'object' but it is not a null,
* function, array or RegExp
*/
export function makeObject<T>(arg: T | any): T | object {
if (isObject(arg))
export function makeObject<T>(arg: any): T {
if (isObject<T>(arg))
return arg;

return {};
return {} as T;
};

/**
Expand Down
8 changes: 3 additions & 5 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ describe('Utils', function() {
});

describe('.isObject(arg: any): boolean', function() {
it('should return true if argument is an object', function() {
it('should return true if argument is an object, but not an array, function, regex or null', function() {
expect(Utils.isObject({})).toBeTruthy();
expect(Utils.isObject([])).toBeTruthy();
});

it('should return false if argument is not an object', function() {
Expand Down Expand Up @@ -174,7 +173,7 @@ describe('Utils', function() {

describe('.makeObject<T>(arg: T | any): T | object', function() {
it(`should return argument if is an object`, function() {
const arg = ['item'];
const arg = {name: 'item'};
expect(Utils.makeObject(arg)).toBe(arg);
});

Expand Down Expand Up @@ -287,8 +286,7 @@ describe('Utils', function() {

it(`should return the value for the first set key in the object that is an object`, function() {
expect(Utils.objectValue('settings', object)).toEqual(object.settings);
expect(Utils.objectValue(['name', 'themes'], object)).toEqual(object.themes);
expect(Utils.objectValue(['settings', 'themes', 'name'], object)).toEqual(object.settings);
expect(Utils.objectValue(['themes', 'settings'], object)).toEqual(object.settings);
});

it(`should return the default value if no key is set in the object that is an object`, function() {
Expand Down

0 comments on commit affb6aa

Please sign in to comment.