weak-type-wizard
A small schema enforcer that attempts to cast the properties of an object to the types you want 'em to be.
Last updated 5 years ago by tehshrike .
Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install weak-type-wizard 
SYNC missed versions from official npm registry.

A small schema enforcer that attempts to cast the properties of an object to the types you want 'em to be.

Travis CI

Install

npm install weak-type-wizard
var Wizard = require('weak-type-wizard')

Example


var cast = new Wizard({
	boolean: 'sexy',
	number: 'devil',
	date: 'Tue May 05 2015 20:51:30 GMT-0500 (CDT)',
	string: ['one', 'two', 'cat']
})

cast({
	sexy: "true",
	devil: "667",
	one: 1,
	two: "200"
}) // => { sexy: true, devil: 667, one: '1', two: '200' }

The "primitive" types of boolean, number, date, and string are supported. You can supply default values, or even add your own types, like so:


var wizard = new Wizard({
	date: 'dateProperty',
	exciting: 'butts',
	number: ['someNumber', 'anotherNumber'],
	default: {
		someNumber: 666,
		anotherNumber: '1337'
	},
	cast: {
		exciting: function castToExciting(input) {
			return "!" + input + "!"
		}
	}
})

LevelUP support

Use a wizard as a LevelUP encoding - serialize the objects to JSON, and get them back out with all of the parameters cast to whatever you like. Example:


var levelmem = require('level-mem')

var wizard = new Wizard({
	date: ['dateProperty'],
	number: 'someNumber'
})

var db = levelmem('test', {
	valueEncoding: wizard.getLevelUpEncoding()
})

License

WTFPL

Current Tags

  • 1.4.0                                ...           latest (5 years ago)

7 Versions

  • 1.4.0                                ...           5 years ago
  • 1.3.0                                ...           5 years ago
  • 1.2.4                                ...           5 years ago
  • 1.2.1                                ...           6 years ago
  • 1.2.0                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 4
Last Day 1
Last Week 2
Last Month 4
Dependencies (1)
Dev Dependencies (4)

Copyright 2014 - 2017 © taobao.org |