@protagonists/locked-array
v1.0.1
Published
Ever wanted readonly arrays?
Downloads
2
Readme
About
An array that can be completely locked from adding/removing elements on demand
Table of content
How to use?
Description
This is a simple array package that can prevent users from modifying the array itself
Elements within it can still be modified and accessed as normal
Import
Terminal
npm install @protagonists/locked-array
Node.js
const LockedArray = require("@protagonists/locked-array");
Example
Code:
// Imports
const LockedArray = require("@protagonists/locked-array");
// Create LockedArray instance
const fruits = new LockedArray(["apple", "orange"]);
// Add "tomato" in the array
fruits.add("tomato");
// Lock the array
fruits.lock();
// Log the array's value
console.log(fruits.value);
// try to add element to the direct value
fruits.value.push("banana");
// Log the array's value
console.log(fruits.value);
Output:
[ 'apple', 'orange', 'tomato' ]
[ 'apple', 'orange', 'tomato' ]