|
| 1 | +/*-------------------------------------------------------------------------- |
| 2 | +
|
| 3 | +@sinclair/typebox/type |
| 4 | +
|
| 5 | +The MIT License (MIT) |
| 6 | +
|
| 7 | +Copyright (c) 2017-2024 Haydn Paterson (sinclair) <[email protected]> |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in |
| 17 | +all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +THE SOFTWARE. |
| 26 | +
|
| 27 | +---------------------------------------------------------------------------*/ |
| 28 | + |
| 29 | +import * as ValueGuard from '../guard/value' |
| 30 | + |
| 31 | +function ImmutableArray(value: unknown[]) { |
| 32 | + return globalThis.Object.freeze(value as any).map((value: unknown) => Immutable(value as any)) |
| 33 | +} |
| 34 | +function ImmutableDate(value: Date) { |
| 35 | + return value |
| 36 | +} |
| 37 | +function ImmutableUint8Array(value: Uint8Array) { |
| 38 | + return value |
| 39 | +} |
| 40 | +function ImmutableRegExp(value: RegExp) { |
| 41 | + return value |
| 42 | +} |
| 43 | +function ImmutableObject(value: Record<keyof any, unknown>) { |
| 44 | + const result = {} as Record<PropertyKey, unknown> |
| 45 | + for (const key of Object.getOwnPropertyNames(value)) { |
| 46 | + result[key] = Immutable(value[key]) |
| 47 | + } |
| 48 | + for (const key of Object.getOwnPropertySymbols(value)) { |
| 49 | + result[key] = Immutable(value[key]) |
| 50 | + } |
| 51 | + return globalThis.Object.freeze(result) |
| 52 | +} |
| 53 | +/** Specialized deep immutable value. Applies freeze recursively to the given value */ |
| 54 | +export function Immutable<T>(value: T): T { |
| 55 | + return ValueGuard.IsArray(value) |
| 56 | + ? ImmutableArray(value) |
| 57 | + : ValueGuard.IsDate(value) |
| 58 | + ? ImmutableDate(value) |
| 59 | + : ValueGuard.IsUint8Array(value) |
| 60 | + ? ImmutableUint8Array(value) |
| 61 | + : ValueGuard.IsRegExp(value) |
| 62 | + ? ImmutableRegExp(value) |
| 63 | + : ValueGuard.IsObject(value) |
| 64 | + ? ImmutableObject(value) |
| 65 | + : value |
| 66 | +} |
0 commit comments