New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

typology

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typology - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

8

package.json
{
"name": "typology",
"version": "0.1.1",
"description": "A type checking library for Node.js and the browser.",
"version": "0.2.0",
"description": "A data validation library for Node.js and the browser.",
"main": "typology.js",

@@ -14,4 +14,4 @@ "scripts": {

"keywords": [
"validation",
"types"
"data",
"validation"
],

@@ -18,0 +18,0 @@ "authors": [

# Typology
Typology is a lightweight type checking library for Node.js and the browser (with or without [Browserify](http://browserify.org/)).
Typology is a lightweight data validation library for Node.js and the browser (with or without [Browserify](http://browserify.org/)).

@@ -76,3 +76,3 @@ It can validate variables against native JavaScript types as well as against custom types you can define.

<tr>
<td><code>type</code></td>
<td><code>'type'</code></td>
<td>required</td>

@@ -83,3 +83,3 @@ <td><code>'string'</code></td>

<tr>
<td><code>?type</code></td>
<td><code>'?type'</code></td>
<td>optional</td>

@@ -90,3 +90,3 @@ <td><code>'?string'</code></td>

<tr>
<td><code>type1|type2</code></td>
<td><code>'type1|type2'</code></td>
<td>multi-types</td>

@@ -97,3 +97,3 @@ <td><code>'string|number'</code></td>

<tr>
<td><code>{prop: type}</code></td>
<td><code>{prop: 'type'}</code></td>
<td>complex</td>

@@ -104,3 +104,3 @@ <td><code>{firstname: 'string'}</code></td>

<tr>
<td><code>[type]</code></td>
<td><code>['type']</code></td>
<td>lists</td>

@@ -110,5 +110,11 @@ <td><code>['number']</code></td>

</tr>
<tr>
<td><code>'!type'</code></td>
<td>exclusive</td>
<td><code>'!string'</code></td>
<td><code>42</code></td>
</tr>
</table>
Note also that expression can be combined. For instance `'?string|number'` means an optional string or number variable.
Note also that expression can be combined. For instance `'?string|number'` means an optional string or number variable and `'!string|object'` means anything but a string or an object.

@@ -153,11 +159,14 @@ *Overkill example*

#### Add a custom type for later user
#### Create your own typology to add custom types
```js
var types = require('typology');
var Typology = require('typology');
types.add(myCustomType);
var myTypology = new Typology();
// Then add custom definitions
myTypology.add(myCustomType);
// Example
types.add('User', {
myTypology.add('User', {
firstname: 'string',

@@ -169,7 +178,7 @@ lastname: 'string',

// Then you can use it likewise
types.check({hello: 'world'}, 'User');
myTypology.check({hello: 'world'}, 'User');
>>> false
// And use it in other types' definition
types.check(myVar, 'User|number');
myTypology.check(myVar, 'User|number');
```

@@ -176,0 +185,0 @@

/**
* typology.js - a type checking library for Node.js and the browser,
* typology.js - A data validation library for Node.js and the browser,
*
* Version: 0.1.1
* Version: 0.2.0
* Sources: http://github.com/jacomyal/typology

@@ -47,4 +47,3 @@ * Doc: http://github.com/jacomyal/typology#readme

class2type = {},
nativeTypes = ['*'],
customTypes = {};
nativeTypes = ['*'];

@@ -58,5 +57,17 @@ // Fill types

var types = {
version: '0.1.1',
add: function(a1, a2) {
/**
* Main object
*/
function Typology(defs) {
defs = defs || {};
// Privates
var customTypes = {};
/**
* Methods
*/
// Adding a custom type
this.add = function(a1, a2) {
var o,

@@ -132,12 +143,20 @@ k,

delete customTypes[k];
},
has: function(key) {
return this;
};
// Check whether this typology has the given type
this.has = function(key) {
return !!customTypes[key];
},
get: function(obj) {
};
// Get the native type of the given variable
this.get = function(obj) {
return (obj === null || obj === undefined) ?
String(obj) :
class2type[Object.prototype.toString.call(obj)] || 'object';
},
check: function(obj, type) {
};
// Validate the given data against the given type
this.check = function(obj, type) {
var a,

@@ -147,6 +166,7 @@ i,

optional = false,
exclusive = false,
typeOf = this.get(obj);
if (this.get(type) === 'string') {
a = type.replace(/^\?/, '').split(/\|/);
a = type.replace(/^[?!]/, '').split(/\|/);
for (i in a)

@@ -163,2 +183,7 @@ if (nativeTypes.indexOf(a[i]) < 0 && !(a[i] in customTypes)) {

if (type.match(/^!/)) {
exclusive = true;
type = type.replace(/^!/, '');
}
for (i in a)

@@ -171,8 +196,10 @@ if (customTypes[a[i]])

)
return true;
return !exclusive;
if (obj === null || obj === undefined)
return optional;
return !exclusive ? optional : !optional;
else
return !!(~a.indexOf('*') || ~a.indexOf(typeOf));
return !exclusive ?
!!(~a.indexOf('*') || ~a.indexOf(typeOf)) :
!(~a.indexOf('*') || ~a.indexOf(typeOf));
} else if (this.get(type) === 'object') {

@@ -205,4 +232,6 @@ if (typeOf !== 'object')

return false;
},
isValid: function(type) {
};
// Is the given type valid?
this.isValid = function(type) {
var a,

@@ -213,3 +242,3 @@ k,

if (this.get(type) === 'string') {
a = type.replace(/^\?/, '').split(/\|/);
a = type.replace(/^[?!]/, '').split(/\|/);
for (i in a)

@@ -232,16 +261,42 @@ if (nativeTypes.indexOf(a[i]) < 0 && !(a[i] in customTypes))

return false;
}
};
};
// Add a type "type" to shortcut the isValid method:
types.add('type', function(v) {
return types.isValid(v);
});
/**
* Instantiation routine
*/
// Add a type "primitive" to match every primitive types (including null):
types.add('primitive', function(v) {
return !v || !(v instanceof Object || typeof v === 'object');
// Add a type "type" to shortcut the isValid method:
this.add('type', (function(v) {
return this.isValid(v);
}).bind(this));
// Add a type "primitive" to match every primitive types (including null):
this.add('primitive', function(v) {
return !v || !(v instanceof Object || typeof v === 'object');
});
// Adding custom types at instantiation
if (this.get(defs) !== 'object')
throw Error('Invalid argument.');
for (var k in defs)
this.add(k, defs[k]);
}
/**
* Public interface
*/
// Creating a "main" typology instance to export
var types = Typology;
Typology.call(types);
// Version
Object.defineProperty(types, 'version', {
value: '0.2.0'
});
// Export:
/**
* Export
*/
if (typeof exports !== 'undefined') {

@@ -248,0 +303,0 @@ if (typeof module !== 'undefined' && module.exports)

/**
* typology - A type checking library for Node.js and the browser.
* @version v0.1.1
* typology - A data validation library for Node.js and the browser.
* @version v0.2.0
* @link https://github.com/jacomyal/typology
* @license MIT
*/
!function(){"use strict";var e,t,r=["Arguments","Boolean","Number","String","Function","Array","Date","RegExp","Object"],i={},n=["*"],o={};for(e in r)t=r[e],n.push(t.toLowerCase()),i["[object "+t+"]"]=t.toLowerCase();var s={version:"0.1.1",add:function(e,t){var r,i,s,f,a,d;if(1===arguments.length){if("object"!==this.get(e))throw new Error("If types.add is called with one argument, this one has to be an object.");r=e,f=r.id,d=r.type}else{if(2!==arguments.length)throw new Error("types.add has to be called with one or three arguments.");if("string"!=typeof e||!e)throw new Error("If types.add is called with more than one argument, the first one must be the string id.");f=e,d=t}if("string"!==this.get(f)||0===f.length)throw new Error("A type requires an string id.");if(void 0!==o[f]&&"proto"!==o[f])throw new Error('The type "'+f+'" already exists.');if(~n.indexOf(f))throw new Error('"'+f+'" is a reserved type name.');o[f]=1,s=(r||{}).proto||[],s=Array.isArray(s)?s:[s],a={};for(i in s)void 0===o[s[i]]&&(o[s[i]]=1,a[s[i]]=1);if("function"!==this.get(d)&&!this.isValid(d))throw new Error("A type requires a valid definition. This one can be a preexistant type or else a function testing given objects.");if(o[f]=void 0===r?{id:f,type:d}:{},void 0!==r)for(i in r)o[f][i]=r[i];for(i in a)i!==f&&delete o[i]},has:function(e){return!!o[e]},get:function(e){return null===e||void 0===e?String(e):i[Object.prototype.toString.call(e)]||"object"},check:function(e,t){var r,i,s,f=!1,a=this.get(e);if("string"===this.get(t)){r=t.replace(/^\?/,"").split(/\|/);for(i in r)if(n.indexOf(r[i])<0&&!(r[i]in o))throw new Error("Invalid type.");t.match(/^\?/)&&(f=!0,t=t.replace(/^\?/,""));for(i in r)if(o[r[i]]&&("function"==typeof o[r[i]].type?o[r[i]].type(e)===!0:this.check(e,o[r[i]].type)))return!0;return null===e||void 0===e?f:!(!~r.indexOf("*")&&!~r.indexOf(a))}if("object"===this.get(t)){if("object"!==a)return!1;for(s in t)if(!this.check(e[s],t[s]))return!1;for(s in e)if(void 0===t[s])return!1;return!0}if("array"===this.get(t)){if("array"!==a)return!1;if(1!==t.length)throw new Error("Invalid type.");for(s in e)if(!this.check(e[s],t[0]))return!1;return!0}return!1},isValid:function(e){var t,r,i;if("string"===this.get(e)){t=e.replace(/^\?/,"").split(/\|/);for(i in t)if(n.indexOf(t[i])<0&&!(t[i]in o))return!1;return!0}if("object"===this.get(e)){for(r in e)if(!this.isValid(e[r]))return!1;return!0}return"array"===this.get(e)&&1===e.length?this.isValid(e[0]):!1}};s.add("type",function(e){return s.isValid(e)}),s.add("primitive",function(e){return!e||!(e instanceof Object||"object"==typeof e)}),"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=s),exports.types=s):"function"==typeof define&&define.amd?define("typology",[],function(){return s}):this.types=s}(this);
!function(){"use strict";function t(t){t=t||{};var e={};if(this.add=function(t,r){var i,n,s,f,a,h;if(1===arguments.length){if("object"!==this.get(t))throw new Error("If types.add is called with one argument, this one has to be an object.");i=t,f=i.id,h=i.type}else{if(2!==arguments.length)throw new Error("types.add has to be called with one or three arguments.");if("string"!=typeof t||!t)throw new Error("If types.add is called with more than one argument, the first one must be the string id.");f=t,h=r}if("string"!==this.get(f)||0===f.length)throw new Error("A type requires an string id.");if(void 0!==e[f]&&"proto"!==e[f])throw new Error('The type "'+f+'" already exists.');if(~o.indexOf(f))throw new Error('"'+f+'" is a reserved type name.');e[f]=1,s=(i||{}).proto||[],s=Array.isArray(s)?s:[s],a={};for(n in s)void 0===e[s[n]]&&(e[s[n]]=1,a[s[n]]=1);if("function"!==this.get(h)&&!this.isValid(h))throw new Error("A type requires a valid definition. This one can be a preexistant type or else a function testing given objects.");if(e[f]=void 0===i?{id:f,type:h}:{},void 0!==i)for(n in i)e[f][n]=i[n];for(n in a)n!==f&&delete e[n];return this},this.has=function(t){return!!e[t]},this.get=function(t){return null===t||void 0===t?String(t):n[Object.prototype.toString.call(t)]||"object"},this.check=function(t,r){var i,n,s,f=!1,a=!1,h=this.get(t);if("string"===this.get(r)){i=r.replace(/^[?!]/,"").split(/\|/);for(n in i)if(o.indexOf(i[n])<0&&!(i[n]in e))throw new Error("Invalid type.");r.match(/^\?/)&&(f=!0,r=r.replace(/^\?/,"")),r.match(/^!/)&&(a=!0,r=r.replace(/^!/,""));for(n in i)if(e[i[n]]&&("function"==typeof e[i[n]].type?e[i[n]].type(t)===!0:this.check(t,e[i[n]].type)))return!a;return null===t||void 0===t?a?!f:f:a?!(~i.indexOf("*")||~i.indexOf(h)):!(!~i.indexOf("*")&&!~i.indexOf(h))}if("object"===this.get(r)){if("object"!==h)return!1;for(s in r)if(!this.check(t[s],r[s]))return!1;for(s in t)if(void 0===r[s])return!1;return!0}if("array"===this.get(r)){if("array"!==h)return!1;if(1!==r.length)throw new Error("Invalid type.");for(s in t)if(!this.check(t[s],r[0]))return!1;return!0}return!1},this.isValid=function(t){var r,i,n;if("string"===this.get(t)){r=t.replace(/^[?!]/,"").split(/\|/);for(n in r)if(o.indexOf(r[n])<0&&!(r[n]in e))return!1;return!0}if("object"===this.get(t)){for(i in t)if(!this.isValid(t[i]))return!1;return!0}return"array"===this.get(t)&&1===t.length?this.isValid(t[0]):!1},this.add("type",function(t){return this.isValid(t)}.bind(this)),this.add("primitive",function(t){return!t||!(t instanceof Object||"object"==typeof t)}),"object"!==this.get(t))throw Error("Invalid argument.");for(var r in t)this.add(r,t[r])}var e,r,i=["Arguments","Boolean","Number","String","Function","Array","Date","RegExp","Object"],n={},o=["*"];for(e in i)r=i[e],o.push(r.toLowerCase()),n["[object "+r+"]"]=r.toLowerCase();var s=t;t.call(s),Object.defineProperty(s,"version",{value:"0.1.1"}),"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=s),exports.types=s):"function"==typeof define&&define.amd?define("typology",[],function(){return s}):this.types=s}(this);
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