Git Product home page Git Product logo

deepfreeze's Introduction

@3imed-jaberi/deepfreeze

Build Status Coverage Status NPM version License Code Size

The modern and amiable deep freeze, works with Map and Set ๐Ÿฅถ

Features

  • ๐Ÿ’…๐Ÿป Inspired by many solutions in a modern way.
  • ๐ŸŒช Recursively Object.freeze() for objects, functions and arrays.
  • ๐Ÿฅž Support for Map and Set.
  • ๐Ÿ’‰ Add .isDeepFrozen() method for each frozen object.
  • ๐ŸŽฏ Support Pure Deep Freeze through deep clone.
  • ๐ŸงŠ Handle primitives types natively.

Installation

# npm
$ npm install @3imed-jaberi/deepfreeze
# yarn
$ yarn add @3imed-jaberi/deepfreeze

Usage

This is a practical example of how to use.

"use strict";

const deepfreeze = require("@3imed-jaberi/deepfreeze");

const object = {
  someBoolean: true,
  someNumber: 10,
  someString: "hello!",
  someObject: { foo: "bar", someNestedObject: { nested: "inObject" } },
  someArray: [false, 20, "bye!", { nested: "inArray" }],
  someMap: new Map(["item1", "item2"]),
  someSet: new Set(["item1", "item2"]),
};

const frozenObject = deepfreeze(object);

Object.isFrozen(frozenObject); // true
Object.isFrozen(frozenObject.someBoolean); // true
Object.isFrozen(frozenObject.someNumber); // true
Object.isFrozen(frozenObject.someString); // true
Object.isFrozen(frozenObject.someObject); // true
Object.isFrozen(frozenObject.someObject.foo); // true
Object.isFrozen(frozenObject.someObject.someNestedObject); // true
Object.isFrozen(frozenObject.someObject.someNestedObject.nested); // true
Object.isFrozen(frozenObject.someArray); // true
frozenObject.someArray.forEach((value) => {
  Object.isFrozen(value); // true
});
frozenObject.someMap.clear(); // Error: Map is read-only.
frozenObject.someSet.clear(); // Error: Set is read-only.

API

You can pass some arguments to the freeze method;

  • object โ€” (Object) object to deep freeze.
  • options.isProd โ€” (Boolean) ignore freezing in production (for better performance) (default to false).
  • options.pureFreeze โ€” (Boolean) use lodash.cloneDeep to create a copy and use it dealing the freezing process (default to false).

If the passed object handled by our method, a method called isDeepFrozen will be inserted as property to the object.

License


MIT ยฉ Imed Jaberi

deepfreeze's People

Contributors

3imed-jaberi avatar

Stargazers

Ahmed Rebai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.