Comparing version 1.4.5 to 1.4.6
{ | ||
"name": "types.js", | ||
"version": "1.4.5", | ||
"version": "1.4.6", | ||
"description": "A tiny (1.9kb), essential Javascript type-check library", | ||
@@ -26,7 +26,3 @@ "main": "types.min.js", | ||
], | ||
"author": { | ||
"name": "Dennis Raymondo", | ||
"email": "phazelift@gmail.com", | ||
"url": "https://github.com/phazelift/" | ||
}, | ||
"author": "Dennis Raymondo <phazelift@gmail.com> (https://github.com/phazelift/)", | ||
"license": "GPL-3", | ||
@@ -38,9 +34,3 @@ "bugs": { | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"readme": "types.js\n========\n<br/>\nA tiny(1.9Kb), essential Javascript type checking library.\n\n- fixes NaN, array, null, etc..\n- checks one or multiple arguments at once\n- 4 convenience forms: isNumber, notNumber, hasNumber and allNumber (with any type of choice)\n- can force a value to be of some type, with optional value if conversion is not possible\n\n___\n**A few quick examples:**\n```javascript\n_.typeof( [] );\t\t\t\t\t\t\t\t// 'array'\n_.typeof( null );\t\t\t\t\t\t\t// 'null'\n_.typeof( /someregexp/ );\t\t\t\t\t// 'regexp'\n_.typeof( parseInt('Not A Number!') );\t\t// 'nan'\n_.forceString( 123 );\t\t\t\t\t\t// '123'\n_.forceNumber( '123mm' );\t\t\t\t\t// 123\n_.forceNumber( 'use next arg..', 123 );\t\t// 123\n_.allDefined( 'good', false, 0 );\t\t\t// true\n_.hasObject( 'not', 'really' );\t\t\t\t// false\n// there is much more, see below.\n```\nForce!\n------\nForce some value to be of some type. A replacement value can be given in case value is invalid, without replacement\na literal of that type is returned (except for Number).\n\nA quick example to show how we can safely call a function that needs to pass a number argument, first in standard JS, next with\ntypes.js force methods:\n```javascript\nvar left= '500px';\nvar callback= null;\nif ( typeof left !== 'number' ){\n \t\tleft= parseInt( left, 10 );\n }\n// check for parseInt returning NaN..\nif ( left !== left || typeof left !== 'number' ){\n\tleft= 100;\n}\n// be safe before calling the function\nif ( typeof callback !== 'function' ){\n\tcallback= function(){}\n}\ncallback( left );\n\n// now with force, exactly the same result:\nleft= _.forceNumber( left, 100 );\n_.forceFunction( callback )( left );\n// see below for more examples\n```\nCheck 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.\n___\n**node.js**\nFor use with node.js you can install with `npm install types.js`\n\n**AMD**\nWhen using AMD, you can load types.js like so:\n```javascript\nrequire.config({\n\tpaths: {\n\t\t'types', [ 'path/to/types.min' ]\n\t}\n});\n\nrequire( ['types'], function( Types ){\n\tconsole.log( Types.isNumber(0) );\n\t// true\n});\n```\n___\n\nBasic usage:\n------------\n\n**force'Type'** Forces a value to be of a given type, and returns that value, a given replacement, or a literal for that Type\n\n**is'Type'** and **not'Type'** are useful for single argument type checking.\n\n**all'Type'** is useful for checking if all given arguments are of a certain type.\n\n**has'Type'** is useful for checking if one or more arguments are of a certain type.\n\n**typeof** Returns a lowercase string representation of the type of the argument value, according to types.js type-definitions.\n___\n\n**some more examples:**\n```javascript\nvar _= Types;\t\t\t\t\t\t\t\t\t// browser\nvar _= require( 'types.js' );\t\t\t\t\t// in node.js with npm\n\nvar x;\n// initialize a variable and be sure what type it will have in any case:\n_.forceString();\t\t\t\t\t\t\t\t// '' (empty String)\n_.forceString( null, 'ok' );\t\t\t\t\t// 'ok' (as String)\n_.forceString( null, [1, 2, 3] );\t\t\t\t// '' (empty String)\n_.forceString(33, 'not used');\t\t\t\t\t// '33' (as String)\n_.forceNumber('35px');\t\t\t\t\t\t\t// 35 (as Number)\n_.forceNumber( true, 0 );\t\t\t\t\t\t// 0 (as Number)\n_.forceBoolean('35px');\t\t\t\t\t\t\t// false (as Boolean)\n_.forceArray(\"you'll get an array!\");\t\t\t// []\n\nvar func= null;\n// call a function that might not exist anymore:\n_.forceFunction( func )( 'arguments for func, or replacement' );\n// no crash, default empty function is called, returns undefined\n\n// some default type checking:\n_.isDefined()\t\t\t\t\t\t\t\t\t// false\n_.isString( 'Hello types.js!' );\t\t\t\t// true\n_.isString( 23456 );\t\t\t\t\t\t\t\t// false\n_.isBoolean( false );\t\t\t\t\t\t\t// true\n_.isArray( [1,2,3] );\t\t\t\t\t\t\t// true\n_.isObject( [1,2,3] );\t\t\t\t\t\t\t// false\n_.isObject( /myRegExp/g );\t\t\t\t\t\t// false\n_.isNaN( parseInt('generate NaN') );\t\t\t// true\n\n_.notNull('');\t\t\t\t\t\t\t\t\t// true\n_.notUndefined( undefined );\t\t\t\t\t// false\n_.isDefined( null );\t\t\t\t\t\t\t// true\n\n// check multiple values in one call:\n_.allString( '', \" \", 'with text' );\t\t\t\t\t// true\n_.allString( '', ' ', 'with text', 123 );\t\t\t\t// false\n_.allStringOrNumber( '', ' ', 'with text', 123 );\t\t// true\n_.allObject( { key: 'nice' }, [], /regexp/ig );\t\t\t// false\n_.allArray( [1,2,3], [{}], new RegExp('stop') );\t\t// false\n_.allArray( [1,2,3], [{}], [false, true] );\t\t\t\t// true\n\n_.hasString( 123, { value: 'nice' }, ['?'] );\t\t\t// false\n_.hasStringOrNumber( [1,2], /reg/, 'true' )\t\t\t\t// true\n_.hasFunction( 123, { value: 'nice' }, function(){} );\t// true\n_.hasUndefined( 'render false!', 123, null );\t\t\t// false\n_.hasUndefined( 'render true!', 123, undefined );\t\t// true\n\n// check for a types.js type definition, returns lowercase string:\n_.typeof( [1,2,3] );\t\t\t\t\t\t\t\t\t// 'array'\n_.typeof( null );\t\t\t\t\t\t\t\t\t\t// 'null'\n_.typeof( parseInt('generate NaN') );\t\t\t\t\t// 'nan'\n_.typeof( new Date() );\t\t\t\t\t\t\t\t\t// 'date'\n// etc..\n```\n___\nAPI\n---\n\n**Types.parseIntBase**\n> `<Number> parseIntBase= 10`\n\nHolds the Radix used by forceNumber, defaults to decimals. Can be set to valid radixes for parseInt(). Note that once set, all\nfollowing forceNumber calls will use the new Radix.\n```javascript\n_.parseIntBase= 0xf;\n// parse from hexadecimal, _.forceNumber will parse character 'a'\nvar nr= _.forceNumber( 'a linefeed' );\nconsole.log( nr );\n// 10 (decimal)\n```\n\n**Types.force'Type'**\n> `<'Type'> force'Type'( <any type> value, <'Type'> replacement )`\n\nForce the return value to be of a particular type. A replacement value can be given for in case value is invalid, if also no replacement\nis given, a literal of the type will be returned, with an exception to Number(see below).\n\nAll force'Types' share the same methodology, only applying the type denoted by the method name. See the following examples\nfor it's workings.\n\n**Types.forceBoolean**\n> `<String> Types.forceBoolean( value, replacement )`\n\n> Returns value if value is a types.js boolean. Otherwise it will try to convert value to be true or false. If that fails too,\n> replacement will be tested for, or converted to boolean if possible. If that fails, the default types.js boolean literal\n> is returned: a Boolean false\n\n```javascript\nvar assert= _.forceBoolean( 'Only a true true returns true' );\nconsole.log( assert );\n// false\nvar assert= _.forceBoolean( NaN != NaN );\nconsole.log( assert );\n// true\n```\n\n**Types.forceString**, **Types.forceArray**, **Types.forceObject**\n\n> Same as .forceBoolean, except for the type being processed.\n\n\n**Types.forceNumber**\n > `<Number> forceNumber( <String>/<Number> value, <String>/<Number> replacement )`\n\nReturns value if it is a types.js number or convertable to a number. Returns replacement if value is invalid or not convertable.\nReturns a native Number object with a .void property set to true if no valid value and replacement were given and conversion\nwas not possible.\n\nYou can check value.void to see if value is ready to be fetched. If value.void is true, you can only fetch it's\ndefault value by doing some mathematical operation on it like: `value+ 0`, otherwise you'll get an object. After\nany mathematical operation on value, value.void will be undefined and value has become a 'real' number.\nIf value.void is true, the default value returned with `value+ 0` is 0.\n\nIf you want to be sure that forceNumber returns a 'real' number, then simply supply a 'real' number replacement,\nlike: `var nr= _.forceNumber(nr, 0);`, and it will never return the Number-object form.\n\n`Types.typeof( nr= Types.forceNumber() );` returns 'number', also if `nr.void === true`\n\nExample: make a numberFilter for arguments with forceNumber:\n```javascript\nfunction numberFilter(){\n\tvar numbers= [];\n\tfor( var arg in arguments ){\n\t\tvar value= _.forceNumber( arguments[arg] );\n\t\tif( value.void )\n\t\t\tcontinue;\n\t\tnumbers.push( value );\n\t}\n\treturn numbers;\n}\n\nfunction forceArgsToNumber(){\n\treturn numberFilter.apply( this, arguments );\n}\nconsole.log( forceArgsToNumber('ignore', 1, 'the', 2, 'strings!', 3) );\n// [ 1, 2, 3 ]\nconsole.log( forceArgsToNumber('1 but', '2 not', '3 unconditional!') );\n// [ 1, 2, 3 ]\n```\n\n**Types.forceFunction**\n> `<Function> Types.forceFunction( <Function> func, <Function> replacement )`\n\nReturns func if it is a Function. forceFunction will not try/catch func for other failures.\n\nIf func or replacement are not a Function, a dummy function(){} will be returned. So you can safely call your function with\n`Types.forceFunction(func)( args )`. If it is a Function, it will call func and pass the given arguments.\n```javascript\n// define a working function and a failing one for the examples\nvar showAuthor= function( name ){\n\tconsole.log( 'Author: '+ _.forceString(name) );\n};\nvar brokenFunc= null;\n\nvar func= _.forceFunction( showAuthor );\n// still the same function?\nconsole.log( func === showAuthor );\n// true\n\n// now func will again become equal to showAuthor because forceFunction will\n// return showAuthor as replacement is the only valid function found:\nvar func= _.forceFunction( brokenFunc, showAuthor );\nconsole.log( func === showAuthor );\n// true\n\n// because in the following example no valid functions are passed as arguments,\n// func will become a dummy function, returning undefined.\nvar func= _.forceFunction( brokenFunc, brokenFunc );\n// save to call, but no effect\nfunc();\n\n// as in the example above you can see that because forceFunction always returns\n// a callable function you can safely call in one go like this:\n_.forceFunction( brokenFunc, showAuthor )( 'Dennis' );\n// Author: Dennis\n\n// now we call with two invalid functions:\n_.forceFunction( brokenFunc, brokenFunc )( 'Dennis' );\n//\n// the empty dummy-function was called, no crash\n```\n___\n**Types.typeof**\n> `<String> Types.typeof( value )`\n\nReturns a lowercase string representation of the type of value, according to types.js types. See all types.js\ntype-definitions below.\n```javascript\nvar number= parseInt( 'damn NaN!' );\nconsole.log( _.typeof(number) );\n// 'nan'\n```\n**Types.isBoolean**\n> `<Boolean> Types.isBoolean( value )`\n\nReturns true if the given argument is a Boolean true or false\n```javascript\nconsole.log( _.isBoolean(false) );\n// true\n```\n**Types.notBoolean**\n> `<Boolean> Types.isBoolean( value )`\n\nReturns true if the given argument is not a Boolean true or false\n```javascript\nconsole.log( _.notBoolean('not a Boolean') );\n// true\n```\n**Types.hasBoolean**\n> `<Boolean> Types.hasBoolean( values, [value1, ..., valueN])`\n\nReturns true if any of the given arguments is a Boolean true or false\n```javascript\nconsole.log( _.hasBoolean('the third', null, false) );\n// true\n```\n**Types.allBoolean**\n> `<Boolean> Types.allBoolean( values, [value1, ..., valueN])`\n\nReturns true only if all given arguments are either a Boolean true or false\n```javascript\nconsole.log( _.allBoolean(false, null, true) );\n// false\n```\n**not / is / has / all'Types'**\n\nAll remaining methods are equal to the last four above, except for that they differ in the type being checked. The complete\nlist of all these methods:\n\nnot\t\t\t\t\t|is\t\t\t\t\t|has\t\t\t\t\t|all\n:-----------------|:----------------|:----------------|:-----------------\nnotBoolean\t\t\t|isBoolean\t\t\t|hasBoolean\t\t\t|allBoolean\nnotString\t\t\t|isString\t\t\t|hasString\t\t\t|allString\nnotNumber\t\t\t|isNumber\t\t\t|hasNumber\t\t\t|allNumber\nnotStringOrNumber\t|isStringOrNumber\t|hasStringOrNumber|allStringOrNumber\nnotObject\t\t\t|isObject\t\t\t|hasObject\t\t\t|allObject\nnotArray\t\t\t\t|isArray\t\t\t\t|hasArray\t\t\t|allArray\nnotFunction\t\t\t|isFunction\t\t\t|hasFunction\t\t|allFunction\nnotRegexp\t\t\t|isRegexp\t\t\t|hasRegexp\t\t\t|allRegexp\nnotDate\t\t\t\t|isDate\t\t\t\t|hasDate\t\t\t\t|allDate\nnotNull\t\t\t\t|isNull\t\t\t\t|hasNull\t\t\t\t|allNull\nnotUndefined\t\t|isUndefined\t\t|hasUndefined\t\t|allUndefined\nnotDefined\t\t\t|isDefined\t\t\t|hasDefined\t\t\t|allDefined\nnotNaN\t\t\t\t|isNaN\t\t\t\t|hasNaN\t\t\t\t|allNaN\n\n___\n**types.js type definitions:**\n\nAll types.js types\n\ntype\t\t\t\t| definition\n:--------------|:-----------------\n'undefined'\t\t| Any value that is undefined, or no value at all\n'null'\t\t\t| Any value that is null\n'boolean'\t\t| A boolean true or false, or the result of a predicate\n'string'\t\t\t| A string literal\n'number'\t\t\t| A number literal or a 'void' Number\n'nan'\t\t\t\t| Any value that is NaN\n'object'\t\t\t| An object literal {}, or any instance of Object that is not a native JS object (except for Object)\n'array'\t\t\t| An array literal [], or any instance of Array\n'function'\t\t| Any function or instance of Function\n'regexp'\t\t\t| Any regular expression literal, or any instance of RegExp\n'date'\t\t\t| Any instance of Date\n\n___\n\n**force'Type' default return values**\n\ntype\t\t\t\t| return value\n:--------------|:-----------------\nforceBoolean\t| false\nforceString\t\t| ''\nforceNumber\t\t| a new Number with a .void property set to true\nforceObject\t\t| {}\nforceArray\t\t| []\nforceFunction\t| function(){}\n___\nchange log\n==========\n**1.4.4**\n\nAdded AMD support.\n___\n**1.4.2**\n\nOptimized and reworked the codebase, and some adjustments to tests.\n\nUpdated the readme.\n___\n**1.3.9**\n\nRemoved 'unknown' from types.js type definitions. It was meant to be like a final state, for if no other matching type could\nbe found, but in the codebase as it is now, that state can never be reached.. If Javascript ever invents a brand new type,\ntypes.js will return 'defined' on that one if I would not take action and implement support for it.\n\nUpdated the readme.\n\n___\n**1.3.5**\n\nChanged:\n-\tforceNumber doesn't return 0 by default anymore. It now returns a Number object with a .void property which is set to\n\ttrue if no valid Number was given or no conversion was possible.\n\n\tJust use: `_.forceNumber( value, 0 );` to return a 0 as replacement, it only is not default anymore.\n\n\tI made this change because I wanted to be able to check if forceNumber was successful. Just a 0 can be very misleading and\n\ta source for bugs. NaN is a failure IMO, so I made a kind of replacement feature in forceNumber.\n\n\tYou can now check for yourNumber.void to see if it is set. If .void is true, yourNumber is a Number object which is ready for\n\tmathemetical operation, and defaults to 0, this in contrast with NaN, which is almost totally unusable.\n\n\texample:\n\t```javascript\n\t// generate a void Number:\n\tvar nr= forceNumber();\n\tconsole.log( nr.void );\n\t// true\n\t// don't do the following after a forceNumber without a valid replacement:\n\tconsole.log( nr );\n\t// { void: true }\n\t// instead do what cannot be done with NaN:\n\tconsole.log( 0 + nr );\n\t// 0\n\t// or check before usage:\n\t( nr.void )\n\t\t? console.log( 'void?', nr+= 36/ 4 );\n\t\t: console.log( nr );\n\t// void? 9\n\tetc..\n\t```\n\nUpdated:\n-\tJasmine tests for forceNumber and isDefined\n-\tspeed optimization for isObject\n\n---------------------------------------------------\n**1.3.1**\n\nAdded:\n- change log in the readme, more convenient overview of changes.\n\n- is/not/has/allDefined<br/>\nNow you can: `if (_.isDefined(value) )`<br/>\ninstead of `if (_.notUndefined(value) )`", | ||
"readmeFilename": "README.md", | ||
"gitHead": "a419abc630074f286d381937c7ddab7361df997c", | ||
"_id": "types.js@1.4.42", | ||
"_shasum": "1ab12569a6d74b268e9a263ebf378cca25d9fd7b", | ||
"_from": "types.js@^1.4.4" | ||
"devDependencies": {} | ||
} |
types.js | ||
======== | ||
<br/> | ||
A tiny(1.9Kb), essential Javascript type checking library. | ||
A tiny(1.9Kb) Javascript type checker/enforcer library. | ||
@@ -6,0 +6,0 @@ - fixes NaN, array, null, etc.. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
152705