@common.js/filter-obj 5.1.0
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 +5 -0
- package/index.d.ts +75 -0
- package/index.js +86 -0
- package/license +9 -0
- package/package.json +28 -0
package/README.md
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Filter object keys and values into a new object.
|
|
3
|
+
|
|
4
|
+
@param object - The source object to filter properties from.
|
|
5
|
+
@param predicate - Predicate function that determines whether a property should be assigned to the new object.
|
|
6
|
+
@param keys - Property keys that should be assigned to the new object.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import {includeKeys} from 'filter-obj';
|
|
11
|
+
|
|
12
|
+
const object = {
|
|
13
|
+
foo: true,
|
|
14
|
+
bar: false
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const newObject = includeKeys(object, (key, value) => value === true);
|
|
18
|
+
//=> {foo: true}
|
|
19
|
+
|
|
20
|
+
const newObject2 = includeKeys(object, ['bar']);
|
|
21
|
+
//=> {bar: false}
|
|
22
|
+
```
|
|
23
|
+
*/
|
|
24
|
+
export function includeKeys<ObjectType extends Record<PropertyKey, any>>(
|
|
25
|
+
object: ObjectType,
|
|
26
|
+
predicate: (
|
|
27
|
+
key: keyof ObjectType,
|
|
28
|
+
value: ObjectType[keyof ObjectType]
|
|
29
|
+
) => boolean
|
|
30
|
+
): Partial<ObjectType>;
|
|
31
|
+
export function includeKeys<
|
|
32
|
+
ObjectType extends Record<PropertyKey, any>,
|
|
33
|
+
IncludedKeys extends keyof ObjectType,
|
|
34
|
+
>(
|
|
35
|
+
object: ObjectType,
|
|
36
|
+
keys: readonly IncludedKeys[]
|
|
37
|
+
): Pick<ObjectType, IncludedKeys>;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
Filter object keys and values into a new object.
|
|
41
|
+
|
|
42
|
+
@param object - The source object to filter properties from.
|
|
43
|
+
@param predicate - Predicate function that determines whether a property should not be assigned to the new object.
|
|
44
|
+
@param keys - Property keys that should not be assigned to the new object.
|
|
45
|
+
|
|
46
|
+
@example
|
|
47
|
+
```
|
|
48
|
+
import {excludeKeys} from 'filter-obj';
|
|
49
|
+
|
|
50
|
+
const object = {
|
|
51
|
+
foo: true,
|
|
52
|
+
bar: false
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const newObject = excludeKeys(object, (key, value) => value === true);
|
|
56
|
+
//=> {bar: false}
|
|
57
|
+
|
|
58
|
+
const newObject3 = excludeKeys(object, ['bar']);
|
|
59
|
+
//=> {foo: true}
|
|
60
|
+
```
|
|
61
|
+
*/
|
|
62
|
+
export function excludeKeys<ObjectType extends Record<PropertyKey, any>>(
|
|
63
|
+
object: ObjectType,
|
|
64
|
+
predicate: (
|
|
65
|
+
key: keyof ObjectType,
|
|
66
|
+
value: ObjectType[keyof ObjectType]
|
|
67
|
+
) => boolean
|
|
68
|
+
): Partial<ObjectType>;
|
|
69
|
+
export function excludeKeys<
|
|
70
|
+
ObjectType extends Record<PropertyKey, any>,
|
|
71
|
+
ExcludedKeys extends keyof ObjectType,
|
|
72
|
+
>(
|
|
73
|
+
object: ObjectType,
|
|
74
|
+
keys: readonly ExcludedKeys[]
|
|
75
|
+
): Omit<ObjectType, ExcludedKeys>;
|
package/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
includeKeys: function() {
|
|
13
|
+
return includeKeys;
|
|
14
|
+
},
|
|
15
|
+
excludeKeys: function() {
|
|
16
|
+
return excludeKeys;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
function includeKeys(object, predicate) {
|
|
20
|
+
var result = {};
|
|
21
|
+
if (Array.isArray(predicate)) {
|
|
22
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
23
|
+
try {
|
|
24
|
+
for(var _iterator = predicate[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
25
|
+
var key = _step.value;
|
|
26
|
+
var descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
27
|
+
if (descriptor === null || descriptor === void 0 ? void 0 : descriptor.enumerable) {
|
|
28
|
+
Object.defineProperty(result, key, descriptor);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
} catch (err) {
|
|
32
|
+
_didIteratorError = true;
|
|
33
|
+
_iteratorError = err;
|
|
34
|
+
} finally{
|
|
35
|
+
try {
|
|
36
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
37
|
+
_iterator.return();
|
|
38
|
+
}
|
|
39
|
+
} finally{
|
|
40
|
+
if (_didIteratorError) {
|
|
41
|
+
throw _iteratorError;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = undefined;
|
|
47
|
+
try {
|
|
48
|
+
// `Reflect.ownKeys()` is required to retrieve symbol properties
|
|
49
|
+
for(var _iterator1 = Reflect.ownKeys(object)[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true){
|
|
50
|
+
var key1 = _step1.value;
|
|
51
|
+
var descriptor1 = Object.getOwnPropertyDescriptor(object, key1);
|
|
52
|
+
if (descriptor1.enumerable) {
|
|
53
|
+
var value = object[key1];
|
|
54
|
+
if (predicate(key1, value, object)) {
|
|
55
|
+
Object.defineProperty(result, key1, descriptor1);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch (err) {
|
|
60
|
+
_didIteratorError1 = true;
|
|
61
|
+
_iteratorError1 = err;
|
|
62
|
+
} finally{
|
|
63
|
+
try {
|
|
64
|
+
if (!_iteratorNormalCompletion1 && _iterator1.return != null) {
|
|
65
|
+
_iterator1.return();
|
|
66
|
+
}
|
|
67
|
+
} finally{
|
|
68
|
+
if (_didIteratorError1) {
|
|
69
|
+
throw _iteratorError1;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
function excludeKeys(object, predicate) {
|
|
77
|
+
if (Array.isArray(predicate)) {
|
|
78
|
+
var set = new Set(predicate);
|
|
79
|
+
return includeKeys(object, function(key) {
|
|
80
|
+
return !set.has(key);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return includeKeys(object, function(key, value, object) {
|
|
84
|
+
return !predicate(key, value, object);
|
|
85
|
+
});
|
|
86
|
+
}
|
package/license
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@common.js/filter-obj",
|
|
3
|
+
"version": "5.1.0",
|
|
4
|
+
"description": "filter-obj package exported as CommonJS modules",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "etienne-martin/common.js",
|
|
7
|
+
"funding": "https://github.com/sponsors/sindresorhus",
|
|
8
|
+
"type": "commonjs",
|
|
9
|
+
"types": "./index.d.ts",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=14.16"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "xo && ava && tsd"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"index.js",
|
|
18
|
+
"index.d.ts"
|
|
19
|
+
],
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"ava": "^4.3.1",
|
|
22
|
+
"tsd": "^0.22.0",
|
|
23
|
+
"xo": "^0.51.0"
|
|
24
|
+
},
|
|
25
|
+
"homepage": "https://github.com/etienne-martin/common.js#readme",
|
|
26
|
+
"dependencies": {},
|
|
27
|
+
"main": "./index.js"
|
|
28
|
+
}
|