Socket
Socket
Sign inDemoInstall

rttc

Package Overview
Dependencies
Maintainers
4
Versions
108
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rttc - npm Package Compare versions

Comparing version 0.1.1 to 0.1.3

lib/coerce.js

16

index.js

@@ -1,2 +0,14 @@

// Expose `rttc()` method.
module.exports = require('./lib/rttc');
module.exports = {
// Expose `validate()` and `coerce()` methods.
validate: require('./lib/validate'),
coerce: require('./lib/coerce'),
// Support for rttc() and infer()
// (these are here for compatibility and will likely
// be removed in a future version)
rttc: require('./lib/rttc'),
infer: require('./lib/rttc'),
// Also expose `types` object for compat.
types: require('./lib/types')
};

36

lib/infer.js

@@ -9,11 +9,12 @@ /**

/**
* Infer a primative type from an example.
*/
/**
* Given a tuple value, check it for primitives
* ________________________________________________________________________________
* @param {*} val there's that "mystery meat" again
* ________________________________________________________________________________
* @returns {String}
*/
var checkTuple = function(val) {
function checkTuple(val) {

@@ -32,12 +33,15 @@ var type;

return type;
};
}
/**
* Recursively parse an object to set tuple types
*
* Recursively perform a destructive mutatation on an object
* to set tuple types.
* ________________________________________________________________________________
* @param {Object} obj
* ________________________________________________________________________________
* @returns {Object} Always.
*/
var parseObject = function(obj) {
function parseObject(obj) {
if(!types.obj.is(obj)) return;

@@ -56,10 +60,16 @@

return obj;
};
}
/**
* Given an example, parse it to infer it's primitive type
* Given an example, parse it to infer it's primitive type.
* ________________________________________________________________________________
* @param {*} example
* ________________________________________________________________________________
* @returns {Object}
*/
var infer = function(example) {
function inferPrimitiveType(example) {

@@ -89,4 +99,4 @@ // If the example isn't an object or array we can run through the primatives and see

};
}
module.exports = infer;
module.exports = inferPrimitiveType;

@@ -17,5 +17,17 @@ /**

* inputs are valid.
* ________________________________________________________________________________
* @param {String} type the expected type
*
* @param {*} val the "mystery meat"
*
* @param {Object} flags an object of boolean flags
* @property {Boolean} coerce
* @property {Boolean} baseType
* ________________________________________________________________________________
* @returns {*} If everything worked
* the value that what was formerly "mystery meat", now coerced to `type`.
* @throws {E_UNDEFINED_VAL} If there were validation errors
*/
var coerceValue = function(type, val, flags) {
function coerceValue (type, val, flags) {

@@ -52,9 +64,15 @@ var coerceFlag = flags && flags.coerce || false;

return val;
};
}
/**
* Given a tuple value, check it for primitives
* ________________________________________________________________________________
* @param {String} type the expected type
*
* @param {*} val the "mystery meat"
* ________________________________________________________________________________
* @return {Boolean} is this a match?
*/
var checkTuple = function(type, val) {
function checkTuple (type, val) {

@@ -77,6 +95,18 @@ // Check for string

return false;
};
}
/**
* Given a definition and a values object, ensure our types match up/
* Given a definition and a values object, ensure our types match up.
* ________________________________________________________________________________
* @param {Object} def the example object used to indicate expected types
*
* @param {Object} val the actual object full of "mystery meat"
*
* @param {Object} options
* @property {Boolean} coerce should `val` be coerced into the proper type?
* @property {Boolean} base if value can't be coerced, use base value of type?
* ________________________________________________________________________________
* @returns {*} If everything worked
* value that was formerly "mystery meat", now coerced to `type`.
* @throws {E_UNDEFINED_VAL} If there were validation errors
*/

@@ -83,0 +113,0 @@

@@ -8,2 +8,4 @@ /**

/**

@@ -13,14 +15,6 @@ * Basic type definitions.

* Roughly based on https://github.com/bishopZ/Typecast.js
* ________________________________________________________________________________
* @type {Object}
*/
// Helper functions
var value = function(v) {
return function() { return v; };
};
var compare = function(v1) {
return function(v2) { return v1 === v2; };
};
var type = {

@@ -31,3 +25,3 @@

is: _.isNaN,
to: value(NaN)
to: function () { return NaN; }
},

@@ -38,3 +32,3 @@

is: _.isNull,
to: value(null)
to: function () { return null; }
},

@@ -45,3 +39,3 @@

is: _.isUndefined,
to: value(undefined)
to: function () { return undefined; }
},

@@ -197,2 +191,4 @@

// Aliases

@@ -199,0 +195,0 @@ type.string = type.email = type.url = type.str;

{
"name": "rttc",
"version": "0.1.1",
"version": "0.1.3",
"description": "Runtime type-checking for JavaScript.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -57,3 +57,3 @@ var assert = require('assert');

describe('when an array of primatives is used', function() {
describe('when an array of primitives is used', function() {

@@ -60,0 +60,0 @@ it('should parse an array with a single string', function() {

@@ -144,3 +144,3 @@ var assert = require('assert');

describe('and primative values are given at run-time', function() {
describe('and primitive values are given at run-time', function() {

@@ -147,0 +147,0 @@ ////////////////////////////////

@@ -8,3 +8,3 @@ var assert = require('assert');

describe('when primative values are used', function() {
describe('when primitive values are used', function() {

@@ -11,0 +11,0 @@ // Build an example input schema

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