-
Notifications
You must be signed in to change notification settings - Fork 69
/
util.ts
87 lines (71 loc) · 2.43 KB
/
util.ts
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
import * as fs from 'fs'
const debug = require('debug')
export function flatMap<T, U>(arr: T[], fn: (i: T) => U[]): U[] {
return arr.reduce((arr, i) => arr.concat(fn(i)), [] as U[])
}
export function mapValues<T extends Record<string, any>, TResult>(obj: {[P in keyof T]: T[P]}, fn: (i: T[keyof T], k: keyof T) => TResult): {[P in keyof T]: TResult} {
return Object.entries(obj)
.reduce((o, [k, v]) => {
o[k] = fn(v as any, k as any)
return o
}, {} as any)
}
export function exists(path: string): Promise<boolean> {
// eslint-disable-next-line no-promise-executor-return
return new Promise(resolve => resolve(fs.existsSync(path)))
}
export function resolvePackage(id: string, paths: { paths: string[] }): string {
return require.resolve(id, paths)
}
export function loadJSON(path: string): Promise<any> {
debug('config')('loadJSON %s', path)
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err: any, d: any) => {
try {
if (err) reject(err)
else resolve(JSON.parse(d))
} catch (error: any) {
reject(error)
}
})
})
}
export function compact<T>(a: (T | undefined)[]): T[] {
return a.filter((a): a is T => Boolean(a))
}
export function uniq<T>(arr: T[]): T[] {
return arr.filter((a, i) => {
return !arr.find((b, j) => j > i && b === a)
})
}
function displayWarnings() {
if (process.listenerCount('warning') > 1) return
process.on('warning', (warning: any) => {
console.error(warning.stack)
if (warning.detail) console.error(warning.detail)
})
}
export function Debug(...scope: string[]): (..._: any) => void {
if (!debug) return (..._: any[]) => {}
const d = debug(['config', ...scope].join(':'))
if (d.enabled) displayWarnings()
return (...args: any[]) => d(...args)
}
// Adapted from https://github.com/angus-c/just/blob/master/packages/array-permutations/index.js
export function permutations(arr: string[]): Array<string[]> {
if (arr.length === 0) return []
if (arr.length === 1) return [arr]
const output = []
const partialPermutations = permutations(arr.slice(1))
const first = arr[0]
for (let i = 0, len = partialPermutations.length; i < len; i++) {
const partial = partialPermutations[i]
for (let j = 0, len2 = partial.length; j <= len2; j++) {
const start = partial.slice(0, j)
const end = partial.slice(j)
const merged = start.concat(first, end)
output.push(merged)
}
}
return output
}