JSPM

  • Created
  • Published
  • Downloads 90
  • Score
    100M100P100Q56400F
  • License ISC

class and object freezer

Package Exports

  • subzero

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (subzero) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

#subzero npm version Build Status

mksfw.gif

##About:

Freeze a class and its prototype, or freeze an object.

##install:

npm install subzero

##usage:

####function list (each function explained in more detail below):

  1. subzero.freezeClass
  2. subzero.deepFreezeClass
  3. subzero.deepFreezeObject
  4. subzero.megaFreezeClass
  5. subzero.megaFreezeObject

Note: the mega freeze functions were inspired by deep-freeze.


###1) subzero.freezeClass( ClassToFreeze ) Freeze a class and its prototype.

'use strict';

const subzero = require( 'subzero' );

class C {

    static f() { return 69; }

    f() { return 22; }
}

subzero.freezeClass( C );

/*
    the following statements will now return true:
*/

// Object.isFrozen( C );
// Object.isFrozen( C.prototype );

/*
    as a result the following statements will now throw TypeErrors in strict mode:
*/

// C.g = function() { return 42 };
// C.f = function() { return 42 };
// delete C.f;
// C.constant = 42;
// C.prototype.g = function() { return 42 };
// C.prototype.f = function() { return 42 };
// delete C.prototype.f;
// C.prototype.constant = 42;

letitgo.gif

###2) subzero.deepFreezeClass( ClassToFreeze ) Deep freeze a class and its prototype. This will not freeze any classes or functions within the class. It works by recursively freezing anything of type "object".

'use strict';

const subzero = require( 'subzero' );

class C {}

class InnerClass {}

InnerClass.x = {

    y: {}
};

function InnerFunction() {}

InnerFunction.x = {

    y: {}
};

const willBeFrozen = {};

C.a = {

    b: {

        c: {

            d: {

                InnerClass,

                InnerFunction,

                e: Object.freeze({ willBeFrozen })
            }
        }
    }
};

C.prototype.x = {

    y: {

        z: {}
    },

    w: {}
};

subzero.deepFreezeClass( C );

/*
    the following statements will now return true:
*/

// Object.isFrozen( C );
// Object.isFrozen( C.a );
// Object.isFrozen( C.a.b );
// Object.isFrozen( C.a.b.c );
// Object.isFrozen( C.a.b.c.d );
// Object.isFrozen( C.prototype );
// Object.isFrozen( C.prototype.x );
// Object.isFrozen( C.prototype.x.y );
// Object.isFrozen( C.prototype.x.y.z );
// Object.isFrozen( C.prototype.x.w );
// !Object.isFrozen( InnerClass );
// !Object.isFrozen( InnerClass.x );
// !Object.isFrozen( InnerClass.x.y );
// !Object.isFrozen( InnerFunction );
// !Object.isFrozen( InnerFunction.x );
// !Object.isFrozen( InnerFunction.x.y );
// Object.isFrozen( C.a.b.c.d.e.willBeFrozen );

β„οΈπŸŽ…πŸΏπŸŽ…πŸ½πŸŽ…πŸΎπŸŽ…πŸΌβ›„οΈπŸŽΏπŸ—»πŸ‚ ###3) subzero.deepFreezeObject( objectToFreeze )

Deep freeze an object. This will not freeze any classes or functions within the object. It works by recursively freezing anything of type "object".

'use strict';

const subzero = require( 'subzero' );

const o = {};

class InnerClass {}

InnerClass.x = {

    y: {}
};

function InnerFunction() {}

InnerFunction.x = {

    y: {}
};

const willBeFrozen = {};

o.a = {

    b: {

        c: {

            d: {

                InnerClass,

                InnerFunction,

                e: Object.freeze({ willBeFrozen })
            }
        }
    }
};

o.x = {

    y: {

        z: {}
    },

    w: {}
};

subzero.deepFreezeObject( o );

/*
    the following statements will now return true:
*/

// Object.isFrozen( o );
// Object.isFrozen( o.a );
// Object.isFrozen( o.a.b );
// Object.isFrozen( o.a.b.c );
// Object.isFrozen( o.a.b.c.d );
// Object.isFrozen( o.x );
// Object.isFrozen( o.x.y );
// Object.isFrozen( o.x.y.z );
// Object.isFrozen( o.x.w );
// !Object.isFrozen( InnerClass );
// !Object.isFrozen( InnerClass.x );
// !Object.isFrozen( InnerClass.x.y );
// !Object.isFrozen( InnerFunction );
// !Object.isFrozen( InnerFunction.x );
// !Object.isFrozen( InnerFunction.x.y );
// Object.isFrozen( o.a.b.c.d.e.willBeFrozen );

freezer.jpg

###4) subzero.megaFreezeClass( objectToFreeze )

Deep freeze an object. This will freeze any classes, functions, and objects within the class. It works by recursively freezing anything of type "object" or "function", if they are unfrozen. Note the * MEGA FREEZE CORNER CASE.

'use strict';

class C {};

class InnerClass {};

InnerClass.x = {

    y: {}
};

const controlFunction = function() {};

controlFunction.x = {

    y: {}
};

/* MEGA FREEZE CORNER CASE:
    be careful about objects and functions within already frozen objects and functions
*/
const wontBeFrozen = {};

C.a = {

    b: {

        c: {

            d: {

                InnerClass,

                controlFunction,

                e: Object.freeze({ wontBeFrozen })
            }
        }
    }
};

C.prototype.x = {

    y: {

        z: {}
    },

    w: {}
};


/*
    the following statements will now return true:
*/

// Object.isFrozen( C );
// Object.isFrozen( C.a );
// Object.isFrozen( C.a.b );
// Object.isFrozen( C.a.b.c );
// Object.isFrozen( C.a.b.c.d );
// Object.isFrozen( C.prototype );
// Object.isFrozen( C.prototype.x );
// Object.isFrozen( C.prototype.x.y );
// Object.isFrozen( C.prototype.x.y.z );
// Object.isFrozen( C.prototype.x.w );
// Object.isFrozen( InnerClass );
// Object.isFrozen( InnerClass.x );
// Object.isFrozen( InnerClass.x.y );
// Object.isFrozen( controlFunction );
// Object.isFrozen( controlFunction.x );
// Object.isFrozen( controlFunction.x.y );
// !Object.isFrozen( C.a.b.c.d.e.wontBeFrozen );

frieza22.gif

###5) subzero.megaFreezeObject( objectToFreeze )

Deep freeze an object. This will freeze any classes, functions, and objects within the object. It works by recursively freezing anything of type "object" or "function", if they are unfrozen. Note the * MEGA FREEZE CORNER CASE.

'use strict';

const o =  {};

const InnerClass = class {};

InnerClass.x = {

    y: {}
};

const controlFunction = function() {};

controlFunction.x = {

    y: {}
};

/* MEGA FREEZE CORNER CASE:
    be careful about objects and functions within already frozen objects and functions
*/
const wontBeFrozen = {};

o.a = {

    b: {

        c: {

            d: {

                InnerClass,

                controlFunction,

                e: Object.freeze( { wontBeFrozen } )
            }
        }
    }
};

o.x = {

    y: {

           z: {}
    },

    w: {}
};

subzero.megaFreezeObject( o ) ).to.equal( o );

Object.isFrozen( o )
Object.isFrozen( o.a )
Object.isFrozen( o.a.b )
Object.isFrozen( o.a.b.c )
Object.isFrozen( o.a.b.c.d )
Object.isFrozen( o.x )
Object.isFrozen( o.x.y )
Object.isFrozen( o.x.y.z )
Object.isFrozen( o.x.w )
Object.isFrozen( InnerClass )
Object.isFrozen( InnerClass.x )
Object.isFrozen( InnerClass.x.y )
Object.isFrozen( controlFunction )
Object.isFrozen( controlFunction.x )
Object.isFrozen( controlFunction.x.y )
Object.isFrozen( o.a.b.c.d.e.wontBeFrozen )