New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

types-magic

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

types-magic

Create and use your own types

latest
npmnpm
Version
2.0.5
Version published
Maintainers
1
Created
Source

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-magic

Usage

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 null

Configurations

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 //incorrect

All 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.

FAQs

Package last updated on 26 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts