-
Notifications
You must be signed in to change notification settings - Fork 116
/
utils.ts
187 lines (164 loc) · 4.05 KB
/
utils.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { IOnigCaptureIndex } from './onigLib';
export function clone<T>(something: T): T {
return doClone(something);
}
function doClone(something: any): any {
if (Array.isArray(something)) {
return cloneArray(something);
}
if (typeof something === 'object') {
return cloneObj(something);
}
return something;
}
function cloneArray(arr: any[]): any[] {
let r: any[] = [];
for (let i = 0, len = arr.length; i < len; i++) {
r[i] = doClone(arr[i]);
}
return r;
}
function cloneObj(obj: any): any {
let r: any = {};
for (let key in obj) {
r[key] = doClone(obj[key]);
}
return r;
}
export function mergeObjects(target: any, ...sources: any[]): any {
sources.forEach(source => {
for (let key in source) {
target[key] = source[key];
}
});
return target;
}
export function basename(path: string): string {
const idx = ~path.lastIndexOf('/') || ~path.lastIndexOf('\\');
if (idx === 0) {
return path;
} else if (~idx === path.length - 1) {
return basename(path.substring(0, path.length - 1));
} else {
return path.substr(~idx + 1);
}
}
let CAPTURING_REGEX_SOURCE = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/g;
export class RegexSource {
public static hasCaptures(regexSource: string | null): boolean {
if (regexSource === null) {
return false;
}
CAPTURING_REGEX_SOURCE.lastIndex = 0;
return CAPTURING_REGEX_SOURCE.test(regexSource);
}
public static replaceCaptures(regexSource: string, captureSource: string, captureIndices: IOnigCaptureIndex[]): string {
return regexSource.replace(CAPTURING_REGEX_SOURCE, (match: string, index: string, commandIndex: string, command: string) => {
let capture = captureIndices[parseInt(index || commandIndex, 10)];
if (capture) {
let result = captureSource.substring(capture.start, capture.end);
// Remove leading dots that would make the selector invalid
while (result[0] === '.') {
result = result.substring(1);
}
switch (command) {
case 'downcase':
return result.toLowerCase();
case 'upcase':
return result.toUpperCase();
default:
return result;
}
} else {
return match;
}
});
}
}
/**
* A union of given const enum values.
*/
export type OrMask<T extends number> = number;
export function strcmp(a: string, b: string): number {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
export function strArrCmp(a: string[] | null, b: string[] | null): number {
if (a === null && b === null) {
return 0;
}
if (!a) {
return -1;
}
if (!b) {
return 1;
}
let len1 = a.length;
let len2 = b.length;
if (len1 === len2) {
for (let i = 0; i < len1; i++) {
let res = strcmp(a[i], b[i]);
if (res !== 0) {
return res;
}
}
return 0;
}
return len1 - len2;
}
export function isValidHexColor(hex: string): boolean {
if (/^#[0-9a-f]{6}$/i.test(hex)) {
// #rrggbb
return true;
}
if (/^#[0-9a-f]{8}$/i.test(hex)) {
// #rrggbbaa
return true;
}
if (/^#[0-9a-f]{3}$/i.test(hex)) {
// #rgb
return true;
}
if (/^#[0-9a-f]{4}$/i.test(hex)) {
// #rgba
return true;
}
return false;
}
/**
* Escapes regular expression characters in a given string
*/
export function escapeRegExpCharacters(value: string): string {
return value.replace(/[\-\\\{\}\*\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&');
}
export class CachedFn<TKey, TValue> {
private readonly cache = new Map<TKey, TValue>();
constructor(private readonly fn: (key: TKey) => TValue) {
}
public get(key: TKey): TValue {
if (this.cache.has(key)) {
return this.cache.get(key)!;
}
const value = this.fn(key);
this.cache.set(key, value);
return value;
}
}
declare let performance: { now: () => number } | undefined;
export const performanceNow =
typeof performance === "undefined"
// performance.now() is not available in this environment, so use Date.now()
? function () {
return Date.now();
}
: function () {
return performance!.now();
};