Giter VIP home page Giter VIP logo

panzerschrank's Introduction

Panzerschrank

The reliable container for your data.

Panzerschrank leverages the power of ES2015+ to bring you an effortless way to help you deal with your objects literals.

Why?

Frankly speaking - out of sheer boredom.

There is one more reason, though.

Well, I haven't any tool fullfiling my requirements that are as follow:

  1. simple to use,
  2. lightweight,
  3. extensive object literals (described below)

What I needed is basically to have a possibility to unfreeze a frozen object. Unfreezing means I want to have a possibility to add new or modify existing properties Is there any way to prevent mutation to happen? There are

  • Object.seal
  • Object.freeze
  • Object.preventExtensions functions available, however none of them matches my needs.

Installation:

Install Panzerschrank to your project...

yarn add panzerschrank

...then use it like so:

import vault from 'panzerschrank/sloppy';
const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });

or

import vault from 'panzerschrank/strict';
const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });

Documentation:

Vault uses proxy under the hood, therefore it intercepts property access. Basically it works like a normal object with quite a few gotchas, though. The key differences are:

  1. In case of non-primitive, you receive a new instance of given object on each get.
  2. Adding/modifying property is changed.
  3. Vault is iterable.
  4. You are not able to list keys - this is intentional, but may be changed in future.
const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });
safeObj.numbers; // [0, 1, 2], but...
safeObj.numbers !== safeObj.numbers; // === true
safeObj.numbers.pop(); // === 2
safeObj.numbers.pop(); // === 2
safeObj.numbers.length; // === 3

Adding a new property or modifying an existing one is easy...

const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });
safeObj(obj => {
  obj.numbers = 'hey, i am string now!';
});
safeObj.numbers; // === 'hey, i am string now!'

To avoid the leak of object, the passed function is sandboxed, meaning you can't accept anything but its own scope.

const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });
const str = '2234';
safeObj(obj => {
  obj.numbers = str; // throws ReferenceError;
});

however, safeObj accepts extra arguments :)

const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });
safeObj((obj, str) => {
  obj.numbers = str;
}, '2234');
safeObj.numbers; // === '2234'

This is the only way to do it. Setting property in a normal way simply won't work.

const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });
safeObj.numbers = 2; // throws exception or does nothing
const safeObj = vault({ foo: 'bar', numbers: [0, 1, 2] });
for (const [key, value] of safeObj) {
  console.log(key, value); // prints foo, bar and then numbers and [0, 1, 2];
}

License

GPLv3

panzerschrank's People

Contributors

p0lip avatar

Stargazers

 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.