Skip to content

Commit cbee453

Browse files
committed
chore: wip
1 parent 3b8210e commit cbee453

File tree

9 files changed

+925
-18
lines changed

9 files changed

+925
-18
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './clone'
2+
export * from './delete-keys'
3+
export * from './is'
4+
export * from './nested-value'
5+
export * from './values'
6+
export * from './variadic'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const is = {
2+
/**
3+
* Checks if the item is an array.
4+
* @param item - The item to check.
5+
* @returns A boolean indicating whether the item is an array.
6+
*/
7+
isArray: (item: unknown): item is Array<unknown> => Array.isArray(item),
8+
9+
/**
10+
* Checks if the item is an object (excluding arrays and null).
11+
* @param item - The item to check.
12+
* @returns A boolean indicating whether the item is an object.
13+
*/
14+
isObject: (item: unknown): item is object => typeof item === 'object' && !Array.isArray(item) && item !== null,
15+
16+
/**
17+
* Checks if the item is a function.
18+
* @param item - The item to check.
19+
* @returns A boolean indicating whether the item is a function.
20+
*/
21+
isFunction: (item: unknown): item is Function => typeof item === 'function',
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
} catch (err) {
12+
// If we end up here, we're not working with an object, and mainObject is the value itself
13+
return mainObject
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)