forked from renovatebot/renovate
/
string.ts
69 lines (60 loc) · 1.57 KB
/
string.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
// Return true if the match string is found at index in content
export function matchAt(
content: string,
index: number,
match: string
): boolean {
return content.substring(index, index + match.length) === match;
}
// Replace oldString with newString at location index of content
export function replaceAt(
content: string,
index: number,
oldString: string,
newString: string
): string {
return (
content.substring(0, index) +
newString +
content.substring(index + oldString.length)
);
}
/**
* Converts from utf-8 string to base64-encoded string
*/
export function toBase64(input: string): string {
return Buffer.from(input).toString('base64');
}
/**
* Converts from base64-encoded string to utf-8 string
*/
export function fromBase64(input: string): string {
return Buffer.from(input, 'base64').toString();
}
export function uniqueStrings(
element: string,
index: number,
elements: string[]
): boolean {
return elements.indexOf(element) === index;
}
export function looseEquals(
a: string | null | undefined,
b: string | null | undefined
): boolean {
if (!(a && b)) {
return a === b;
}
return a.localeCompare(b, undefined, { sensitivity: 'base' }) === 0;
}
export function isDockerDigest(input: string): boolean {
return /^sha256:[a-f0-9]{64}$/i.test(input);
}
export function titleCase(input: string): string {
const words = input.toLowerCase().split(' ');
for (let i = 0; i < words.length; i++) {
const word = words[i];
words[i] = word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
return words.join(' ');
}