forked from alexandercerutti/passkit-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
75 lines (62 loc) · 1.61 KB
/
utils.ts
File metadata and controls
75 lines (62 loc) · 1.61 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as Messages from "./messages.js";
import type Bundle from "./Bundle.js";
/**
* Converts a date to W3C / UTC string
* @param date
* @returns
*/
export function processDate(date: Date): string | undefined {
if (!(date instanceof Date) || Number.isNaN(Number(date))) {
throw "Invalid date";
}
/**
* @see https://www.w3.org/TR/NOTE-datetime
*/
return date.toISOString();
}
/**
* Removes hidden files from a list (those starting with dot)
*
* @params from - list of file names
* @return
*/
export function removeHidden(from: Array<string>): Array<string> {
return from.filter((e) => e.charAt(0) !== ".");
}
/**
* Clones recursively an object and all of its properties
*
* @param object
* @returns
*/
export function cloneRecursive<T extends Object>(object: T) {
const objectCopy = {} as Record<keyof T, any>;
const objectEntries = Object.entries(object) as [keyof T, T[keyof T]][];
for (let i = 0; i < objectEntries.length; i++) {
const [key, value] = objectEntries[i];
if (value && typeof value === "object") {
if (Array.isArray(value)) {
objectCopy[key] = value.slice();
for (let j = 0; j < value.length; j++) {
const item = value[j];
objectCopy[key][j] =
item && typeof item === "object"
? cloneRecursive(item)
: item;
}
} else {
objectCopy[key] = cloneRecursive(value);
}
} else {
objectCopy[key] = value;
}
}
return objectCopy;
}
export function assertUnfrozen(
instance: InstanceType<typeof Bundle>,
): asserts instance is Bundle & { isFrozen: false } {
if (instance.isFrozen) {
throw new Error(Messages.BUNDLE.CLOSED);
}
}