@stacksjs/collections 0.64.6 → 0.65.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/dist/index.js +2 -2
- package/dist/index.js.map +11 -133
- package/package.json +7 -12
- package/src/helpers/clone.ts +17 -0
- package/src/helpers/delete-keys.ts +16 -0
- package/src/helpers/index.ts +6 -0
- package/src/helpers/is.ts +23 -0
- package/src/helpers/nested-value.ts +17 -0
- package/src/helpers/values.ts +19 -0
- package/src/helpers/variadic.ts +13 -0
- package/src/index.ts +2024 -1
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/collections",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.65.0",
|
|
5
5
|
"description": "The Stacks collections.",
|
|
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/collections#readme",
|
|
@@ -36,23 +37,17 @@
|
|
|
36
37
|
},
|
|
37
38
|
"module": "dist/index.js",
|
|
38
39
|
"types": "dist/index.d.ts",
|
|
39
|
-
"
|
|
40
|
-
"Chris Breuer <chris@stacksjs.org>"
|
|
41
|
-
],
|
|
42
|
-
"files": [
|
|
43
|
-
"README.md",
|
|
44
|
-
"dist",
|
|
45
|
-
"src"
|
|
46
|
-
],
|
|
40
|
+
"files": ["README.md", "dist", "src"],
|
|
47
41
|
"scripts": {
|
|
48
|
-
"build": "bun
|
|
49
|
-
"typecheck": "bun
|
|
42
|
+
"build": "bun build.ts",
|
|
43
|
+
"typecheck": "bun tsc --noEmit",
|
|
44
|
+
"test": "bun test",
|
|
50
45
|
"prepublishOnly": "bun run build"
|
|
51
46
|
},
|
|
52
47
|
"dependencies": {
|
|
53
48
|
"collect.js": "^4.36.1"
|
|
54
49
|
},
|
|
55
50
|
"devDependencies": {
|
|
56
|
-
"@stacksjs/development": "
|
|
51
|
+
"@stacksjs/development": "0.64.6"
|
|
57
52
|
}
|
|
58
53
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clone helper
|
|
3
|
+
*
|
|
4
|
+
* Clone an array or object
|
|
5
|
+
*
|
|
6
|
+
* @param items - The array or object to clone
|
|
7
|
+
* @returns A deep clone of the input
|
|
8
|
+
*/
|
|
9
|
+
export function clone<T>(items: T[]): T[]
|
|
10
|
+
export function clone<T extends object>(items: T): T
|
|
11
|
+
export function clone<T>(items: T[] | T): T[] | T {
|
|
12
|
+
if (Array.isArray(items)) {
|
|
13
|
+
return [...items]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return { ...items }
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { variadic } from './variadic'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Delete keys helper
|
|
5
|
+
*
|
|
6
|
+
* Delete one or multiple keys from an object
|
|
7
|
+
*
|
|
8
|
+
* @param obj - The object from which to delete keys
|
|
9
|
+
* @param keys - The keys to delete
|
|
10
|
+
* @returns {void}
|
|
11
|
+
*/
|
|
12
|
+
export function deleteKeys<T extends object>(obj: T, ...keys: (keyof T)[]): void {
|
|
13
|
+
variadic(keys).forEach((key) => {
|
|
14
|
+
delete obj[key]
|
|
15
|
+
})
|
|
16
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the item is an array.
|
|
3
|
+
* @param item - The item to check.
|
|
4
|
+
* @returns A boolean indicating whether the item is an array.
|
|
5
|
+
*/
|
|
6
|
+
export const isArray = (item: unknown): item is Array<unknown> => Array.isArray(item)
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Checks if the item is an object (excluding arrays and null).
|
|
10
|
+
* @param item - The item to check.
|
|
11
|
+
* @returns A boolean indicating whether the item is an object.
|
|
12
|
+
*/
|
|
13
|
+
export function isObject(item: unknown): item is object {
|
|
14
|
+
return typeof item === 'object' && !Array.isArray(item) && item !== null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the item is a function.
|
|
19
|
+
* @param item - The item to check.
|
|
20
|
+
* @returns A boolean indicating whether the item is a function.
|
|
21
|
+
*/
|
|
22
|
+
// eslint-disable-next-line ts/no-unsafe-function-type
|
|
23
|
+
export const isFunction = (item: unknown): item is Function => typeof item === 'function'
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get value of a nested property
|
|
3
|
+
*
|
|
4
|
+
* @param mainObject - The object to search for the nested property
|
|
5
|
+
* @param key - The key path to the nested property, using dot notation
|
|
6
|
+
* @returns The value of the nested property, or the mainObject if not found
|
|
7
|
+
*/
|
|
8
|
+
export function nestedValue<T>(mainObject: T, key: string): any {
|
|
9
|
+
try {
|
|
10
|
+
return key.split('.').reduce((obj: any, property: string) => obj[property], mainObject)
|
|
11
|
+
}
|
|
12
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
13
|
+
catch (err) {
|
|
14
|
+
// If we end up here, we're not working with an object, and mainObject is the value itself
|
|
15
|
+
return mainObject
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Values helper
|
|
3
|
+
*
|
|
4
|
+
* Retrieve values from items when it is an array, object or Collection
|
|
5
|
+
*
|
|
6
|
+
* @param items - The input array, object, or Collection
|
|
7
|
+
* @returns An array of values
|
|
8
|
+
*/
|
|
9
|
+
export function values<T>(items: T[] | { [key: string]: T } | { all: () => T[] }): T[] {
|
|
10
|
+
if (Array.isArray(items)) {
|
|
11
|
+
return [...items]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if ('all' in items && typeof items.all === 'function') {
|
|
15
|
+
return items.all()
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return Object.values(items)
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Variadic helper function
|
|
3
|
+
*
|
|
4
|
+
* @param args - The arguments to process
|
|
5
|
+
* @returns An array of the processed arguments
|
|
6
|
+
*/
|
|
7
|
+
export function variadic<T>(args: T[] | T): T[] {
|
|
8
|
+
if (Array.isArray(args)) {
|
|
9
|
+
return args
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return [args]
|
|
13
|
+
}
|