Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

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
7.1.0
to
7.2.0
+48
lib/exemplar.js
/**
* Module dependencies
*/
var _ = require('lodash');
var typeInfo = require('./type-info');
var dehydrate = require('./dehydrate');
var buildSchemaIterator = require('./helpers/build-schema-iterator');
/**
* Given a type schema, return a value usable as an exemplar
*
* @param {String} typeSchema
* @return {[===]}
*/
module.exports = function example (typeSchema) {
// Dehydrate the type schema to avoid circular recursion
var dehydratedTypeSchema = dehydrate(typeSchema);
// Configure type schema iterator
return buildSchemaIterator(
function onFacetDict(facetDictionary, parentKeyOrIndex, callRecursive){
return _.reduce(facetDictionary, function (memo, val, key) {
var facet = callRecursive(val, key);
memo[key] = facet;
return memo;
}, {});
},
function onPatternArray(patternArray, parentKeyOrIndex, iterateRecursive){
var pattern = iterateRecursive(patternArray[0], 0);
return [ pattern ];
},
function onGenericDict(schema, parentKeyOrIndex){
return typeInfo('dictionary').getExemplar();
},
function onGenericArray(schema, parentKeyOrIndex){
return typeInfo('array').getExemplar();
},
function onOther(schema, parentKeyOrIndex){
return typeInfo(schema).getExemplar();
}
)(dehydratedTypeSchema);
};
/**
* Module dependencies
*/
var _ = require('lodash');
var TYPES = require('./helpers/types');
var dehydrate = require('./dehydrate');
var buildSchemaIterator = require('./helpers/build-schema-iterator');
/**
* Given an exemplar schema and a keypath, return information
* about the specified segment.
*
* If the path is inside of a generic, then the exemplar is '*',
* and this path is optional. If the path is inside of a `ref`,
* then the exemplar is '===', and this path is optional.
*
* @param {*} schema
* @return {{}}
*/
module.exports = function getPathInfo (schema, path) {
// Dehydrate the schema to avoid circular recursion
var dehydratedSchema = dehydrate(schema);
// Derive an array of "hops" from the provided keypath.
var hops = (path === '') ? [] : path.split('.');
// These variables are used by the iterator below.
var currentExemplar = dehydratedSchema;
// By default the path is not optional.
var optional = false;
_.each(hops, function dereferenceEach(hop){
if (_.isArray(currentExemplar)) {
if (!_.isFinite(+hop)) {
// If the hop cannot be cast to a positive integer,
// something funny is going on.
throw new Error('Invalid keypath: "'+path+'" (has weird hop: '+hop+').');
}
// generic array
if (_.isEqual(currentExemplar, [])) {
optional = true;
currentExemplar = TYPES.json.getExemplar();
}
// patterned array
else {
optional = false;
if (_.isUndefined(currentExemplar[0])) {
throw new Error('Invalid keypath: "'+path+'" (unreachable in schema)');
}
else {
currentExemplar = currentExemplar[0];
}
}
}
else if (_.isObject(currentExemplar)) {
// generic dictionary
if (_.isEqual(currentExemplar, {})) {
optional = true;
currentExemplar = TYPES.json.getExemplar();
}
// facted dictionary
else {
optional = false;
if (_.isUndefined(currentExemplar[hop])) {
throw new Error('Invalid keypath: "'+path+'" (unreachable in schema)');
}
else {
currentExemplar = currentExemplar[hop];
}
}
}
// generic json
else if (TYPES.json.isExemplar(currentExemplar)) {
optional = true;
currentExemplar = TYPES.json.getExemplar();
}
// mutable reference
else if (TYPES.ref.isExemplar(currentExemplar)) {
optional = true;
currentExemplar = TYPES.ref.getExemplar();
}
// lamda
else if (TYPES.lamda.isExemplar(currentExemplar)) {
throw new Error('Invalid keypath: "'+path+'" (unreachable in schema)');
}
// primitive
else {
throw new Error('Invalid keypath: "'+path+'" (unreachable in schema)');
}
});
return {
exemplar: currentExemplar,
optional: optional
};
};
+2
-0

@@ -15,2 +15,3 @@ module.exports = {

sample: require('./lib/sample'),
exemplar: require('./lib/exemplar'),
getDisplayType: require('./lib/get-display-type'),

@@ -22,2 +23,3 @@ isStrictType: require('./lib/is-strict-type'),

union: require('./lib/union'),
getPathInfo: require('./lib/get-path-info'),
};

@@ -24,0 +26,0 @@

+1
-1

@@ -10,3 +10,3 @@ /**

/**
* Build an iterator/reducer function for a type schema that builds up and returns a new value.
* Build an iterator/reducer function for a schema that builds up and returns a new value.
*

@@ -13,0 +13,0 @@ * @param {Function} onFacetDict

@@ -89,2 +89,5 @@ /**

},
getExemplar: function() {
return 'a string';
},
isExemplar: function (eg){

@@ -168,2 +171,5 @@ return TYPES.str.is(eg) && !TYPES.lamda.isExemplar(eg) && !TYPES.json.isExemplar(eg) && !TYPES.ref.isExemplar(eg);

},
getExemplar: function() {
return 123;
},
isExemplar: function (eg){

@@ -215,2 +221,5 @@ return TYPES.number.is(eg);

},
getExemplar: function() {
return true;
},
isExemplar: function (eg){

@@ -293,2 +302,5 @@ return TYPES.boolean.is(eg);

},
getExemplar: function() {
return {};
},
isExemplar: function (eg){

@@ -330,2 +342,5 @@ return TYPES.dictionary.is(eg);

},
getExemplar: function() {
return [];
},
isExemplar: function (eg){

@@ -377,2 +392,5 @@ return TYPES.array.is(eg);

},
getExemplar: function() {
return '===';
},
isExemplar: function (eg){

@@ -453,2 +471,5 @@ return eg === '===' || eg === undefined;

},
getExemplar: function() {
return '*';
},
isExemplar: function (eg){

@@ -499,2 +520,5 @@ return eg === '*';

},
getExemplar: function() {
return '->';
},
isExemplar: function (eg){

@@ -501,0 +525,0 @@ return _.isString(eg) && eg.match(/(^-+>$)|(^=+>$)|(^<=+$)|(^<-+$)/);

{
"name": "rttc",
"version": "7.1.0",
"version": "7.2.0",
"description": "Runtime type-checking for JavaScript.",

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