firest commit
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014-19 Lloyd Brookes <75pound@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
[](https://www.npmjs.org/package/typical)
|
||||
[](https://www.npmjs.org/package/typical)
|
||||
[](https://travis-ci.org/75lb/typical)
|
||||
[](https://coveralls.io/github/75lb/typical?branch=master)
|
||||
[](https://david-dm.org/75lb/typical)
|
||||
[](https://github.com/feross/standard)
|
||||
|
||||
{{>main}}
|
||||
|
||||
## Load anywhere
|
||||
|
||||
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
|
||||
|
||||
Node.js:
|
||||
|
||||
```js
|
||||
const typical = require('typical')
|
||||
```
|
||||
|
||||
Within Node.js with ECMAScript Module support enabled:
|
||||
|
||||
```js
|
||||
import typical from 'typical'
|
||||
```
|
||||
|
||||
Within a modern browser ECMAScript Module:
|
||||
|
||||
```js
|
||||
import typical from './node_modules/typical/index.mjs'
|
||||
```
|
||||
|
||||
Old browser (adds `window.typical`):
|
||||
|
||||
```html
|
||||
<script nomodule src="./node_modules/typical/dist/index.js"></script>
|
||||
```
|
||||
|
||||
* * *
|
||||
|
||||
© 2014-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
|
||||
+302
@@ -0,0 +1,302 @@
|
||||
[](https://www.npmjs.org/package/typical)
|
||||
[](https://www.npmjs.org/package/typical)
|
||||
[](https://travis-ci.org/75lb/typical)
|
||||
[](https://coveralls.io/github/75lb/typical?branch=master)
|
||||
[](https://david-dm.org/75lb/typical)
|
||||
[](https://github.com/feross/standard)
|
||||
|
||||
<a name="module_typical"></a>
|
||||
|
||||
## typical
|
||||
Isomorphic, functional type-checking for Javascript.
|
||||
|
||||
**Example**
|
||||
```js
|
||||
const t = require('typical')
|
||||
const allDefined = array.every(t.isDefined)
|
||||
```
|
||||
|
||||
* [typical](#module_typical)
|
||||
* [.isNumber(n)](#module_typical.isNumber) ⇒ <code>boolean</code>
|
||||
* [.isPlainObject(input)](#module_typical.isPlainObject) ⇒ <code>boolean</code>
|
||||
* [.isArrayLike(input)](#module_typical.isArrayLike) ⇒ <code>boolean</code>
|
||||
* [.isObject(input)](#module_typical.isObject) ⇒ <code>boolean</code>
|
||||
* [.isDefined(input)](#module_typical.isDefined) ⇒ <code>boolean</code>
|
||||
* [.isUndefined(input)](#module_typical.isUndefined) ⇒ <code>boolean</code>
|
||||
* [.isNull(input)](#module_typical.isNull) ⇒ <code>boolean</code>
|
||||
* [.isDefinedValue(input)](#module_typical.isDefinedValue) ⇒ <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>
|
||||
* [.isIterable(input)](#module_typical.isIterable) ⇒ <code>boolean</code>
|
||||
* [.isString(input)](#module_typical.isString) ⇒ <code>boolean</code>
|
||||
* [.isFunction(input)](#module_typical.isFunction) ⇒ <code>boolean</code>
|
||||
|
||||
<a name="module_typical.isNumber"></a>
|
||||
|
||||
### t.isNumber(n) ⇒ <code>boolean</code>
|
||||
Returns true if input is a number. It is a more reasonable alternative to `typeof n` which returns `number` for `NaN` and `Infinity`.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| n | <code>\*</code> | the input to test |
|
||||
|
||||
**Example**
|
||||
```js
|
||||
> 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
|
||||
```
|
||||
<a name="module_typical.isPlainObject"></a>
|
||||
|
||||
### t.isPlainObject(input) ⇒ <code>boolean</code>
|
||||
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`.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
**Example**
|
||||
```js
|
||||
> t.isPlainObject({ something: 'one' })
|
||||
true
|
||||
> t.isPlainObject(new Date())
|
||||
false
|
||||
> t.isPlainObject([ 0, 1 ])
|
||||
false
|
||||
> t.isPlainObject(/test/)
|
||||
false
|
||||
> t.isPlainObject(1)
|
||||
false
|
||||
> t.isPlainObject('one')
|
||||
false
|
||||
> t.isPlainObject(null)
|
||||
false
|
||||
> t.isPlainObject((function * () {})())
|
||||
false
|
||||
> t.isPlainObject(function * () {})
|
||||
false
|
||||
```
|
||||
<a name="module_typical.isArrayLike"></a>
|
||||
|
||||
### t.isArrayLike(input) ⇒ <code>boolean</code>
|
||||
An array-like value has all the properties of an array yet is not an array instance. An example is the `arguments` object. Returns `true`` if the input value is an object, not `null`` and has a `length` property set with a numeric value.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
**Example**
|
||||
```js
|
||||
function sum(x, y){
|
||||
console.log(t.isArrayLike(arguments))
|
||||
// prints `true`
|
||||
}
|
||||
```
|
||||
<a name="module_typical.isObject"></a>
|
||||
|
||||
### t.isObject(input) ⇒ <code>boolean</code>
|
||||
Returns true if the typeof input is `'object'` but not null.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
<a name="module_typical.isDefined"></a>
|
||||
|
||||
### t.isDefined(input) ⇒ <code>boolean</code>
|
||||
Returns true if the input value is defined.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
<a name="module_typical.isUndefined"></a>
|
||||
|
||||
### t.isUndefined(input) ⇒ <code>boolean</code>
|
||||
Returns true if the input value is undefined.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
<a name="module_typical.isNull"></a>
|
||||
|
||||
### t.isNull(input) ⇒ <code>boolean</code>
|
||||
Returns true if the input value is null.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
<a name="module_typical.isDefinedValue"></a>
|
||||
|
||||
### t.isDefinedValue(input) ⇒ <code>boolean</code>
|
||||
Returns true if the input value is not one of `undefined`, `null`, or `NaN`.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
<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</code>](#module_typical)
|
||||
|
||||
| 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</code>](#module_typical)
|
||||
|
||||
| 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 Promise.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
<a name="module_typical.isIterable"></a>
|
||||
|
||||
### t.isIterable(input) ⇒ <code>boolean</code>
|
||||
Returns true if the input is an iterable (`Map`, `Set`, `Array`, Generator etc.).
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
**Example**
|
||||
```js
|
||||
> t.isIterable('string')
|
||||
true
|
||||
> t.isIterable(new Map())
|
||||
true
|
||||
> t.isIterable([])
|
||||
true
|
||||
> t.isIterable((function * () {})())
|
||||
true
|
||||
> t.isIterable(Promise.resolve())
|
||||
false
|
||||
> t.isIterable(Promise)
|
||||
false
|
||||
> t.isIterable(true)
|
||||
false
|
||||
> t.isIterable({})
|
||||
false
|
||||
> t.isIterable(0)
|
||||
false
|
||||
> t.isIterable(1.1)
|
||||
false
|
||||
> t.isIterable(NaN)
|
||||
false
|
||||
> t.isIterable(Infinity)
|
||||
false
|
||||
> t.isIterable(function () {})
|
||||
false
|
||||
> t.isIterable(Date)
|
||||
false
|
||||
> t.isIterable()
|
||||
false
|
||||
> t.isIterable({ then: function () {} })
|
||||
false
|
||||
```
|
||||
<a name="module_typical.isString"></a>
|
||||
|
||||
### t.isString(input) ⇒ <code>boolean</code>
|
||||
Returns true if the input value is a string. The equivalent of `typeof input === 'string'` for use in funcitonal contexts.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
<a name="module_typical.isFunction"></a>
|
||||
|
||||
### t.isFunction(input) ⇒ <code>boolean</code>
|
||||
Returns true if the input value is a function. The equivalent of `typeof input === 'function'` for use in funcitonal contexts.
|
||||
|
||||
**Kind**: static method of [<code>typical</code>](#module_typical)
|
||||
|
||||
| Param | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| input | <code>\*</code> | the input to test |
|
||||
|
||||
|
||||
## Load anywhere
|
||||
|
||||
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
|
||||
|
||||
Node.js:
|
||||
|
||||
```js
|
||||
const typical = require('typical')
|
||||
```
|
||||
|
||||
Within Node.js with ECMAScript Module support enabled:
|
||||
|
||||
```js
|
||||
import typical from 'typical'
|
||||
```
|
||||
|
||||
Within a modern browser ECMAScript Module:
|
||||
|
||||
```js
|
||||
import typical from './node_modules/typical/index.mjs'
|
||||
```
|
||||
|
||||
Old browser (adds `window.typical`):
|
||||
|
||||
```html
|
||||
<script nomodule src="./node_modules/typical/dist/index.js"></script>
|
||||
```
|
||||
|
||||
* * *
|
||||
|
||||
© 2014-19 Lloyd Brookes \<75pound@gmail.com\>. Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(global = global || self, factory(global.typical = {}));
|
||||
}(this, function (exports) { 'use strict';
|
||||
|
||||
/**
|
||||
* Isomorphic, functional type-checking for Javascript.
|
||||
* @module typical
|
||||
* @typicalname t
|
||||
* @example
|
||||
* const t = require('typical')
|
||||
* const allDefined = array.every(t.isDefined)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns true if input is a number. It is a more reasonable alternative to `typeof n` which returns `number` for `NaN` and `Infinity`.
|
||||
*
|
||||
* @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) {
|
||||
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({ something: 'one' })
|
||||
* true
|
||||
* > t.isPlainObject(new Date())
|
||||
* false
|
||||
* > t.isPlainObject([ 0, 1 ])
|
||||
* false
|
||||
* > t.isPlainObject(/test/)
|
||||
* false
|
||||
* > t.isPlainObject(1)
|
||||
* false
|
||||
* > t.isPlainObject('one')
|
||||
* false
|
||||
* > t.isPlainObject(null)
|
||||
* false
|
||||
* > t.isPlainObject((function * () {})())
|
||||
* false
|
||||
* > t.isPlainObject(function * () {})
|
||||
* false
|
||||
*/
|
||||
function isPlainObject (input) {
|
||||
return input !== null && typeof input === 'object' && input.constructor === Object
|
||||
}
|
||||
|
||||
/**
|
||||
* An array-like value has all the properties of an array yet is not an array instance. An example is the `arguments` object. Returns `true`` if the input value is an object, not `null`` and has a `length` property set 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) {
|
||||
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
|
||||
*/
|
||||
function isObject (input) {
|
||||
return typeof input === 'object' && input !== null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is defined.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isDefined (input) {
|
||||
return typeof input !== 'undefined'
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is undefined.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isUndefined (input) {
|
||||
return !isDefined(input)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is null.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isNull (input) {
|
||||
return input === null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is both defined and not null.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isDefinedValue (input) {
|
||||
return isDefined(input) && !isNull(input) && !Number.isNaN(input)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is an ES2015 `class`.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isClass (input) {
|
||||
if (typeof input === 'function') {
|
||||
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 Promise.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isPromise (input) {
|
||||
if (input) {
|
||||
const isPromise = isDefined(Promise) && input instanceof Promise;
|
||||
const isThenable = input.then && typeof input.then === 'function';
|
||||
return !!(isPromise || isThenable)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input is an iterable (`Map`, `Set`, `Array`, Generator etc.).
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
* @example
|
||||
* > t.isIterable('string')
|
||||
* true
|
||||
* > t.isIterable(new Map())
|
||||
* true
|
||||
* > t.isIterable([])
|
||||
* true
|
||||
* > t.isIterable((function * () {})())
|
||||
* true
|
||||
* > t.isIterable(Promise.resolve())
|
||||
* false
|
||||
* > t.isIterable(Promise)
|
||||
* false
|
||||
* > t.isIterable(true)
|
||||
* false
|
||||
* > t.isIterable({})
|
||||
* false
|
||||
* > t.isIterable(0)
|
||||
* false
|
||||
* > t.isIterable(1.1)
|
||||
* false
|
||||
* > t.isIterable(NaN)
|
||||
* false
|
||||
* > t.isIterable(Infinity)
|
||||
* false
|
||||
* > t.isIterable(function () {})
|
||||
* false
|
||||
* > t.isIterable(Date)
|
||||
* false
|
||||
* > t.isIterable()
|
||||
* false
|
||||
* > t.isIterable({ then: function () {} })
|
||||
* false
|
||||
*/
|
||||
function isIterable (input) {
|
||||
if (input === null || !isDefined(input)) {
|
||||
return false
|
||||
} else {
|
||||
return (
|
||||
typeof input[Symbol.iterator] === 'function' ||
|
||||
typeof input[Symbol.asyncIterator] === 'function'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is a string. The equivalent of `typeof input === 'string'` for use in funcitonal contexts.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isString (input) {
|
||||
return typeof input === 'string'
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is a function. The equivalent of `typeof input === 'function'` for use in funcitonal contexts.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
function isFunction (input) {
|
||||
return typeof input === 'function'
|
||||
}
|
||||
|
||||
var index = {
|
||||
isNumber,
|
||||
isPlainObject,
|
||||
isArrayLike,
|
||||
isObject,
|
||||
isDefined,
|
||||
isUndefined,
|
||||
isNull,
|
||||
isDefinedValue,
|
||||
isClass,
|
||||
isPrimitive,
|
||||
isPromise,
|
||||
isIterable,
|
||||
isString,
|
||||
isFunction
|
||||
};
|
||||
|
||||
exports.default = index;
|
||||
exports.isArrayLike = isArrayLike;
|
||||
exports.isClass = isClass;
|
||||
exports.isDefined = isDefined;
|
||||
exports.isDefinedValue = isDefinedValue;
|
||||
exports.isFunction = isFunction;
|
||||
exports.isIterable = isIterable;
|
||||
exports.isNull = isNull;
|
||||
exports.isNumber = isNumber;
|
||||
exports.isObject = isObject;
|
||||
exports.isPlainObject = isPlainObject;
|
||||
exports.isPrimitive = isPrimitive;
|
||||
exports.isPromise = isPromise;
|
||||
exports.isString = isString;
|
||||
exports.isUndefined = isUndefined;
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
}));
|
||||
+269
@@ -0,0 +1,269 @@
|
||||
/**
|
||||
* Isomorphic, functional type-checking for Javascript.
|
||||
* @module typical
|
||||
* @typicalname t
|
||||
* @example
|
||||
* const t = require('typical')
|
||||
* const allDefined = array.every(t.isDefined)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns true if input is a number. It is a more reasonable alternative to `typeof n` which returns `number` for `NaN` and `Infinity`.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
export function isNumber (n) {
|
||||
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({ something: 'one' })
|
||||
* true
|
||||
* > t.isPlainObject(new Date())
|
||||
* false
|
||||
* > t.isPlainObject([ 0, 1 ])
|
||||
* false
|
||||
* > t.isPlainObject(/test/)
|
||||
* false
|
||||
* > t.isPlainObject(1)
|
||||
* false
|
||||
* > t.isPlainObject('one')
|
||||
* false
|
||||
* > t.isPlainObject(null)
|
||||
* false
|
||||
* > t.isPlainObject((function * () {})())
|
||||
* false
|
||||
* > t.isPlainObject(function * () {})
|
||||
* false
|
||||
*/
|
||||
export function isPlainObject (input) {
|
||||
return input !== null && typeof input === 'object' && input.constructor === Object
|
||||
}
|
||||
|
||||
/**
|
||||
* An array-like value has all the properties of an array yet is not an array instance. An example is the `arguments` object. Returns `true`` if the input value is an object, not `null`` and has a `length` property set with a numeric value.
|
||||
*
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
* @example
|
||||
* function sum(x, y){
|
||||
* console.log(t.isArrayLike(arguments))
|
||||
* // prints `true`
|
||||
* }
|
||||
*/
|
||||
export function isArrayLike (input) {
|
||||
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
|
||||
*/
|
||||
export function isObject (input) {
|
||||
return typeof input === 'object' && input !== null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is defined.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isDefined (input) {
|
||||
return typeof input !== 'undefined'
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is undefined.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isUndefined (input) {
|
||||
return !isDefined(input)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is null.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isNull (input) {
|
||||
return input === null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is not one of `undefined`, `null`, or `NaN`.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isDefinedValue (input) {
|
||||
return isDefined(input) && !isNull(input) && !Number.isNaN(input)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is an ES2015 `class`.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isClass (input) {
|
||||
if (typeof input === 'function') {
|
||||
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
|
||||
*/
|
||||
export 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 Promise.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isPromise (input) {
|
||||
if (input) {
|
||||
const isPromise = isDefined(Promise) && input instanceof Promise
|
||||
const isThenable = input.then && typeof input.then === 'function'
|
||||
return !!(isPromise || isThenable)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input is an iterable (`Map`, `Set`, `Array`, Generator etc.).
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
* @example
|
||||
* > t.isIterable('string')
|
||||
* true
|
||||
* > t.isIterable(new Map())
|
||||
* true
|
||||
* > t.isIterable([])
|
||||
* true
|
||||
* > t.isIterable((function * () {})())
|
||||
* true
|
||||
* > t.isIterable(Promise.resolve())
|
||||
* false
|
||||
* > t.isIterable(Promise)
|
||||
* false
|
||||
* > t.isIterable(true)
|
||||
* false
|
||||
* > t.isIterable({})
|
||||
* false
|
||||
* > t.isIterable(0)
|
||||
* false
|
||||
* > t.isIterable(1.1)
|
||||
* false
|
||||
* > t.isIterable(NaN)
|
||||
* false
|
||||
* > t.isIterable(Infinity)
|
||||
* false
|
||||
* > t.isIterable(function () {})
|
||||
* false
|
||||
* > t.isIterable(Date)
|
||||
* false
|
||||
* > t.isIterable()
|
||||
* false
|
||||
* > t.isIterable({ then: function () {} })
|
||||
* false
|
||||
*/
|
||||
export function isIterable (input) {
|
||||
if (input === null || !isDefined(input)) {
|
||||
return false
|
||||
} else {
|
||||
return (
|
||||
typeof input[Symbol.iterator] === 'function' ||
|
||||
typeof input[Symbol.asyncIterator] === 'function'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is a string. The equivalent of `typeof input === 'string'` for use in funcitonal contexts.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isString (input) {
|
||||
return typeof input === 'string'
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the input value is a function. The equivalent of `typeof input === 'function'` for use in funcitonal contexts.
|
||||
* @param {*} - the input to test
|
||||
* @returns {boolean}
|
||||
* @static
|
||||
*/
|
||||
export function isFunction (input) {
|
||||
return typeof input === 'function'
|
||||
}
|
||||
|
||||
export default {
|
||||
isNumber,
|
||||
isPlainObject,
|
||||
isArrayLike,
|
||||
isObject,
|
||||
isDefined,
|
||||
isUndefined,
|
||||
isNull,
|
||||
isDefinedValue,
|
||||
isClass,
|
||||
isPrimitive,
|
||||
isPromise,
|
||||
isIterable,
|
||||
isString,
|
||||
isFunction
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
{
|
||||
"name": "typical",
|
||||
"author": "Lloyd Brookes <75pound@gmail.com>",
|
||||
"version": "5.2.0",
|
||||
"description": "Isomorphic, functional type-checking for Javascript",
|
||||
"repository": "https://github.com/75lb/typical",
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"keywords": [
|
||||
"type",
|
||||
"checking",
|
||||
"check",
|
||||
"value",
|
||||
"valid",
|
||||
"is",
|
||||
"number",
|
||||
"object",
|
||||
"plainobject",
|
||||
"array",
|
||||
"like",
|
||||
"defined",
|
||||
"string",
|
||||
"boolean",
|
||||
"function",
|
||||
"promise",
|
||||
"iterable",
|
||||
"class",
|
||||
"primitive",
|
||||
"isstring",
|
||||
"isclass",
|
||||
"isiterable",
|
||||
"isdefined",
|
||||
"isobject",
|
||||
"isomorphic"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "npm run dist && npm run test:js && npm run test:esm",
|
||||
"test:all": "npm run test:js && npm run test:esm && npm run test:web",
|
||||
"test:js": "rollup test/*.mjs -f cjs -d tmp/test -e assert && test-runner tmp/test/test*.js",
|
||||
"test:esm": "esm-runner test/*.mjs",
|
||||
"test:web": "web-runner test/test.mjs",
|
||||
"test:v8": "rollup test/test.mjs test/test-default.mjs -f cjs -d tmp/testv8 && test-runner tmp/testv8/test*.js",
|
||||
"dist": "rollup index.mjs -f umd -n typical -o dist/index.js --exports named",
|
||||
"docs": "jsdoc2md -c jsdoc.conf -t README.hbs index.mjs > README.md; echo",
|
||||
"cover": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
|
||||
},
|
||||
"devDependencies": {
|
||||
"coveralls": "^3.0.7",
|
||||
"esm-runner": "^0.1.5",
|
||||
"jsdoc-to-markdown": "^5.0.2",
|
||||
"nyc": "^14.1.1",
|
||||
"rollup": "^1.25.1",
|
||||
"test-object-model": "^0.4.4",
|
||||
"test-runner": "^0.6.0"
|
||||
},
|
||||
"files": [
|
||||
"index.mjs",
|
||||
"dist/index.js"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user