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

typical

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typical - npm Package Compare versions

Comparing version 2.4.2 to 2.5.0

215

lib/typical.js
'use strict'
/**
For type-checking Javascript values.
@module typical
@typicalname t
@example
var t = require("typical")
*/
* For type-checking Javascript values.
* @module typical
* @typicalname t
* @example
* const t = require('typical')
*/
exports.isNumber = isNumber

@@ -18,26 +18,29 @@ exports.isString = isString

exports.isFunction = isFunction
exports.isClass = isClass
exports.isPrimitive = isPrimitive
exports.isPromise = isPromise
/**
Returns true if input is a number
@param {*} - the input to test
@returns {boolean}
@static
@example
> t.isNumber(0)
true
> t.isNumber(1)
true
> t.isNumber(1.1)
true
> t.isNumber(0xff)
true
> t.isNumber(0644)
true
> t.isNumber(6.2e5)
true
> t.isNumber(NaN)
false
> t.isNumber(Infinity)
false
*/
* Returns true if input is a number
* @param {*} - the input to test
* @returns {boolean}
* @static
* @example
* > t.isNumber(0)
* true
* > t.isNumber(1)
* true
* > t.isNumber(1.1)
* true
* > t.isNumber(0xff)
* true
* > t.isNumber(0644)
* true
* > t.isNumber(6.2e5)
* true
* > t.isNumber(NaN)
* false
* > t.isNumber(Infinity)
* false
*/
function isNumber (n) {

@@ -48,19 +51,19 @@ return !isNaN(parseFloat(n)) && isFinite(n)

/**
A plain object is a simple object literal, it is not an instance of a class. Returns true if the input `typeof` is `object` and directly decends from `Object`.
@param {*} - the input to test
@returns {boolean}
@static
@example
> t.isPlainObject({ clive: "hater" })
true
> t.isPlainObject(new Date())
false
> t.isPlainObject([ 0, 1 ])
false
> t.isPlainObject(1)
false
> t.isPlainObject(/test/)
false
*/
* A plain object is a simple object literal, it is not an instance of a class. Returns true if the input `typeof` is `object` and directly decends from `Object`.
*
* @param {*} - the input to test
* @returns {boolean}
* @static
* @example
* > t.isPlainObject({ clive: 'hater' })
* true
* > t.isPlainObject(new Date())
* false
* > t.isPlainObject([ 0, 1 ])
* false
* > t.isPlainObject(1)
* false
* > t.isPlainObject(/test/)
* false
*/
function isPlainObject (input) {

@@ -71,13 +74,13 @@ return input !== null && typeof input === 'object' && input.constructor === Object

/**
An array-like value has all the properties of an array, but is not an array instance. Examples in the `arguments` object. Returns true if the input value is an object, not null and has a `length` property with a numeric value.
@param {*} - the input to test
@returns {boolean}
@static
@example
function sum(x, y){
console.log(t.isArrayLike(arguments))
// prints `true`
}
*/
* An array-like value has all the properties of an array, but is not an array instance. Examples in the `arguments` object. Returns true if the input value is an object, not null and has a `length` property with a numeric value.
*
* @param {*} - the input to test
* @returns {boolean}
* @static
* @example
* function sum(x, y){
* console.log(t.isArrayLike(arguments))
* // prints `true`
* }
*/
function isArrayLike (input) {

@@ -88,7 +91,7 @@ return isObject(input) && typeof input.length === 'number'

/**
returns true if the typeof input is `"object"`, but not null!
@param {*} - the input to test
@returns {boolean}
@static
*/
* returns true if the typeof input is `'object'`, but not null!
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isObject (input) {

@@ -99,7 +102,7 @@ return typeof input === 'object' && input !== null

/**
Returns true if the input value is defined
@param {*} - the input to test
@returns {boolean}
@static
*/
* Returns true if the input value is defined
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isDefined (input) {

@@ -110,7 +113,7 @@ return typeof input !== 'undefined'

/**
Returns true if the input value is a string
@param {*} - the input to test
@returns {boolean}
@static
*/
* Returns true if the input value is a string
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isString (input) {

@@ -121,7 +124,7 @@ return typeof input === 'string'

/**
Returns true if the input value is a boolean
@param {*} - the input to test
@returns {boolean}
@static
*/
* Returns true if the input value is a boolean
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isBoolean (input) {

@@ -132,9 +135,59 @@ return typeof input === 'boolean'

/**
Returns true if the input value is a function
@param {*} - the input to test
@returns {boolean}
@static
*/
* Returns true if the input value is a function
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isFunction (input) {
return typeof input === 'function'
}
/**
* Returns true if the input value is an es2015 `class`.
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isClass (input) {
if (isFunction(input)) {
return /^class /.test(Function.prototype.toString.call(input))
} else {
return false
}
}
/**
* Returns true if the input is a string, number, symbol, boolean, null or undefined value.
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isPrimitive (input) {
if (input === null) return true
switch (typeof input) {
case "string":
case "number":
case "symbol":
case "undefined":
case "boolean":
return true
default:
return false
}
}
/**
* Returns true if the input is a string, number, symbol, boolean, null or undefined value.
* @param {*} - the input to test
* @returns {boolean}
* @static
*/
function isPromise (input) {
if (input) {
var isPromise = isDefined(Promise) && input instanceof Promise
var isThenable = input.then && typeof input.then === 'function'
return isPromise || isThenable ? true : false
} else {
return false
}
}
{
"name": "typical",
"author": "Lloyd Brookes <75pound@gmail.com>",
"version": "2.4.2",
"version": "2.5.0",
"description": "For type-checking Javascript values.",

@@ -31,5 +31,6 @@ "repository": "https://github.com/75lb/typical.git",

"devDependencies": {
"jsdoc-to-markdown": "^1.3.3",
"tape": "^4.4"
"feature-detect-es6": "^1.3.1",
"jsdoc-to-markdown": "^1.3.6",
"tape": "^4.6.0"
}
}

@@ -8,2 +8,3 @@ [![view on npm](http://img.shields.io/npm/v/typical.svg)](https://www.npmjs.org/package/typical)

<a name="module_typical"></a>
## typical

@@ -14,3 +15,3 @@ For type-checking Javascript values.

```js
var t = require("typical")
const t = require('typical')
```

@@ -27,4 +28,8 @@

* [.isFunction(input)](#module_typical.isFunction) ⇒ <code>boolean</code>
* [.isClass(input)](#module_typical.isClass) ⇒ <code>boolean</code>
* [.isPrimitive(input)](#module_typical.isPrimitive) ⇒ <code>boolean</code>
* [.isPromise(input)](#module_typical.isPromise) ⇒ <code>boolean</code>
<a name="module_typical.isNumber"></a>
### t.isNumber(n) ⇒ <code>boolean</code>

@@ -59,2 +64,3 @@ Returns true if input is a number

<a name="module_typical.isPlainObject"></a>
### t.isPlainObject(input) ⇒ <code>boolean</code>

@@ -71,3 +77,3 @@ A plain object is a simple object literal, it is not an instance of a class. Returns true if the input `typeof` is `object` and directly decends from `Object`.

```js
> t.isPlainObject({ clive: "hater" })
> t.isPlainObject({ clive: 'hater' })
true

@@ -84,2 +90,3 @@ > t.isPlainObject(new Date())

<a name="module_typical.isArrayLike"></a>
### t.isArrayLike(input) ⇒ <code>boolean</code>

@@ -102,4 +109,5 @@ An array-like value has all the properties of an array, but is not an array instance. Examples in the `arguments` object. Returns true if the input value is an object, not null and has a `length` property with a numeric value.

<a name="module_typical.isObject"></a>
### t.isObject(input) ⇒ <code>boolean</code>
returns true if the typeof input is `"object"`, but not null!
returns true if the typeof input is `'object'`, but not null!

@@ -113,2 +121,3 @@ **Kind**: static method of <code>[typical](#module_typical)</code>

<a name="module_typical.isDefined"></a>
### t.isDefined(input) ⇒ <code>boolean</code>

@@ -124,2 +133,3 @@ Returns true if the input value is defined

<a name="module_typical.isString"></a>
### t.isString(input) ⇒ <code>boolean</code>

@@ -135,2 +145,3 @@ Returns true if the input value is a string

<a name="module_typical.isBoolean"></a>
### t.isBoolean(input) ⇒ <code>boolean</code>

@@ -146,2 +157,3 @@ Returns true if the input value is a boolean

<a name="module_typical.isFunction"></a>
### t.isFunction(input) ⇒ <code>boolean</code>

@@ -156,5 +168,38 @@ Returns true if the input value is a function

<a name="module_typical.isClass"></a>
### t.isClass(input) ⇒ <code>boolean</code>
Returns true if the input value is an es2015 `class`.
**Kind**: static method of <code>[typical](#module_typical)</code>
| Param | Type | Description |
| --- | --- | --- |
| input | <code>\*</code> | the input to test |
<a name="module_typical.isPrimitive"></a>
### t.isPrimitive(input) ⇒ <code>boolean</code>
Returns true if the input is a string, number, symbol, boolean, null or undefined value.
**Kind**: static method of <code>[typical](#module_typical)</code>
| Param | Type | Description |
| --- | --- | --- |
| input | <code>\*</code> | the input to test |
<a name="module_typical.isPromise"></a>
### t.isPromise(input) ⇒ <code>boolean</code>
Returns true if the input is a string, number, symbol, boolean, null or undefined value.
**Kind**: static method of <code>[typical](#module_typical)</code>
| Param | Type | Description |
| --- | --- | --- |
| input | <code>\*</code> | the input to test |
* * *
&copy; 2015 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
&copy; 2014-16 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
'use strict'
var test = require('tape')
var type = require('../')
var detect = require('feature-detect-es6')
function evaluates (statement) {
try {
eval(statement)
return true
} catch (err) {
return false
}
}
test('.isNumber(value)', function (t) {

@@ -64,5 +74,73 @@ t.equal(type.isNumber(0), true)

t.equal(type.isFunction(Infinity), false)
t.equal(type.isFunction(function(){}), true)
t.equal(type.isFunction(function () {}), true)
t.equal(type.isFunction(Date), true)
t.end()
})
test('.isPrimitive(value)', function (t) {
t.equal(type.isPrimitive(true), true)
t.equal(type.isPrimitive({}), false)
t.equal(type.isPrimitive(0), true)
t.equal(type.isPrimitive('1'), true)
t.equal(type.isPrimitive(1.1), true)
t.equal(type.isPrimitive(NaN), true)
t.equal(type.isPrimitive(Infinity), true)
t.equal(type.isPrimitive(function () {}), false)
t.equal(type.isPrimitive(Date), false)
t.equal(type.isPrimitive(null), true)
t.equal(type.isPrimitive(undefined), true)
t.end()
})
if (detect.symbols()) {
test('.isPrimitive(value) ES6', function (t) {
t.equal(type.isPrimitive(Symbol()), true)
t.end()
})
}
test('.isClass(value)', function (t) {
t.equal(type.isClass(true), false)
t.equal(type.isClass({}), false)
t.equal(type.isClass(0), false)
t.equal(type.isClass('1'), false)
t.equal(type.isClass(1.1), false)
t.equal(type.isClass(NaN), false)
t.equal(type.isClass(Infinity), false)
t.equal(type.isClass(function () {}), false)
t.equal(type.isClass(Date), false)
t.equal(type.isClass(), false)
function broken () { }
broken.toString = function () { throw new Error() }
t.equal(type.isClass(broken), false)
t.end()
})
if (detect.class()) {
test('.isClass(value) ES6', function (t) {
var result = eval('type.isClass(class {})')
t.equal(result, true)
t.end()
})
}
if (detect.promises()) {
test('.isPromise', function (t) {
t.strictEqual(type.isPromise(Promise.resolve()), true)
t.strictEqual(type.isPromise(Promise), false)
t.strictEqual(type.isPromise(true), false)
t.strictEqual(type.isPromise({}), false)
t.strictEqual(type.isPromise(0), false)
t.strictEqual(type.isPromise('1'), false)
t.strictEqual(type.isPromise(1.1), false)
t.strictEqual(type.isPromise(NaN), false)
t.strictEqual(type.isPromise(Infinity), false)
t.strictEqual(type.isPromise(function () {}), false)
t.strictEqual(type.isPromise(Date), false)
t.strictEqual(type.isPromise(), false)
t.strictEqual(type.isPromise({ then: function () {} }), true)
t.end()
})
}

Sorry, the diff of this file is not supported yet

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