Comparing version 1.3.8 to 1.3.9
{ | ||
"name": "types.js", | ||
"version": "1.3.8", | ||
"version": "1.3.9", | ||
"description": "A tiny (1.8kb), but essential Javascript type-check library", | ||
@@ -5,0 +5,0 @@ "main": "types.min.js", |
198
README.md
@@ -10,3 +10,3 @@ types.js | ||
A few quick examples with types.js: | ||
**A few quick examples with types.js:** | ||
```javascript | ||
@@ -47,25 +47,2 @@ _.typeof( [] ); // 'array' | ||
Check it out, it's sweet! I've added force to types.js because I use it all the time and it seems to belong in here. | ||
____________________________________________________________ | ||
**.void**<br/> | ||
Make a numberFilter for arguments with the new forceNumber: | ||
```javascript | ||
function numberFilter(){ | ||
var numbers= []; | ||
for( var arg in arguments ){ | ||
var value= _.forceNumber( arguments[arg] ); | ||
if( value.void ) | ||
continue; | ||
numbers.push( value ); | ||
} | ||
return numbers; | ||
} | ||
function someFunc(){ | ||
return numberFilter.apply( this, arguments ); | ||
} | ||
console.log( someFunc('ignore', 1, 'the', 2, 'strings!', 3) ); | ||
// [ 1, 2, 3 ] | ||
console.log( someFunc('1 but', '2 not', '3 unconditional!') ); | ||
// [ 1, 2, 3 ] | ||
``` | ||
___ | ||
@@ -82,5 +59,5 @@ For use with node.js you can install with `npm install types.js` | ||
**all'Type'** are useful for checking if all given arguments are of a certain type. | ||
**all'Type'** is useful for checking if all given arguments are of a certain type. | ||
**has'Type'** are useful for checking if one or more arguments are of a certain type. | ||
**has'Type'** is useful for checking if one or more arguments are of a certain type. | ||
@@ -97,30 +74,18 @@ **typeof** Returns a lowercase string representation of the type of the argument value, according to types.js type-definitions. | ||
// initialize a variable and be sure what type it will have in any case: | ||
x= _.forceString(); // (empty String) | ||
x= _.forceString( null, 'ok' ); // ok (as String) | ||
x= _.forceString( null, [1, 2, 3] ); // (empty String) | ||
x= _.forceString(33); // 33 (as String) | ||
x= _.forceString(); // '' (empty String) | ||
x= _.forceString( null, 'ok' ); // 'ok' (as String) | ||
x= _.forceString( null, [1, 2, 3] ); // '' (empty String) | ||
x= _.forceString(33); // '33' (as String) | ||
x= _.forceNumber('35px'); // 35 (as Number) | ||
x= _.forceNumber( true, function(){} ); // 0 (as Number) | ||
x= _.forceBoolean('35px'); // false (as Boolean) | ||
x= _.forceArray("you'll get an array"); // [] | ||
// initialize your object: | ||
var Client= function( name ){ | ||
this.name= _.forceString( name, 'no name given yet' ); | ||
}; | ||
var unborn= new Client(); | ||
console.log( unborn.name ); // 'no name given yet' | ||
var func= null; | ||
// call a function that might not exist anymore: | ||
_.forceFunction( func )( 'some arguments', 'for func' ); | ||
_.forceFunction( func )( 'arguments for func, or replacement' ); | ||
// no crash, default empty function is called, returns undefined | ||
// or add a replacement function in case the first one fails: | ||
var message= _.forceFunction( func, function(args){ | ||
return 'replacement function used '+ args; | ||
})( 'with some arguments' ); | ||
console.log( message ); | ||
// replacement function used with some arguments | ||
// some default type checking: | ||
x= _.isDefined() // false | ||
x= _.isString( 'Hello types.js!' ); // true | ||
@@ -166,63 +131,128 @@ x= _.isString( 23456 ); // false | ||
> Holds the Radix used by forceNumber, defaults to decimals. Can be set to valid radixes for parseInt(). | ||
Holds the Radix used by forceNumber, defaults to decimals. Can be set to valid radixes for parseInt(). Note that once set, all | ||
following forceNumber calls will use the new Radix. | ||
```javascript | ||
_.parseIntBase= 0xf; | ||
// parse from hexadecimal | ||
var nr= _.forceNumber( 'a linefeed' ); | ||
console.log( nr ); | ||
// 10 (decimal) | ||
``` | ||
**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` | ||
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` | ||
```javascript | ||
var assert= _.forceBoolean( 'Only a true true returns true' ); | ||
console.log( assert ); | ||
var assert= _.forceBoolean( NaN != NaN ); | ||
console.log( assert ); | ||
// true | ||
``` | ||
**Types.forceString**, **Types.forceArray**, **Types.forceObject** | ||
> Just like forceBoolean, only applying the type denoted by the method name. See the force'Type' literals for | ||
> the different methods below. | ||
Just like forceBoolean, only applying the type denoted by the method name. See the force'Type' literals for | ||
the different methods below. | ||
**Types.forceNumber** | ||
> `<Number> forceNumber( <String>/<Number> value, <String>/<Number> replacement )` | ||
> `<Number> forceNumber( <String>/<Number> value, <String>/<Number> replacement )` | ||
> Returns value if it is a Number or convertable to a Number. Returns replacement if value is invalid or not convertable. | ||
> Returns a Number object with a .void property set to true if no valid value and replacement were given or no conversion was possible. | ||
Returns value if it is a Number or convertable to a Number. Returns replacement if value is invalid or not convertable. | ||
Returns a Number object with a .void property set to true if no valid value and replacement were given or no conversion was possible. | ||
> You can check yourNumber.void to see if yourNumber is set to a valid number. If .void is true, yourNumber is not set to a | ||
> number, but to a Number object which is ready for mathemetical operation, and defaults to 0. | ||
You can check yourNumber.void to see if yourNumber is set to a valid number. If .void is true, yourNumber is not set to a | ||
number, but to a Number object which is ready for mathemetical operation, and defaults to 0. | ||
> `Types.typeof( Types.forceNumber() );` returns 'number', as it is a Number and you can use it as number. | ||
`Types.typeof( Types.forceNumber() );` returns 'number', as it is a Number and you can use it as number. | ||
Example: make a numberFilter for arguments with the new forceNumber: | ||
```javascript | ||
function numberFilter(){ | ||
var numbers= []; | ||
for( var arg in arguments ){ | ||
var value= _.forceNumber( arguments[arg] ); | ||
if( value.void ) | ||
continue; | ||
numbers.push( value ); | ||
} | ||
return numbers; | ||
} | ||
function someFunc(){ | ||
return numberFilter.apply( this, arguments ); | ||
} | ||
console.log( someFunc('ignore', 1, 'the', 2, 'strings!', 3) ); | ||
// [ 1, 2, 3 ] | ||
console.log( someFunc('1 but', '2 not', '3 unconditional!') ); | ||
// [ 1, 2, 3 ] | ||
``` | ||
**Types.forceFunction** | ||
> `<Function> Types.forceFunction( <Function> func, <Function> replacement )` | ||
> Returns func if it is a Function. So you can call your function with Types.forceFunction(func)( args ). If it is | ||
> a Function, it will call and pass the given arguments. | ||
Returns func if it is a Function. So you can call your function with Types.forceFunction(func)( args ). If it is | ||
a Function, it will call and pass the given arguments. | ||
> forceFunction will not try/catch func for other failures. | ||
forceFunction will not try/catch func for other failures. | ||
> If func or replacement are not of type Function, a dummy function will be called returning undefined. | ||
If func or replacement are not of type Function, a dummy function will be called returning undefined. | ||
```javascript | ||
var showAuthor= function( name ){ | ||
console.log( 'Author: '+ _.forceString(name) ); | ||
}; | ||
_.forceFunction( showAuthor )( 'Dennis Raymondo' ); | ||
// Author: Dennis Raymondo | ||
var showAuthor= null; | ||
// now call with an anonymus replacement function as showAuthor will fail: | ||
_.forceFunction( showAuthor, function(name){ | ||
console.log( 'Could not call showAuthor! Arguments: '+ _.forceString(name) ); | ||
})( 'Dennis Raymondo' ); | ||
// Could not call showAuthor! Arguments: Dennis Raymondo | ||
``` | ||
**Types.typeof** | ||
> `<String> Types.typeof( value )` | ||
> Returns a lowercase string representation of the type of value, according to types.js types. See all types.js | ||
> type-definitions below. | ||
Returns a lowercase string representation of the type of value, according to types.js types. See all types.js | ||
type-definitions below. | ||
```javascript | ||
var nan= parseInt( 'damn NaN!' ); | ||
console.log( _.typeof(nan) ); | ||
// 'nan' | ||
``` | ||
**Types.isBoolean** | ||
> `<Boolean> Types.isBoolean( value )` | ||
> Returns true if the given argument is a Boolean true or false | ||
Returns true if the given argument is a Boolean true or false | ||
```javascript | ||
console.log( _.isBoolean(false) ); | ||
// true | ||
``` | ||
**Types.notBoolean** | ||
> `<Boolean> Types.isBoolean( value )` | ||
> Returns true if the given argument is not a Boolean true or false | ||
Returns true if the given argument is not a Boolean true or false | ||
```javascript | ||
console.log( _.notBoolean('not a Boolean') ); | ||
// true | ||
``` | ||
**Types.hasBoolean** | ||
> `<Boolean> Types.hasBoolean( values, [value1, ..., valueN])` | ||
> Returns true if any of the given arguments is a Boolean true or false | ||
Returns true if any of the given arguments is a Boolean true or false | ||
```javascript | ||
console.log( _.hasBoolean('the third', null, false) ); | ||
// true | ||
``` | ||
**Types.allBoolean** | ||
> `<Boolean> Types.allBoolean( values, [value1, ..., valueN])` | ||
> Returns true only if all given arguments are either a Boolean true or false | ||
Returns true only if all given arguments are either a Boolean true or false | ||
```javascript | ||
console.log( _.allBoolean(false, null, true) ); | ||
// false | ||
``` | ||
All remaining methods are equal to the last four above, except for that they differ in the type being checked. The complete | ||
@@ -250,14 +280,13 @@ list of all these methods: | ||
'boolean', 'string', 'number', 'object', 'array', 'function', 'regexp', 'date', 'null', 'undefined', 'nan', 'unknown' | ||
'boolean', 'string', 'number', 'object', 'array', 'function', 'regexp', 'date', 'null', 'undefined', 'nan' | ||
____________________________ | ||
**force'Type' method and default literals** | ||
> `<'Type'> force'Type'( <any type> value, <'Type'> replacement )` | ||
> format: `<'Type'> force'Type'( <any type> value, <'Type'> replacement )` | ||
<br/><br/> | ||
> The literals returned by default: | ||
The literals returned by default: | ||
forceBoolean |forceString |forceNumber |forceObject |forceArray |forceFunction | ||
---------------|--------------|--------------|---------------|--------------|-------------- | ||
`false` |`''` |`0` |`{}` |`[]` |`function(){}` | ||
---------------|--------------|--------------|--------------|--------------|-------------- | ||
`false` |`''` |`0` (Number) |`{}` |`[]` |`function(){}` | ||
@@ -267,2 +296,11 @@ change log | ||
1.3.9 | ||
Removed 'unknown' from types.js type definitions. It was meant to be like a final state, for if no other matching type could | ||
be found, but in the codebase as it is now, that state can never be reached.. If Javascript ever invents a brand new type, | ||
types.js will return 'defined' on that one if I would not take action and implement support for it. | ||
Updated the readme. | ||
__________________________________________ | ||
1.3.5 | ||
@@ -269,0 +307,0 @@ |
@@ -155,3 +155,2 @@ // Generated by CoffeeScript 1.8.0 | ||
} | ||
return 'unknown'; | ||
}; | ||
@@ -158,0 +157,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(){"use strict";var n,t,r,e,u,o,i;e=function(){var n;return n=new Number,n["void"]=!0,n},n={parseIntBase:10},u={Boolean:!1,String:"",Number:e(),Object:{},Array:[],Function:function(){}},r=function(t){var r;return r=function(r){switch(t){case"Number":if(n.isNumber(r=parseInt(r,n.parseIntBase)))return r;break;case"String":if(n.isStringOrNumber(r))return r+"";break;default:if(n["is"+t](r))return r}return!1},function(n,e){return!1!==(n=r(n))?n:!1!==(e=r(e))?e:u[t]}},o=function(n,t,r){var e,u,o;if(null==r&&(r=[]),r.length<1)return n===i.Undefined?!0:!1;for(u=0,o=r.length;o>u;u++)if(e=r[u],n(e)===t)return t;return!t},i={Undefined:function(n){return void 0===n},Null:function(n){return null===n},Boolean:function(n){return"boolean"==typeof n},String:function(n){return"string"==typeof n},Function:function(n){return"function"==typeof n},Number:function(n){return"number"==typeof n&&n===n||"object"==typeof n&&n instanceof Number&&n["void"]},Array:function(n){return"object"==typeof n&&n instanceof Array},RegExp:function(n){return"object"==typeof n&&n instanceof RegExp},Date:function(n){return"object"==typeof n&&n instanceof Date},Object:function(n){return!("object"!=typeof n||null===n||n instanceof Array||n instanceof RegExp||n instanceof Date)},NaN:function(n){return"number"==typeof n&&n!==n},Defined:function(n){return void 0!==n}},i.StringOrNumber=function(n){return i.String(n)||i.Number(n)},t=!0,function(){var e,f,c;c=[];for(e in i)f=i[e],c.push(function(e,i){return n["is"+e]=i,n["not"+e]=function(n){return!i(n)},n["has"+e]=function(){return o(i,t,arguments)},n["all"+e]=function(){return o(i,!t,arguments)},e in u?n["force"+e]=r(e):void 0}(e,f));return c}(),n["typeof"]=function(n){var t,r;for(r in i)if(t=i[r],t(n)===!0)return r.toLowerCase();return"unknown"},"undefined"!=typeof window&&null!==window?window.Types=n:module&&(module.exports=n)}).call(this); | ||
(function(){"use strict";var n,t,e,r,u,o,i;r=function(){var n;return n=new Number,n["void"]=!0,n},n={parseIntBase:10},u={Boolean:!1,String:"",Number:r(),Object:{},Array:[],Function:function(){}},e=function(t){var e;return e=function(e){switch(t){case"Number":if(n.isNumber(e=parseInt(e,n.parseIntBase)))return e;break;case"String":if(n.isStringOrNumber(e))return e+"";break;default:if(n["is"+t](e))return e}return!1},function(n,r){return!1!==(n=e(n))?n:!1!==(r=e(r))?r:u[t]}},o=function(n,t,e){var r,u,o;if(null==e&&(e=[]),e.length<1)return n===i.Undefined?!0:!1;for(u=0,o=e.length;o>u;u++)if(r=e[u],n(r)===t)return t;return!t},i={Undefined:function(n){return void 0===n},Null:function(n){return null===n},Boolean:function(n){return"boolean"==typeof n},String:function(n){return"string"==typeof n},Function:function(n){return"function"==typeof n},Number:function(n){return"number"==typeof n&&n===n||"object"==typeof n&&n instanceof Number&&n["void"]},Array:function(n){return"object"==typeof n&&n instanceof Array},RegExp:function(n){return"object"==typeof n&&n instanceof RegExp},Date:function(n){return"object"==typeof n&&n instanceof Date},Object:function(n){return!("object"!=typeof n||null===n||n instanceof Array||n instanceof RegExp||n instanceof Date)},NaN:function(n){return"number"==typeof n&&n!==n},Defined:function(n){return void 0!==n}},i.StringOrNumber=function(n){return i.String(n)||i.Number(n)},t=!0,function(){var r,f,c;c=[];for(r in i)f=i[r],c.push(function(r,i){return n["is"+r]=i,n["not"+r]=function(n){return!i(n)},n["has"+r]=function(){return o(i,t,arguments)},n["all"+r]=function(){return o(i,!t,arguments)},r in u?n["force"+r]=e(r):void 0}(r,f));return c}(),n["typeof"]=function(n){var t,e;for(e in i)if(t=i[e],t(n)===!0)return e.toLowerCase()},"undefined"!=typeof window&&null!==window?window.Types=n:module&&(module.exports=n)}).call(this); |
Sorry, the diff of this file is not supported yet
149674
345
2015