@stacksjs/objects 0.64.6 → 0.67.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.
@@ -0,0 +1,85 @@
1
+ import type { DeepMerge } from "@stacksjs/types";
2
+ /**
3
+ * Map key/value pairs for an object, and construct a new one
4
+ *
5
+ *
6
+ * @category Object
7
+ *
8
+ * Transform:
9
+ * @example
10
+ * ```
11
+ * objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()])
12
+ * // { A: '1', B: '2' }
13
+ * ```
14
+ *
15
+ * Swap key/value:
16
+ * @example
17
+ * ```
18
+ * objectMap({ a: 1, b: 2 }, (k, v) => [v, k])
19
+ * // { 1: 'a', 2: 'b' }
20
+ * ```
21
+ *
22
+ * Filter keys:
23
+ * @example
24
+ * ```
25
+ * objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v])
26
+ * // { b: 2 }
27
+ * ```
28
+ */
29
+ export declare function objectMap<
30
+ K extends string,
31
+ V,
32
+ NK = K,
33
+ NV = V
34
+ >(obj: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<K, V>;
35
+ /**
36
+ * Type guard for any key, `k`.
37
+ * Marks `k` as a key of `T` if `k` is in `obj`.
38
+ *
39
+ * @category Object
40
+ * @param obj object to query for key `k`
41
+ * @param k key to check existence in `obj`
42
+ */
43
+ export declare function isKeyOf<T extends object>(obj: T, k: keyof any): k is keyof T;
44
+ /**
45
+ * Strict typed `Object.keys`
46
+ *
47
+ * @category Object
48
+ */
49
+ export declare function objectKeys<T extends object>(obj: T): Array<`${keyof T & (string | number | boolean | null | undefined)}`>;
50
+ /**
51
+ * Strict typed `Object.entries`
52
+ *
53
+ * @category Object
54
+ */
55
+ export declare function objectEntries<T extends object>(obj: T): Array<[keyof T, T[keyof T]]>;
56
+ /**
57
+ * Deep merge :P
58
+ *
59
+ * @category Object
60
+ */
61
+ export declare function deepMerge<
62
+ T extends object = object,
63
+ S extends object = T
64
+ >(target: T, ...sources: S[]): DeepMerge<T, S>;
65
+ /**
66
+ * Create a new subset object by providing keys.
67
+ *
68
+ * @category Object
69
+ */
70
+ export declare function objectPick<
71
+ O extends object,
72
+ T extends keyof O
73
+ >(obj: O, keys: T[], omitUndefined?: boolean): Pick<O, T>;
74
+ /**
75
+ * Clear undefined fields from an object. It mutates the object.
76
+ *
77
+ * @category Object
78
+ */
79
+ export declare function clearUndefined<T extends object>(obj: T): T;
80
+ /**
81
+ * Determines whether an object has a property with the specified name
82
+ *
83
+ * @category Object
84
+ */
85
+ export declare function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean;
package/dist/index.js CHANGED
@@ -1,5 +1,2 @@
1
1
  // @bun
2
- import{notNullish as F}from"@stacksjs/utils";import{isObject as G}from"@stacksjs/validation";function L(q,x){return Object.fromEntries(Object.entries(q).map(([A,z])=>x(A,z)).filter(F))}function P(q,x){return x in q}function H(q){return Object.keys(q)}function Q(q){return Object.entries(q)}function E(q,...x){if(!x.length)return q;const A=x.shift();if(A===void 0)return q;if(C(q)&&C(A))H(A).forEach((z)=>{if(C(A[z])){if(!q[z])q[z]={};E(q[z],A[z])}else q[z]=A[z]});return E(q,...x)}function C(q){return G(q)&&!Array.isArray(q)}function R(q,x,A=!1){return x.reduce((z,B)=>{if(B in q){if(!A||q[B]!==void 0)z[B]=q[B]}return z},{})}function S(q){return Object.keys(q).forEach((x)=>q[x]===void 0?delete q[x]:{}),q}function T(q,x){return q==null?!1:Object.prototype.hasOwnProperty.call(q,x)}export{R as objectPick,L as objectMap,H as objectKeys,Q as objectEntries,P as isKeyOf,T as hasOwnProperty,E as deepMerge,S as clearUndefined};
3
-
4
- //# debugId=827CAABF0174DF6764756E2164756E21
5
- //# sourceMappingURL=index.js.map
2
+ import{notNullish as F}from"@stacksjs/utils";import{isObject as G}from"@stacksjs/validation";function L(q,x){return Object.fromEntries(Object.entries(q).map(([A,z])=>x(A,z)).filter(F))}function P(q,x){return x in q}function H(q){return Object.keys(q)}function Q(q){return Object.entries(q)}function E(q,...x){if(!x.length)return q;let A=x.shift();if(A===void 0)return q;if(C(q)&&C(A))H(A).forEach((z)=>{if(C(A[z])){if(!q[z])q[z]={};E(q[z],A[z])}else q[z]=A[z]});return E(q,...x)}function C(q){return G(q)&&!Array.isArray(q)}function R(q,x,A=!1){return x.reduce((z,B)=>{if(B in q){if(!A||q[B]!==void 0)z[B]=q[B]}return z},{})}function S(q){return Object.keys(q).forEach((x)=>q[x]===void 0&&delete q[x]),q}function T(q,x){return q==null?!1:Object.prototype.hasOwnProperty.call(q,x)}export{R as objectPick,L as objectMap,H as objectKeys,Q as objectEntries,P as isKeyOf,T as hasOwnProperty,E as deepMerge,S as clearUndefined};
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@stacksjs/objects",
3
3
  "type": "module",
4
- "version": "0.64.6",
4
+ "version": "0.67.0",
5
5
  "description": "The Stacks objects.",
6
6
  "author": "Chris Breuer",
7
+ "contributors": ["Chris Breuer <chris@stacksjs.org>"],
7
8
  "license": "MIT",
8
9
  "funding": "https://github.com/sponsors/chrisbbreuer",
9
10
  "homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/objects#readme",
@@ -15,44 +16,30 @@
15
16
  "bugs": {
16
17
  "url": "https://github.com/stacksjs/stacks/issues"
17
18
  },
18
- "keywords": [
19
- "objects",
20
- "utilities",
21
- "functions",
22
- "stacks"
23
- ],
19
+ "keywords": ["objects", "utilities", "functions", "stacks"],
24
20
  "exports": {
25
21
  ".": {
26
- "bun": "./src/index.ts",
27
22
  "import": "./dist/index.js"
28
23
  },
29
24
  "./*": {
30
- "bun": "./src/*",
31
25
  "import": "./dist/*"
32
26
  }
33
27
  },
34
28
  "module": "dist/index.js",
35
29
  "types": "dist/index.d.ts",
36
- "contributors": [
37
- "Chris Breuer <chris@stacksjs.org>"
38
- ],
39
- "files": [
40
- "README.md",
41
- "dist",
42
- "src"
43
- ],
30
+ "files": ["README.md", "dist"],
44
31
  "scripts": {
45
- "build": "bun --bun build.ts",
46
- "typecheck": "bun --bun tsc --noEmit",
32
+ "build": "bun build.ts",
33
+ "typecheck": "bun tsc --noEmit",
47
34
  "prepublishOnly": "bun run build"
48
35
  },
49
36
  "dependencies": {
50
- "@stacksjs/collections": "latest",
51
- "@stacksjs/types": "latest",
52
- "@stacksjs/utils": "latest",
53
- "@stacksjs/validation": "latest"
37
+ "@stacksjs/collections": "0.67.0",
38
+ "@stacksjs/types": "0.67.0",
39
+ "@stacksjs/utils": "0.67.0",
40
+ "@stacksjs/validation": "0.67.0"
54
41
  },
55
42
  "devDependencies": {
56
- "@stacksjs/development": "latest"
43
+ "@stacksjs/development": "0.67.0"
57
44
  }
58
45
  }
package/dist/index.js.map DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../src/index.ts"],
4
- "sourcesContent": [
5
- "import type { DeepMerge } from '@stacksjs/types'\nimport { notNullish } from '@stacksjs/utils'\nimport { isObject } from '@stacksjs/validation'\n\n/**\n * Map key/value pairs for an object, and construct a new one\n *\n *\n * @category Object\n *\n * Transform:\n * @example\n * ```\n * objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()])\n * // { A: '1', B: '2' }\n * ```\n *\n * Swap key/value:\n * @example\n * ```\n * objectMap({ a: 1, b: 2 }, (k, v) => [v, k])\n * // { 1: 'a', 2: 'b' }\n * ```\n *\n * Filter keys:\n * @example\n * ```\n * objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v])\n * // { b: 2 }\n * ```\n */\nexport function objectMap<K extends string, V, NK = K, NV = V>(\n obj: Record<K, V>,\n fn: (key: K, value: V) => [NK, NV] | undefined,\n): Record<K, V> {\n return Object.fromEntries(\n Object.entries(obj)\n .map(([k, v]) => fn(k as K, v as V))\n .filter(notNullish),\n )\n}\n\n/**\n * Type guard for any key, `k`.\n * Marks `k` as a key of `T` if `k` is in `obj`.\n *\n * @category Object\n * @param obj object to query for key `k`\n * @param k key to check existence in `obj`\n */\nexport function isKeyOf<T extends object>(obj: T, k: keyof any): k is keyof T {\n return k in obj\n}\n\n/**\n * Strict typed `Object.keys`\n *\n * @category Object\n */\nexport function objectKeys<T extends object>(obj: T) {\n return Object.keys(obj) as Array<`${keyof T & (string | number | boolean | null | undefined)}`>\n}\n\n/**\n * Strict typed `Object.entries`\n *\n * @category Object\n */\nexport function objectEntries<T extends object>(obj: T) {\n return Object.entries(obj) as Array<[keyof T, T[keyof T]]>\n}\n\n/**\n * Deep merge :P\n *\n * @category Object\n */\nexport function deepMerge<T extends object = object, S extends object = T>(\n target: T,\n ...sources: S[]\n): DeepMerge<T, S> {\n if (!sources.length) return target as any\n\n const source = sources.shift()\n if (source === undefined) return target as any\n\n if (isMergeableObject(target) && isMergeableObject(source)) {\n objectKeys(source).forEach((key) => {\n // @ts-expect-error some description\n if (isMergeableObject(source[key])) {\n // @ts-expect-error some description\n if (!target[key])\n // @ts-expect-error some description\n target[key] = {}\n\n // @ts-expect-error some description\n deepMerge(target[key], source[key])\n } else {\n // @ts-expect-error some description\n target[key] = source[key]\n }\n })\n }\n\n return deepMerge(target, ...sources)\n}\n\nfunction isMergeableObject(item: any): item is object {\n return isObject(item) && !Array.isArray(item)\n}\n\n/**\n * Create a new subset object by providing keys.\n *\n * @category Object\n */\nexport function objectPick<O extends object, T extends keyof O>(obj: O, keys: T[], omitUndefined = false) {\n return keys.reduce(\n (n, k) => {\n if (k in obj) {\n if (!omitUndefined || obj[k] !== undefined) n[k] = obj[k]\n }\n return n\n },\n {} as Pick<O, T>,\n )\n}\n\n/**\n * Clear undefined fields from an object. It mutates the object.\n *\n * @category Object\n */\nexport function clearUndefined<T extends object>(obj: T): T {\n Object.keys(obj).forEach((key: string) => (obj[key] === undefined ? delete obj[key] : {}))\n return obj\n}\n\n/**\n * Determines whether an object has a property with the specified name\n *\n * @category Object\n */\nexport function hasOwnProperty<T>(obj: T, v: PropertyKey) {\n return obj ==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AO,CAAC,CAAE,EAClF,EAQF,SAAS,CAAiB,CAAC,EAAQ,EAAgB,CACxD,OAAO,GAAO,KAAO,GAAQ,OAAO,UAAU,eAAe,KAAK,EAAK,CAAC",
8
- "debugId": "827CAABF0174DF6764756E2164756E21",
9
- "names": []
10
- }
package/src/index.ts DELETED
@@ -1,146 +0,0 @@
1
- import type { DeepMerge } from '@stacksjs/types'
2
- import { notNullish } from '@stacksjs/utils'
3
- import { isObject } from '@stacksjs/validation'
4
-
5
- /**
6
- * Map key/value pairs for an object, and construct a new one
7
- *
8
- *
9
- * @category Object
10
- *
11
- * Transform:
12
- * @example
13
- * ```
14
- * objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()])
15
- * // { A: '1', B: '2' }
16
- * ```
17
- *
18
- * Swap key/value:
19
- * @example
20
- * ```
21
- * objectMap({ a: 1, b: 2 }, (k, v) => [v, k])
22
- * // { 1: 'a', 2: 'b' }
23
- * ```
24
- *
25
- * Filter keys:
26
- * @example
27
- * ```
28
- * objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v])
29
- * // { b: 2 }
30
- * ```
31
- */
32
- export function objectMap<K extends string, V, NK = K, NV = V>(
33
- obj: Record<K, V>,
34
- fn: (key: K, value: V) => [NK, NV] | undefined,
35
- ): Record<K, V> {
36
- return Object.fromEntries(
37
- Object.entries(obj)
38
- .map(([k, v]) => fn(k as K, v as V))
39
- .filter(notNullish),
40
- )
41
- }
42
-
43
- /**
44
- * Type guard for any key, `k`.
45
- * Marks `k` as a key of `T` if `k` is in `obj`.
46
- *
47
- * @category Object
48
- * @param obj object to query for key `k`
49
- * @param k key to check existence in `obj`
50
- */
51
- export function isKeyOf<T extends object>(obj: T, k: keyof any): k is keyof T {
52
- return k in obj
53
- }
54
-
55
- /**
56
- * Strict typed `Object.keys`
57
- *
58
- * @category Object
59
- */
60
- export function objectKeys<T extends object>(obj: T) {
61
- return Object.keys(obj) as Array<`${keyof T & (string | number | boolean | null | undefined)}`>
62
- }
63
-
64
- /**
65
- * Strict typed `Object.entries`
66
- *
67
- * @category Object
68
- */
69
- export function objectEntries<T extends object>(obj: T) {
70
- return Object.entries(obj) as Array<[keyof T, T[keyof T]]>
71
- }
72
-
73
- /**
74
- * Deep merge :P
75
- *
76
- * @category Object
77
- */
78
- export function deepMerge<T extends object = object, S extends object = T>(
79
- target: T,
80
- ...sources: S[]
81
- ): DeepMerge<T, S> {
82
- if (!sources.length) return target as any
83
-
84
- const source = sources.shift()
85
- if (source === undefined) return target as any
86
-
87
- if (isMergeableObject(target) && isMergeableObject(source)) {
88
- objectKeys(source).forEach((key) => {
89
- // @ts-expect-error some description
90
- if (isMergeableObject(source[key])) {
91
- // @ts-expect-error some description
92
- if (!target[key])
93
- // @ts-expect-error some description
94
- target[key] = {}
95
-
96
- // @ts-expect-error some description
97
- deepMerge(target[key], source[key])
98
- } else {
99
- // @ts-expect-error some description
100
- target[key] = source[key]
101
- }
102
- })
103
- }
104
-
105
- return deepMerge(target, ...sources)
106
- }
107
-
108
- function isMergeableObject(item: any): item is object {
109
- return isObject(item) && !Array.isArray(item)
110
- }
111
-
112
- /**
113
- * Create a new subset object by providing keys.
114
- *
115
- * @category Object
116
- */
117
- export function objectPick<O extends object, T extends keyof O>(obj: O, keys: T[], omitUndefined = false) {
118
- return keys.reduce(
119
- (n, k) => {
120
- if (k in obj) {
121
- if (!omitUndefined || obj[k] !== undefined) n[k] = obj[k]
122
- }
123
- return n
124
- },
125
- {} as Pick<O, T>,
126
- )
127
- }
128
-
129
- /**
130
- * Clear undefined fields from an object. It mutates the object.
131
- *
132
- * @category Object
133
- */
134
- export function clearUndefined<T extends object>(obj: T): T {
135
- Object.keys(obj).forEach((key: string) => (obj[key] === undefined ? delete obj[key] : {}))
136
- return obj
137
- }
138
-
139
- /**
140
- * Determines whether an object has a property with the specified name
141
- *
142
- * @category Object
143
- */
144
- export function hasOwnProperty<T>(obj: T, v: PropertyKey) {
145
- return obj == null ? false : Object.prototype.hasOwnProperty.call(obj, v)
146
- }