@stacksjs/objects 0.57.3 โ 0.58.19
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 -5
- package/dist/index.js +64 -0
- package/package.json +30 -21
- package/LICENSE.md +0 -21
- package/dist/index.d.ts +0 -76
- package/dist/index.mjs +0 -57
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ Easily work with objects.
|
|
|
10
10
|
## ๐ค Usage
|
|
11
11
|
|
|
12
12
|
```bash
|
|
13
|
-
|
|
13
|
+
bun install -d @stacksjs/objects
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
Now, you can easily access it in your project:
|
|
@@ -29,12 +29,12 @@ const collection = collect([{
|
|
|
29
29
|
console.log(collection.avg('pages')) // 636
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
To view the full documentation, please visit [https://stacksjs.
|
|
32
|
+
To view the full documentation, please visit [https://stacksjs.org/objects](https://stacksjs.org/objects).
|
|
33
33
|
|
|
34
34
|
## ๐งช Testing
|
|
35
35
|
|
|
36
36
|
```bash
|
|
37
|
-
|
|
37
|
+
bun test
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
## ๐ Changelog
|
|
@@ -53,7 +53,7 @@ For help, discussion about best practices, or any other conversation that would
|
|
|
53
53
|
|
|
54
54
|
For casual chit-chat with others using this package:
|
|
55
55
|
|
|
56
|
-
[Join the Stacks Discord Server](https://discord.
|
|
56
|
+
[Join the Stacks Discord Server](https://discord.gg/stacksjs)
|
|
57
57
|
|
|
58
58
|
## ๐๐ผ Credits
|
|
59
59
|
|
|
@@ -68,4 +68,4 @@ Many thanks to the following core technologies & people who have contributed to
|
|
|
68
68
|
|
|
69
69
|
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
|
|
70
70
|
|
|
71
|
-
Made with
|
|
71
|
+
Made with ๐
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
import {notNullish} from "@stacksjs/utils";
|
|
4
|
+
import {isObject} from "@stacksjs/validation";
|
|
5
|
+
function objectMap(obj, fn) {
|
|
6
|
+
return Object.fromEntries(Object.entries(obj).map(([k, v]) => fn(k, v)).filter(notNullish));
|
|
7
|
+
}
|
|
8
|
+
function isKeyOf(obj, k) {
|
|
9
|
+
return k in obj;
|
|
10
|
+
}
|
|
11
|
+
function objectKeys(obj) {
|
|
12
|
+
return Object.keys(obj);
|
|
13
|
+
}
|
|
14
|
+
function objectEntries(obj) {
|
|
15
|
+
return Object.entries(obj);
|
|
16
|
+
}
|
|
17
|
+
function deepMerge(target, ...sources) {
|
|
18
|
+
if (!sources.length)
|
|
19
|
+
return target;
|
|
20
|
+
const source = sources.shift();
|
|
21
|
+
if (source === undefined)
|
|
22
|
+
return target;
|
|
23
|
+
if (isMergeableObject(target) && isMergeableObject(source)) {
|
|
24
|
+
objectKeys(source).forEach((key) => {
|
|
25
|
+
if (isMergeableObject(source[key])) {
|
|
26
|
+
if (!target[key])
|
|
27
|
+
target[key] = {};
|
|
28
|
+
deepMerge(target[key], source[key]);
|
|
29
|
+
} else {
|
|
30
|
+
target[key] = source[key];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return deepMerge(target, ...sources);
|
|
35
|
+
}
|
|
36
|
+
var isMergeableObject = function(item) {
|
|
37
|
+
return isObject(item) && !Array.isArray(item);
|
|
38
|
+
};
|
|
39
|
+
function objectPick(obj, keys, omitUndefined = false) {
|
|
40
|
+
return keys.reduce((n, k) => {
|
|
41
|
+
if (k in obj) {
|
|
42
|
+
if (!omitUndefined || obj[k] !== undefined)
|
|
43
|
+
n[k] = obj[k];
|
|
44
|
+
}
|
|
45
|
+
return n;
|
|
46
|
+
}, {});
|
|
47
|
+
}
|
|
48
|
+
function clearUndefined(obj) {
|
|
49
|
+
Object.keys(obj).forEach((key) => obj[key] === undefined ? delete obj[key] : {});
|
|
50
|
+
return obj;
|
|
51
|
+
}
|
|
52
|
+
function hasOwnProperty(obj, v) {
|
|
53
|
+
return obj == null ? false : Object.prototype.hasOwnProperty.call(obj, v);
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
objectPick,
|
|
57
|
+
objectMap,
|
|
58
|
+
objectKeys,
|
|
59
|
+
objectEntries,
|
|
60
|
+
isKeyOf,
|
|
61
|
+
hasOwnProperty,
|
|
62
|
+
deepMerge,
|
|
63
|
+
clearUndefined
|
|
64
|
+
};
|
package/package.json
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/objects",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@8.6.6",
|
|
4
|
+
"version": "0.58.19",
|
|
6
5
|
"description": "The Stacks objects.",
|
|
7
6
|
"author": "Chris Breuer",
|
|
8
7
|
"license": "MIT",
|
|
9
8
|
"funding": "https://github.com/sponsors/chrisbbreuer",
|
|
10
|
-
"homepage": "https://github.com/stacksjs/stacks/tree/main
|
|
9
|
+
"homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/src/objects#readme",
|
|
11
10
|
"repository": {
|
|
12
11
|
"type": "git",
|
|
13
12
|
"url": "git+https://github.com/stacksjs/stacks.git",
|
|
14
|
-
"directory": "
|
|
13
|
+
"directory": "./storage/framework/core/src/objects"
|
|
15
14
|
},
|
|
16
15
|
"bugs": {
|
|
17
16
|
"url": "https://github.com/stacksjs/stacks/issues"
|
|
@@ -24,31 +23,41 @@
|
|
|
24
23
|
],
|
|
25
24
|
"exports": {
|
|
26
25
|
".": {
|
|
27
|
-
"
|
|
28
|
-
"import": "./dist/index.
|
|
26
|
+
"bun": "./src/index.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./*": {
|
|
30
|
+
"bun": "./src/*",
|
|
31
|
+
"import": "./dist/*"
|
|
29
32
|
}
|
|
30
33
|
},
|
|
31
|
-
"module": "dist/index.
|
|
34
|
+
"module": "dist/index.js",
|
|
32
35
|
"types": "dist/index.d.ts",
|
|
33
36
|
"contributors": [
|
|
34
|
-
"Chris Breuer <chris@
|
|
37
|
+
"Chris Breuer <chris@stacksjs.org>"
|
|
35
38
|
],
|
|
36
39
|
"files": [
|
|
37
|
-
"
|
|
38
|
-
"
|
|
40
|
+
"README.md",
|
|
41
|
+
"dist"
|
|
39
42
|
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "bun --bun build.ts",
|
|
45
|
+
"typecheck": "bun --bun tsc --noEmit",
|
|
46
|
+
"prepublishOnly": "bun --bun run build"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@stacksjs/collections": "workspace:*",
|
|
50
|
+
"@stacksjs/types": "workspace:*",
|
|
51
|
+
"@stacksjs/utils": "workspace:*",
|
|
52
|
+
"@stacksjs/validation": "workspace:*"
|
|
53
|
+
},
|
|
40
54
|
"dependencies": {
|
|
41
|
-
"@stacksjs/collections": "
|
|
42
|
-
"@stacksjs/types": "
|
|
43
|
-
"@stacksjs/utils": "
|
|
44
|
-
"@stacksjs/validation": "
|
|
55
|
+
"@stacksjs/collections": "workspace:*",
|
|
56
|
+
"@stacksjs/types": "workspace:*",
|
|
57
|
+
"@stacksjs/utils": "workspace:*",
|
|
58
|
+
"@stacksjs/validation": "workspace:*"
|
|
45
59
|
},
|
|
46
60
|
"devDependencies": {
|
|
47
|
-
"@stacksjs/development": "
|
|
48
|
-
},
|
|
49
|
-
"scripts": {
|
|
50
|
-
"build": "unbuild",
|
|
51
|
-
"dev": "unbuild --stub",
|
|
52
|
-
"typecheck": "tsc --noEmit"
|
|
61
|
+
"@stacksjs/development": "workspace:*"
|
|
53
62
|
}
|
|
54
|
-
}
|
|
63
|
+
}
|
package/LICENSE.md
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Open Web Foundation
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/index.d.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import type { DeepMerge } from '@stacksjs/types';
|
|
2
|
-
export { Collection, collect } from '@stacksjs/collections';
|
|
3
|
-
/**
|
|
4
|
-
* Map key/value pairs for an object, and construct a new one
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @category Object
|
|
8
|
-
*
|
|
9
|
-
* Transform:
|
|
10
|
-
* @example
|
|
11
|
-
* ```
|
|
12
|
-
* objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()])
|
|
13
|
-
* // { A: '1', B: '2' }
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* Swap key/value:
|
|
17
|
-
* @example
|
|
18
|
-
* ```
|
|
19
|
-
* objectMap({ a: 1, b: 2 }, (k, v) => [v, k])
|
|
20
|
-
* // { 1: 'a', 2: 'b' }
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* Filter keys:
|
|
24
|
-
* @example
|
|
25
|
-
* ```
|
|
26
|
-
* objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v])
|
|
27
|
-
* // { b: 2 }
|
|
28
|
-
* ```
|
|
29
|
-
*/
|
|
30
|
-
export declare function objectMap<K extends string, V, NK = K, NV = V>(obj: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<K, V>;
|
|
31
|
-
/**
|
|
32
|
-
* Type guard for any key, `k`.
|
|
33
|
-
* Marks `k` as a key of `T` if `k` is in `obj`.
|
|
34
|
-
*
|
|
35
|
-
* @category Object
|
|
36
|
-
* @param obj object to query for key `k`
|
|
37
|
-
* @param k key to check existence in `obj`
|
|
38
|
-
*/
|
|
39
|
-
export declare function isKeyOf<T extends object>(obj: T, k: keyof any): k is keyof T;
|
|
40
|
-
/**
|
|
41
|
-
* Strict typed `Object.keys`
|
|
42
|
-
*
|
|
43
|
-
* @category Object
|
|
44
|
-
*/
|
|
45
|
-
export declare function objectKeys<T extends object>(obj: T): (`${keyof T & undefined}` | `${keyof T & null}` | `${keyof T & string}` | `${keyof T & number}` | `${keyof T & false}` | `${keyof T & true}`)[];
|
|
46
|
-
/**
|
|
47
|
-
* Strict typed `Object.entries`
|
|
48
|
-
*
|
|
49
|
-
* @category Object
|
|
50
|
-
*/
|
|
51
|
-
export declare function objectEntries<T extends object>(obj: T): [keyof T, T[keyof T]][];
|
|
52
|
-
/**
|
|
53
|
-
* Deep merge :P
|
|
54
|
-
*
|
|
55
|
-
* @category Object
|
|
56
|
-
*/
|
|
57
|
-
export declare function deepMerge<T extends object = object, S extends object = T>(target: T, ...sources: S[]): DeepMerge<T, S>;
|
|
58
|
-
/**
|
|
59
|
-
* Create a new subset object by providing keys.
|
|
60
|
-
*
|
|
61
|
-
* @category Object
|
|
62
|
-
*/
|
|
63
|
-
export declare function objectPick<O extends object, T extends keyof O>(obj: O, keys: T[], omitUndefined?: boolean): Pick<O, T>;
|
|
64
|
-
/**
|
|
65
|
-
* Clear undefined fields from an object. It mutates the object.
|
|
66
|
-
*
|
|
67
|
-
* @category Object
|
|
68
|
-
*/
|
|
69
|
-
export declare function clearUndefined<T extends object>(obj: T): T;
|
|
70
|
-
/**
|
|
71
|
-
* Determines whether an object has a property with the specified name
|
|
72
|
-
*
|
|
73
|
-
* @see https://eslint.org/docs/rules/no-prototype-builtins
|
|
74
|
-
* @category Object
|
|
75
|
-
*/
|
|
76
|
-
export declare function hasOwnProperty<T>(obj: T, v: PropertyKey): any;
|
package/dist/index.mjs
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { notNullish } from "@stacksjs/utils";
|
|
2
|
-
import { isObject } from "@stacksjs/validation";
|
|
3
|
-
export { Collection, collect } from "@stacksjs/collections";
|
|
4
|
-
export function objectMap(obj, fn) {
|
|
5
|
-
return Object.fromEntries(
|
|
6
|
-
Object.entries(obj).map(([k, v]) => fn(k, v)).filter(notNullish)
|
|
7
|
-
);
|
|
8
|
-
}
|
|
9
|
-
export function isKeyOf(obj, k) {
|
|
10
|
-
return k in obj;
|
|
11
|
-
}
|
|
12
|
-
export function objectKeys(obj) {
|
|
13
|
-
return Object.keys(obj);
|
|
14
|
-
}
|
|
15
|
-
export function objectEntries(obj) {
|
|
16
|
-
return Object.entries(obj);
|
|
17
|
-
}
|
|
18
|
-
export function deepMerge(target, ...sources) {
|
|
19
|
-
if (!sources.length)
|
|
20
|
-
return target;
|
|
21
|
-
const source = sources.shift();
|
|
22
|
-
if (source === void 0)
|
|
23
|
-
return target;
|
|
24
|
-
if (isMergeableObject(target) && isMergeableObject(source)) {
|
|
25
|
-
objectKeys(source).forEach((key) => {
|
|
26
|
-
if (isMergeableObject(source[key])) {
|
|
27
|
-
if (!target[key])
|
|
28
|
-
target[key] = {};
|
|
29
|
-
deepMerge(target[key], source[key]);
|
|
30
|
-
} else {
|
|
31
|
-
target[key] = source[key];
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
return deepMerge(target, ...sources);
|
|
36
|
-
}
|
|
37
|
-
function isMergeableObject(item) {
|
|
38
|
-
return isObject(item) && !Array.isArray(item);
|
|
39
|
-
}
|
|
40
|
-
export function objectPick(obj, keys, omitUndefined = false) {
|
|
41
|
-
return keys.reduce((n, k) => {
|
|
42
|
-
if (k in obj) {
|
|
43
|
-
if (!omitUndefined || obj[k] !== void 0)
|
|
44
|
-
n[k] = obj[k];
|
|
45
|
-
}
|
|
46
|
-
return n;
|
|
47
|
-
}, {});
|
|
48
|
-
}
|
|
49
|
-
export function clearUndefined(obj) {
|
|
50
|
-
Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {});
|
|
51
|
-
return obj;
|
|
52
|
-
}
|
|
53
|
-
export function hasOwnProperty(obj, v) {
|
|
54
|
-
if (obj == null)
|
|
55
|
-
return false;
|
|
56
|
-
return Object.prototype.hasOwnProperty.call(obj, v);
|
|
57
|
-
}
|