@kj4ezj/is
v1.0.0
Published
Extremely lightweight, zero dependency variable checks missing in nodeJS but common in other languages
Downloads
4
Readme
is.js
Extremely lightweight, zero dependency variable checks missing in nodeJS but common in other languages.
is.nullOrEmpty(input: any): boolean
is.string(input: any): boolean;Install @kj4ezj/is from NPM with your preferred package manager!
Background
In the old days™, nodeJS lacked most utilities developers take for granted in other languages. The community created libraries like lodash (commonly imported and used as _ in projects) to fill this void. However, modern node includes intrinsics that provide almost all of the functionality these large libraries were built to provide. It seems silly to import a 1.4 MB library just to test if a variable is empty.
The is.js library provides the most fundamental utilities remaining absent in modern node that I expect to have in any language, and nothing more. At the time of writing, is.js weighs in at just 461 bytes, five orders of magnitude smaller than lodash!
Usage
Install @kj4ezj/is from NPM with your preferred package manager...
bun add @kj4ezj/is
cnpm install @kj4ezj/is
npm install @kj4ezj/is
pnpm add @kj4ezj/is
yarn add @kj4ezj/is...then import it into your source code.
const is = require('@kj4ezj/is');Two utilities are provided.
is.nullOrEmpty(input: any): boolean
is.string(input: any): boolean;These are documented in the sections below, but the test cases written against expectations should be considered authoritative.
is.nullOrEmpty()
This is a simple variable emptiness check. Pass it literally anything and it will return true if it is undefined, null, or empty; and false otherwise.
is.nullOrEmpty(input: any): booleanEmptiness is considered in a practical sense, and may be slightly different than implementations in other languages.
// literals
undefined
null
[] // zero-length arrays
[[]] // zero-length multi-dimension arrays
{} // empty objects
'' // equivalent to "" or ``
' ' // strings containing only whitespace
' \n\t'
// primitive objects
Array([])
Object({})
String(' \n ')
// constructed objects
new Array([])
new Array([[]])
new Object({})
new String(' \n ')// literals
true
false
-1
0
123
0xFFFF00
0b00101010
'yeet'
[[],[]] // non-zero array length
['one', 'two', 'three']
{key: 'value'}
// primitive types
Boolean(false)
Number(0)
BigInt(81129638414606663681390495662081)
// constructed types
new Boolean(false)
new Number(0)
new String('yeet')
new Array([[],[]])
new Object({key: 'value'})
// functions, no matter the return type or contents
() => undefined
() => null
() => false
() => 0
() => ''
() => []
() => {}
() => new Number(0)
() => new String('')See the test cases written against expectations for more info, or try it in an interactive shell.
is.string()
JavaScript has two different types of strings:
- String primitives -
'',"",``,String(), andString('') - String objects -
new String()
These are two different types, even though JavaScript pretends not to have types. String primitives are fundamentally immutable literals of type string, while string objects are fundamentally type Object of class String.
[!TIP]
JavaScript hides this from you using autoboxing. When you access a property or method on a string primitive, JavaScript temporarily converts (or "boxes") the string primitive into aStringobject. This allows the string primitive to access the properties and methods available onString.prototype. Once the property or method is accessed, the temporaryStringobject is discarded, and the original string primitive remains unchanged.
This makes it complicated for programmers to determine if a variable is a string because String objects are not strings.
Most of the time we do not care about the internal mechanics of the language, we just want to know if a variable contains a string in a practical sense.
is.string() // false
is.string(undefined) // false
is.string(null) // false
is.string('') // TRUE
is.string(' ') // TRUE
is.string('yeet') // TRUE
is.string(' \n\t') // TRUE
is.string([]) // false
is.string({}) // false
is.string(String('')) // TRUE
is.string(Array([])) // false
is.string(Array(['yeet'])) // false
is.string(Object({})) // false
is.string(Object({key: 'value'})) // false
is.string(new String('')) // TRUE
is.string(new Array([])) // false
is.string(new Array(['yeet'])) // false
is.string(new Object({})) // false
is.string(new Object({key: 'value'})) // false
is.string(() => undefined) // false
is.string(() => null) // false
is.string(() => '') // false
is.string(() => new String('')) // false
is.string(() => new String('yeet')) // falseSee the test cases written against expectations for more info, or try it in an interactive shell.
Development
Start here to contribute to this repo.
[!NOTE]
The source of truth for the version of nodeJS this project uses is the.nvmrcfile. As a utility, as many versions ofnodeare supported as possible on a best-effort basis. Check out thenode-versionkey in theci.ymlto see which versions are being tested.
Prerequisites
Contributors will need the following tools:
- act
- docker - required by
act- Docker Desktop is not required, you only need the free Docker Engine.
- docker - required by
- nvm
- nodeJS
Installnodeusingnvm. In the root of this repo:
This will automagically install and use the correct version ofnvm installnodefor this project, as defined in the.nvmrcfile. - yarn version 1
The easiest way to install this is usingnpm, which is installed withnodebynvm.npm install --global yarn
These tools are all you need to get started!
Initialization
Once you have the prerequisites installed, you can get going by navigating to the root of this repo, making sure nvm is using the correct version of nodeJS...
nvm install...then downloading all project dependencies.
yarnEasy.
Lint
This project uses eslint with customizations on top of the airbnb-base config to perform static code analysis.
yarn lintThe purpose of linting is to catch bugs early, not to create unnecessary friction, so many rules which will not realistically catch bugs are disabled.
Test
This project uses the jest test framework.
yarn testThe goal is full test coverage, not to chase a number but to exhaustively test all expectations.
Build
This is how release artifacts are generated and, in CI, published.
yarn buildThe "build" command calls scripts/build.sh, which packs build metadata into the package.json under the top-level git key and calls npm pack to generate a *.tgz file for distribution. If this script is called in a CI environment then it will continue on to install the newly generated package to validate it can be installed, per NPM's instructions. In a tagged build, it will verify that the tag matches the version string in the package.json and publish it to NPM with provenance.
Upgrade
[!CAUTION] ESLint 9 introduced numerous breaking changes including dropping support for node 14, 16, breaking eslint-config-airbnb-base, and breaking my configuration.
Upgrade dependencies in the yarn.lock only.
yarn upgradeUpgrade both the yarn.lock and the package.json.
yarn upgrade --latestRun yarn act after either to verify the upgraded dependencies work with the project on all supported nodeJS versions.
Reset
This project contains a script to sanitize the project's node environment.
[!WARNING]
This will delete build artifacts!
yarn resetThis makes it easy to switch between node major versions cleanly.
CI
This project uses GitHub Actions for CI.
- is.js CICD - initialize, lint, and test, and publish the
is.jsproject.
You can run the GitHub Actions workflow(s) locally using act.
yarn actPlease make sure your changes do not break act compatibility.
See Also
- CI
- Tooling
Legal Notice
This repo contains assets created in collaboration with a large language model, machine learning algorithm, or weak artificial intelligence (AI). This notice is required in some countries.
