Skip to content

Commit

Permalink
feat: make use of argument destructing
Browse files Browse the repository at this point in the history
  • Loading branch information
mesqueeb committed Oct 22, 2022
1 parent 855b2d4 commit ca789b7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ const no1 = removeProp(payload, 1)
no1 // { b: undefined }
```

### Remove multiple props

You can keep on passing parameters to remove additional props

```js
const payload = { a: 1, b: undefined }
removeProp(payload, 1, undefined)

// returns
// { a: 1 }
```

### Remove Empty Objects and Arrays

Expand All @@ -47,4 +58,3 @@ removeProps(payload, {}, [])
- [compare-anything 🛰](https://github.com/mesqueeb/compare-anything)
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
- [is-what 🙉](https://github.com/mesqueeb/is-what)

7 changes: 5 additions & 2 deletions dist/index.cjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var isWhat = require('is-what');

/**
Expand All @@ -11,14 +9,19 @@ function removeProps(payload, valuesToRemove = []) {
if (!isWhat.isPlainObject(payload) || !isWhat.isFullArray(valuesToRemove))
return payload;
const removeEmptyObjects = !!valuesToRemove.find((val) => isWhat.isEmptyObject(val));
const removeEmptyArrays = !!valuesToRemove.find((val) => isWhat.isEmptyArray(val));
return Object.entries(payload).reduce((carry, [key, value]) => {
if (removeEmptyObjects && isWhat.isEmptyObject(value))
return carry;
if (removeEmptyArrays && isWhat.isEmptyArray(value))
return carry;
if (valuesToRemove.includes(value))
return carry;
const newVal = removeProps(value, valuesToRemove);
if (removeEmptyObjects && isWhat.isEmptyObject(newVal))
return carry;
if (removeEmptyArrays && isWhat.isEmptyArray(newVal))
return carry;
carry[key] = newVal;
return carry;
}, {});
Expand Down
7 changes: 6 additions & 1 deletion dist/index.es.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPlainObject, isFullArray, isEmptyObject } from 'is-what';
import { isPlainObject, isFullArray, isEmptyObject, isEmptyArray } from 'is-what';

/**
* Recursively remove props from an object, if the prop's value matches any of those in `valuesToRemove`
Expand All @@ -7,14 +7,19 @@ function removeProps(payload, valuesToRemove = []) {
if (!isPlainObject(payload) || !isFullArray(valuesToRemove))
return payload;
const removeEmptyObjects = !!valuesToRemove.find((val) => isEmptyObject(val));
const removeEmptyArrays = !!valuesToRemove.find((val) => isEmptyArray(val));
return Object.entries(payload).reduce((carry, [key, value]) => {
if (removeEmptyObjects && isEmptyObject(value))
return carry;
if (removeEmptyArrays && isEmptyArray(value))
return carry;
if (valuesToRemove.includes(value))
return carry;
const newVal = removeProps(value, valuesToRemove);
if (removeEmptyObjects && isEmptyObject(newVal))
return carry;
if (removeEmptyArrays && isEmptyArray(newVal))
return carry;
carry[key] = newVal;
return carry;
}, {});
Expand Down
33 changes: 15 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { isEmptyObject, isEmptyArray, isFullArray, isPlainObject } from 'is-what'
import { isEmptyObject, isEmptyArray, isPlainObject } from 'is-what'

/**
* Recursively remove props from an object, if the prop's value matches any of those in `valuesToRemove`
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
*/
export function removeProps(
export function removeProp(
payload: Record<string, any>,
valuesToRemove: any[] = []
valueToRemove: any,
...valuesToRemove: any[]
): Record<string, unknown> {
if (!isPlainObject(payload) || !isFullArray(valuesToRemove)) return payload
if (!isPlainObject(payload)) return payload

const remove = [valueToRemove, ...valuesToRemove]

const removeEmptyObjects = !!valuesToRemove.find((val) => isEmptyObject(val))
const removeEmptyArrays = !!valuesToRemove.find((val) => isEmptyArray(val))
const removeEmptyObjects = !!remove.find((val) => isEmptyObject(val))
const removeEmptyArrays = !!remove.find((val) => isEmptyArray(val))

return Object.entries(payload).reduce<Record<string, any>>((carry, [key, value]) => {
if (removeEmptyObjects && isEmptyObject(value)) return carry
if (removeEmptyArrays && isEmptyArray(value)) return carry
if (valuesToRemove.includes(value)) return carry
if (remove.includes(value)) return carry

const newVal = removeProps(value, valuesToRemove)
const newVal = removeProp(value, remove[0], ...remove.slice(1))
if (removeEmptyObjects && isEmptyObject(newVal)) return carry
if (removeEmptyArrays && isEmptyArray(newVal)) return carry

Expand All @@ -26,14 +29,8 @@ export function removeProps(
}, {})
}

// function overload for external use & type accuracy
export function removeProp<Payload extends Record<string, any>>(
payload: Payload,
valueToRemove: any
): Payload
/**
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
* @deprecated use `removeProp` instead and pass multiple parameters
*/
export function removeProp(payload: Record<string, any>, valueToRemove: any): Record<string, any> {
return removeProps(payload, [valueToRemove])
}
export const removeProps = (payload: Record<string, any>, valuesToRemove: any[]) =>
removeProp(payload, valuesToRemove[0], ...valuesToRemove)
16 changes: 16 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ test('removeProp', () => {
expect(removeProp({ a: 1, b: undefined }, undefined)).toEqual({ a: 1 })
expect(removeProp({ a: 1, b: undefined }, 1)).toEqual({ b: undefined })
})

test('removeProp empty objects', () => {
expect(removeProp({}, {})).toEqual({})
expect(removeProp({ a: {} }, {})).toEqual({})
Expand All @@ -13,6 +14,7 @@ test('removeProp empty objects', () => {
expect(removeProp({ a: { b: { c: { d: { e: {} } } }, b2: {} } }, {})).toEqual({})
expect(removeProp({ a: { b: { c: { d: { e: {}, e2: {} } } }, b2: {} } }, {})).toEqual({})
})

test('removeProp empty arrays', () => {
expect(removeProp({}, {})).toEqual({})
expect(removeProp({ a: [] }, [])).toEqual({})
Expand All @@ -27,7 +29,21 @@ test('removeProp empty arrays', () => {
a: { b: { c: { d: {} } } },
})
})

test('removeProp empty objects and undefined', () => {
expect(removeProp({}, {}, undefined)).toEqual({})
expect(removeProp({ a: {} }, {}, undefined)).toEqual({})
expect(removeProp({ a: { b: { c: undefined } } }, {}, undefined)).toEqual({})
expect(removeProp({ a: { b: { c: { d: { e: {} } } } } }, {}, undefined)).toEqual({})
expect(removeProp({ a: { b: { c: { d: { e: {} } } }, b2: undefined } }, {}, undefined)).toEqual(
{}
)
expect(
removeProp({ a: { b: { c: { d: { e: {}, e2: undefined } } }, b2: {} } }, {}, undefined)
).toEqual({})
})

test('removeProps empty objects and undefined', () => {
expect(removeProps({}, [{}, undefined])).toEqual({})
expect(removeProps({ a: {} }, [{}, undefined])).toEqual({})
expect(removeProps({ a: { b: { c: undefined } } }, [{}, undefined])).toEqual({})
Expand Down

0 comments on commit ca789b7

Please sign in to comment.