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.5.2 to 1.5.3

tests/spec/typesTests.js

2

package.json
{
"name": "types.js",
"version": "1.5.2",
"version": "1.5.3",
"description": "A tiny (~2kb) Javascript type checker/enforcer library",

@@ -5,0 +5,0 @@ "main": "types.min.js",

types.js
========
<br/>
A tiny (2.2kB) Javascript type checker/enforcer library.
A tiny (~2kB) Javascript type checker/enforcer library.

@@ -11,16 +11,20 @@ - fixes NaN, array, null, etc..

<br/>
___
**A few quick examples:**
```javascript
_.typeof( [] ); // 'array'
_.typeof( null ); // 'null'
_.typeof( /someregexp/ ); // 'regexp'
_.typeof( parseInt('Not A Number!') ); // 'nan'
_.forceString( 123 ); // '123'
_.forceNumber( '123mm' ); // 123
_.forceNumber( 'use next arg..', 123 ); // 123
_.intoArray( '1 2 3' ); // ['1', '2', '3']
_.intoArray( '1', '2', '3' ); // ['1', '2', '3']
_.allDefined( 'good', false, 0 ); // true
_.hasObject( 'not', 'really' ); // false
var types= require( 'types.js' );
types.typeof( [] ); // 'array'
types.typeof( null ); // 'null'
types.typeof( /someregexp/ ); // 'regexp'
types.typeof( parseInt('Not A Number!') ); // 'nan'
types.forceString( 123 ); // '123'
types.forceNumber( '123mm' ); // 123
types.forceNumber( 'use next arg..', 123 ); // 123
types.intoArray( '1 2 3' ); // ['1', '2', '3']
types.intoArray( '1', '2', '3' ); // ['1', '2', '3']
types.allDefined( 'good', false, 0 ); // true
types.hasObject( 'not', 'really' ); // false
// there is much more, see below.

@@ -55,4 +59,4 @@ ```

// 2 lines with force, exactly the same result:
left= _.forceNumber( left, 100 );
_.forceFunction( callback )( left );
left= types.forceNumber( left, 100 );
types.forceFunction( callback )( left );

@@ -75,3 +79,3 @@ // see below for more examples

require( ['types'], function( _ ){
console.log( _.isNumber(0) );
console.log( types.isNumber(0) );
// true

@@ -100,4 +104,5 @@ });

```javascript
var _= Types; // browser
var _= require( 'types.js' ); // in node.js with npm
var types= Types; // browser
// or:
var types= require( 'types.js' ); // in node.js with npm

@@ -107,13 +112,13 @@ var x;

// initialize a variable and be sure what type it will have in any case:
_.forceString(); // '' (empty String)
_.forceString( null, 'ok' ); // 'ok' (as String)
_.forceString( null, [1, 2, 3] ); // '' (empty String)
_.forceString(33, 'not used'); // '33' (as String)
_.forceNumber('35px'); // 35 (as Number)
_.forceNumber( true, 0 ); // 0 (as Number)
_.forceBoolean('35px'); // false (as Boolean)
_.forceArray("you'll get an array!"); // []
_.intoArray( 'hi', 'there' ); // [ 'hi', 'there' ]
_.intoArray( ' hi there ' ); // [ 'hi', 'there' ]
_.intoArray( '', 0, {}, [] ); // [ '', 1, {}, [] ]
types.forceString(); // '' (empty String)
types.forceString( null, 'ok' ); // 'ok' (as String)
types.forceString( null, [1, 2, 3] ); // '' (empty String)
types.forceString(33, 'not used'); // '33' (as String)
types.forceNumber('35px'); // 35 (as Number)
types.forceNumber( true, 0 ); // 0 (as Number)
types.forceBoolean('35px'); // false (as Boolean)
types.forceArray("you'll get an array!"); // []
types.intoArray( 'hi', 'there' ); // [ 'hi', 'there' ]
types.intoArray( ' hi there ' ); // [ 'hi', 'there' ]
types.intoArray( '', 0, {}, [] ); // [ '', 1, {}, [] ]

@@ -123,38 +128,38 @@ var func= null;

// call a function that might not exist anymore:
_.forceFunction( func )( 'arguments for func' );
types.forceFunction( func )( 'arguments for func' );
// no crash, default empty function is called, returns undefined
// some default type checking:
_.isDefined() // false
_.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
types.isDefined() // false
types.isString( 'Hello types.js!' ); // true
types.isString( 23456 ); // false
types.isBoolean( false ); // true
types.isArray( [1,2,3] ); // true
types.isObject( [1,2,3] ); // false
types.isObject( /myRegExp/g ); // false
types.isNaN( parseInt('generate NaN') ); // true
_.notNull(''); // true
_.notUndefined( undefined ); // false
_.isDefined( null ); // true
types.notNull(''); // true
types.notUndefined( undefined ); // false
types.isDefined( null ); // true
// check multiple values in one call:
_.allString( '', " ", 'with text' ); // true
_.allString( '', ' ', 'with text', 123 ); // false
_.allStringOrNumber( '', ' ', 'with text', 123 ); // true
_.allObject( { key: 'nice' }, [], /regexp/ig ); // false
_.allArray( [1,2,3], [{}], new RegExp('stop') ); // false
_.allArray( [1,2,3], [{}], [false, true] ); // true
types.allString( '', " ", 'with text' ); // true
types.allString( '', ' ', 'with text', 123 ); // false
types.allStringOrNumber( '', ' ', 'with text', 123 ); // true
types.allObject( { key: 'nice' }, [], /regexp/ig ); // false
types.allArray( [1,2,3], [{}], new RegExp('stop') ); // false
types.allArray( [1,2,3], [{}], [false, true] ); // true
_.hasString( 123, { value: 'nice' }, ['?'] ); // false
_.hasStringOrNumber( [1,2], /reg/, 'true' ) // true
_.hasFunction( 123, { value: 'nice' }, function(){} ); // true
_.hasUndefined( 'render false!', 123, null ); // false
_.hasUndefined( 'render true!', 123, undefined ); // true
types.hasString( 123, { value: 'nice' }, ['?'] ); // false
types.hasStringOrNumber( [1,2], /reg/, 'true' ) // true
types.hasFunction( 123, { value: 'nice' }, function(){} ); // true
types.hasUndefined( 'render false!', 123, null ); // false
types.hasUndefined( 'render true!', 123, undefined ); // true
// check for a types.js type definition, returns lowercase string:
_.typeof( [1,2,3] ); // 'array'
_.typeof( null ); // 'null'
_.typeof( parseInt('generate NaN') ); // 'nan'
_.typeof( new Date() ); // 'date'
types.typeof( [1,2,3] ); // 'array'
types.typeof( null ); // 'null'
types.typeof( parseInt('generate NaN') ); // 'nan'
types.typeof( new Date() ); // 'date'

@@ -173,5 +178,5 @@ // etc..

```javascript
_.parseIntBase= 0xf;
// parse from hexadecimal, _.forceNumber will parse character 'a'
var nr= _.forceNumber( 'a linefeed' );
types.parseIntBase= 0xf;
// parse from hexadecimal, types.forceNumber will parse character 'a'
var nr= types.forceNumber( 'a linefeed' );
console.log( nr );

@@ -198,6 +203,6 @@ // 10 (decimal)

```javascript
var assert= _.forceBoolean( 'Only a true true returns true' );
var assert= types.forceBoolean( 'Only a true true returns true' );
console.log( assert );
// false
var assert= _.forceBoolean( NaN != NaN );
var assert= types.forceBoolean( NaN != NaN );
console.log( assert );

@@ -225,3 +230,3 @@ // true

If you want to be sure that forceNumber returns a 'real' number, then simply supply a 'real' number replacement,
like: `var nr= _.forceNumber(nr, 0);`, and it will never return the Number-object form.
like: `var nr= types.forceNumber(nr, 0);`, and it will never return the Number-object form.

@@ -235,3 +240,3 @@ `Types.typeof( nr= Types.forceNumber() );` returns 'number', also if `nr.void === true`

for( var arg in arguments ){
var value= _.forceNumber( arguments[arg] );
var value= types.forceNumber( arguments[arg] );
if( value.void )

@@ -263,7 +268,7 @@ continue;

var showAuthor= function( name ){
console.log( 'Author: '+ _.forceString(name) );
console.log( 'Author: '+ types.forceString(name) );
};
var brokenFunc= null;
var func= _.forceFunction( showAuthor );
var func= types.forceFunction( showAuthor );
// still the same function?

@@ -275,3 +280,3 @@ console.log( func === showAuthor );

// return showAuthor as replacement is the only valid function found:
var func= _.forceFunction( brokenFunc, showAuthor );
var func= types.forceFunction( brokenFunc, showAuthor );
console.log( func === showAuthor );

@@ -282,3 +287,3 @@ // true

// func will become a dummy function, returning undefined.
var func= _.forceFunction( brokenFunc, brokenFunc );
var func= types.forceFunction( brokenFunc, brokenFunc );
// save to call, but no effect

@@ -289,7 +294,7 @@ func();

// a callable function you can safely call in one go like this:
_.forceFunction( brokenFunc, showAuthor )( 'Dennis' );
types.forceFunction( brokenFunc, showAuthor )( 'Dennis' );
// Author: Dennis
// now we call with two invalid functions:
_.forceFunction( brokenFunc, brokenFunc )( 'Dennis' );
types.forceFunction( brokenFunc, brokenFunc )( 'Dennis' );
//

@@ -318,3 +323,3 @@ // the empty dummy-function was called, no crash

// need to .apply with context for all arguments to pass
var array= _.intoArray.apply( this, arguments );
var array= types.intoArray.apply( this, arguments );
console.log( array );

@@ -342,3 +347,3 @@ }

var number= parseInt( 'damn NaN!' );
console.log( _.typeof(number) );
console.log( types.typeof(number) );
// 'nan'

@@ -351,3 +356,3 @@ ```

```javascript
console.log( _.isBoolean(false) );
console.log( types.isBoolean(false) );
// true

@@ -360,3 +365,3 @@ ```

```javascript
console.log( _.notBoolean('not a Boolean') );
console.log( types.notBoolean('not a Boolean') );
// true

@@ -369,3 +374,3 @@ ```

```javascript
console.log( _.hasBoolean('the third', null, false) );
console.log( types.hasBoolean('the third', null, false) );
// true

@@ -378,3 +383,3 @@ ```

```javascript
console.log( _.allBoolean(false, null, true) );
console.log( types.allBoolean(false, null, true) );
// false

@@ -438,2 +443,6 @@ ```

**1.5.1**
Some minor changes to this readme
___
**1.5.0**

@@ -469,3 +478,3 @@

Just use: `_.forceNumber( value, 0 );` to return a 0 as replacement, it only is not default anymore.
Just use: `types.forceNumber( value, 0 );` to return a 0 as replacement, it only is not default anymore.

@@ -509,3 +518,3 @@ I made this change because I wanted to be able to check if forceNumber was successful. Just a 0 can be very misleading and

- is/not/has/allDefined<br/>
Now you can: `if (_.isDefined(value) )`<br/>
instead of `if (_.notUndefined(value) )`
Now you can: `if (types.isDefined(value) )`<br/>
instead of `if (types.notUndefined(value) )`

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

// Generated by CoffeeScript 1.9.1
// Generated by CoffeeScript 1.10.0
(function() {

@@ -3,0 +3,0 @@ "use strict";

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