Skip to content

Commit

Permalink
Merge pull request #17 from vigan-abd/feature/deep-freeze
Browse files Browse the repository at this point in the history
feat: object freeze deep
  • Loading branch information
prdn authored Dec 1, 2023
2 parents cb64fad + e79eff1 commit 770e396
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.7.0
- feat: freezeDeep

# 1.6.0
- feat: isEqual

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ General node js utils library.
Currently supported utils:
- `camelize` - simple camel case
- `cloneDeep` - deep clone functionality for objects
- `freezeDeep` - deep freezes objects
- `get` - get the object members by path
- `getArrayHasIntersect` - checks if arrays have at least one common value
- `getArrayUniq` - gets unique values form array
Expand Down
29 changes: 15 additions & 14 deletions index.d.ts
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>
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const camelize = require('./src/camelize')
const cloneDeep = require('./src/cloneDeep')
const freezeDeep = require('./src/freezeDeep')
const get = require('./src/get')
const getArrayHasIntersect = require('./src/getArrayHasIntersect')
const getArrayUniq = require('./src/getArrayUniq')
Expand All @@ -21,6 +22,7 @@ const shuffle = require('./src/shuffle')
module.exports = {
camelize,
cloneDeep,
freezeDeep,
get,
getArrayHasIntersect,
getArrayUniq,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinexcom/lib-js-util-base",
"version": "1.6.0",
"version": "1.7.0",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions src/freezeDeep.js
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
51 changes: 51 additions & 0 deletions test/freezeDeep.js
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)
})
})

0 comments on commit 770e396

Please sign in to comment.