Comparing version 0.5.1 to 0.6.0
declare type Class<T = unknown> = new (...args: any[]) => T; | ||
interface IPlainObject { | ||
[key: string]: unknown; | ||
} | ||
declare const enum Types { | ||
@@ -30,9 +33,12 @@ array = "Array", | ||
array: (arg: any) => arg is any[]; | ||
arrayOf(target: unknown[], predicate: (v: unknown) => boolean): boolean; | ||
asyncFunction: (value: unknown) => value is Function; | ||
boolean(value: unknown): value is boolean; | ||
date: (value: unknown) => value is Date; | ||
defined(value: unknown): boolean; | ||
domElement(value: unknown): value is Element; | ||
empty(value: unknown): boolean; | ||
error: (value: unknown) => value is Error; | ||
function: (value: unknown) => value is Function; | ||
generator(value: unknown): value is Generator; | ||
generator(value: unknown): value is Generator<unknown, any, unknown>; | ||
generatorFunction: (value: unknown) => value is GeneratorFunction; | ||
@@ -48,6 +54,6 @@ instanceOf<T>(instance: unknown, class_: Class<T>): instance is T; | ||
object(value: unknown): value is object; | ||
plainObject(value: unknown): value is { | ||
[key: string]: unknown; | ||
}; | ||
oneOf(target: unknown[], value: any): boolean; | ||
plainObject(value: unknown): value is IPlainObject; | ||
promise: (value: unknown) => value is Promise<unknown>; | ||
propertyOf(target: object, key: string, predicate?: ((v: unknown) => boolean) | undefined): boolean; | ||
regexp: (value: unknown) => value is RegExp; | ||
@@ -54,0 +60,0 @@ set: (value: unknown) => value is Set<object>; |
@@ -47,2 +47,8 @@ "use strict"; | ||
is.array = Array.isArray; | ||
is.arrayOf = function (target, predicate) { | ||
if (!is.array(target) && !is.function(predicate)) { | ||
return false; | ||
} | ||
return target.every(function (d) { return predicate(d); }); | ||
}; | ||
// tslint:disable-next-line:ban-types | ||
@@ -54,2 +60,3 @@ is.asyncFunction = isObjectOfType("AsyncFunction" /* asyncFunction */); | ||
is.date = isObjectOfType("Date" /* date */); | ||
is.defined = function (value) { return !is.undefined(value); }; | ||
is.domElement = function (value) { | ||
@@ -69,2 +76,9 @@ var DOM_PROPERTIES_TO_CHECK = [ | ||
}; | ||
is.empty = function (value) { | ||
return ((is.string(value) && value.length === 0) || | ||
(is.array(value) && value.length === 0) || | ||
(is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0) || | ||
(is.set(value) && value.size === 0) || | ||
(is.map(value) && value.size === 0)); | ||
}; | ||
is.error = isObjectOfType("Error" /* error */); | ||
@@ -108,2 +122,8 @@ // tslint:disable-next-line:ban-types | ||
}; | ||
is.oneOf = function (target, value) { | ||
if (!is.array(target)) { | ||
return false; | ||
} | ||
return target.indexOf(value) > -1; | ||
}; | ||
is.plainObject = function (value) { | ||
@@ -117,2 +137,13 @@ if (exports.getObjectType(value) !== 'Object') { | ||
is.promise = isObjectOfType("Promise" /* promise */); | ||
is.propertyOf = function (target, key, predicate) { | ||
if (!is.object(target) || !key) { | ||
return false; | ||
} | ||
// @ts-ignore | ||
var value = target[key]; | ||
if (is.function(predicate)) { | ||
return predicate(value); | ||
} | ||
return is.defined(value); | ||
}; | ||
is.regexp = isObjectOfType("RegExp" /* regExp */); | ||
@@ -119,0 +150,0 @@ is.set = isObjectOfType("Set" /* set */); |
{ | ||
"name": "is-lite", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "A tiny javascript type testing tool", | ||
@@ -27,8 +27,7 @@ "author": "Gil Barbara <gilbarbara@gmail.com>", | ||
"@gilbarbara/tsconfig": "^0.1.0", | ||
"@size-limit/preset-small-lib": "^2.1.0", | ||
"@types/jest": "^24.0.17", | ||
"@types/node": "^12.7.2", | ||
"@size-limit/preset-small-lib": "^2.1.6", | ||
"@types/jest": "^24.0.18", | ||
"@types/node": "^12.7.5", | ||
"@types/yargs": "^13.0.2", | ||
"cross-env": "^5.2.0", | ||
"husky": "^3.0.4", | ||
"husky": "^3.0.5", | ||
"jest": "^24.9.0", | ||
@@ -38,8 +37,6 @@ "prettier": "^1.18.2", | ||
"rimraf": "^3.0.0", | ||
"size-limit": "^2.1.0", | ||
"ts-jest": "^24.0.2", | ||
"ts-node": "^8.3.0", | ||
"tslint": "^5.18.0", | ||
"ts-jest": "^24.1.0", | ||
"tslint": "^5.20.0", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.5.3" | ||
"typescript": "^3.6.3" | ||
}, | ||
@@ -53,4 +50,4 @@ "scripts": { | ||
"test": "jest", | ||
"test:coverage": "jest --coverage", | ||
"test:watch": "jest --watch", | ||
"test:coverage": "jest --bail --coverage", | ||
"test:watch": "jest --watch --verbose", | ||
"format": "prettier \"**/*.{js,jsx,json,yml,yaml,css,less,scss,ts,tsx,md,graphql,mdx}\" --write", | ||
@@ -99,3 +96,3 @@ "validate": "npm run lint && npm run test:coverage && npm run build && npm run size", | ||
"testRegex": "/test/.*?\\.(test|spec)\\.ts$", | ||
"verbose": true | ||
"verbose": false | ||
}, | ||
@@ -102,0 +99,0 @@ "size-limit": [ |
148
README.md
# is-lite | ||
[](https://www.npmjs.com/package/is-lite) [](https://travis-ci.org/gilbarbara/is-lite) [](https://codeclimate.com/github/gilbarbara/is-lite/maintainability) [](https://codeclimate.com/github/gilbarbara/is-lite/test_coverage) | ||
[](https://www.npmjs.com/package/is-lite) [](https://travis-ci.org/gilbarbara/is-lite) [](https://bundlephobia.com/result?p=is-lite) [](https://codeclimate.com/github/gilbarbara/is-lite/maintainability) [](https://codeclimate.com/github/gilbarbara/is-lite/test_coverage) | ||
Type check tool (just 0.6k minified+gzipped) | ||
> Lightweight type check tool. | ||
Typescript ready with [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) to infer the correct type inside conditionals. | ||
## Setup | ||
@@ -18,6 +20,4 @@ | ||
const value = ''; | ||
is.string(value) // true; | ||
is('value'); // string | ||
is.string('value'); // true | ||
``` | ||
@@ -27,4 +27,3 @@ | ||
#### is(value) | ||
**is(value)** | ||
Returns the type of the `value`. | ||
@@ -35,30 +34,51 @@ | ||
#### is.array(value) | ||
**is.array(value)** | ||
#### is.asyncFunction(value) | ||
**is.arrayOf(target: any[], predicate: (value: unknown) => boolean)** | ||
Check if all items in an array are of same type. | ||
```js | ||
is.arrayOf(['a', 'b'], is.string); // true | ||
is.arrayOf([123, 456], is.nnumber); // true | ||
is.arrayOf(['a', 1], is.string); // false | ||
``` | ||
**is.asyncFunction(value)** | ||
Check if `value` is an `async` function that can be called with `await` | ||
```js | ||
is.asyncFunction(async () => {}); // true | ||
is.asyncFunction(() => {}); // false | ||
``` | ||
is.asyncFunction(async () => {}); // => true | ||
is.asyncFunction(() => {}); // => false | ||
``` | ||
#### is.boolean(value) | ||
**is.boolean(value)** | ||
#### is.date(value) | ||
**is.date(value)** | ||
#### is.domElement(value) | ||
**is.defined(value)** | ||
Check if `value` is anything but `undefined`. | ||
**is.domElement(value) ** | ||
Check if `value` is a DOM Element. | ||
#### is.error(value) | ||
**is.empty(value)** | ||
Returns `true` if: | ||
#### is.function(value) | ||
- the value is a `string` and `length` is 0 | ||
- the value is an `Object` and `Object.keys(value).length` is 0 | ||
- the value is an `Array` and `length` is 0 | ||
- the value is a `Map` and `size` is 0 | ||
- the value is a `Set` and `size` is 0 | ||
#### is.generator(value) | ||
**is.error(value)** | ||
**is.function(value)** | ||
**is.generator(value) ** | ||
Check for an object that has its own .next() and .throw() methods and has a function definition for `Symbol.iterator` | ||
#### is.generatorFunction(value) | ||
**is.generatorFunction(value)** | ||
#### is.instanceOf(value, class) | ||
**is.instanceOf(value, class)** | ||
Check if `value` is a direct instance of `class` | ||
@@ -75,44 +95,88 @@ | ||
#### is.iterable(value) | ||
**is.iterable(value)** | ||
#### is.map(value) | ||
**is.map(value)** | ||
#### is.nan(value) | ||
**is.nan(value)** | ||
#### is.null(value) | ||
**is.null(value)** | ||
#### is.nullOrUndefined(value) | ||
**is.nullOrUndefined(value)** | ||
#### is.number(value) | ||
**is.number(value) ** | ||
Note: `is.number(NaN)` returns `false` | ||
#### is.numericString(value) | ||
Check for a string that represents a number. For example, '42' and '-8'. | ||
Note: 'NaN' returns false, but 'Infinity' and '-Infinity' return true | ||
**is.numericString(value)** | ||
Check for a string that represents a number. | ||
#### is.object(value) | ||
```js | ||
is.numericString('42'); // true | ||
is.numericString('-5'); // true | ||
is.numericString('Inifinity'); // true | ||
is.numericString('NaN'); // true | ||
``` | ||
**is.object(value) ** | ||
Remember that functions and arrays are objects too. | ||
#### is.plainObject(value) | ||
Check if the object is created by either `{}`, `new Object()`, or `Object.create(null)`. | ||
**is.oneOf(target: any[], value: any)** | ||
Check if `value` exists is the `target` | ||
#### is.promise(value) | ||
```js | ||
const colors = ['red', 'green', 'blue']; | ||
#### is.regexp(value) | ||
is.oneOf(colors, 'green'); // true | ||
is.oneOf(colors, 'brown'); // false | ||
``` | ||
#### is.set(value) | ||
**is.plainObject(value) ** | ||
Check if the object is created by either `{}`, `new Object()` or `Object.create(null)`. | ||
#### is.string(value) | ||
**is.promise(value)** | ||
#### is.symbol(value) | ||
**is.propertyOf(target: object, key: string, predicate?: (value: unknown) => boolean)** | ||
Check if `key` exists of `target`. if you pass a `predicate` function, it will check the value's type. | ||
#### is.undefined(value) | ||
```js | ||
const map = { items: [1], isLogged: false, retries: 0 }; | ||
#### is.weakMap(value) | ||
is.propertyOf(map, 'retries'); // true | ||
is.propertyOf(map, 'auth'); // false | ||
#### is.weakSet(value) | ||
is.propertyOf(map, 'retries', is.number); // true | ||
is.propertyOf(map, 'items', is.array); // true | ||
is.propertyOf(map, 'isLogged', is.string); // false | ||
``` | ||
**is.regexp(value)** | ||
**is.set(value)** | ||
**is.string(value)** | ||
**is.symbol(value)** | ||
**is.undefined(value)** | ||
**is.weakMap(value)** | ||
**is.weakSet(value)** | ||
## Contributing | ||
Contributions, issues and feature requests are welcome! | ||
Feel free to check [issues page](https://github.com/gilbarbara/is-lite/issues). | ||
## Show your support | ||
Give a ⭐️ if this project helped you! | ||
## License | ||
Copyright © 2019 [Gil Barbara <gilbarbara@gmail.com>](https://github.com/gilbarbara). | ||
This project is [MIT](https://github.com/gilbarbara/is-lite/blob/master/LICENSE) licensed. | ||
## FAQ | ||
[@sindresorhus/is](https://github.com/sindresorhus/is) is amazing but I needed something even smaller (and simpler). | ||
This package cover the basics and is only 0.6k minified+gzipped. | ||
This package cover the basics and is less than 1k minified+gzipped. |
Sorry, the diff of this file is not supported yet
21436
14
216
178