node-containerpattern
A 'Container pattern' object for a clean global use of data.
Basicaly, it extends Map object with lot of controls, recursive methods and documentation
Installation
$ npm install node-containerpattern
Features
- Remember data by there choosen keys
- use optional skeleton to ensure data formats
- Access to the data easily and recursively
- Check recursively data by the key and the value
- Manipule the data : set, get, has, delete, ...
- Easily create documenation
Doc
-- Inheritance --
check the official 'Map' object documentation
-- Attributes --
object documentations
used to save documentation by data's keyobject limits
used to limit data's values possibilitiesstring recursionSeparator
used to parse recursive keys (default : '.')object skeletons
used to force data type
-- Constructor --
constructor([ string recursionSeparator = "." ])
-- Methods --
clear() : return this
clearData & clearDocumentations & clearLimits & clearSkeletonsclearData() : return this
forget all the keys and there values and documentations (=> Map.clear)clearDocumentations() : return this
forget all the skeletonsclearLimits() : return this
forget all the limitsclearSkeletons() : return this
forget all the skeletonsdelete(string key) : return this
forget a key and its valuedocument(string key, string documentation) : return this
attach a documentation on the data. only visible if "set" method is applied with this key.documentation() : return JSON object
generate a documentation for all the stored dataget(string key) : return mixed
return the value in association with this key (may be recursive)has(string key) : return bool
check if a key is used (may be recursive)limit(string key, array limit) : return this
associate a key with a limitset(string key, mixed value) : return this
associate and remember a key with a value (may be recursive)skeleton(string key, string skeleton) : return this
skeleton must be "array", "boolean", "email", "float", "integer", "ipv4", "ipv6", "number", "object", "string"
-- notes --
- if the skeleton is an 'array' or an 'object', and value data is a string, JSON.parse is apply before throw any error
- if the skeleton is an 'email', value must be a string. it can be empty, but must be a valid address if not
Examples
const Container = require('node-containerpattern');
var container = new Container();
container
.skeleton("contact", "email").document("contact", "Contact address")
.skeleton("debug", "boolean").document("debug", "This is the debug module")
.skeleton("vat", "float")
.skeleton("heigth", "integer")
.set('contact', "myaddress@provider.com")
.set('debug', true)
.set('vat', '5.5').set('vat', 5.5)
.set('conf', { usr : { login : 'login', pwd : 'pwd' } })
.set('conf.usr.login', "login2")
.set('object', new Object())
.limit("debug", [true, false]);
console.log(container.get('conf').usr.login);
console.log(container.get('conf.usr.login'));
console.log(container.get('object'));
console.log(container.get('conf'));
container.delete('object');
console.log(container.has('object'));
console.log(container.size);
for (let key of container.keys()) {
console.log(key);
}
for (let value of container.values()) {
console.log(value);
}
container.forEach((value, key) => {
console.log("container[" + key + "] = " + value);
});
console.log(container.documentation());
container.clear();
Tests
$ gulp
License
ISC