Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

castaway

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

castaway

cast JS objects to a specified schema, ahoy!

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

browser support

Build Status

castaway

cast an object's values to a specified schema

npm install castaway

Given an object, and a schema defining the types of the objects properties, castaway will convert the values to the appropriate JS primitives.

var cast = require('castaway');

var obj = { foo: '1337'},
	schema = { foo: { type: Number }};

cast(obj, schema);

console.log(typeof obj.foo); // number

Supported type values are:

  • Number
  • Boolean
  • Date
  • Array (converts a CSV string into an Array)

Arrays also support defining member schemas via an each property, and specifying the schema or types:

Here's an example of specifying the schema via each for an array...

var schema = {
		foo: {
			type: Array,
			each: {
				schema: {
					id: { type: Number },
					when: { type: Date }
				}
			}
		}
	},
	obj = { foo: [
		{ id: '1', hee: 'haw', when: '2013-02-28 16:31:13' },
		{ id: '2', hee: 'hoo', when: '2013-02-28' }
	]};

	cast(obj, schema);
	// for each entry in foo, id is a Number, when is a Date, hee is still a String

And here's an example of just specifying the type for an array via each...

var schema = { foo: { type: Array, each: { type: Number } } },
	obj = { foo: '1,2,3,4,5,6,7,8,9' };

cast(obj, schema);
// foo is an array of numbers

Keywords

FAQs

Package last updated on 20 Jun 2013

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc