forked from TypeScriptToLua/TypeScriptToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
59 lines (49 loc) · 1.72 KB
/
utils.ts
File metadata and controls
59 lines (49 loc) · 1.72 KB
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
import * as path from "path";
export const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/");
export const trimExtension = (filePath: string) => filePath.slice(0, -path.extname(filePath).length);
export function formatPathToLuaPath(filePath: string): string {
filePath = filePath.replace(/\.json$/, "");
if (process.platform === "win32") {
// Windows can use backslashes
filePath = filePath.replace(/\.\\/g, "").replace(/\\/g, ".");
}
return filePath.replace(/\.\//g, "").replace(/\//g, ".");
}
type NoInfer<T> = [T][T extends any ? 0 : never];
export function getOrUpdate<K, V>(
map: Map<K, V> | (K extends object ? WeakMap<K, V> : never),
key: K,
getDefaultValue: () => NoInfer<V>
): V {
if (!map.has(key)) {
map.set(key, getDefaultValue());
}
return map.get(key)!;
}
export function isNonNull<T>(value: T | undefined | null): value is T {
return value !== undefined && value !== null;
}
export function cast<TOriginal, TCast extends TOriginal>(
item: TOriginal,
cast: (item: TOriginal) => item is TCast
): TCast {
if (cast(item)) {
return item;
} else {
throw new Error(`Failed to cast value to expected type using ${cast.name}.`);
}
}
export function castEach<TOriginal, TCast extends TOriginal>(
items: TOriginal[],
cast: (item: TOriginal) => item is TCast
): TCast[] {
if (items.every(cast)) {
return items as TCast[];
} else {
throw new Error(`Failed to cast all elements to expected type using ${cast.name}.`);
}
}
export function assertNever(_value: never): never {
throw new Error("Value is expected to be never");
}
export function assume<T>(_value: any): asserts _value is T {}