subzero
v7.0.2
Published
class, function, and object freezer
Downloads
143
Maintainers
Readme
subzero
About:
Freeze a class, freeze a function, or freeze an object. For Node version 6 or greater.
install:
npm install subzero
usage:
function list (each function explained in more detail below):
Note: the mega freeze function was inspired by MDN and deep-freeze.
1) subzero.megaFreeze( functionOrClassOrObjectToFreeze )
Deep freeze a class, a function, or an object. This will freeze any classes, functions, and objects within the class/function/object being mega-frozen. It works by recursively freezing anything of type "object"
or "function"
.
'use strict';
const subzero = require( 'subzero' );
/*
this example uses a function,
subzero.megaFreeze also works with objects and classes
*/
function f() {}
const InnerClass = class {};
InnerClass.x = {
y: {}
};
const innerFunction = function() {};
innerFunction.x = {
y: {}
};
const objectInsideAlreadyFrozenObject = {};
const functionInsideAlreadyFrozenObject = function() {};
const objectInsideAlreadyFrozenFunction = {};
const functionInsideAlreadyFrozenFunction = function() {};
const functionToFreeze = function() {};
functionToFreeze.objectInsideAlreadyFrozenFunction = objectInsideAlreadyFrozenFunction;
functionToFreeze.functionInsideAlreadyFrozenFunction = functionInsideAlreadyFrozenFunction;
const frozenFunctionWithNonFrozenObjectAndFunctionInside = Object.freeze( functionToFreeze );
const buff = new Buffer( 69 );
buff.x = {
y: {},
f: function() {}
};
f.a = {
b: {
c: {
d: {
InnerClass,
innerFunction,
buff,
e: Object.freeze({
objectInsideAlreadyFrozenObject,
functionInsideAlreadyFrozenObject
}),
frozenFunctionWithNonFrozenObjectAndFunctionInside
}
}
}
};
f.prototype.x = {
y: {
z: {}
},
w: {}
};
/*
It's time to freeze your opponent for the flawless victory with fatality.
None of the following assertions will throw an AssertionError:
*/
console.assert( !Object.isFrozen( f ) );
console.assert( !Object.isFrozen( f.prototype ) );
console.assert( !Object.isFrozen( f.a ) );
console.assert( !Object.isFrozen( f.a.b ) );
console.assert( !Object.isFrozen( f.a.b.c ) );
console.assert( !Object.isFrozen( f.a.b.c.d ) );
console.assert( !Object.isFrozen( f.prototype.x ) );
console.assert( !Object.isFrozen( f.prototype.x.y ) );
console.assert( !Object.isFrozen( f.prototype.x.y.z ) );
console.assert( !Object.isFrozen( f.prototype.x.w ) );
console.assert( !Object.isFrozen( InnerClass ) );
console.assert( !Object.isFrozen( InnerClass.prototype ) );
console.assert( !Object.isFrozen( InnerClass.x ) );
console.assert( !Object.isFrozen( InnerClass.x.y ) );
console.assert( !Object.isFrozen( innerFunction ) );
console.assert( !Object.isFrozen( innerFunction.prototype ) );
console.assert( !Object.isFrozen( innerFunction.x ) );
console.assert( !Object.isFrozen( innerFunction.x.y ) );
console.assert( !Object.isFrozen( objectInsideAlreadyFrozenObject ) );
console.assert( !Object.isFrozen( objectInsideAlreadyFrozenFunction ) );
console.assert( !Object.isFrozen( functionInsideAlreadyFrozenObject ) );
console.assert( !Object.isFrozen( functionInsideAlreadyFrozenObject.prototype ) );
console.assert( !Object.isFrozen( functionInsideAlreadyFrozenFunction ) );
console.assert( !Object.isFrozen( functionInsideAlreadyFrozenFunction.prototype ) );
console.assert( !Object.isSealed( buff ) ); // you cannot freeze buffers
console.assert( !Object.isFrozen( buff.x ) );
console.assert( !Object.isFrozen( buff.x.y ) );
console.assert( !Object.isFrozen( buff.x.f ) );
console.assert( !Object.isFrozen( buff.x.f.prototype ) );
// Mega Freeze f
const reference = subzero.megaFreeze( f );
console.assert( ( reference === f ) );
console.assert( Object.isFrozen( f ) );
console.assert( Object.isFrozen( f.prototype ) );
console.assert( Object.isFrozen( f.a ) );
console.assert( Object.isFrozen( f.a.b ) );
console.assert( Object.isFrozen( f.a.b.c ) );
console.assert( Object.isFrozen( f.a.b.c.d ) );
console.assert( Object.isFrozen( f.prototype.x ) );
console.assert( Object.isFrozen( f.prototype.x.y ) );
console.assert( Object.isFrozen( f.prototype.x.y.z ) );
console.assert( Object.isFrozen( f.prototype.x.w ) );
console.assert( Object.isFrozen( InnerClass ) );
console.assert( Object.isFrozen( InnerClass.prototype ) );
console.assert( Object.isFrozen( InnerClass.x ) );
console.assert( Object.isFrozen( InnerClass.x.y ) );
console.assert( Object.isFrozen( innerFunction ) );
console.assert( Object.isFrozen( innerFunction.prototype ) );
console.assert( Object.isFrozen( innerFunction.x ) );
console.assert( Object.isFrozen( innerFunction.x.y ) );
console.assert( Object.isFrozen( objectInsideAlreadyFrozenObject ) );
console.assert( Object.isFrozen( objectInsideAlreadyFrozenFunction ) );
console.assert( Object.isFrozen( functionInsideAlreadyFrozenObject ) );
console.assert( Object.isFrozen( functionInsideAlreadyFrozenObject.prototype ) );
console.assert( Object.isFrozen( functionInsideAlreadyFrozenFunction ) );
console.assert( Object.isFrozen( functionInsideAlreadyFrozenFunction.prototype ) );
console.assert( Object.isSealed( buff ) );
console.assert( Object.isFrozen( buff.x ) );
console.assert( Object.isFrozen( buff.x.y ) );
console.assert( Object.isFrozen( buff.x.f ) );
console.assert( Object.isFrozen( buff.x.f.prototype ) );
no-op:
If the input value is not an object, a function, or a class for subzero's megaFreeze function, the return value will be the input value unaltered.