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.
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:
- simple to use,
- 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.preventExtensions functions available, however none of them matches my needs.
Install Panzerschrank to your project...
yarn add panzerschrank
...then use it like so:
;const safeObj = ;
;const safeObj = ;
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:
- In case of non-primitive, you receive a new instance of given object on each get.
- Adding/modifying property is changed.
- Vault is iterable.
- You are not able to list keys - this is intentional, but may be changed in future.
const safeObj = ;safeObjnumbers; // [0, 1, 2], but...safeObjnumbers !== safeObjnumbers; // === truesafeObjnumbers; // === 2safeObjnumbers; // === 2safeObjnumberslength; // === 3
Adding a new property or modifying an existing one is easy...
const safeObj = ;;safeObjnumbers; // === '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 = ;const str = '2234';;
however, safeObj accepts extra arguments :)
const safeObj = ;;safeObjnumbers; // === '2234'
This is the only way to do it. Setting property in a normal way simply won't work.
const safeObj = ;safeObjnumbers = 2; // throws exception or does nothing
const safeObj = ;for const key value of safeObjconsole; // prints foo, bar and then numbers and [0, 1, 2];