-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from vigan-abd/feature/deep-freeze
feat: object freeze deep
- Loading branch information
Showing
7 changed files
with
88 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# 1.7.0 | ||
- feat: freezeDeep | ||
|
||
# 1.6.0 | ||
- feat: isEqual | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
export function camelize(str: string): string | ||
export function camelize (str: string): string | ||
export function cloneDeep (obj: Object): Object | ||
export function get(obj: Object, path: string | Array<string | number>, defaultValue: any) | ||
export function freezeDeep (obj: Object): Object | ||
export function get (obj: Object, path: string | Array<string | number>, defaultValue: any): any | ||
export function getArrayHasIntersect (arr1: Array<any>, arr2: Array<any>): boolean | ||
export function getArrayUniq (arr: Array<any>): Array<any> | ||
export function isEmpty(val: any): boolean | ||
export function isEqual(value: any, another: any): boolean | ||
export function isFunction(val: any): boolean | ||
export function isNil(val: any): boolean | ||
export function isObject(verifiable: any): Boolean | ||
export function isPlainObject(val: any): boolean | ||
export function merge(obj: Object, ...sources: Object[]): Object | ||
export function omit(obj: Object, keys: Array<string>): Object | ||
export function omitBy(obj: Object, predicate: (val: any, key: string) => boolean): Object | ||
export function pick(obj: Object, keys: Array<string>): Object | ||
export function pickBy(obj: Object, predicate: (val: any, key: string) => boolean): Object | ||
export function shuffle<T>(array: Array<T>): Array<T> | ||
export function isEmpty (val: any): boolean | ||
export function isEqual (value: any, another: any): boolean | ||
export function isFunction (val: any): boolean | ||
export function isNil (val: any): boolean | ||
export function isObject (verifiable: any): Boolean | ||
export function isPlainObject (val: any): boolean | ||
export function merge (obj: Object, ...sources: Object[]): Object | ||
export function omit (obj: Object, keys: Array<string>): Object | ||
export function omitBy (obj: Object, predicate: (val: any, key: string) => boolean): Object | ||
export function pick (obj: Object, keys: Array<string>): Object | ||
export function pickBy (obj: Object, predicate: (val: any, key: string) => boolean): Object | ||
export function shuffle<T> (array: Array<T>): Array<T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict' | ||
|
||
const freezeDeep = (obj) => { | ||
Object.freeze(obj) | ||
|
||
Object.getOwnPropertyNames(obj).forEach(prop => { | ||
if (obj[prop] !== null && typeof obj[prop] === 'object' && !Object.isFrozen(obj[prop])) { | ||
freezeDeep(obj[prop]) | ||
} | ||
}) | ||
|
||
return obj | ||
} | ||
|
||
module.exports = freezeDeep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
/* eslint-env mocha */ | ||
|
||
const assert = require('assert') | ||
const { freezeDeep } = require('../index') | ||
|
||
describe('freezeDeep', () => { | ||
it('should freeze a simple object', () => { | ||
const obj = { a: 1 } | ||
freezeDeep(obj) | ||
assert.throws(() => { obj.a = 2 }, TypeError) | ||
}) | ||
|
||
it('should deeply freeze an object', () => { | ||
const obj = { a: { b: { c: 1 } } } | ||
freezeDeep(obj) | ||
assert.throws(() => { obj.a.b.c = 2 }, TypeError) | ||
}) | ||
|
||
it('should not affect non-object properties', () => { | ||
const obj = { a: 1, b: 'test', c: true } | ||
freezeDeep(obj) | ||
assert.strictEqual(obj.a, 1) | ||
assert.strictEqual(obj.b, 'test') | ||
assert.strictEqual(obj.c, true) | ||
}) | ||
|
||
it('should handle objects that are already frozen', () => { | ||
const obj = Object.freeze({ a: 1 }) | ||
assert.doesNotThrow(() => freezeDeep(obj)) | ||
}) | ||
|
||
it('should handle null and undefined properties without errors', () => { | ||
const obj = { a: null, b: undefined } | ||
assert.doesNotThrow(() => freezeDeep(obj)) | ||
}) | ||
|
||
it('should deeply freeze an object containing nested arrays', () => { | ||
const obj = { | ||
a: [1, 2, [3, 4]], | ||
b: { c: [5, 6] } | ||
} | ||
|
||
freezeDeep(obj) | ||
|
||
assert.throws(() => obj.a.push(5), TypeError) | ||
assert.throws(() => obj.a[2].push(7), TypeError) | ||
assert.throws(() => obj.b.c.push(8), TypeError) | ||
}) | ||
}) |