From 2acc89ea796d461b5d9c094856960f0e4811e694 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Tue, 10 Nov 2020 22:04:09 +0200 Subject: [PATCH 1/4] Implement deep freeze and freeze for namespaces --- README.md | 20 ++++++++++++++++++++ lib/oop.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/README.md b/README.md index 2fa3add6..2c8cd04d 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,8 @@ $ npm install @metarhia/common - [parseHost](#parsehosthost) - [override](#overrideobj-fn) - [mixin](#mixintarget-source) +- [deepFreeze](#deepfreezeinstance) +- [freezeNamespaces](#freezenamespacesnamespaces-exceptnames) - [Pool](#class-pool) - [Pool.prototype.constructor](#poolprototypeconstructorfactory--null) - [Pool.prototype.get](#poolprototypeget) @@ -1334,6 +1336,24 @@ Previous function will be accessible by obj.fnName.inherited Mixin for ES6 classes without overriding existing methods +### deepFreeze(instance) + +- `instance`: [``][object] target nested objects structure + +_Returns:_ [``][object] - deep freezed instance + +Deep freeze object structure + +### freezeNamespaces(namespaces, exceptNames) + +- `namespaces`: [``][object] target nested objects structure +- `exceptNames`: [``][array] nemes to be skipped for freezing at first + level + +_Returns:_ undefined + +Deep freeze namespaces + ### class Pool #### Pool.prototype.constructor(factory = null) diff --git a/lib/oop.js b/lib/oop.js index 92e056e3..6aa84d4d 100644 --- a/lib/oop.js +++ b/lib/oop.js @@ -23,7 +23,43 @@ const mixin = (target, source) => { Object.assign(target, mix); }; +const REF_TYPES = ['object', 'function']; + +// Deep freeze object structure +// instance - , target nested objects structure +// Returns: - deep freezed instance +// +const deepFreeze = instance => { + Object.freeze(instance); + const fields = Object.getOwnPropertyNames(instance); + for (const fieldName of fields) { + const value = instance[fieldName]; + if (value === null) continue; + if (!REF_TYPES.includes(typeof value) || Object.isFrozen(value)) continue; + deepFreeze(value); + } + return instance; +}; + +// Deep freeze namespaces +// namespaces - , target nested objects structure +// exceptNames - , nemes to be skipped for freezing at first level +// Returns: undefined +// +const freezeNamespaces = (namespaces, exceptNames) => { + for (const namespace of namespaces) { + const names = Object.keys(namespace); + for (const name of names) { + const target = namespace[name]; + if (exceptNames.includes(name)) continue; + deepFreeze(target); + } + } +}; + module.exports = { override, mixin, + deepFreeze, + freezeNamespaces, }; From e8ec58801dadd4cb4670cc9d9ff5fad39e3183f5 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Tue, 10 Nov 2020 22:14:54 +0200 Subject: [PATCH 2/4] fixup! Implement deep freeze and freeze for namespaces --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34b600ed..5a5fbe90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to ## [Unreleased][unreleased] +- Implement `deepFreeze` and `freezeNamespaces` + ## [2.2.0][] - 2020-07-10 ### Added From 0564fad059d656b468d63f20b9b74958a9b8753d Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Tue, 10 Nov 2020 23:10:55 +0200 Subject: [PATCH 3/4] fixup! Implement deep freeze and freeze for namespaces --- lib/oop.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/oop.js b/lib/oop.js index 6aa84d4d..07b806d1 100644 --- a/lib/oop.js +++ b/lib/oop.js @@ -46,12 +46,12 @@ const deepFreeze = instance => { // exceptNames - , nemes to be skipped for freezing at first level // Returns: undefined // -const freezeNamespaces = (namespaces, exceptNames) => { +const freezeNamespaces = (namespaces, skipNames = []) => { for (const namespace of namespaces) { const names = Object.keys(namespace); for (const name of names) { const target = namespace[name]; - if (exceptNames.includes(name)) continue; + if (skipNames.includes(name)) continue; deepFreeze(target); } } From a5d59dee036d1065986515aa0f104a001888b180 Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Wed, 11 Nov 2020 10:58:14 +0200 Subject: [PATCH 4/4] fixup! Implement deep freeze and freeze for namespaces --- README.md | 6 +++--- lib/oop.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 2c8cd04d..ed1380d5 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ $ npm install @metarhia/common - [override](#overrideobj-fn) - [mixin](#mixintarget-source) - [deepFreeze](#deepfreezeinstance) -- [freezeNamespaces](#freezenamespacesnamespaces-exceptnames) +- [freezeNamespaces](#freezenamespacesnamespaces-skipnames) - [Pool](#class-pool) - [Pool.prototype.constructor](#poolprototypeconstructorfactory--null) - [Pool.prototype.get](#poolprototypeget) @@ -1344,10 +1344,10 @@ _Returns:_ [``][object] - deep freezed instance Deep freeze object structure -### freezeNamespaces(namespaces, exceptNames) +### freezeNamespaces(namespaces, skipNames) - `namespaces`: [``][object] target nested objects structure -- `exceptNames`: [``][array] nemes to be skipped for freezing at first +- `skipNames`: [``][array] nemes to be skipped for freezing at first level _Returns:_ undefined diff --git a/lib/oop.js b/lib/oop.js index 07b806d1..264c9bf7 100644 --- a/lib/oop.js +++ b/lib/oop.js @@ -43,7 +43,7 @@ const deepFreeze = instance => { // Deep freeze namespaces // namespaces - , target nested objects structure -// exceptNames - , nemes to be skipped for freezing at first level +// skipNames - , nemes to be skipped for freezing at first level // Returns: undefined // const freezeNamespaces = (namespaces, skipNames = []) => {