remove-anything 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +33 -7
- package/dist/index.cjs +14 -12
- package/dist/index.es.js +15 -11
- package/dist/types/index.d.ts +6 -3
- package/package.json +16 -16
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ npm i remove-anything
|
|
|
9
9
|
|
|
10
10
|
Removes props (eg. undefined) from an object
|
|
11
11
|
|
|
12
|
-
An optimised way to remove any prop (eg. `undefined
|
|
12
|
+
An optimised way to remove any prop (eg. `undefined`, empty objects, ...) from an object. A small and simple integration.
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
@@ -27,13 +27,39 @@ const no1 = removeProp(payload, 1)
|
|
|
27
27
|
no1 // { b: undefined }
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
### Remove multiple props
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
You can keep on passing parameters to remove additional props
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
const payload = { a: 1, b: undefined }
|
|
36
|
+
removeProp(payload, 1, undefined)
|
|
37
|
+
|
|
38
|
+
// returns
|
|
39
|
+
// {}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Remove Empty Objects and Arrays
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
const payload = { a: 1, b: undefined, c: {}, d: [] }
|
|
46
|
+
removeProps(payload, {}, [])
|
|
47
|
+
|
|
48
|
+
// returns
|
|
49
|
+
// { a: 1, b: undefined }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Meet the family (more tiny utils with TS support)
|
|
53
|
+
|
|
54
|
+
- [is-what 🙉](https://github.com/mesqueeb/is-what)
|
|
55
|
+
- [is-where 🙈](https://github.com/mesqueeb/is-where)
|
|
33
56
|
- [merge-anything 🥡](https://github.com/mesqueeb/merge-anything)
|
|
57
|
+
- [check-anything 👁](https://github.com/mesqueeb/check-anything)
|
|
58
|
+
- [remove-anything ✂️](https://github.com/mesqueeb/remove-anything)
|
|
59
|
+
- [getorset-anything 🐊](https://github.com/mesqueeb/getorset-anything)
|
|
60
|
+
- [map-anything 🗺](https://github.com/mesqueeb/map-anything)
|
|
34
61
|
- [filter-anything ⚔️](https://github.com/mesqueeb/filter-anything)
|
|
35
|
-
- [
|
|
36
|
-
- [
|
|
62
|
+
- [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
|
|
63
|
+
- [case-anything 🐫](https://github.com/mesqueeb/case-anything)
|
|
37
64
|
- [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
|
|
38
|
-
- [
|
|
39
|
-
|
|
65
|
+
- [nestify-anything 🧅](https://github.com/mesqueeb/nestify-anything)
|
package/dist/index.cjs
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var isWhat = require('is-what');
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
|
-
* Recursively remove props from an object, if the prop's value matches
|
|
6
|
+
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
|
|
9
7
|
*/
|
|
10
|
-
function
|
|
11
|
-
if (!isWhat.isPlainObject(payload)
|
|
8
|
+
function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
9
|
+
if (!isWhat.isPlainObject(payload))
|
|
12
10
|
return payload;
|
|
13
|
-
const
|
|
11
|
+
const remove = [valueToRemove, ...valuesToRemove];
|
|
12
|
+
const removeEmptyObjects = !!remove.find((val) => isWhat.isEmptyObject(val));
|
|
13
|
+
const removeEmptyArrays = !!remove.find((val) => isWhat.isEmptyArray(val));
|
|
14
14
|
return Object.entries(payload).reduce((carry, [key, value]) => {
|
|
15
15
|
if (removeEmptyObjects && isWhat.isEmptyObject(value))
|
|
16
16
|
return carry;
|
|
17
|
-
if (
|
|
17
|
+
if (removeEmptyArrays && isWhat.isEmptyArray(value))
|
|
18
|
+
return carry;
|
|
19
|
+
if (remove.includes(value))
|
|
18
20
|
return carry;
|
|
19
|
-
const newVal =
|
|
21
|
+
const newVal = removeProp(value, remove[0], ...remove.slice(1));
|
|
20
22
|
if (removeEmptyObjects && isWhat.isEmptyObject(newVal))
|
|
21
23
|
return carry;
|
|
24
|
+
if (removeEmptyArrays && isWhat.isEmptyArray(newVal))
|
|
25
|
+
return carry;
|
|
22
26
|
carry[key] = newVal;
|
|
23
27
|
return carry;
|
|
24
28
|
}, {});
|
|
25
29
|
}
|
|
26
30
|
/**
|
|
27
|
-
*
|
|
31
|
+
* @deprecated use `removeProp` instead and pass multiple parameters
|
|
28
32
|
*/
|
|
29
|
-
|
|
30
|
-
return removeProps(payload, [valueToRemove]);
|
|
31
|
-
}
|
|
33
|
+
const removeProps = (payload, valuesToRemove) => removeProp(payload, valuesToRemove[0], ...valuesToRemove);
|
|
32
34
|
|
|
33
35
|
exports.removeProp = removeProp;
|
|
34
36
|
exports.removeProps = removeProps;
|
package/dist/index.es.js
CHANGED
|
@@ -1,29 +1,33 @@
|
|
|
1
|
-
import { isPlainObject,
|
|
1
|
+
import { isPlainObject, isEmptyObject, isEmptyArray } from 'is-what';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Recursively remove props from an object, if the prop's value matches
|
|
4
|
+
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
|
|
5
5
|
*/
|
|
6
|
-
function
|
|
7
|
-
if (!isPlainObject(payload)
|
|
6
|
+
function removeProp(payload, valueToRemove, ...valuesToRemove) {
|
|
7
|
+
if (!isPlainObject(payload))
|
|
8
8
|
return payload;
|
|
9
|
-
const
|
|
9
|
+
const remove = [valueToRemove, ...valuesToRemove];
|
|
10
|
+
const removeEmptyObjects = !!remove.find((val) => isEmptyObject(val));
|
|
11
|
+
const removeEmptyArrays = !!remove.find((val) => isEmptyArray(val));
|
|
10
12
|
return Object.entries(payload).reduce((carry, [key, value]) => {
|
|
11
13
|
if (removeEmptyObjects && isEmptyObject(value))
|
|
12
14
|
return carry;
|
|
13
|
-
if (
|
|
15
|
+
if (removeEmptyArrays && isEmptyArray(value))
|
|
16
|
+
return carry;
|
|
17
|
+
if (remove.includes(value))
|
|
14
18
|
return carry;
|
|
15
|
-
const newVal =
|
|
19
|
+
const newVal = removeProp(value, remove[0], ...remove.slice(1));
|
|
16
20
|
if (removeEmptyObjects && isEmptyObject(newVal))
|
|
17
21
|
return carry;
|
|
22
|
+
if (removeEmptyArrays && isEmptyArray(newVal))
|
|
23
|
+
return carry;
|
|
18
24
|
carry[key] = newVal;
|
|
19
25
|
return carry;
|
|
20
26
|
}, {});
|
|
21
27
|
}
|
|
22
28
|
/**
|
|
23
|
-
*
|
|
29
|
+
* @deprecated use `removeProp` instead and pass multiple parameters
|
|
24
30
|
*/
|
|
25
|
-
|
|
26
|
-
return removeProps(payload, [valueToRemove]);
|
|
27
|
-
}
|
|
31
|
+
const removeProps = (payload, valuesToRemove) => removeProp(payload, valuesToRemove[0], ...valuesToRemove);
|
|
28
32
|
|
|
29
33
|
export { removeProp, removeProps };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Recursively remove props from an object, if the prop's value matches
|
|
2
|
+
* Recursively remove props from an object, if the prop's value matches `valueToRemove`
|
|
3
3
|
*/
|
|
4
|
-
export declare function
|
|
5
|
-
|
|
4
|
+
export declare function removeProp(payload: Record<string, any>, valueToRemove: any, ...valuesToRemove: any[]): Record<string, unknown>;
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated use `removeProp` instead and pass multiple parameters
|
|
7
|
+
*/
|
|
8
|
+
export declare const removeProps: (payload: Record<string, any>, valuesToRemove: any[]) => Record<string, unknown>;
|
package/package.json
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
"name": "remove-anything",
|
|
3
3
|
"sideEffects": false,
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "1.0.
|
|
5
|
+
"version": "1.0.4",
|
|
6
6
|
"description": "Remove any prop (eg. `undefined`) from an object",
|
|
7
7
|
"module": "./dist/index.es.js",
|
|
8
8
|
"main": "./dist/index.cjs",
|
|
9
9
|
"types": "./dist/types/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"types": "./dist/types/index.d.ts",
|
|
12
13
|
"import": "./dist/index.es.js",
|
|
13
|
-
"require": "./dist/index.cjs"
|
|
14
|
-
"types": "./dist/types/index.d.ts"
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
@@ -23,25 +23,25 @@
|
|
|
23
23
|
"scripts": {
|
|
24
24
|
"test": "vitest run",
|
|
25
25
|
"lint": "tsc --noEmit && eslint ./src --ext .ts",
|
|
26
|
-
"build": "rollup -c ./scripts/build.js",
|
|
26
|
+
"build": "rollup --bundleConfigAsCjs -c ./scripts/build.js",
|
|
27
27
|
"release": "npm run lint && del dist && npm run build && np"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"is-what": "^4.1.
|
|
30
|
+
"is-what": "^4.1.8"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
34
|
-
"@typescript-eslint/parser": "^5.
|
|
35
|
-
"del-cli": "^
|
|
36
|
-
"eslint": "^8.
|
|
37
|
-
"eslint-config-prettier": "^8.
|
|
33
|
+
"@typescript-eslint/eslint-plugin": "^5.59.2",
|
|
34
|
+
"@typescript-eslint/parser": "^5.59.2",
|
|
35
|
+
"del-cli": "^5.0.0",
|
|
36
|
+
"eslint": "^8.40.0",
|
|
37
|
+
"eslint-config-prettier": "^8.8.0",
|
|
38
38
|
"eslint-plugin-tree-shaking": "^1.10.0",
|
|
39
|
-
"np": "^7.
|
|
40
|
-
"prettier": "^2.
|
|
41
|
-
"rollup": "^
|
|
42
|
-
"rollup-plugin-typescript2": "^0.
|
|
43
|
-
"typescript": "^4.
|
|
44
|
-
"vitest": "^0.
|
|
39
|
+
"np": "^7.7.0",
|
|
40
|
+
"prettier": "^2.8.8",
|
|
41
|
+
"rollup": "^3.21.5",
|
|
42
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
43
|
+
"typescript": "^4.9.5",
|
|
44
|
+
"vitest": "^0.31.0"
|
|
45
45
|
},
|
|
46
46
|
"keywords": [
|
|
47
47
|
"remove",
|