jis
v2.2.0
Published
When you need validate the variable data type
Maintainers
Readme
Jis
jis is a lightweight runtime data type verifier designed for browsers and environments where static typing or modern language features may not be reliable or available.
Based on Object.prototype.toString.call, the library provides a set of methods to perform reliable runtime data type checks.
Why jis?
jis comes from is, the core of the library, prefixed with j (my initial).
Phonetically, it sounds like “ji-is”, a subtle nod to JS itself.
In the browser, everything runs as JavaScript at runtime. Static types disappear after transpilation, and runtime type checks are often limited, inconsistent, or dependent on modern language features that are not always available.
jis focuses on providing simple, reliable runtime is checks using the most stable and widely supported mechanisms in JavaScript.
Installation
You can install this library with npm or yarn.
npm install jisUsage
Import in ES5 or higher
const jis = require('jis')Import in TS
import jis from 'jis'On browser
<script src="https://unpkg.com/jis@latest"></script>
<!-- or
<script src="https://cdn.jsdelivr.net/npm/jis@latest"></script>
-->
<script>
// jis is exposed globally as window.jis
var is = jis.types.is;
console.log(is(new Date(), 'Date'))
</script>You can also pin a specific version (≥2.2.0-preview.1), e.g.:
- jis@preview
- jis@rc
- [email protected]
- [email protected]
- [email protected]
⚠️ Backward compatibility
In v2.x, legacy access via jis.is and jis.$* still works for compatibility reasons, but it is deprecated and will be removed in v3.0.0.
If you are starting a new project or updating existing code, use the new namespaces shown below.
// Deprecated
jis.is(arg, type);
jis.$object(arg);
jis.$array(arg);
jis.$number(arg);
jis.$string(arg);
jis.$boolean(arg);
jis.$undefined(arg);
jis.$function(arg);
jis.$null(arg);
jis.$empty(arg);
jis.$numeric(arg);
jis.$primitive(arg);Use the new namespaces instead:
jis.types.is(arg, type);
jis.types.$object(arg);
jis.types.$array(arg);
jis.types.$number(arg);
jis.types.$string(arg);
jis.types.$boolean(arg);
jis.types.$undefined(arg);
jis.types.$function(arg);
jis.types.$null(arg);
jis.semantic.$empty(arg);
jis.semantic.$numeric(arg);
jis.semantic.$primitive(arg);API
Type checks (jis.types)
$array
const { $array } = jis.types // jis.types.$array
$array( [] ) // true
$array( true ) // false
$array( function() {} ) // false
$array( null ) // false
$array( 12 ) // false
$array( {} ) // false
$array( '' ) // false
$array( undefined ) // false$boolean
const { $boolean } = jis.types
$boolean( true ) // true
$boolean( false ) // true
$boolean( [] ) // false
$boolean( function() {} ) // false
// ...$function
const { $function } = jis.types
$function( function() {} ) // true
$function( 12 ) // false
$function( {} ) // false
$function( '' ) // false
// ...$null
const { $null } = jis.types
$null( null ) // true
$null( {} ) // false
$null( '' ) // false
$null( undefined ) // false
// ...$number
const { $number } = jis.types
$number( 12 ) // true
$number( function() {} ) // false
$number( null ) // false
$number( undefined ) // false
// ...$object
const { $object } = jis.types
$object( {} ) // true
$object( [] ) // false
$object( true ) // false
$object( 12 ) // false
// ...$string
const { $string } = jis.types
$string( 'Some text' ) // true
$string( [] ) // false
$string( true ) // false
$string( undefined ) // false
// ...$undefined
const { $undefined } = jis.types
$undefined( undefined ) // true
$undefined( [] ) // false
$undefined( {} ) // false
$undefined( '' ) // false
// ...is
This method is the core of the library and provides more flexible and advanced type checks.
const { is } = jis.types
is( [], 'Array' ) // true
is( false, 'Boolean' ) // true
is( true, 'Boolean' ) // true
is( function(){}, 'Function' ) // true
is( null, 'Null' ) // true
is( 12, 'Number' ) // true
is( {}, 'Object' ) // true
is( 'Text', 'String' ) // true
is( undefined, 'Undefined' ) // true
let date = new Date();
is(date, Date) // true
is(/^$/g, RegExp) // true
is(/^$/g, 'RegExp') // true
is(12, 12) // true
is(12, 13) // false
// ... experiment with this method :DSemantic checks (jis.semantic)
$numeric
Check if the argument is a number or number string (including exponential)
const { $numeric } = jis.semantic // jis.semantic.$numeric
$numeric( 12 ) // true
$numeric( '12' ) // true
$numeric( '-12' ) // true
$numeric( '+12' ) // true
$numeric( '12.' ) // true
$numeric( '12.e5' ) // true
$numeric( '12.E5' ) // true
$numeric( '12.E-5' ) // true
$numeric( '-12.E-5' ) // true
$numeric( '+12.E-5' ) // true
$numeric( '12.E-' ) // false
$numeric( 'A3B' ) // false
$numeric( undefined ) // false
$numeric( null ) // false$primitive
Check if the argument is a primitive value
const { $primitive } = jis.semantic
$primitive(undefined) // true
$primitive(null) // true
$primitive("something") // true
$primitive(true) // true
$primitive(false) // true
$primitive(12) // true
$primitive(Symbol()) // true
$primitive({}) // false
$primitive([]) // false
$primitive(new Date()) // false$empty
$empty follows a semantic definition of emptiness, where certain falsy or zero-like values are intentionally considered empty.
const { $empty } = jis.semantic
$empty(null) // true
$empty(undefined) // true
$empty(false) // true
$empty(0) // true
$empty(0.0) // true
$empty("") // true
$empty("0") // true
$empty([]) // true
$empty(true) // false
$empty(12) // false
$empty(12.0) // false
$empty("something") // false
$empty("012") // false
$empty([1, 2, 3]) // falseChangelog
See CHANGELOG.md for version history and breaking changes.
