npm.io
1.5.6 • Published 2d ago

typeofanything

Licence
GPL-3.0-or-later
Version
1.5.6
Deps
0
Size
115 kB
Vulns
0
Weekly
0
Stars
2

typeofAnything

A little ES20xx module to determine the type of ... well, almost anything one throws at it.

The code is available as an ESM module, as a browser script and as a browser script containing a factory function.

The module is also available from NPM.

Module version

The module exports:

  • default: a function to determine the type of a given input. Syntax:

    [imported default function](input,...type | {...})

    Note: for the {...} parameter, see the demonstration/test page

  • $Wrap: a function that wraps input and returns an Object with keys:

    • value: the input value (can also be undefined, null, NaN or Infinity)
    • is([...types]|{...}): function to determine the type of the wrapped input. This may also be [Symbol.is].
    • type: returns (a string representation) of the type of the wrapped input. This may also be [Symbol.type]
  • maybe: a utility function that returns either a tested value, an error value of choice or undefined, syntax

    maybe({trial: function[, whenError: function]}

    Note: without a whenError parameter, maybe returns undefined when trial fails.

  • isNothing(input:any[, all: boolean]: a function to check if input is either null || undefined (all: false) or null || undefined || NaN || Infinity (all: true).

  • proxyWrapper is an small utility to enable 'type retrieval' of Proxy instances . Syntax:

    • proxyWrapper.wrap(proxy2Wrap: proxy): wrap a constructor to enable type checking
    • proxyWrapper.create(target: object, traps: object): create a 'detectable' Proxy
  • addSymbolicExtensions: a method to add Symbol extension methods to check instances of (nearly) anything using Symbols. By default the extensions are not added. Invoking addSymbolicExtensions creates Symbol.is
    and Symbol.type. With it one can directly check types, e.g. "some string"[Symbol.type] or "some string"[Symbol.is](String).

For an extensive set of examples see the module demonstration page or fiddle with it with a fork of this Stackblitz project.

Script version

The script version makes the factory TOAFactory globally available. Invoking the factory delivers an Object encapsulating the beforementioned exports (except default, that's just IS).

For an extensive set of examples see the script demonstration page

Import the module

Your script should be of type module.

<script type="module">
  import {default as IS, $Wrap} from "[install path]/Src/typeofany.module.js";
  const isObject = IS({}, Array, Object);
  const nothing = null;
  addSymbolicExtensions();
  const is = Symbol.is;
  const isNothingNull = nothing?.[is](null) || $Wrap(nothing).is(null);
</script>

Use in browser

Create a script tag in your html:

<script src="[install path]/Src/typeofany.browser.js")</script>

Subsequently in your script (for example)

<script>
  const {IS, $Wrap} = TOAFactory({useSymbolicExtension: true}); 
  const isObject = IS({}, Array, Object);
  const nothing = null;
  // because [useSymbolicExtension: true], we can assign Symbol.is to [is]
  const is = Symbol.is;
  const isNothingNull = nothing?.[is](null) || $Wrap(nothing).is(null); 
  /* ... */
</script>

Minified

Both the module- and browser-script versions are available as minified js files in the ./Dist directory, downloadable from e.g. unpkg

Keywords