2.8.1 • Published 3 years ago

@honeo/check v2.8.1

Weekly downloads
232
License
MIT
Repository
github
Last release
3 years ago

check

なにこれ

型・インスタンス等をチェックするやつ。

使い方

$ npm i @honeo/check
import {is, not, any} from '@honeo/check';

is.arr([]); // true

is.arr([], {}); // false
not.arr([], {}); // false
any.arr([], {}); // true


// single
import is from '@honeo/check/is.mjs'; // or not.mjs, any.mjs

// default export
import check from '@honeo/check';
check.is.foobar();
// webpack v4~ webpack.config.js
{
	node: {
		fs: 'empty'
	}
}

API

is, not, any共用。

Alias

is.FooBar===is.foobar; // true

Type, Instance

is.array([]); // true
is.arr([], []); // true

is.boolean(false); // true
is.bool(true, false); // true

is.buffer(new Buffer('foobar')); // true
is.arraybuffer( new ArrayBuffer(0)); // true

is.error(new Error('hoge')); // true

is.function(function(){}); // true
is.func(()=>{}); // true
is.fn(_=>_); // true

is.asyncfunction(async()=>{}); // true
is.asyncgeneratorfunction(async function*(){}); // true
is.generatorfunction(function*(){}); // true

is.number(1); // true
is.num(0, 1); // true

is.regexp(/hoge/); // true
is.re(/foo/, /bar/); // true

is.string('hoge'); // true
is.str('fuga', 'piyo'); // true

is.undefined(undefined); // true
is.undef(null); // false

is.null(null); // true

is.nan(NaN); // true

is.date(new Date()); // true

is.object({}); // true
is.obj(null); // false

is.promise(new Promise(_=>_)); // true

is.stats( fs.statSync('./') ); // true

is.abortcontroller(new AbortController() ); // true
is.abortsignal(new AbortController().signal); // true
nametypevarargsdescription
Array, Arr, array, arrany
Boolean, Bool, boolean, boolany
Buffer, Buf, buffer, bufany
Error, Err, error, errany
Function, Func, Fn, function, func, fnany
AsyncFunction, asyncfunctionany
AsyncGeneratorFunction, asyncgeneratorfunctionany
GeneratorFunction, generatorfunctionany
Number, Num, number, numany
RegExp, RE, regexp, reany
String, Str, string, strany
Undefined, Undef, undefined, undefany
Null, nullany
NaN, nanany
Date, dateany
Object, Obj, object, objany
Promise, promiseany
Stats, statsany
ArrayBuffer, ArrBuf, arraybuffer, arrbufany
AbortController, abortcontrolleranyAbortControllerインスタンスか(Polyfill対応)。
AbortSignal, abortsignalanyAbortSignalインスタンスか(Polyfill対応)。

Number

is.even(2); // true

is.odd(3); // true

is.multiple(8080, 80); // true
nametypevarargsdescription
Odd, oddnumber引数がすべて奇数か。
Even, evennumber引数がすべて偶数か。
Multiple, multiplenumber引数1が引数2の倍数か。

String

is.ipv4('192.168.1.1'); // true

is.hostname('www.example.com'); // true

is.lowercase('hoge'); // true

is.uppercase('FOO', 'BAR'); // true
nametypevarargsdescription
IPv4, ipv4string
Hostname, hostnamestringやっつけ実装。
Lowercase, lowercasestring
Uppercase, uppercasestring

DOM

is.node(document.body, document.createTextNode('hoge')); // true

is.textnode(document.createTextNode('hoge')); // true

is.element(document.head, document.body); // true

is.df(document.createDocumentFragment()); // true

is.event( new Event("hoge") ); // true

is.eventtarget(document, window); // true
nametypevarargsdescription
Node, nodeany
TextNode, textnodeany
Element, Elm, element, elmany
DocumentFragment, DF, documentfragment, dfany
Event, eventany
EventTarget, eventtargetany

その他

is.true(true, !0); // true

is.false(false !1); // true

is.truthy(true, "hoge", 1, [], {}); // true

is.falsy(null, undefined, "", 0, NaN); // true

is.instance([], {}); // true
is.instance("hoge"); // false

is.instanceof(new Date(), Date); //true

is.objectliteral({}); // true
not.objectliteral([], new function(){}); // true

is.arraylike([], 'hoge'); // true

is.comparisonoperator('<='); // true

is.sameDay(new Date(), new Date()); // true

is.semver('1.2.3'); // true
is.semver('1.0.0-foo.bar'); // true

is.version('7.7.4', '2.5.0.1') // true
is.version('1.2A', 1.0); // false

is.empty('', [], {}) // true
is.empty(0, null); // false

is.leapyear(2020, new Date('2024'));

is.validdate(2019, 4, 17); // true
is.validdate(2020, 12, 32); // false
nametypevarargsdescription
ArrayLike(), arraylike()any
True(), true()any
False(), false()any
Truthy(), truthy()any
Falsy(), falsy()any
Empty(), empty()any要素が空か。
Instance(), instance()any何らかのインスタンスであるオブジェクトか。
Instanceof(), instanceof()any引数1が引数2のConstructor/Classのインスタンスか。
Nullish(), nullish()anynull or undefined
ObjectLiteral(), objectliteral()any未継承の素のオブジェクトか。
ComparisonOperator(), comparisonoperator()any有効な比較演算子の文字列か。
SameDay(), sameday()date同じ日か
SemVer(), semver()any有効なSemVer文字列か。
Version(), Ver(), versiom(), ver()any有効な数字, dotのバージョン文字列か。
LeapYear(), leapyear()number, date閏年か。
ValidDate(year, mon, day), validdate()number存在する日付か。

Breaking Changes

v2.0.0

CommonJS => ES Modules

// before CJS
const {is, not, any} = require('@honeo/check');


// after ESM
import {is, not, any} from '@honeo/check';

rename: instance() => instanceof()

// before
is.instance([], Array);

// after
is.instanceof([], Array);
2.8.1

3 years ago

2.8.0

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.3.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.9.2

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.2

7 years ago

1.0.1

7 years ago