types-magic
v2.0.5
Published
Create and use your own types
Readme
Create types with Magic 🪄
WARNING ⚠️
Use versions 2.0.5 < For stable use.
Description
The types-magic module provides a versatile class for managing labeled parameters with customizable types. It offers methods for easy access, manipulation, and conversion of parameter values. This module is suitable for various data management tasks in JavaScript applications.
Installation
You can install the types-magic module via npm by running:
npm install types-magicUsage
const $ = require('types-magic');
let instance = new $(['name', 'age'], ['string', 'number']);
instance.name = 'John';
instance.age = 30;
console.log(instance.toString());
Or:
const $ = require('types-magic');
const person = new $([['name', 'age'], ['string', 'number']]);
let instance1 = person.new()
instance1.name = 'John';
instance1.age = 30;
let instance2 = person.new({
name:'Jane',
age:34,
})
console.log(instance1.toString());
console.log(instance2.toXML());
Proxy and _pHandler
To proxy data you can use _pHandler
const $ = require('types-magic');
const person = new $([['name', 'age', 'incognito'], ['string', 'number', 'boolean']]);
person._pHandler = {
get: function (target, prop, receiver) {
if(person.incognito){
return "[Unknown]"
}
return target[prop];
},
set: function (target, prop, value, receiver) {
if(value === null){
throw new Error('What would you do if your name was null!')
}
else{
console.log(`I like the name ${value}`)
target[prop] = value;
}
},
}
person.initProxy()
person._proxy.name = 'bob' //I like the name bob
person.incognito = false
console.log(person._proxy.name)//bob
person.incognito = true
console.log(person._proxy.name)//[unknown]
person._proxy.name = null // What would you do if your name was nullConfigurations
const $ = require('types-magic');
const person = new $([['name', 'age'], ['string', 'number']]);
person.configure('const', true) //the persons name can't change after set
person.configure('loose', true) //type errors will be reduced to logs. (not recommened)
person.configure('looser', true)//type errors will be not be logged. (not recommened)
person.configure('nullBypass', true)//null will be allowed on any data type. (recommended)
let bob = person.new({name:"bob",age:25})//correct
let stewart = person.new()
stewart.name = 'stewart'//incorrect
stewart.age = 33 //incorrectAll conversion functions include:
- toString()
- toObject()
- toArray()
- toJSON()
- toXML()
- toYAML()
- toCSV()
Known bugs:
- Bug: Dates will not appear in some data types.
- Fix: Use objects instead of dates.
