-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.ts
64 lines (55 loc) · 1.82 KB
/
helpers.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
import { BinaryToTextEncoding, createHash } from "crypto";
import { Buffer } from "buffer";
export function interpolatePattern(
string: string,
replacer: (name: string, params: string[]) => string | null
) {
let result = "";
let rest = string;
while (rest !== "") {
let match = rest.match(/(\[.+?\])/);
if (match != null) {
const text = match[0];
const index = match.index ?? 0;
let match1 = text.match(/^\[(.+)\]$/);
if (match1 == null) {
// todo: could this happen?
throw new Error(`Unexpected case`);
}
const [name, ...params] = match1[1].split(":");
const replacedString = replacer(name, params);
result +=
rest.substr(0, index) +
(replacedString == null ? text : replacedString);
rest = rest.substr(index + text.length);
} else {
result += rest;
rest = "";
}
}
return result;
}
export function escapeClassName(string: string) {
return string.replace(/^[^a-zA-Z_]/g, "").replace(/[^a-zA-Z0-9_-]/g, "-");
}
export const SUPPORTED_HASHES = ["md4", "md5", "sha256", "sha512"] as const;
export const SUPPORTED_DIGESTS = ["hex", "base64"] as const;
export type HashType = typeof SUPPORTED_HASHES[number];
export type HashDigest = typeof SUPPORTED_DIGESTS[number];
export function isSupportedHashType(raw: string): raw is HashType {
return SUPPORTED_HASHES.indexOf(raw as HashType) !== -1;
}
export function isSupportedHashDigest(raw: unknown): raw is HashDigest {
return SUPPORTED_DIGESTS.indexOf(raw as HashDigest) !== -1;
}
export function makeNameHash(
name: string,
maxLength: number = 32,
type: HashType = "sha256",
digest: HashDigest = "hex"
) {
const buffer = Buffer.from(name, "utf8");
const hash = createHash(type);
hash.update(buffer);
return `h${hash.digest(digest).substr(0, maxLength)}`;
}