# javascript-interface-library

> various classification, validation and utility functions

Latest version **1.1.3** (published 2026-07-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install javascript-interface-library
pnpm add javascript-interface-library
yarn add javascript-interface-library
bun add javascript-interface-library
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2026-07-13 |
| First published | 2021-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 251.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 1 |
| Author | Andreas Rozek |
| Maintainers | arozek |
| Keywords | javascript, typescript, type-checking, input-validation, utility-functions |

## Links

- npm: https://www.npmjs.com/package/javascript-interface-library
- Repository: https://github.com/rozek/javascript-interface-library
- Homepage: https://github.com/rozek/javascript-interface-library#readme
- Issues: https://github.com/rozek/javascript-interface-library/issues
- npm.io page: https://npm.io/package/javascript-interface-library

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.1.3 (latest) — 2026-07-13
- 1.1.2 — 2026-07-12
- 1.1.1 — 2026-07-11
- 1.1.0 — 2026-07-03
- 1.0.9 — 2026-07-03
- 1.0.8 — 2024-09-11
- 1.0.7 — 2024-07-19
- 1.0.6 — 2024-05-20
- 1.0.5 — 2023-11-24
- 1.0.4 — 2023-11-24
- 1.0.3 — 2022-11-24
- 1.0.2 — 2022-11-24
- 1.0.1 — 2022-02-16
- 1.0.0 — 2021-08-01
- 0.1.14 — 2021-07-21
- … 14 more at https://npm.io/package/javascript-interface-library/versions

## README

# javascript-interface-library #

[![CI](https://github.com/rozek/javascript-interface-library/actions/workflows/ci.yml/badge.svg)](https://github.com/rozek/javascript-interface-library/actions/workflows/ci.yml)

various classification, validation and utility functions for JavaScript and TypeScript

From time to time, it's necessary to classify and/or validate the values of user inputs, data read from input streams (like files or network connections) or arguments passed as part of a function call. While TypeScript type annotations already eliminate the need for many of these tests, there still exist lots of interfaces to the outer (non-TypeScript) world where value checking remains important.

These situations are, what the `javascript-interface-library` has been made for.

**NPM users**: please consider the [Github README](https://github.com/rozek/javascript-interface-library/blob/main/README.md) for the latest description of this package (as updating the docs would otherwise always require a new NPM package version)

## Installation ##

Since v1.1.0, `javascript-interface-library` is a pure ECMAScript module (ESM) - UMD, CommonJS and AMD builds (and the global variable `JIL`) are no longer provided.

You may either install the package into your build environment using [NPM](https://docs.npmjs.com/) with the command

```
npm install javascript-interface-library
```

and bundle it with your application - in that case, no code needs to be loaded from any third party at runtime.

For buildless setups, it is recommended to **host the module yourself**: simply download the ready-made file [javascript-interface-library.esm.js](https://raw.githubusercontent.com/rozek/javascript-interface-library/main/dist/javascript-interface-library.esm.js) (a single ESM file without any dependencies) and serve it from your own web server:

```html
<script type="module">
  import { ValueIsOrdinal } from '/js/javascript-interface-library.esm.js'
</script>
```

Serving the file from your own origin keeps your visitors' IP addresses away from third-party servers - which **may be relevant for GDPR compliance**: loading assets from public CDNs (such as unpkg, jsDelivr or cdnjs) or other third-party hosts discloses visitor IPs to those parties and may require consent. For quick experiments, however, importing the module directly is still the fastest way to get started:

```html
<script type="module">
  import { ValueIsOrdinal } from 'https://rozek.github.io/javascript-interface-library/dist/javascript-interface-library.esm.js'
</script>
```

(please keep in mind that the latter also applies to imports from `rozek.github.io` - GitHub Pages is a third-party host as well)

## Access ##

Import the functions and values you actually need

```javascript
import { ValueIsListSatisfying, ValueIsOrdinal } from 'javascript-interface-library'
```

or import the complete module as a namespace

```javascript
import * as JIL from 'javascript-interface-library'
```

All module functions and values are exported individually, thus allowing your bundler to perform some "tree-shaking" in order to include actually used functions or values (together with their dependencies) only. The package also declares itself free of side effects (`"sideEffects": false` in its `package.json`), so bundlers may safely drop any unused parts of it.


## Usage within Svelte ##

For Svelte, it is recommended to import the package in a module context. From then on, its exports may be used as usual:

```html
<script context="module">
  import { ValueIsListSatisfying, ValueIsOrdinal } from 'javascript-interface-library'
</script>

<script>
  console.log(ValueIsListSatisfying(
    [1,2,3,4], ValueIsOrdinal, 1,10
  ))
</script>
```

## Usage as ECMAScript Module ##

```javascript
import * as JIL from 'javascript-interface-library'

console.log(JIL.ValueIsListSatisfying(
  [1,2,3,4], JIL.ValueIsOrdinal, 1,10
))
```

## API Reference ##

As shown above, the individual functions and values may either be accessed directly (when used as an ESM) or by prefixing them with their namespace `JIL` (in all other cases). The following documentation lists all module contents without namespace prefix only, and the shown function signatures are those used by TypeScript.

### Object Functions ###

The JavaScript `Object` class provides a few useful functions (or "static methods") for inspecting or converting a given object. Unfortunately, these functions are often used without prior checking whether the given target object actually inherits from the `Object` protoype or was built using `Object.create(null)` - and will fail whenever such a "vanilla" object is given.

`JIL` therefore contains the following functions which mimic their counterparts from the `Object` class, but succeed even if the given target object is "vanilla".

* **`Object_hasOwnProperty (Value:Object, PropertyName:string):boolean`**<br>returns `true` if the given `Value` contains a property with the name `PropertyName` as its own property - or `false` otherwise. This function mimics the JavaScript method [Object.hasOwnProperty](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty)
* **`Object_isPrototypeOf (Value:Object, Candidate:any):boolean`**<br>returns `true` if the given `Value` exists in the prototype chain of a given `Candidate` - or `false` otherwise. This function mimics the JavaScript method [Object.isPrototypeOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isPrototypeOf)
* **`Object_propertyIsEnumerable (Value:Object, PropertyName:string):boolean`**<br>returns `true` if the given `Value` contains a property with the name `PropertyName` as its own property and that one is enumerable - or `false` otherwise. This function mimics the JavaScript method [Object.propertyIsEnumerable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable)
* **`Object_toString (Value:Object):string`**<br>returns a string which represents the given `Value`. This function mimics the JavaScript method [Object.toString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString)
* **`Object_toLocaleString (Value:Object):string`**<br>returns a locale-specific string which represents the given `Value`. This function mimics the JavaScript method [Object.toLocaleString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString)
* **`Object_valueOf (Value:Object):any`**<br>returns the primitive value of the given `Value` object. This function mimics the JavaScript method [Object.valueOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf)

### Value Classification Functions ###

The following functions check whether a given argument satisfies a certain constraint (e.g., belongs to a certain category) and return either `true` (if the constrain is met) or false otherwise.

* **`ValueExists (Value:any):boolean`**<br>returns `true` if the given `Value` exists, i.e., if it differs from both `null` and `undefined` - or `false` otherwise
* **`ValueIsMissing (Value:any):boolean`**<br>returns `true` if the given `Value` is either `null` or `undefined` - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsBoolean (Value:any):boolean`**<br>returns `true` if the given `Value` is either a primitive boolean value or an instance of `Boolean` - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsNumber (Value:any):boolean`**<br>returns `true` if the given `Value` is either a primitive numeric value or an instance of `Number` - or `false` otherwise
* **`ValueIsFiniteNumber (Value:any):boolean`**<br>returns `true` if the given `Value` is a finite number, i.e. a number which is not `NaN` and whose value is greater than negative and smaller than positive infinity - or `false` otherwise
* **`ValueIsNaN (Value:any):boolean`**<br>returns `true` if the given `Value` is `NaN` - or `false` otherwise
* **`ValueIsNumberInRange (Value:any, minValue?:number, maxValue?:number, withMin:boolean = true, withMax:boolean = true):boolean`**<br>returns `true` if the given `Value` is a number whose value is within the range given by `minValue` and `maxValue` - or `false` otherwise. `minValue` is optional and defaults to negative infinity, `maxValue` is also optional but defaults to positive infinity. When `true`, `withMin` indicates that `Value` may also be *equal* to the lower end of the given range, otherwise it must just be *greater than* the lower limit. When `true`, `withMax` indicates that `Value` may also be *equal* to the upper end of the given range, otherwise it must just be *lower than* the upper limit
* **`ValueIsInteger (Value:any):boolean`**<br>returns `true` if the given `Value` is a whole number - or `false` otherwise
* **`ValueIsIntegerInRange (Value:any, minValue?:number, maxValue?:number):boolean`**<br>returns `true` if the given `Value` is a whole number whose value is within the range given by `minValue` and `maxValue` - or `false` otherwise. `minValue` is optional and defaults to negative infinity, `maxValue` is also optional but defaults to positive infinity
* **`ValueIsOrdinal (Value:any):boolean`**<br>returns `true` if the given `Value` is a whole number greater than or equal to zero - or `false` otherwise
* **`ValueIsCardinal (Value:any):boolean`**<br>returns `true` if the given `Value` is a whole number greater than or equal to one - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsString (Value:any):boolean`**<br>returns `true` if the given `Value` is either a primitive literal value or an instance of `String` - or `false` otherwise
* **`ValueIsEmptyString (Value:any):boolean`**<br>returns `true` if the given `Value` is a string without any characters or with some content that consists of white-space characters only - or `false` otherwise
* **`ValueIsNonEmptyString (Value:any):boolean`**<br>returns `true` if the given `Value` is a string with some content that does not just consist of white-space characters - or `false` otherwise
* **`ValueIsStringMatching (Value:any, Pattern:RegExp):boolean`**<br>returns `true` if the given `Value` is a string whose content matches the given regular expression `Pattern` - or `false` otherwise
* **`ValueIsText (Value:any):boolean`**<br>returns `true` if the given `Value` is a string containing "ordinary" text only (i.e., a string which lacks any kind of control characters except \\n or \\r) - or `false` otherwise
* **`ValueIsTextline (Value:any):boolean`**<br>returns `true` if the given `Value` is a string containing a single line of "ordinary" text only (i.e., a string which lacks any kind of control characters) - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsFunction (Value:any):boolean`**<br>returns `true` if the given `Value` is a JavaScript function - or `false` otherwise
* **`ValueIsAnonymousFunction (Value:unknown):boolean`**<br>returns `true` if the given `Value` is an anonymous JavaScript function (i.e., a function with an empty `name` property - please note that, since ES2015, JavaScript infers function names, such that `const f = () => {}` is *not* anonymous) - or `false` otherwise
* **`ValueIsNamedFunction (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a "named" JavaScript function (i.e., a function with a non-empty `name` property) - or `false` otherwise
* **`ValueIsNativeFunction (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a native JavaScript function - or `false` otherwise. "Bound" functions (created with `Function.prototype.bind`) do not count as "native"
* **`ValueIsScriptedFunction (Value:any):boolean`**<br>returns `true` if the given `Value` is a scripted JavaScript function - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsObject (Value:any):boolean`**<br>returns `true` if the given `Value` is a JavaScript object (and not `null`) - or `false` otherwise
* **`ValueIsPlainObject (Value:any):boolean`**<br>returns `true` if the given `Value` is a JavaScript object (different from `null`) which directly inherits from `Object` (such as a Javascript object literal) - or `false` otherwise
* **`ValueIsVanillaObject (Value:any):boolean`**<br>returns `true` if the given `Value` is a JavaScript object which has been built using `Object.create(null)` - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsArray (Value:any):boolean`**<br>returns `true` if the given `Value` is an `Array` instance - or `false` otherwise
* **`ValueIsList (Value:any, minLength?:number, maxLength?:number):boolean`**<br>returns `true` if the given `Value` is a "dense" JavaScript array (i.e., an array whose indices 0...n-1 all exist, where n is the `length` of the given array) - or `false` otherwise
* **`ValueIsListSatisfying (Value:any, Validator:Function, minLength?:number, maxLength?:number):boolean`**<br>returns `true` if the given `Value` is a "dense" JavaScript array, whose elements all pass the given `Validator` - or `false` otherwise. `Validator` is a function which receives a list element as its sole argument and returns `true` if the given element is "valid" or `false` otherwise - a *throwing* `Validator` marks the whole list as invalid (this allows `expectXXX` functions to be used as validators). If given, `minLength` specifies the minimal required list length and `maxLength` specifies the maximal allowed list length<br>&nbsp;<br>
* **`ValueIsInstanceOf (Value:any, Constructor:Function):boolean`**<br>returns `true` if the given `Value` was constructed using the given `Constructor` function - or `false` otherwise
* **`ValueInheritsFrom (Value:any, Prototype:Object):boolean`**<br>returns `true` if `Prototype` exists in the prototype chain of the given `Value` - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsDate (Value:any):boolean`**<br>returns `true` if the given `Value` is a `Date` instance - or `false` otherwise
* **`ValueIsError (Value:any):boolean`**<br>returns `true` if the given `Value` is an `Error` instance  - or `false` otherwise
* **`ValueIsPromise (Value:any):boolean`**<br>returns `true` if the given `Value` is a "Promise", i.e., an object with a property named `then` which contains a function - or `false` otherwise
* **`ValueIsRegExp (Value:any):boolean`**<br>returns `true` if the given `Value` is a `RegExp` instance - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsOneOf (Value:any, ValueList:any[]):boolean`**<br>returns `true` if the given `Value` equals (at least) one of the items found in the given `ValueList` - or `false` otherwise. Equality is checked using the JavaScript `===` operator
* **`ValueIsListOf (Value:any, ValueList:any[]):boolean`**<br>returns `true` if the given `Value` is a "dense" JavaScript array whose elements all equal (at least) one of the items found in the given `ValueList` - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsColor (Value:any):boolean`**<br>returns `true` if the given `Value` is a string containing a syntactically valid CSS color specification (checked case-insensitively) - or `false` otherwise
* **`ValueIsEMailAddress (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing a syntactically valid EMail address (checked case-insensitively and against the complete string) - or `false` otherwise
* **`ValueIsURL (Value:any):boolean`**<br>returns `true` if the given `Value` is a string containing a syntactically valid URL (absolute or relative) - or `false` otherwise
* **`ValueIsAbsoluteURL (Value:any, allowedProtocols?:string[]):boolean`**<br>returns `true` if the given `Value` is a string containing a syntactically valid *absolute* URL (i.e., one that includes a scheme) - or `false` otherwise. If given, `allowedProtocols` restricts accepted values to URLs using one of the listed protocols (matched case-insensitively, with or without a trailing colon)<br>&nbsp;<br>
* **`ValueIsPhoneNumber (Value:any):boolean`**<br>returns `true` if the given `Value` is a string containing a syntactically plausible phone number in a common national or international notation (digits with the format characters space, `-`, `.`, `/` and parentheses, optionally led by a `+`). With a leading `+`, the digits must follow E.164 rules (7-15 digits, no leading zero), otherwise 3-16 digits are accepted. Please note: this is a plausibility check only - it does not verify that prefixes or number lengths actually exist
* **`ValueIsE164PhoneNumber (Value:any):boolean`**<br>returns `true` if the given `Value` is a string containing a phone number in the canonical [E.164](https://en.wikipedia.org/wiki/E.164) format (a `+` followed by 7-15 digits without leading zero and without any formatting characters, e.g., `+4972112345`) - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsBigInt (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a primitive `BigInt` value - or `false` otherwise
* **`ValueIsSymbol (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a symbol - or `false` otherwise
* **`ValueIsMap (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a JavaScript `Map` - or `false` otherwise
* **`ValueIsSet (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a JavaScript `Set` - or `false` otherwise
* **`ValueIsTypedArray (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a typed array (like `Uint8Array`, but not a `DataView`) - or `false` otherwise
* **`ValueIsArrayBuffer (Value:unknown):boolean`**<br>returns `true` if the given `Value` is an `ArrayBuffer` - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsUUID (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing a UUID in its canonical format (checked case-insensitively) - or `false` otherwise
* **`ValueIsISODate (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing a calendar date in the format `YYYY-MM-DD` (calendar overflows like `2026-02-31` are detected) - or `false` otherwise
* **`ValueIsISOTimestamp (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing an ISO 8601 timestamp (like `2026-07-03T10:56:00Z`) - or `false` otherwise
* **`ValueIsIPv4Address (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing an IPv4 address in dotted-quad notation - or `false` otherwise
* **`ValueIsIPv6Address (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing a syntactically valid IPv6 address - or `false` otherwise
* **`ValueIsHostName (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing a host name according to RFC 1123 - or `false` otherwise
* **`ValueIsPortNumber (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a whole number in the range 1...65535 - or `false` otherwise
* **`ValueIsJSONString (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string which can be parsed with `JSON.parse` - or `false` otherwise
* **`ValueIsBase64 (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a non-empty, correctly padded Base64-encoded string (using the standard alphabet) - or an empty string - and `false` otherwise
* **`ValueIsHexString (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a non-empty string consisting of hexadecimal digits only - or `false` otherwise
* **`ValueIsIdentifier (Value:unknown):boolean`**<br>returns `true` if the given `Value` is a string containing a syntactically valid JavaScript identifier (checked against the full Unicode grammar, i.e., `ID_Start` followed by `ID_Continue` characters, incl. `$`, `_`, ZWNJ and ZWJ - reserved words are *not* rejected) - or `false` otherwise<br>&nbsp;<br>
* **`ValueIsSerializableValue (Value:any):boolean`**<br>returns `true` if the given `Value` matches the exported `serializableValue` type, i.e., if it can be serialized without loss using `JSON.stringify` - that is, if it is `null`, a boolean, a finite number, a string, or a "dense" array or plain object whose elements/properties are themselves serializable values (`NaN`, `Infinity`, `undefined` array elements and circular references are all rejected) - or `false` otherwise
* **`ValueIsSerializableObject (Value:any):boolean`**<br>returns `true` if the given `Value` is a plain object all of whose own enumerable properties satisfy `ValueIsSerializableValue` (matching the exported `serializableObject` type) - or `false` otherwise

### Argument Validation Functions ###

The following functions check whether a given argument satisfies a certain constraint (e.g., belongs to a certain category) and either return the given argument (sometimes after some normalization), if the constrain is met, or throw an error otherwise.

Unless stated otherwise, these functions exist in four different "flavours", as indicated by their name prefixes:

* `allowXXX`<br>validates the given argument and returns it, if it is either missing (i.e., equals `null` or `undefined`) or meets the condition defined for `XXX` - or throws an exception otherwise
* `allowedXXX`<br>synonym for `allowXXX`, looks better when used as an expression
* `expectXXX`<br>validates the given argument and returns it, if it exists (i.e., differs both from `null` and `undefined`) and meets the condition defined for `XXX` - or throws an exception otherwise  
* `expectedXXX`<br>synonym for `expectXXX`, looks better when used as an expression

For the sake of clarity, however, only the first "flavour" (namely `allowXXX`) is shown in the list below (provided that this flavour actually exists).

* **`expectValue (Description:string, Argument:any, Validator?:(Value:any) => boolean):any`**<br>checks if the given `Argument` exists (i.e., if it differs from both `null` and `undefined`) and, if given, satisfies the optional `Validator` function. If this is the case, the function returns the (unboxed) primitive value of `Argument`, otherwise an error is thrown whose message contains the given `Description`. Unlike most other `expectXXX` functions, `expectValue` also exists in the flavours `allowXXX` and `allowedXXX` - with `allowXXX` simply returning `undefined` if `Argument` is missing<br>&nbsp;<br>
* **`allowBoolean (Description:string, Argument:any):boolean|null|undefined`**<br>checks if the given `Argument` (if it exists) is either a primitive boolean value or an instance of `Boolean`. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error with the message `"the given ${Description} is no valid boolean value"` is thrown, which uses the given `Description`<br>&nbsp;<br>
* **`allowNumber (Description:string, Argument:any):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is either a primitive numeric value or an instance of `Number`. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error with the message `"the given ${Description} is no valid numeric value"` is thrown, which uses the given `Description`
* **`allowFiniteNumber (Description:string, Argument:any):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is a finite number, i.e. a number which is not `NaN` and whose value is greater than negative and smaller than positive infinity. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowNaN (Description:string, Argument:any):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is `NaN`. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowNumberInRange (Description:string, Argument:any, minValue?:number, maxValue?:number, withMin?:boolean, withMax?:boolean):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is a number whose value is within the range given by `minValue` and `maxValue`. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`. `minValue` is optional and defaults to negative infinity, `maxValue` is also optional but defaults to positive infinity. When true, `withMin` indicates that `Value` may also be *equal* to the lower end of the given range, otherwise it must just be *greater than* the lower limit. When true, `withMax` indicates that `Value` may also be *equal* to the upper end of the given range, otherwise it must just be *lower than* the upper limit
* **`allowInteger (Description:string, Argument:any):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is a whole number. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowIntegerInRange (Description:string, Argument:any, minValue?:number, maxValue?:number):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is a whole number whose value is within the range given by `minValue` and `maxValue`. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`. `minValue` is optional and defaults to negative infinity, `maxValue` is also optional but defaults to positive infinity
* **`allowOrdinal (Description:string, Argument:any):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is a whole number greater than or equal to zero. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowCardinal (Description:string, Argument:any):number|null|undefined`**<br>checks if the given `Argument` (if it exists) is a whole number greater than or equal to one. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowString (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is either a primitive literal value or an instance of `String`. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error with the message `"the given ${Description} is no valid literal string"` is thrown, which uses the given `Description`
* **`allowNonEmptyString (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string with some content that does not just consist of white-space characters. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowStringMatching (Description:string, Argument:any, Pattern:RegExp):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string whose content matches the given regular expression `Pattern`. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowText (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing "ordinary" text only (i.e., a string which lacks any kind of control characters except \\n or \\r). If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowTextline (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing a single line of "ordinary" text only (i.e., a string which lacks any kind of control characters). If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowFunction (Description:string, Argument:any):Function|null|undefined`**<br>checks if the given `Argument` (if it exists) is a JavaScript function. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowAnonymousFunction (Description:string, Argument:any):Function|null|undefined`**<br>checks if the given `Argument` (if it exists) is an anonymous JavaScript function (i.e., a function with an empty `name` property). If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowNamedFunction (Description:string, Argument:any):Function|null|undefined`**<br>checks if the given `Argument` (if it exists) is a "named" JavaScript function (i.e., a function with a non-empty `name` property). If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowNativeFunction (Description:string, Argument:any):Function|null|undefined`**<br>checks if the given `Argument` (if it exists) is a native JavaScript function. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowScriptedFunction (Description:string, Argument:any):Function|null|undefined`**<br>checks if the given `Argument` (if it exists) is a scripted JavaScript function. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowObject (Description:string, Argument:any):any|null|undefined`**<br>checks if the given `Argument` (if it exists) is a JavaScript object (and not `null`). If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowPlainObject (Description:string, Argument:any):any|null|undefined`**<br>checks if the given `Argument` (if it exists) is a JavaScript object (different from `null`) which directly inherits from `Object` (such as a Javascript object literal). If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowVanillaObject (Description:string, Argument:any):any|null|undefined`**<br>checks if the given `Argument` (if it exists) is a JavaScript object which has been built using `Object.create(null)`. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowArray (Description:string, Argument:any):any[]|null|undefined`**<br>checks if the given `Argument` (if it exists) is an `Array` instance. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowList (Description:string, Argument:any, Expectation?:string,minLength?:number, maxLength?:number):any[]|null|undefined`**<br>checks if the given `Argument` (if it exists) is a "dense" JavaScript array (i.e., an array whose indices 0...n-1 all exist, where n is the length of the given array). If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`. If given, `minLength` specifies the minimal required list length and `maxLength` specifies the maximal allowed list length
* **`allowListSatisfying (Description:string, Argument:any, Validator:(Value:any) => boolean,Expectation?:string, minLength?:number, maxLength?:number):any[]|null|undefined`**<br>checks if the given `Argument` (if it exists) is a "dense" JavaScript array, whose elements all pass the given `Validator`. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`. `Validator` is a function which receives a list element as its sole argument and returns `true` if the given element is "valid" or `false` otherwise. If given, `minLength` specifies the minimal required list length and `maxLength` specifies the maximal allowed list length<br>&nbsp;<br>
* **`allowInstanceOf (Description:string, Argument:any, constructor:Function, Expectation:string):any|null|undefined`**<br>checks if the given `Argument` (if it exists) was constructed using the given `Constructor` function. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowValueInheritingFrom (Description:string, Argument:any, prototype:any, Expectation:string):any|null|undefined`**<br>checks if  `Prototype` exists in the prototype chain of the given `Argument` (if that exists). If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowDate (Description:string, Argument:any):Date|null|undefined`**<br>checks if the given `Argument` (if it exists) is a `Date` instance. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowError (Description:string, Argument:any):Error|null|undefined`**<br>checks if the given `Argument` (if it exists) is an `Error` instance. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowPromise (Description:string, Argument:any):any|null|undefined`**<br>checks if the given `Argument` (if it exists) is a "Promise", i.e., an object with a property named `then` which contains a function. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowRegExp (Description:string, Argument:any):RegExp|null|undefined`**<br>checks if the given `Argument` (if it exists) is a `RegExp` instance. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowOneOf (Description:string, Argument:any, ValueList:any[]):any|null|undefined`**<br>checks if the given `Argument` (if it exists) equals (at least) one of the items found in the given `ValueList`. If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`. Equality is checked using the JavaScript `===` operator
* **`allowListOf (Description:string, Argument:any, ValueList:any[]):any[]|null|undefined`**<br>checks if the given `Argument` (if it exists) is a "dense" JavaScript array whose elements all equal (at least) one of the items found in the given `ValueList` (see `ValueIsListOf`). If this is the case (or `Argument` is missing), the function returns the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowColor (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing a syntactically valid CSS color specification. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowEMailAddress (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing a syntactically valid EMail address. If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowURL (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing a syntactically valid URL (absolute or relative). If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowAbsoluteURL (Description:string, Argument:any, allowedProtocols?:string[]):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing a syntactically valid absolute URL (see `ValueIsAbsoluteURL`). If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowPhoneNumber (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing a syntactically plausible phone number in a common national or international notation (see `ValueIsPhoneNumber`). If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`
* **`allowE164PhoneNumber (Description:string, Argument:any):string|null|undefined`**<br>checks if the given `Argument` (if it exists) is a string containing a phone number in the canonical E.164 format (see `ValueIsE164PhoneNumber`). If this is the case (or `Argument` is missing), the function returns the primitive value of the given `Argument`, otherwise an error is thrown whose message contains the given `Description`<br>&nbsp;<br>
* **`allowBigInt`**, **`allowSymbol`**, **`allowMap`**, **`allowSet`**, **`allowTypedArray`**, **`allowArrayBuffer`**<br>validate the given `Argument` using `ValueIsBigInt`, `ValueIsSymbol`, `ValueIsMap`, `ValueIsSet`, `ValueIsTypedArray` or `ValueIsArrayBuffer`, resp., and behave like any other `allowXXX` function described above (incl. their `allowedXXX`, `expectXXX` and `expectedXXX` flavours)
* **`allowUUID`**, **`allowISODate`**, **`allowISOTimestamp`**, **`allowIPv4Address`**, **`allowIPv6Address`**, **`allowHostName`**, **`allowPortNumber`**, **`allowJSONString`**, **`allowBase64`**, **`allowHexString`**, **`allowIdentifier`**<br>validate the given `Argument` using the corresponding `ValueIsXXX` classifier and behave like any other `allowXXX` function described above (incl. their `allowedXXX`, `expectXXX` and `expectedXXX` flavours)
* **`allowSerializableValue`**, **`allowSerializableObject`**<br>validate the given `Argument` using `ValueIsSerializableValue` or `ValueIsSerializableObject`, resp., and behave like any other `allowXXX` function described above (incl. their `allowedXXX`, `expectXXX` and `expectedXXX` flavours)

### Utility Functions ###

* **`global`**<br>a cross-environment reference to the global object (`globalThis`) - useful for code that is meant to run in a browser as well as in Node.js or Deno<br>&nbsp;<br>
* **`throwError (Message:string):never`**<br>this function has been provided in order to simplify throwing "named" `Error` instances: if the given `Message` starts with a JavaScript identifier followed by a colon, identifier and colon are stripped apart and the identifier is used as the `name` property of a newly constructed `Error` instance for the remaining part of `Message`. Otherwise, this function is equivalent to `throw new Error(Message)` <br>&nbsp;<br>
* **`ObjectMergedWith (TargetObject:object, ...otherObjectList:object[]):object`**<br>`Object.assign` can not be used to copy properties with getters and setters from one object into another - this is what `ObjectMergedWith` is good for: it copies the descriptors of all *own* enumerable properties (incl. those with symbol keys) from any object found in `otherObjectList` into the given `TargetObject` and also returns that object as its function result. Any  descriptor already existing for a given property in `TargetObject` will be overwritten<br>&nbsp;<br>
* **`constrained (Value:number, Minimum:number = -Infinity, Maximum:number = Infinity):number`**<br>limits the given `Value` to the range specified by `Minimum` and `Maximum` - i.e., the function returns `Minimum` if `Value` is less than (or equal to) `Minimum`, `Maximum` if `Value` is greater than (or equal to) `Maximum`, or `Value` itself otherwise. `Minimum` and `Maximum` are optional and default to `-Infinity` or `+Infinity`, resp.<br>&nbsp;<br>
* **`escaped (Text:string):string`**<br>returns a copy of the given `Text` in which all control characters have been replaced by their corresponding escape sequences
* **`unescaped (Text:string):string`**<br>returns a copy of the given `Text` in which all character escape sequences (incl. code point escapes like `\u{1F600}`) have been replaced by their corresponding characters - invalid hex digits or code points leave a sequence untouched<br>&nbsp;<br>
* **`quotable (Text:string, Quote:'"' | "'" | '`' = '"'):string`**<br>returns a copy of the given `Text` in which all control characters and `Quote`s have been replaced by their corresponding escape sequences (when quoting for template literals, i.e., with backticks, any `${` is escaped as well). The outcome of this function may, f.e., be used to construct literal values in JSON files. `Quote` is optional and defaults to the double-quotes character
* **`quoted (Text:string, Quote:'"' | "'" | '`' = '"'):string`**<br>returns a copy of the given `Text` (embedded within a pair of `Quote`s) in which all control characters and `Quote`s have been replaced by their corresponding escape sequences. The outcome of this function may, f.e., used to construct literal values in JSON files. `Quote` is optional and defaults to the double-quotes character<br>&nbsp;<br>
* **`HTMLsafe (Argument:string, EOLReplacement?:string):string`**<br>returns a copy of the given `Argument` in which all control characters (except `\n`) and characters with a special meaning for HTML have been replaced by their corresponding HTML entities. Any linefeed characters (`\n`) will be replaced by the given `EOLReplacement` string - specification of `EOLReplacement` is optional and defaults to `<br/>`. **Warning**: `EOLReplacement` is inserted *as given* - it must be trusted HTML and should never contain unchecked user input
* **`MarkDownSafe (Argument:string, EOLReplacement?:string):string`**<br>returns a copy of the given `Argument` in which all control characters (except `\n`) and characters with a special meaning for HTML or MarkDown (such as backticks, `*`, `_`, `[`, `]`, `#`, `|`, `~` and `:`) have been replaced by their corresponding HTML entities. Any linefeed characters (`\n`) will be replaced by the given `EOLReplacement` string - specification of `EOLReplacement` is optional and defaults to `<br/>`. **Warning**: `EOLReplacement` is inserted *as given* - it must be trusted HTML without any MarkDown-relevant characters<br>&nbsp;<br>
* **`ValuesDiffer (thisValue:any, otherValue:any, ModeOrOptions?:'by-value'|'by-reference'|{ Mode?:string, Tolerance?:number }):boolean`**<br>returns `true` if `thisValue` differs from `otherValue` - or `false` otherwise. Equality is checked by inspection: `null`, `undefined`, booleans, strings and functions are compared using the JavaScript `===` operator; numbers are compared taking care of `NaN` (two `NaN` values are considered equal) and allowing for a small *relative* deviation (based on `Number.EPSILON`); instances of `Boolean`, `Number` and `String` are compared by their primitive values; `Date` instances are compared by their timestamps (two "invalid" dates are considered equal); `RegExp` instances are compared by their sources and flags; `Map`s are compared by size and their (identity-matched) keys with recursively compared values; `Set`s are compared by size and identity-matched elements; typed arrays (and `DataView`s) are compared byte-wise; all other objects and arrays are compared element by element (with cycle detection - matching circular references are considered equal). If the optional `Mode` is set to `by-reference`, objects (except arrays, whose elements are still compared individually) are compared by reference instead. Instead of a mode string, an options object may be passed: `{ Mode, Tolerance }` where the optional `Tolerance` specifies an *absolute* tolerance for number comparisons (overriding the default relative one). Mode `by-value` is deprecated - it behaves like the default
* **`ValuesAreEqual (thisValue:any, otherValue:any, ModeOrOptions?:'by-value'|'by-reference'|{ Mode?:string, Tolerance?:number }):boolean`**<br>returns `true` if `thisValue` equals `otherValue` - or `false` otherwise. `ValuesAreEqual` is the exact negation of `ValuesDiffer` (see there for the comparison rules)<br>&nbsp;<br>
* **`ObjectIsEmpty (Candidate:any):boolean`**<br>returns true if the given `Candidate` is an empty object (i.e., an object without any *own* properties) - or `false` otherwise. **Please note**: `Candidate` is mandatory and must be an object - an error is thrown otherwise
* **`ObjectIsNotEmpty (Candidate:any):boolean`**<br>returns true if the given `Candidate` is a non-empty empty object (i.e., an object with at least one *own* property) - or `false` otherwise. **Please note**: `Candidate` is mandatory and must be an object - an error is thrown otherwise
* **`StringIsEmpty (Candidate:string):boolean`**<br>returns true if the given `Candidate` is an empty string (i.e., a string which either contains no characters at all or only whitespace characters) - or `false` otherwise
* **`StringIsNotEmpty (Candidate:string):boolean`**<br>returns true if the given `Candidate` is a non-empty string (i.e., a string which contains one or more characters and not all of them are whitespace characters) - or `false` otherwise<br>&nbsp;<br>
* **`ValidatorForClassifier (Classifier:(Value:any) => boolean, NilIsAcceptable:boolean, Expectation:string):Function`**<br>this function is used internally to construct many of the offered argument validation functions: it returns a new function which takes a `Description` and an `Argument`, uses the given `Classifier` to check if `Argument` belongs to the expected category of values and - if it does - returns the primitive value of the given `Argument`. Otherwise, an error message is constructed, which includes the given `Description` and complains about the given value not being a "valid ${Expectation}" - i.e., `Expectation` should describe the expected kind of argument. If set to `true`, `NilIsAcceptable` indicates that `Argument` may be missing (i.e., `null` or `undefined`), otherwise the given `Argument` is mandatory (the exported constants `acceptNil` and `rejectNil` may be used instead of `true`/`false` for better readability at call sites).<br>&nbsp;<br>**Important Note**: if you plan to develop a library which may be "tree-shaked" by application bundlers (such as WebPack and Rollup) and export functions built with `ValidatorForClassifier`, you should mark all invocations of `ValidatorForClassifier` as "free of side-effects" by prepending them with `/*#__PURE__*/` - otherwise those invocations will remain in the bundled code even if you don't use the corresponding exports
* **`validatedArgument (Description:string, Argument:any, ValueIsValid:(Value:any) => boolean,NilIsAcceptable:boolean, Expectation:string):any|null|undefined`**<br>this function is used internally to actually validate a given `Argument` and throw an `Error` with a message containing the given `Description`, if not. `ValueIsValid` is the function used check `Argument` and should return `true` if `Argument` is "valid" or `false` if not. If set to `true`, `NilIsAcceptable` indicates that `Argument` may be missing (i.e., `null` or `undefined`), otherwise the given `Argument` is mandatory. If validation fails, an error message is constructed, which includes the given `Description` and complains about the given value not being a "valid ${Expectation}" - i.e., `Expectation` should describe the expected kind of argument<br>&nbsp;<br>
* **`FunctionWithName (originalFunction:Function, desiredName:string|String):Function`**<br>this function is used internally to convert an anonymous function `originalFunction` into a named one - either by setting the `desiredName` for the existing function or by wrapping it into a new function with that name

### Color Utilities ###

* **`ColorSet`**<br>is an object, whose keys are the names of all colors known by (and built into) a web browser and the corresponding values are the RGBA specifications of these colors
* **`HexColor (Color:string):string`**<br>converts a given `Color` string (which must be a valid CSS color specification) into the long hexadecimal format (`#rrggbbaa`)
* **`shortHexColor (Color:string):string`**<br>converts a given `Color` string (which must be a valid CSS color specification) into the short hexadecimal format (`#rrggbb`) - such a format must be used for HTML input elements of type "color"
* **`RGBAColor (Color:string):string`**<br>converts a given `Color` string (which must be a valid CSS color specification) into the RGBA format (`rgba(r,g,b,a)`)

## Build Instructions ##

You may easily build this package yourself.

Just install [NPM](https://docs.npmjs.com/) according to the instructions for your platform and follow these steps:

1. either clone this repository using [git](https://git-scm.com/) or [download a ZIP archive](https://github.com/rozek/javascript-interface-library/archive/refs/heads/main.zip) with its contents to your disk and unpack it there 
2. open a shell and navigate to the root directory of this repository
3. run `npm install` in order to install the complete build environment
4. execute `npm run build` to create a new build (using [Vite](https://vite.dev/) and [vite-plugin-dts](https://github.com/qmhc/vite-plugin-dts) for the bundled type declarations)

The package comes with a complete test suite (based on [Vitest](https://vitest.dev/)): run `npm test` for watch mode or `npm run test:run` for a single pass. Both testing and building also run automatically in GitHub Actions on every push and pull request - and `npm run agadoo` checks whether the build result is still tree-shakeable.

## License ##

[MIT License](LICENSE.md)

---
_Source: https://npm.io/package/javascript-interface-library · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
