Skip to content

Commit

Permalink
feat(Object): created compact
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Aug 7, 2020
1 parent 12c0730 commit 8c9cc7e
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 2 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ const clonedObject = clone(object);
// object.b.d === clonedObject.b.d // false
```

#### [Compact](https://thalesrc.github.io/js-utils/modules/_object_compact_.html)
Removes `null` and `undefined` values and their keys from an object

```typescript
import { compact } from "@thalesrc/js-utils/object";

const a = {
x: null,
y: undefined,
z: 20
};

compact(a); // {z: 20}
```

#### [Deepest](https://thalesrc.github.io/js-utils/modules/_object_deepest_.html)
Get deepest value in an object chain

Expand Down Expand Up @@ -308,6 +323,25 @@ limit(str, 3); // 'foo'

### Etc.

#### [Compact](https://thalesrc.github.io/js-utils/modules/_compact_.html)
Filters falsy values of the given array
Removes `null` and `undefined` values and their keys from an object

```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}
```

#### [Is Falsy](https://thalesrc.github.io/js-utils/modules/_is_falsy_.html)
Returns whether the entered value is falsy

Expand Down
27 changes: 27 additions & 0 deletions src/compact.spec.ts
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);
});
});
42 changes: 42 additions & 0 deletions src/compact.ts
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');
}

5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export { ReplaceByMapOptions, ReplaceItemsOptions, TInclusion, TSubstraction, asyncMap, compact, difference, findByKey, intersection, remove, replace, uniquify } from './array';
export { ReplaceByMapOptions, ReplaceItemsOptions, TInclusion, TSubstraction, asyncMap, difference, findByKey, intersection, remove, replace, uniquify } from './array';
export * from './function';
export * from './map';
export * from './math';
export * from './object';
export { clone, deepest } from './object';
export * from './promise';
export { } from './string';

export * from './compact';
export * from './is-falsy';
export * from './is-truthy';
export * from './open-promise';
Expand Down
23 changes: 23 additions & 0 deletions src/object/compact.spec.ts
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});
});
});
44 changes: 44 additions & 0 deletions src/object/compact.ts
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;
}
1 change: 1 addition & 0 deletions src/object/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './clone';
export * from './compact';
export * from './deepest';
14 changes: 14 additions & 0 deletions src/object/static/compact.spec.ts
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);
});
});
29 changes: 29 additions & 0 deletions src/object/static/compact.ts
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;

0 comments on commit 8c9cc7e

Please sign in to comment.