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

types.js

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

types.js - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

13

package.json
{
"name": "types.js",
"version": "1.1.0",
"version": "1.2.0",
"description": "A tiny Javascript type-check library",

@@ -11,3 +11,3 @@ "main": "types.min.js",

"type": "git",
"url": "https://github.com/phazelift/Types.js"
"url": "https://github.com/phazelift/types.js"
},

@@ -18,3 +18,6 @@ "keywords": [

"types",
"validation"
"validation",
"check",
"validation",
"force"
],

@@ -24,7 +27,7 @@ "author": "Dennis Raymondo <phazelift@gmail.com> (https://github.com/phazelift/)",

"bugs": {
"url": "https://github.com/phazelift/Types.js/issues"
"url": "https://github.com/phazelift/types.js/issues"
},
"homepage": "https://github.com/phazelift/Types.js",
"homepage": "https://github.com/phazelift/types.js",
"dependencies": {},
"devDependencies": {}
}
types.js
========
<br/>
A tiny (1.8kb), but essential Javascript type checking library.
A tiny (1.3kb), but essential Javascript type checking library.
Especially in non-typed scripting languages like Javascript, proper manual type checking is crucial.
Because type checking in Javascript is so confusing and contradictory sometimes, I decided to make a
definitive library for myself, with for me understandable standards.
Especially in non-typed scripting languages like Javascript, proper manual type checking is crucial. Unfortunately
the Javascript `typeof` operator gives us in some cases vague clues about what type some value is. `typeof []`
renders `'object'`, even in strict mode! So if we want to know if some input is an `'object'`, we are constantly fooled
by `typeof` returning `'object'` for `[]`, `null` and `/regexp/`. The latter return `'array'`, `'null'` and `'regexp'`
respectively in types.js.
`(NaN !== NaN) == true` still freakes me out, I just don't like to see that kind of stuff in my code.
With types.js I can now test NaN with: `Types.isNaN(NaN)` or `Types.typeof( parseInt('Not A Number!') ) === 'nan'`,
both will return true.
types.js treats `NaN` a little different. In Javascript `NaN` (Not A Number) is of type Number, and `NaN === NaN`
returns `false`... For that I added `nan` to Type's type-definitions. Now you can test NaN with: `Types.isNaN(NaN)` or
`Types.typeof( parseInt('Not A Number!') ) === 'nan'`, both will return true.
Object is another one; `typeof ['array']` renders `'object'`, even in strict mode. So if we want to know
if some input is an `'object'`, we are constantly fooled by `typeof` returning `'object'` for `[]`, `null`
and `/regexp/`. With types.js the latter returns `'array'`, `'null'` and `'regexp'` respectively.
Be careful with using types.js for variables created with `new Number()` or other non-literal type instantiations. No
Be careful with using types.js for variables created with `new Number()` or other non-literal instantiations. No
support for them, because I don't want to get `'number'` on `Types.typeof( new Number(10) )`, as it actually is an object
where you can add stuff to.
To give it a little more functionality, I've added support for multiple arguments so you can test for multiple values in one
call.
I've added support for multiple arguments so we can test for multiple values in one call. For save variable
instantiation/usage or save function calls I added forceType, ideal for testing a value and setting it (or a replacement)
to a variable in a definite type, in one statement.
___
For use with node.js you can install with `npm install types.js`
___

@@ -28,2 +32,4 @@ Basic usage:

**forceString** Forces a value to be of a given type, and returns that value, a replacement, or it's literal default.
**isString** and **notString** (and the other is and not-types) are useful for single argument type checking.

@@ -39,19 +45,39 @@

**some examples:**
```javascript
var _= Types; // browser
var _= require( './types.min.js' ); // node.js
var _= Types; // browser
var _= require( 'types.js' ); // in node.js with npm
// comments reflect the result
_.isString( 'Hello types.js!' ); // true
_.isString( 23456 ); // false
_.isBoolean( false ); // true
_.isArray( [1,2,3] ); // true
_.isObject( [1,2,3] ); // false
_.isObject( /myRegExp/g ); // false
_.isNaN( parseInt('generate NaN') ); // true
_.forceString(); // (empty String)
_.forceString( null, 'ok' ); // ok (as String)
_.forceString( null, [1, 2, 3] ); // (empty String (== default literal) )
_.forceString(33); // 33 (as String)
_.forceNumber('35px'); // 35 (as Number)
_.forceNumber( true, function(){} ); // 0 (as Number)
_.forceBoolean('35px'); // false (as Boolean)
_.notNull(''); // true
_.notUndefined( undefined ); // false
MyObject= function( data ){
this.data= _.forceString( data ); // (empty string if data is not of type string or number)
// or
this.data= _.forceString( data, 'default init'); // default init
}
var invalidMethod= null;
_.forceFunction( invalidMethod, function(){
return 'replacement function used.';
})(); // replacement function used
_.forceFunction( invalidMethod, /regexp/ )(); // (default empty/nop function is called)
_.isString( 'Hello types.js!' ); // true
_.isString( 23456 ); // false
_.isBoolean( false ); // true
_.isArray( [1,2,3] ); // true
_.isObject( [1,2,3] ); // false
_.isObject( /myRegExp/g ); // false
_.isNaN( parseInt('generate NaN') ); // true
_.notNull(''); // true
_.notUndefined( undefined ); // false
_.allString( '', " ", 'with text' ); // true

@@ -80,2 +106,10 @@ _.allString( '', ' ', 'with text', 123 ); // false

**Types.forceBoolean**
> `<String> Types.forceBoolean( value, replacement )`
> Returns value if value is of type Boolean. Otherwise it will try to convert value to be a Boolean. If that
> fails too, replacement will be tested for, or converted to, 'boolean' if possible. If that fails, the default
> types.js boolean literal is returned: a Boolean `false`
**Types.typeof**

@@ -110,3 +144,2 @@ > `<String> Types.typeof( value )`

not |is |has |all

@@ -132,1 +165,7 @@ :-----------------|:----------------|:----------------|:-----------------

____________________________
**types.js default literals**
Boolean `false` | String `''` | Number `0` | Object `{}` | Array `[]` | Function `function(){}`
----------------|-------------|------------|-------------|------------|----------------
// Generated by CoffeeScript 1.8.0
(function() {
"use strict";
var Types, breakIfEqual, testValues, typesPredicates;
var Types, breakIfEqual, createForce, literals, testValues, typesPredicates;
Types = {};
Types = {
parseIntBase: 10
};
literals = {
'Boolean': false,
'String': '',
'Number': 0,
'Object': {},
'Array': [],
'Function': function() {}
};
createForce = function(type) {
var convertType;
convertType = function(value) {
switch (type) {
case 'Number':
if (Types.notNaN(value = parseInt(value, Types.parseIntBase))) {
return value;
}
break;
case 'String':
if (Types.isStringOrNumber(value)) {
return value + '';
}
break;
default:
if (Types["typeof"](value) === type.toLowerCase()) {
return value;
}
}
return false;
};
return function(value, replacement) {
if (replacement == null) {
replacement = value;
}
if (Types['is' + type](value)) {
return value;
}
if (false !== (replacement = convertType(replacement))) {
return replacement;
}
return literals[type];
};
};
testValues = function(predicate, breakState, values) {

@@ -38,2 +84,5 @@ var value, _i, _len;

},
'StringOrNumber': function(value) {
return typesPredicates['String'](value) || typesPredicates['Number'](value);
},
'Function': function(value) {

@@ -62,6 +111,2 @@ return typeof value === 'function';

typesPredicates.StringOrNumber = function(value) {
return typesPredicates['String'](value) || typesPredicates['Number'](value);
};
breakIfEqual = true;

@@ -82,5 +127,8 @@

};
return Types['all' + name] = function() {
Types['all' + name] = function() {
return testValues(predicate, !breakIfEqual, arguments);
};
if (name in literals) {
return Types['force' + name] = createForce(name);
}
})(name, predicate));

@@ -87,0 +135,0 @@ }

@@ -1,1 +0,1 @@

(function(){"use strict";var n,t,r,e;n={};r=function(n,t,r){var e,u,o;if(r==null){r=[]}if(r.length<1){return false}for(u=0,o=r.length;u<o;u++){e=r[u];if(n(e)===t){return t}}return!t};e={Undefined:function(n){return n===void 0},Null:function(n){return n===null},Boolean:function(n){return typeof n==="boolean"},String:function(n){return typeof n==="string"},Function:function(n){return typeof n==="function"},Number:function(n){return typeof n==="number"&&n===n},Array:function(n){return typeof n==="object"&&n instanceof Array},RegExp:function(n){return n instanceof RegExp},Date:function(n){return n instanceof Date},Object:function(n){return typeof n==="object"&&!(n instanceof Array)&&!(n instanceof RegExp)&&!(n===null)},NaN:function(n){return typeof n==="number"&&n!==n}};e.StringOrNumber=function(n){return e["String"](n)||e["Number"](n)};t=true;(function(){var u,o,i;i=[];for(u in e){o=e[u];i.push(function(e,u){n["is"+e]=u;n["not"+e]=function(n){return!u(n)};n["has"+e]=function(){return r(u,t,arguments)};return n["all"+e]=function(){return r(u,!t,arguments)}}(u,o))}return i})();n["typeof"]=function(n){var t,r;for(r in e){t=e[r];if(t(n)===true){return r.toLowerCase()}}return"unknown"};if(typeof window!=="undefined"&&window!==null){window.Types=n}else if(module){module.exports=n}}).call(this);
(function(){"use strict";var n,r,t,e,u,o;n={parseIntBase:10};e={Boolean:false,String:"",Number:0,Object:{},Array:[],Function:function(){}};t=function(r){var t;t=function(t){switch(r){case"Number":if(n.notNaN(t=parseInt(t,n.parseIntBase))){return t}break;case"String":if(n.isStringOrNumber(t)){return t+""}break;default:if(n["typeof"](t)===r.toLowerCase()){return t}}return false};return function(u,o){if(o==null){o=u}if(n["is"+r](u)){return u}if(false!==(o=t(o))){return o}return e[r]}};u=function(n,r,t){var e,u,o;if(t==null){t=[]}if(t.length<1){return false}for(u=0,o=t.length;u<o;u++){e=t[u];if(n(e)===r){return r}}return!r};o={Undefined:function(n){return n===void 0},Null:function(n){return n===null},Boolean:function(n){return typeof n==="boolean"},String:function(n){return typeof n==="string"},StringOrNumber:function(n){return o["String"](n)||o["Number"](n)},Function:function(n){return typeof n==="function"},Number:function(n){return typeof n==="number"&&n===n},Array:function(n){return typeof n==="object"&&n instanceof Array},RegExp:function(n){return n instanceof RegExp},Date:function(n){return n instanceof Date},Object:function(n){return typeof n==="object"&&!(n instanceof Array)&&!(n instanceof RegExp)&&!(n===null)},NaN:function(n){return typeof n==="number"&&n!==n}};r=true;(function(){var i,f,a;a=[];for(i in o){f=o[i];a.push(function(o,i){n["is"+o]=i;n["not"+o]=function(n){return!i(n)};n["has"+o]=function(){return u(i,r,arguments)};n["all"+o]=function(){return u(i,!r,arguments)};if(o in e){return n["force"+o]=t(o)}}(i,f))}return a})();n["typeof"]=function(n){var r,t;for(t in o){r=o[t];if(r(n)===true){return t.toLowerCase()}}return"unknown"};if(typeof window!=="undefined"&&window!==null){window.Types=n}else if(module){module.exports=n}}).call(this);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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