remove-anything 1.0.1 → 1.0.3

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 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`) from an object. A small and simple integration.
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,6 +27,28 @@ const no1 = removeProp(payload, 1)
27
27
  no1 // { b: undefined }
28
28
  ```
29
29
 
30
+ ### Remove multiple props
31
+
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
+ // { a: 1 }
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
+
30
52
  ## Meet the family
31
53
 
32
54
  - [copy-anything 🎭](https://github.com/mesqueeb/copy-anything)
@@ -36,4 +58,3 @@ no1 // { b: undefined }
36
58
  - [compare-anything 🛰](https://github.com/mesqueeb/compare-anything)
37
59
  - [flatten-anything 🏏](https://github.com/mesqueeb/flatten-anything)
38
60
  - [is-what 🙉](https://github.com/mesqueeb/is-what)
39
-
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 any of those in `valuesToRemove`
6
+ * Recursively remove props from an object, if the prop's value matches `valueToRemove`
9
7
  */
10
- function removeProps(payload, valuesToRemove = []) {
11
- if (!isWhat.isPlainObject(payload) || !isWhat.isFullArray(valuesToRemove))
8
+ function removeProp(payload, valueToRemove, ...valuesToRemove) {
9
+ if (!isWhat.isPlainObject(payload))
12
10
  return payload;
13
- const removeEmptyObjects = !!valuesToRemove.find((val) => isWhat.isEmptyObject(val));
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 (valuesToRemove.includes(value))
17
+ if (removeEmptyArrays && isWhat.isEmptyArray(value))
18
+ return carry;
19
+ if (remove.includes(value))
18
20
  return carry;
19
- const newVal = removeProps(value, valuesToRemove);
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
- * Recursively remove props from an object, if the prop's value matches `valueToRemove`
31
+ * @deprecated use `removeProp` instead and pass multiple parameters
28
32
  */
29
- function removeProp(payload, valueToRemove) {
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, isFullArray, isEmptyObject } from 'is-what';
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 any of those in `valuesToRemove`
4
+ * Recursively remove props from an object, if the prop's value matches `valueToRemove`
5
5
  */
6
- function removeProps(payload, valuesToRemove = []) {
7
- if (!isPlainObject(payload) || !isFullArray(valuesToRemove))
6
+ function removeProp(payload, valueToRemove, ...valuesToRemove) {
7
+ if (!isPlainObject(payload))
8
8
  return payload;
9
- const removeEmptyObjects = !!valuesToRemove.find((val) => isEmptyObject(val));
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 (valuesToRemove.includes(value))
15
+ if (removeEmptyArrays && isEmptyArray(value))
16
+ return carry;
17
+ if (remove.includes(value))
14
18
  return carry;
15
- const newVal = removeProps(value, valuesToRemove);
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
- * Recursively remove props from an object, if the prop's value matches `valueToRemove`
29
+ * @deprecated use `removeProp` instead and pass multiple parameters
24
30
  */
25
- function removeProp(payload, valueToRemove) {
26
- return removeProps(payload, [valueToRemove]);
27
- }
31
+ const removeProps = (payload, valuesToRemove) => removeProp(payload, valuesToRemove[0], ...valuesToRemove);
28
32
 
29
33
  export { removeProp, removeProps };
@@ -1,5 +1,8 @@
1
1
  /**
2
- * Recursively remove props from an object, if the prop's value matches any of those in `valuesToRemove`
2
+ * Recursively remove props from an object, if the prop's value matches `valueToRemove`
3
3
  */
4
- export declare function removeProps(payload: Record<string, any>, valuesToRemove?: any[]): Record<string, unknown>;
5
- export declare function removeProp<Payload extends Record<string, any>>(payload: Payload, valueToRemove: any): Payload;
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,7 +2,7 @@
2
2
  "name": "remove-anything",
3
3
  "sideEffects": false,
4
4
  "type": "module",
5
- "version": "1.0.1",
5
+ "version": "1.0.3",
6
6
  "description": "Remove any prop (eg. `undefined`) from an object",
7
7
  "module": "./dist/index.es.js",
8
8
  "main": "./dist/index.cjs",
@@ -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 -c=./scripts/build.js --bundleConfigAsCjs",
27
27
  "release": "npm run lint && del dist && npm run build && np"
28
28
  },
29
29
  "dependencies": {
30
- "is-what": "^4.1.1"
30
+ "is-what": "^4.1.7"
31
31
  },
32
32
  "devDependencies": {
33
- "@typescript-eslint/eslint-plugin": "^5.10.1",
34
- "@typescript-eslint/parser": "^5.10.1",
35
- "del-cli": "^4.0.1",
36
- "eslint": "^8.7.0",
37
- "eslint-config-prettier": "^8.3.0",
33
+ "@typescript-eslint/eslint-plugin": "^5.40.1",
34
+ "@typescript-eslint/parser": "^5.40.1",
35
+ "del-cli": "^5.0.0",
36
+ "eslint": "^8.26.0",
37
+ "eslint-config-prettier": "^8.5.0",
38
38
  "eslint-plugin-tree-shaking": "^1.10.0",
39
- "np": "^7.6.0",
40
- "prettier": "^2.5.1",
41
- "rollup": "^2.66.0",
42
- "rollup-plugin-typescript2": "^0.31.1",
43
- "typescript": "^4.5.5",
44
- "vitest": "^0.2.1"
39
+ "np": "^7.6.2",
40
+ "prettier": "^2.7.1",
41
+ "rollup": "^3.2.3",
42
+ "rollup-plugin-typescript2": "^0.34.1",
43
+ "typescript": "^4.8.4",
44
+ "vitest": "^0.24.3"
45
45
  },
46
46
  "keywords": [
47
47
  "remove",