@cohesivesystems/processes-presentation 0.1.0-alpha.129

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ export type DeepReadonly<T> =
2
+ T extends (...args: never[]) => unknown
3
+ ? T
4
+ : T extends readonly (infer TItem)[]
5
+ ? readonly DeepReadonly<TItem>[]
6
+ : T extends object
7
+ ? { readonly [TKey in keyof T]: DeepReadonly<T[TKey]> }
8
+ : T
9
+
10
+ export function isRecord(value: unknown): value is Readonly<Record<string, unknown>> {
11
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
12
+ }
13
+
14
+ export function compareOrdinal(left: string, right: string): number {
15
+ return left < right ? -1 : left > right ? 1 : 0
16
+ }
17
+
18
+ export function cloneWireValue<T>(value: T): DeepReadonly<T> {
19
+ return cloneWireValueCore(value, new WeakMap()) as DeepReadonly<T>
20
+ }
21
+
22
+ function cloneWireValueCore(value: unknown, seen: WeakMap<object, unknown>): unknown {
23
+ if (typeof value !== 'object' || value === null) {
24
+ return value
25
+ }
26
+ const existing = seen.get(value)
27
+ if (existing !== undefined) {
28
+ return existing
29
+ }
30
+ if (Array.isArray(value)) {
31
+ const clone: unknown[] = []
32
+ seen.set(value, clone)
33
+ for (const item of value) {
34
+ clone.push(cloneWireValueCore(item, seen))
35
+ }
36
+ return Object.freeze(clone)
37
+ }
38
+
39
+ const clone: Record<string, unknown> = {}
40
+ seen.set(value, clone)
41
+ for (const [key, item] of Object.entries(value)) {
42
+ clone[key] = cloneWireValueCore(item, seen)
43
+ }
44
+ return Object.freeze(clone)
45
+ }
46
+
47
+ export function deepFreeze<T>(value: T): T {
48
+ if (typeof value !== 'object' || value === null || Object.isFrozen(value)) {
49
+ return value
50
+ }
51
+ for (const nested of Object.values(value)) {
52
+ deepFreeze(nested)
53
+ }
54
+ return Object.freeze(value)
55
+ }