-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathlodash-replacements.ts
More file actions
122 lines (108 loc) · 3.05 KB
/
lodash-replacements.ts
File metadata and controls
122 lines (108 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Shared lodash replacement utilities
// These functions replace lodash with native JavaScript implementations
// Deep clone using JSON serialization (works for serializable objects)
export function cloneDeep<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj)) as T
}
// Deep equality check using JSON serialization
export function isEqual(a: unknown, b: unknown): boolean {
try {
return JSON.stringify(a) === JSON.stringify(b)
} catch {
return a === b
}
}
// Fisher-Yates shuffle algorithm
export function shuffle<T>(array: T[]): T[] {
const result = [...array]
for (let i = result.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[result[i], result[j]] = [result[j], result[i]]
}
return result
}
// Generate a range of numbers
export function range(count: number): number[] {
return Array.from({ length: count }, (_, i) => i)
}
// Sum an array by extracting numeric values with a function
export function sumBy<T>(arr: T[], fn: (item: T) => number): number {
return arr.reduce((sum, item) => sum + fn(item), 0)
}
// Map values of an object
export function mapValues<T extends object, R>(
obj: T,
fn: (value: any, key: keyof T) => R,
): { [K in keyof T]: R } {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k, fn(v, k as keyof T)]),
) as { [K in keyof T]: R }
}
// Union of two arrays
export function union<T>(arr1: T[], arr2: T[]): T[] {
return Array.from(new Set([...arr1, ...arr2]))
}
// Partition an array into two arrays based on a predicate
export function partition<T, S extends T>(
array: T[],
predicate: (value: T) => value is S,
): [S[], Exclude<T, S>[]]
export function partition<T>(
array: T[],
predicate: (value: T) => boolean,
): [T[], T[]]
export function partition<T>(
array: T[],
predicate: (value: T) => boolean,
): [T[], T[]] {
const truthy: T[] = []
const falsy: T[] = []
for (const item of array) {
if (predicate(item)) {
truthy.push(item)
} else {
falsy.push(item)
}
}
return [truthy, falsy]
}
// Remove duplicates from an array
export function uniq<T>(arr: T[]): T[] {
return Array.from(new Set(arr))
}
// Add debounce implementation export below
export function debounce<T extends (...args: any[]) => any>(fn: T, wait = 0) {
let timeout: ReturnType<typeof setTimeout> | null = null
let lastArgs: any
let lastThis: any
let result: ReturnType<T>
const debounced = function (this: any, ...args: Parameters<T>) {
lastArgs = args
lastThis = this
if (timeout) clearTimeout(timeout)
timeout = setTimeout(() => {
timeout = null
result = fn.apply(lastThis, lastArgs)
}, wait)
return result
} as unknown as T & {
cancel: () => void
flush: () => ReturnType<T> | undefined
}
debounced.cancel = () => {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
}
debounced.flush = () => {
if (timeout) {
clearTimeout(timeout)
timeout = null
result = fn.apply(lastThis, lastArgs)
return result
}
return result
}
return debounced
}