@@ -27,18 +27,41 @@ export type JsonObject = { [key: string]: JsonValue };
2727export function deepFreezeJsonValue ( value : JsonValue ) : undefined {
2828 if ( value === null || typeof value !== "object" ) return ;
2929
30- // Circular references are not possible in JSON, so no need to handle them here
3130 if ( isArray ( value ) ) {
32- for ( let i = 0 , len = value . length ; i !== len ; i ++ ) {
33- deepFreezeJsonValue ( value [ i ] ) ;
34- }
31+ deepFreezeJsonArray ( value ) ;
3532 } else {
36- // Symbol properties are not possible in JSON, so no need to handle them here.
37- // All properties are enumerable own properties, so can use simple `for..in` loop.
38- for ( const key in value ) {
39- deepFreezeJsonValue ( value [ key ] ) ;
40- }
33+ deepFreezeJsonObject ( value ) ;
4134 }
35+ }
4236
43- freeze ( value ) ;
37+ /**
38+ * Deep freeze a JSON object, recursively freezing all nested objects and arrays.
39+ *
40+ * Freezes the object in place, and returns `undefined`.
41+ *
42+ * @param obj - The value to deep freeze
43+ */
44+ export function deepFreezeJsonObject ( obj : JsonObject ) : undefined {
45+ // Circular references are not possible in JSON, so no need to handle them here.
46+ // Symbol properties are not possible in JSON, so no need to handle them here.
47+ // All properties are enumerable own properties, so can use simple `for..in` loop.
48+ for ( const key in obj ) {
49+ deepFreezeJsonValue ( obj [ key ] ) ;
50+ }
51+ freeze ( obj ) ;
52+ }
53+
54+ /**
55+ * Deep freeze a JSON array, recursively freezing all nested objects and arrays.
56+ *
57+ * Freezes the array in place, and returns `undefined`.
58+ *
59+ * @param arr - The value to deep freeze
60+ */
61+ export function deepFreezeJsonArray ( arr : JsonValue [ ] ) : undefined {
62+ // Circular references are not possible in JSON, so no need to handle them here
63+ for ( let i = 0 , len = arr . length ; i !== len ; i ++ ) {
64+ deepFreezeJsonValue ( arr [ i ] ) ;
65+ }
66+ freeze ( arr ) ;
4467}
0 commit comments