Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript strict type checking functions
export {
isNull,
isUndef,
isBool,
isNum,
isInt,
isStr,
isArr,
isObj,
isFunc,
isSymbol
}
/**
* Does not match: `undefined`
*/
function isNull(val) {
return val === null
}
/**
* Does not match: `null`
*/
function isUndef(val) {
return typeof val === 'undefined'
}
/**
*/
function isBool(val) {
return typeof val === 'boolean'
}
/**
* Does not match: `NaN`, `Infinity`, `-Infinity`
*/
function isNum(val) {
return typeof val === 'number' && isFinite(val)
}
/**
* Does not match: `NaN`, `Infinity`, `-Infinity`
*/
function isInt(val) {
return typeof val === 'number' && isFinite(val) && Math.floor(val) === val
}
/**
*/
function isStr(val) {
return typeof val === 'string'
}
/**
*/
function isArr(val) {
return Array.isArray(val)
}
/**
* Does not match: arrays, functions, symbols
*/
function isObj(val) {
return val === Object(val) && !isArr(val) && !isFunc(val) && !isSymbol(val)
}
/**
*/
function isFunc(val) {
return typeof val === 'function'
}
/**
*/
function isSymbol(val) {
return typeof val === 'symbol'
}
import * as validations from './validations'
const types = [
undefined,
null,
// booleans:
false,
true,
Boolean(),
// numbers:
0,
1,
1.01,
0.30000000000000004, // 0.1 + 0.2
0xbada55, // base 16: hex
0o2471, // base 8: octal
Number.MAX_SAFE_INTEGER,
NaN,
Infinity,
-Infinity,
// strings:
'',
String(),
'latin',
'日本語',
// arrays:
[],
Array(3), // [ <3 empty items> ]
[null, undefined],
new class A extends Array {}(), // []
// objects:
{},
{ answer: 42 },
new class A {}(),
Object(),
Object.prototype,
Object.create(null),
// symbols:
Symbol(),
Symbol('label'),
// functions:
// The last 5 items should be functions, see: isFunc()
() => 0,
function named() {},
Function(),
parseInt,
Symbol // function
]
describe('validations', () => {
it('isNull()', () => {
const fn = validations.isNull
const valid = [null]
expect(types.filter(fn)).toStrictEqual(valid)
})
it('isUndef()', () => {
const fn = validations.isUndef
const valid = [undefined]
expect(types.filter(fn)).toStrictEqual(valid)
})
it('isBool()', () => {
const fn = validations.isBool
const valid = [false, true, false]
expect(types.filter(fn)).toStrictEqual(valid)
})
it('isNum()', () => {
const fn = validations.isNum
const valid = [
0,
1,
1.01,
0.30000000000000004,
12245589,
1337,
Number.MAX_SAFE_INTEGER
]
expect(types.filter(fn)).toStrictEqual(valid)
})
it('isInt()', () => {
const fn = validations.isInt
const valid = [0, 1, 12245589, 1337, Number.MAX_SAFE_INTEGER]
expect(types.filter(fn)).toStrictEqual(valid)
})
it('isStr()', () => {
const fn = validations.isStr
const valid = ['', '', 'latin', '日本語']
expect(types.filter(fn)).toStrictEqual(valid)
})
it('isArr()', () => {
const fn = validations.isArr
const valid = [[], Array(3), [null, undefined], []]
expect(types.filter(fn)).toEqual(valid)
})
it('isObj()', () => {
const fn = validations.isObj
const valid = [{}, { answer: 42 }, {}, {}, {}, {}]
expect(types.filter(fn)).toEqual(valid)
})
it('isFunc()', () => {
const fn = validations.isFunc
const valid = types.slice(-5)
expect(types.filter(fn)).toStrictEqual(valid)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.