![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
js-heuristics
Advanced tools
Useful heuristics, type checks, and validation helpers for JavaScript
js-heuristics
is a library of useful heuristics, type checks, and validation helpers for JavaScript. Instead of repeatedly checking types, evaluating whether or not an API response is null (or indeed an object, only entirely empty), you can depend on this tested, consistent library API to get the job done.
using this lib
heuristics
currently supports UMD, CommonJS (node versions >= 10), and ESM build-targets. Is your preferred build not supported? Open an issue!
npm install js-heuristics
# OR
yarn add js-heuristics
Commonjs:
const { isObject } = require('js-heuristics');
console.log(isObject({})); // true
ESM:
import { isObject } from 'js-heuristics';
evaluate whether the provided argument is a function
Example
import { isFunction } from 'js-heuristics';
var fn = () => ({});
var t = isFunction(fn);
console.log(t); // true
evaluate whether the provided argument is a generator function
Example
import { isGenerator } from 'js-heuristics';
var gen = function* () { yield true; };
var t = isGenerator(gen);
console.log(t); // true
evaluate whether the provided argument is an async function
Example
import { isAsyncFunction } from 'js-heuristics';
var fn = async function () { ... };
var t = isAsyncFunction(fn);
console.log(t); // true
evaluate whether the provided argument is an anonymous function
Example
import { isAnonymousFunction } from 'js-heuristics';
var fn = function () { ... };
var t = isAnonymousFunction(fn);
console.log(t); // true
evaluate whether the provided argument is a named, synchronous function
Example
import { isRegularFunction } from 'js-heuristics';
var fn = function name () { ... };
var t = isRegularFunction(fn);
console.log(t); // true
evaluate whether the provided argument is a string
Example
import { isFunction } from 'js-heuristics';
var fn = () => ({});
var t = isFunction(fn);
console.log(t); // true
evaluate whether the provided argument is a Boolean
Example
import { isBoolean } from 'js-heuristics';
...
if (isBoolean(true)) // true
evaluate whether the provided argument is an Error object
Example
import { isError } from 'js-heuristics';
...
var result = await fetchData();
...
if (isError(result)) this.error = true;
else this.data = result.data;
evaluate whether the provided argument is an object
Example
import { isObject } from 'js-heuristics';
const r = corneliusCardewIsDaBeezKnees();
if (isObject(r)) ...
evaluate whether the provided argument is an array
Example
import { isArray } from 'js-heuristics';
var notAnArr = '';
console.log(isArray(notAnArr)); // false
evaluate whether the provided argument is a number
Note Will return false for NaN and single element Arrays (see: toString
gotchas)
Example
import { isNumber } from 'js-heuristics';
console.log(isNumber(9)); // true
console.log(isNumber(NaN)); // false
evaluate whether the provided argument is a floating point number
Example
import { isFloat } from 'js-heuristics';
console.log(isFloat(9.1)); // true
console.log(isFloat(1)); // false
convert any expression or value to a negated boolean
Example
import { not } from 'js-heuristics';
if (not(obj)) ...
if (not(bool)) ...
if (not(expr)) ...
evaluate whether the provided argument is not empty Note Will return undefined for non array, object, or string arguments
Example
import { notEmpty } from 'js-heuristics';
if (notEmpty(obj)) ...
evaluate whether the provided object is not empty (no keys)
Example
import { objNotEmpty } from 'js-heuristics';
if (objNotEmpty(obj)) ...
evaluate whether the provided object is not empty, no matter how nested
Note Object's values are not null, NaN, or undefined
Example
import { objNotEmptyDeep } from 'js-heuristics';
var o = {
a: {
b: {
c: {
d: 1
}
}
}
}
if (objNotEmptyDeep(o)) ... // true
Explicitly determine if given value is not null or undefined
Example
import { notNullOrUndefined } from 'js-heuristics';
if (notNullOrUndefined(o)) ...
Determine if a property does not exist on an object or its prototype chain
Example
import { notInPrototype } from 'js-heuristics';
var proto = { foo: 'foo' };
var obj = Object.create(proto);
if (notInPrototype(obj, 'foo')) ... // false
if (notInPrototype(obj, 'bar')) ... // true
Generate a predicate-bound contract; either returns true or throws a violation
Example
import { contract, isObject } from 'js-heuristics';
const mustBeObject = contract(isObject);
var o = {};
var a = [];
if (mustBeObject(o)) ... // true
if (mustBeObject(a)) ... // throws
const contractWithMessage = contract(isObject, 'Must be an object');
try {
contractWithMessage('str');
} catch ({ message }) {
console.log(message); // 'Must be an object'
}
Generate a reducer that enforces all provided predicates on a given argument
Example
import { testForEach, isObject } from 'js-heuristics';
function hasData () { ... }
const isApiData = testForEach(isObject, hasData);
if (isApiData(response)) ...
Generate an iterable range
Example
import { range } from 'js-heuristics';
const enumValue = 10;
const enumChar = 'E';
if (enumValue in range(1, 20)) ... // true
if (enumValue in range(1, 5)) ... // false
if (enumChar in range('A', 'z')) ... // true
if (enumChar in range('a', 'd')) ... // false
FAQs
Useful heuristics, type checks, and validation helpers for JavaScript
The npm package js-heuristics receives a total of 4 weekly downloads. As such, js-heuristics popularity was classified as not popular.
We found that js-heuristics demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.