-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c377c1
commit c950d30
Showing
15 changed files
with
266 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { Array } from "laser-utils"; | ||
import type { Object } from "laser-utils"; | ||
import { isArray, isObject } from "laser-utils"; | ||
|
||
export class Collection { | ||
/** | ||
* Pick | ||
* @param target Object.Any | ||
* @param keys keyof Object.Any | ||
*/ | ||
public static pick<T extends Object.Any, K extends keyof T>( | ||
target: T, | ||
keys: K | K[] | ||
): Pick<T, K> { | ||
const set: Set<unknown> = new Set(isArray(keys) ? keys : [keys]); | ||
const next = {} as T; | ||
for (const key in target) { | ||
if (!set.has(key)) continue; | ||
next[key] = target[key]; | ||
} | ||
return next; | ||
} | ||
|
||
/** | ||
* Omit | ||
* @param target Array.Any | Object.Any | ||
* @param keys keys: Array.Any | ||
*/ | ||
public static omit<T extends Array.Any>(target: T, keys: T): T; | ||
public static omit<T extends Object.Any, K extends keyof T>(target: T, keys: K | K[]): Omit<T, K>; | ||
public static omit<T extends Array.Any | Object.Any>(target: T, keys: Array.Any): T | Object.Any { | ||
const set = new Set(isArray(keys) ? keys : [keys]); | ||
if (isObject(target)) { | ||
const next = {} as Object.Unknown; | ||
for (const key in target) { | ||
if (set.has(key)) continue; | ||
next[key] = target[key]; | ||
} | ||
return next; | ||
} | ||
return target.filter(item => !set.has(item)); | ||
} | ||
|
||
/** | ||
* Patch | ||
* @param a Set<T> | T[] | ||
* @param b Set<T> | T[] | ||
*/ | ||
public static patch<T>(a: Set<T> | T[], b: Set<T> | T[]) { | ||
const prev = a instanceof Set ? a : new Set(a); | ||
const next = b instanceof Set ? b : new Set(b); | ||
const effects: T[] = []; | ||
const added: T[] = []; | ||
const removed: T[] = []; | ||
for (const id of next) { | ||
if (!prev.has(id)) added.push(id); | ||
} | ||
for (const id of prev) { | ||
if (!next.has(id)) removed.push(id); | ||
} | ||
effects.push(...added, ...removed); | ||
return { effects, added, removed }; | ||
} | ||
|
||
/** | ||
* Union | ||
* @param a Set<T> | T[] | ||
* @param b Set<T> | T[] | ||
*/ | ||
public static union<T>(a: Set<T> | T[], b: Set<T> | T[]) { | ||
return [...a, ...b]; | ||
} | ||
|
||
/** | ||
* Intersection | ||
* @param a Set<T> | T[] | ||
* @param b Set<T> | T[] | ||
*/ | ||
public static intersection<T>(a: Set<T> | T[], b: Set<T> | T[]) { | ||
const prev = [...a]; | ||
const next = b instanceof Set ? b : new Set(b); | ||
return new Set([...prev].filter(id => next.has(id))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { isFunction } from "laser-utils"; | ||
|
||
// ExperimentalDecorators | ||
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html | ||
|
||
/** | ||
* Bind 装饰器 | ||
* @param _ | ||
* @param key | ||
* @param descriptor | ||
*/ | ||
export function Bind<T>(_: T, key: string, descriptor: PropertyDescriptor): PropertyDescriptor { | ||
const originalMethod = descriptor.value; | ||
if (!isFunction(originalMethod)) { | ||
throw new TypeError(`${originalMethod} is not a function`); | ||
} | ||
|
||
return { | ||
configurable: true, | ||
get() { | ||
const boundFunction = originalMethod.bind(this); | ||
Object.defineProperty(this, key, { | ||
value: boundFunction, | ||
configurable: true, | ||
writable: true, | ||
enumerable: false, | ||
}); | ||
return boundFunction; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.