Skip to content

Instantly share code, notes, and snippets.

@zheeeng
Last active March 12, 2020 04:37
Show Gist options
  • Save zheeeng/c1393dcd5711b8656ed3f81a3ef57646 to your computer and use it in GitHub Desktop.
Save zheeeng/c1393dcd5711b8656ed3f81a3ef57646 to your computer and use it in GitHub Desktop.
common utils
export function findIndex <T> (items: T[], fn: (item: T, index: number) => boolean) {
for (let index = 0, len = items.length; index < len; index++) {
if (fn(items[index], index)) return index
}
return -1
}
export function flatMap <T, U> (items: T[], fn: (item: T, index: number) => U[]) {
return items.reduce((acc, item, index) => acc.concat(fn(item, index)), [] as U[])
}
export function toKeys<O extends {}> (o: O): Array<keyof O> {
const ks: Array<keyof O> = []
for (const k in o) if (o.hasOwnProperty(k)) ks.push(k)
return ks
}
export function toEntries<T extends string, U> (obj: Record<T, U>): Array<[T, U]> {
const entries: Array<[T, U]> = []
for (const key in obj) if (obj.hasOwnProperty(key)) entries[entries.length] = [key, obj[key]]
return entries
}
export function startWith (toTest: string, searchStr: string, position: number = 0) {
const pos = position > 0 ? (position | 0) : 0
return toTest.slice(pos, pos + searchStr.length) === searchStr
}
export function withIndex <T> (items: T[]): Array<{ value: T, index: number }> {
return items.map((value, index) => ({ value, index }))
}
export function mapValue <K extends string, T, U> (
o: Record<K, T>, mapper: (i: T) => U,
): Record<K, U> {
return toKeys(o).reduce<Partial<Record<K, U>>>(
(acc, key) => {
acc[key] = mapper(o[key])
return acc
},
{},
) as Record<K, U>
}
export function mapKey <K extends string, K2 extends string, T> (
o: Record<K, T>, mapper: (i: K) => K2,
): Record<K2, T> {
return toKeys(o).reduce<Partial<Record<K2, T>>>(
(acc, key) => {
acc[mapper(key)] = o[key]
return acc
},
{},
) as Record<K2, T>
}
export function mapKeyAndValue <K extends string, K2 extends string, T, U> (
o: Record<K, T>, keyMapper: (i: K) => K2, valueMapper: (i: T) => U,
): Record<K2, U> {
return toKeys(o).reduce<Partial<Record<K2, U>>>(
(acc, key) => {
acc[keyMapper(key)] = valueMapper(o[key])
return acc
},
{},
) as Record<K2, U>
}
export function range (start: number, end: number) {
const nums: number[] = []
for (let i = start; i <= end; i++) {
nums.push(i)
}
return nums
}
function foldFn(m: Record<string, number>, value: string, index: number) {
m[value] = index;
return m;
}
export function chunk<T>(items: T[], count: number) {
const chunksNum = Math.ceil(items.length / count);
const chunks: T[][] = [];
for (let i = 0; i < chunksNum; i++) {
chunks.push(items.slice(i * count, i * count + count));
}
return chunks;
}
export function deDuplicateBy<
K extends string,
T extends { [key in K]: string }
>(byKey: K, items: T[], fromEnd = true): T[] {
const values = items.map(item => item[byKey]);
const keyIndicesMap = fromEnd
? values.reduce(foldFn, {})
: values.reduceRight(foldFn, {});
return items.filter((item, index) => keyIndicesMap[item[byKey]] === index);
}
export function deDuplicate<T>(items: T[]) {
return items.filter((item, index, all) => all.indexOf(item) === index);
}
export async function sleep(ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment