exhelper-js
Install
npm install exhelper-js
// CommonJS
const ex = require('exhelper-js').default
// ESModules
import ex from 'exhelper-js'
Documentation
?:
ex['?:']<S, T>(value1: S, value2: T): S | T
value1 || value2
の結果を返します。
Example
ex['?:']('foo', 'bar') // => 'foo'
ex['?:'](undefined, 'bar') // => 'bar'
??
ex['??']<S, T>(value1: S, value2: T): T | NonNullable<S>
value1 ?? value2
(Nullish coalescing operator) の結果を返します。
Example
ex['??']('foo', 'bar') // => 'foo'
ex['??'](undefined, 'bar') // => 'bar'
ex['??'](null, 'bar') // => 'bar'
<=>
ex['<=>'](value1: number, value2: number): 1 | -1 | 0
value1
が value2
より大きければ 1
, value2
が value1
より大きければ -1
, それ以外の場合は 0
を返します。
Example
ex['<=>'](2, 1) // => 1
ex['<=>'](1, 2) // => -1
ex['<=>'](1, 1) // => 0
isObject
ex.isObject(arg: unknown): boolean
arg
がオブジェクトなら true
, そうでない場合は false
を返します。
Example
ex.isObject({ a: 1 }) // => true
ex.isObject({}) // => true
ex.isObject([1, 2, 3]) // => false
ex.isObject(null) // => false
typeof
ex.typeof(arg: unknown): 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function' | 'array' | 'null'
arg
が配列の場合は 'array'
, null
の場合は 'null'
, それ以外の場合は typeof arg
の結果を返します。
Example
ex.typeof([1, 2, 3]) // => 'array'
ex.typeof(null) // => 'null'
ex.typeof('foo') // => 'string'
length
ex.length(arg: unknown): number | undefined
arg
が文字列か配列の場合は arg.length
, arg
がオブジェクトの場合は Object.keys(arg).length
, それ以外の場合は undefined
を返します。
Example
ex.length([1, 2, 3]) // => 3
ex.length({ a: 1, b: 2 }) // => 2
sum
ex.sum(nums: number[]): number
数値の配列の全要素の和を返します。
Example
ex.sum([1, 2, 3, 4]) // => 10
product
ex.product(nums: number[]): number
数値の配列の全要素の積を返します。
Example
ex.product([1, 2, 3, 4]) // => 24