diff --git a/src/utils/fn.utils.ts b/src/utils/fn.utils.ts index d6ad8d56..f8d2f7bb 100644 --- a/src/utils/fn.utils.ts +++ b/src/utils/fn.utils.ts @@ -11,7 +11,7 @@ export function isEmpty(value: any[] | string): boolean { } export function trim(value: string): string { - return typeof value === 'string' ? value.trim() : ''; + return isNil(value) ? '' : value.trim(); } export function has(value: any, prop: string): boolean { @@ -22,7 +22,7 @@ export function isFunction(value: any) { return typeof value === 'function'; } -export function get(value: any, path: string = '', defaultValue?: any) { +export function get(value: any, path: string, defaultValue?: any) { let result = value; for (const prop of path.split('.')) { @@ -64,7 +64,7 @@ export function once(fn: Once): Once { } export function defaultsDeep(target: any, ... sources: any[]): any { - return [target].concat(sources || []).reduce((result: any, source: any) => { + return [target].concat(sources).reduce((result: any, source: any) => { if (!source) { return result; } @@ -90,11 +90,8 @@ export function includes(target: string | any[], value: any): boolean { return false; } - if (typeof target === 'string') { - return target.indexOf(value as string) > -1; - } - - return target.indexOf(value) > -1; + const index = typeof target === 'string' ? target.indexOf(value as string) : target.indexOf(value); + return index > -1; } export function isNil(value: any): boolean { diff --git a/test/utils/fn.utils.spec.ts b/test/utils/fn.utils.spec.ts new file mode 100644 index 00000000..fd75a473 --- /dev/null +++ b/test/utils/fn.utils.spec.ts @@ -0,0 +1,53 @@ +import * as fn from '../../src/utils/fn.utils'; + +describe('fn.utils - multipurpose functions', () => { + describe('trim', () => { + it('returns empty string when input value is null or undefined', () => { + expect(fn.trim(null)).toEqual(''); + expect(fn.trim(undefined)).toEqual(''); + }); + + it('uses native trim method under the hood', () => { + const stringSpy = jasmine.createSpyObj('string', ['trim']); + stringSpy.trim.and.returnValue('Boo!'); + expect(fn.trim(stringSpy as string)).toEqual('Boo!'); + }); + }); + + describe('once', () => { + it('executes function only ones', () => { + const onceTargetSpy = jasmine.createSpy('once'); + const onceExecutableFn = fn.once(onceTargetSpy); + + onceExecutableFn('Hello', ', ', 'World'); + onceExecutableFn('Hello'); + + expect(onceTargetSpy).toHaveBeenCalledTimes(1); + expect(onceTargetSpy).toHaveBeenCalledWith('Hello', ', ', 'World'); + expect(onceTargetSpy).not.toHaveBeenCalledWith('Hello'); + }); + }); + + describe('defaultsDeep', () => { + it('uses empty array if there were no sources given', () => { + const options = fn.defaultsDeep({ msg: 'Boo!' }); + + expect(options).toEqual({ msg: 'Boo!' }); + }); + }); + + describe('defaultsDeep', () => { + it('uses empty array if there were no sources given', () => { + const options = fn.defaultsDeep({ msg: 'Boo!' }); + + expect(options).toEqual({ msg: 'Boo!' }); + }); + }); + + describe('includes', () => { + it('works with strings', () => { + expect(fn.includes('world', 'rl')).toEqual(true); + expect(fn.includes('world', 'rrl')).toEqual(false); + }); + }); +});