is-what
Advanced tools
Comparing version 3.14.1 to 4.0.0
46
build.js
/* eslint-disable */ | ||
// npm install rollup-plugin-typescript2 typescript --save-dev | ||
// npm i -D rollup rollup-plugin-typescript2 typescript | ||
import typescript from 'rollup-plugin-typescript2' | ||
// import { terser } from 'rollup-plugin-terser' | ||
// import resolve from 'rollup-plugin-node-resolve' | ||
@@ -23,21 +21,11 @@ // ------------------------------------------------------------------------------------------ | ||
const name = pkg.name | ||
const className = name.replace(/(^\w|-\w)/g, c => c.replace('-', '').toUpperCase()) | ||
const external = Object.keys(pkg.dependencies || []) | ||
const plugins = [ | ||
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }), | ||
] | ||
const className = name.replace(/(^\w|-\w)/g, (c) => c.replace('-', '').toUpperCase()) | ||
// ------------------------------------------------------------------------------------------ | ||
// Builds | ||
// ------------------------------------------------------------------------------------------ | ||
function defaults (config) { | ||
// defaults | ||
const defaults = { | ||
plugins, | ||
external, | ||
} | ||
// defaults.output | ||
config.output = config.output.map(output => { | ||
return Object.assign( | ||
export default [ | ||
{ | ||
input: 'src/index.ts', | ||
output: [ | ||
{ | ||
file: 'dist/index.js', | ||
format: 'esm', | ||
sourcemap: false, | ||
@@ -47,16 +35,8 @@ name: className, | ||
}, | ||
output | ||
) | ||
}) | ||
return Object.assign(defaults, config) | ||
} | ||
export default [ | ||
defaults({ | ||
input: 'src/index.ts', | ||
output: [ | ||
{ file: 'dist/index.cjs.js', format: 'cjs' }, | ||
{ file: 'dist/index.esm.js', format: 'esm' }, | ||
], | ||
}), | ||
plugins: [ | ||
typescript({ useTsconfigDeclarationDir: true, tsconfigOverride: { exclude: ['test/**/*'] } }), | ||
], | ||
external: Object.keys(pkg.dependencies || []), | ||
}, | ||
] |
{ | ||
"name": "is-what", | ||
"sideEffects": false, | ||
"version": "3.14.1", | ||
"version": "4.0.0", | ||
"description": "JS type check (TypeScript supported) functions like `isPlainObject() isArray()` etc. A simple & small integration.", | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm.js", | ||
"typings": "types/index.d.ts", | ||
"type": "module", | ||
"exports": "./dist/index.js", | ||
"typings": "./types/index.d.ts", | ||
"scripts": { | ||
"test": "ava", | ||
"jest": "jest", | ||
"jest-w": "jest --watchAll", | ||
"lint": "tsc --noEmit src/index.ts && eslint . --ext .js,.jsx,.ts,.tsx", | ||
"rollup": "rollup -c ./build.js", | ||
"build": "rimraf types && rimraf dist && npm run lint && npm run rollup && npm run test && npm run jest", | ||
"build": "rimraf types && rimraf dist && npm run lint && npm run rollup && npm run test", | ||
"release": "npm run build && np" | ||
@@ -50,32 +48,33 @@ }, | ||
"devDependencies": { | ||
"@babel/core": "^7.12.17", | ||
"@babel/core": "^7.13.8", | ||
"@types/babel-core": "^6.25.6", | ||
"@types/jest": "^26.0.20", | ||
"@typescript-eslint/eslint-plugin": "^4.15.1", | ||
"@typescript-eslint/parser": "^4.15.1", | ||
"@typescript-eslint/eslint-plugin": "^4.16.1", | ||
"@typescript-eslint/parser": "^4.16.1", | ||
"ava": "^3.15.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^26.6.3", | ||
"babel-preset-env": "^1.7.0", | ||
"eslint": "^7.20.0", | ||
"eslint-config-prettier": "^7.2.0", | ||
"eslint": "^7.21.0", | ||
"eslint-config-prettier": "^8.1.0", | ||
"eslint-plugin-tree-shaking": "^1.8.0", | ||
"jest": "^26.6.3", | ||
"np": "^7.4.0", | ||
"prettier": "^2.2.1", | ||
"regenerator-runtime": "^0.13.7", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.39.0", | ||
"rollup": "^2.40.0", | ||
"rollup-plugin-typescript2": "^0.30.0", | ||
"ts-node": "^9.1.1", | ||
"tsconfig-paths": "^3.9.0", | ||
"typescript": "^4.1.5" | ||
"typescript": "^4.2.3" | ||
}, | ||
"engines": { | ||
"node": ">=12.13", | ||
"npm": ">=7" | ||
}, | ||
"ava": { | ||
"extensions": [ | ||
"ts" | ||
], | ||
"require": [ | ||
"tsconfig-paths/register", | ||
"ts-node/register" | ||
"extensions": { | ||
"ts": "module" | ||
}, | ||
"nonSemVerExperiments": { | ||
"configurableModuleFormat": true | ||
}, | ||
"nodeArguments": [ | ||
"--loader=ts-node/esm" | ||
] | ||
@@ -86,3 +85,23 @@ }, | ||
"branch": "production" | ||
}, | ||
"eslintConfig": { | ||
"root": true, | ||
"parser": "@typescript-eslint/parser", | ||
"plugins": [ | ||
"@typescript-eslint", | ||
"tree-shaking" | ||
], | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier" | ||
], | ||
"rules": { | ||
"@typescript-eslint/no-explicit-any": "off", | ||
"@typescript-eslint/ban-ts-ignore": "off", | ||
"tree-shaking/no-side-effects-in-initialization": "error", | ||
"@typescript-eslint/explicit-module-boundary-types": "off" | ||
} | ||
} | ||
} |
# is What? 🙉 | ||
Very simple & small JS type check functions. It's fully TypeScript supported! | ||
Very simple & small JS type check functions. It's fully TypeScript supported! | ||
@@ -16,2 +16,3 @@ ``` | ||
I was looking for: | ||
- A simple way to check any kind of type (including non-primitives) | ||
@@ -89,7 +90,7 @@ - Be able to check if an object is a plain object `{}` or a special object (like a class instance) ‼️ | ||
// define a plain object | ||
const plainObject = {hello: 'I am a good old object.'} | ||
const plainObject = { hello: 'I am a good old object.' } | ||
// define a special object | ||
class SpecialObject { | ||
constructor (somethingSpecial) { | ||
constructor(somethingSpecial) { | ||
this.speciality = somethingSpecial | ||
@@ -118,3 +119,3 @@ } | ||
```ts | ||
function isNumber (payload: any): payload is number { | ||
function isNumber(payload: any): payload is number { | ||
// return boolean | ||
@@ -125,3 +126,3 @@ } | ||
// usage example: | ||
function fn (payload: string | number): number { | ||
function fn(payload: string | number): number { | ||
if (isNumber(payload)) { | ||
@@ -138,4 +139,4 @@ // ↑ TypeScript already knows payload is a number here! | ||
```ts | ||
function isPlainObject (payload: any): payload is {[key: string]: any} | ||
function isAnyObject (payload: any): payload is {[key: string]: any} | ||
function isPlainObject(payload: any): payload is { [key: string]: any } | ||
function isAnyObject(payload: any): payload is { [key: string]: any } | ||
// The reason to return `{[key: string]: any}` is to be able to do | ||
@@ -153,3 +154,3 @@ if (isPlainObject(payload) && payload.id) return payload.id | ||
// usage examples: | ||
isObjectLike<{specificKey: string}>(payload) | ||
isObjectLike<{ specificKey: string }>(payload) | ||
isObjectLike<object>(payload) | ||
@@ -162,3 +163,3 @@ // you can pass a specific type for TS to check on. | ||
```ts | ||
function isObjectLike<T extends object> (payload: any): payload is T { | ||
function isObjectLike<T extends object>(payload: any): payload is T { | ||
return isAnyObject(payload) | ||
@@ -183,12 +184,12 @@ } | ||
```js | ||
function getType (payload) { | ||
function getType(payload) { | ||
return Object.prototype.toString.call(payload).slice(8, -1) | ||
} | ||
function isUndefined (payload) { | ||
function isUndefined(payload) { | ||
return getType(payload) === 'Undefined' | ||
} | ||
function isString (payload) { | ||
function isString(payload) { | ||
return getType(payload) === 'String' | ||
} | ||
function isAnyObject (payload) { | ||
function isAnyObject(payload) { | ||
return getType(payload) === 'Object' | ||
@@ -195,0 +196,0 @@ } |
import test from 'ava' | ||
import { | ||
@@ -35,7 +36,17 @@ isError, | ||
isFullObject, | ||
} from '../src/index' | ||
} from '../src/index.js' | ||
// const blob = Buffer.from([]) | ||
// TODO: test isBlob | ||
// test('isBlob', () => { | ||
// expect(isBlob(Blob)).toBe(false) | ||
// expect(isBlob(new Blob())).toBe(true) | ||
// }) | ||
test('Basic true tests', t => { | ||
// TODO: test isFile | ||
// test('isFile', () => { | ||
// expect(isFile(File)).toBe(false) | ||
// expect(isFile(new File([''], '', { type: 'text/html' }))).toBe(true) | ||
// }) | ||
test('Basic true tests', (t: any) => { | ||
t.is(isError(new Error('')), true) | ||
@@ -48,4 +59,4 @@ t.is(isUndefined(undefined), true) | ||
t.is(isEmptyObject({}), true) | ||
t.is(isFullObject({0: ''}), true) | ||
t.is(isFullObject({'': ''}), true) | ||
t.is(isFullObject({ 0: '' }), true) | ||
t.is(isFullObject({ '': '' }), true) | ||
t.is(isObject(new Object()), true) | ||
@@ -77,3 +88,3 @@ t.is(isArray([]), true) | ||
test('Basic false tests', t => { | ||
test('Basic false tests', (t: any) => { | ||
t.is(isError({}), false) | ||
@@ -100,11 +111,29 @@ t.is(isNumber(NaN), false) | ||
test('isFunction', t => { | ||
test('isFunction', (t: any) => { | ||
t.is(isFunction(NaN), false) | ||
t.is(isFunction(() => {}), true) | ||
t.is(isFunction(function () {}), true) | ||
t.is(isFunction(async () => {}), true) | ||
t.is(isFunction(async function () {}), true) | ||
t.is(isFunction(function * () {}), true) | ||
t.is(isFunction(async function * () {}), true) | ||
const _ = { fn: () => {}, method () {} } | ||
t.is( | ||
isFunction(() => {}), | ||
true | ||
) | ||
t.is( | ||
isFunction(function () {}), | ||
true | ||
) | ||
t.is( | ||
isFunction(async () => {}), | ||
true | ||
) | ||
t.is( | ||
isFunction(async function () {}), | ||
true | ||
) | ||
t.is( | ||
isFunction(function* () {}), | ||
true | ||
) | ||
t.is( | ||
isFunction(async function* () {}), | ||
true | ||
) | ||
const _ = { fn: () => {}, method() {} } | ||
t.is(isFunction(_.fn), true) | ||
@@ -114,3 +143,3 @@ t.is(isFunction(_.method), true) | ||
test('isEmptyObject', t => { | ||
test('isEmptyObject', (t: any) => { | ||
t.is(isEmptyObject({}), true) | ||
@@ -132,3 +161,3 @@ t.is(isEmptyObject(new Object()), true) | ||
test('isEmptyArray', t => { | ||
test('isEmptyArray', (t: any) => { | ||
t.is(isEmptyArray([]), true) | ||
@@ -151,3 +180,3 @@ t.is(isEmptyArray(new Array()), true) | ||
test('isFullArray', t => { | ||
test('isFullArray', (t: any) => { | ||
t.is(isFullArray(new Array(1)), true) | ||
@@ -173,3 +202,3 @@ t.is(isFullArray([undefined]), true) | ||
test('NaN tests', t => { | ||
test('NaN tests', (t: any) => { | ||
t.is(isNaNValue(NaN), true) | ||
@@ -207,3 +236,3 @@ t.is(isNaNValue(new Error('')), false) | ||
test('Primitive tests', t => { | ||
test('Primitive tests', (t: any) => { | ||
// true | ||
@@ -231,10 +260,10 @@ t.is(isPrimitive(0), true) | ||
test('Date exception', t => { | ||
test('Date exception', (t: any) => { | ||
t.is(isDate(new Date('_')), false) | ||
}) | ||
test('Generic isType', t => { | ||
test('Generic isType', (t: any) => { | ||
// ----------------------------- | ||
// This is correct old fashion syntax for classes, if this is missing | ||
function MyClass () {} | ||
function MyClass() {} | ||
MyClass.prototype.constructor = MyClass | ||
@@ -244,3 +273,5 @@ // @ts-ignore | ||
// ----------------------------- | ||
class MyOtherClass { constructor() {} } | ||
class MyOtherClass { | ||
constructor() {} | ||
} | ||
// this is expected behaviour | ||
@@ -281,6 +312,6 @@ t.is(isType('', String), true) | ||
test('isObject vs isAnyObject', t => { | ||
test('isObject vs isAnyObject', (t: any) => { | ||
// ----------------------------- | ||
// This is correct old fashion syntax for classes, if this is missing | ||
function MyClass () {} | ||
function MyClass() {} | ||
MyClass.prototype.constructor = MyClass | ||
@@ -290,3 +321,5 @@ // @ts-ignore | ||
// ----------------------------- | ||
class MyClass2 { constructor() {} } | ||
class MyClass2 { | ||
constructor() {} | ||
} | ||
const myClass2 = new MyClass2() | ||
@@ -336,3 +369,3 @@ const mySpecialObject = {} | ||
test('isOneOf', t => { | ||
test('isOneOf', (t: any) => { | ||
t.is(isOneOf(isString, isNumber)('_'), true) | ||
@@ -361,3 +394,3 @@ t.is(isOneOf(isString, isNumber)(1), true) | ||
test('type related tests', t => { | ||
test('type related tests', (t: any) => { | ||
t.pass() | ||
@@ -390,3 +423,2 @@ // const fn: string | ((k: number) => string) = (p) => 'a' | ||
// a[myArray[1]] = myArray[0] | ||
}) |
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"module": "ES2020", | ||
"target": "ES2019", | ||
"lib": ["ES2020", "DOM"], | ||
"strict": true, | ||
"moduleResolution": "node", | ||
"isolatedModules": true, | ||
@@ -6,0 +10,0 @@ "esModuleInterop": true, |
17
193
Yes
52084
14
1375