-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
12c0730
commit 8c9cc7e
Showing
9 changed files
with
217 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'jest'; | ||
|
||
import { compact } from './compact'; | ||
import { compact as arrCompact } from './array/compact'; | ||
import { compact as objCompact } from './object/compact'; | ||
|
||
describe('Compact Function', () => { | ||
it('should act as array compact', () => { | ||
const arr = [0, undefined, null, '', 'a']; | ||
|
||
expect(compact(arr)).toEqual(arrCompact(arr)); | ||
}); | ||
|
||
it('should act as object compact', () => { | ||
const object = {x: undefined, y: null, z: 20}; | ||
|
||
expect(compact(object)).toEqual(objCompact(object)); | ||
}); | ||
|
||
it('should throw error when the value is not array nor object', () => { | ||
expect(() => compact(null)).toThrow(TypeError); | ||
expect(() => compact(0)).toThrow(TypeError); | ||
expect(() => compact('')).toThrow(TypeError); | ||
expect(() => compact(false)).toThrow(TypeError); | ||
expect(() => compact(Symbol())).toThrow(TypeError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { compact as objCompact } from './object'; | ||
import { compact as arrCompact } from './array'; | ||
|
||
export function compact<T = any>(array: T[]): T[]; | ||
export function compact<T extends Object>(object: T): Partial<T>; | ||
/** | ||
* #### Compact | ||
* | ||
* Filters falsy values of the given array | ||
* Removes `null` and `undefined` values and their keys from an object | ||
* | ||
* * * * | ||
* Example usage: | ||
* ```typescript | ||
* import { compact } from "@thalesrc/js-utils"; | ||
* | ||
* const arr = [undefined, "", false, 0, 1, "1"]; | ||
* const compacted = compact(arr); // [1, "1"]; | ||
* | ||
* const object = { | ||
* x: null, | ||
* y: undefined, | ||
* z: 20 | ||
* }; | ||
* | ||
* const compacted = compact(object); // {z: 20} | ||
* ``` | ||
* * * * | ||
* @param arrayOrObject Array or Object to compact | ||
*/ | ||
export function compact<T extends (U[] | Object), U>(arrayOrObject: T): T extends U[] ? U[] : Object { | ||
if (arrayOrObject instanceof Array) { | ||
return arrCompact(arrayOrObject) as any; | ||
} | ||
|
||
if (arrayOrObject instanceof Object) { | ||
return objCompact(arrayOrObject) as any; | ||
} | ||
|
||
throw new TypeError('Value is not object nor array'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'jest'; | ||
|
||
import { compact } from './compact'; | ||
|
||
describe('Compact Function', () => { | ||
it('should delete null values', () => { | ||
const a = {x: null, y: null, z: 20}; | ||
|
||
expect(compact(a)).toEqual({z: 20}); | ||
}); | ||
|
||
it('should delete undefined values', () => { | ||
const a = {x: undefined, y: undefined, z: 20}; | ||
|
||
expect(compact(a)).toEqual({z: 20}); | ||
}); | ||
|
||
it('should delete undefined/null values', () => { | ||
const a = {x: undefined, y: null, z: 20}; | ||
|
||
expect(compact(a)).toEqual({z: 20}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* #### Removes `null` and `undefined` values and their keys from an object | ||
* | ||
* * * * * | ||
* Example usage: | ||
* ```typescript | ||
* import { compact } from "@thalesrc/js-utils/object"; | ||
* | ||
* const a = { | ||
* x: null, | ||
* y: undefined, | ||
* z: 20 | ||
* }; | ||
* | ||
* compact(a); // {z: 20} | ||
* | ||
* ``` | ||
* Static usage example: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/object/static/compact"; | ||
* | ||
* const a = { | ||
* x: null, | ||
* y: undefined, | ||
* z: 20 | ||
* }; | ||
* | ||
* Object.compact(a); // {z: 20} | ||
* ``` | ||
* * * * | ||
* @param object Object delete empty keys | ||
* @returns Compacted object | ||
*/ | ||
export function compact<T extends Object>(object: T): Partial<T> { | ||
const newObject = {...object}; | ||
|
||
for (const [key, value] of Object.entries(newObject)) { | ||
if (value === undefined || value === null) { | ||
delete newObject[key]; | ||
} | ||
} | ||
|
||
return newObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './clone'; | ||
export * from './compact'; | ||
export * from './deepest'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'jest'; | ||
|
||
import './compact'; | ||
import { compact } from '../compact'; | ||
|
||
describe('Compact Static Function', () => { | ||
it('should act as same', () => { | ||
const foo = {x: undefined, y: null, z: 20}; | ||
const bar = compact(foo); | ||
const baz = Object.compact(foo); | ||
|
||
expect(bar).toEqual(baz); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { compact } from '../compact'; | ||
|
||
declare global { | ||
export interface ObjectConstructor { | ||
/** | ||
* #### Removes `null` and `undefined` values and their keys from an object | ||
* | ||
* * * * * | ||
* Example usage: | ||
* ```typescript | ||
* import "@thalesrc/js-utils/object/static/compact"; | ||
* | ||
* const a = { | ||
* x: null, | ||
* y: undefined, | ||
* z: 20 | ||
* }; | ||
* | ||
* Object.compact(a); // {z: 20} | ||
* ``` | ||
* * * * | ||
* @param object Object delete empty keys | ||
* @returns Compacted object | ||
*/ | ||
compact: typeof compact; | ||
} | ||
} | ||
|
||
Object.compact = compact; |