@browndragon/func

0.0.13 • Public • Published

@browndragon/func

Functional utilities.

switchType

I got very fed up with the mess that is javascript type introspection, so I wrote my own.

switchType(unknownObject, handler) will do type analysis on the unknown object and then invoke the correctly named method on handler with the object.

The methods it will call are (roughly in order, omit irrelevants):

  • undefined (will also try null)
  • null (will also try undefined)
  • boolean (a value)
  • bigint (a value)
  • number (a value)
  • string (a value and an iterable)
  • function (a value)

Then we start considering (non-null) objects;

  • array (an iterable): Anything that is Array.isArray.
  • map (an associative and an iterable) -- uses instanceof Map (as do the other specialized types).
  • set (an iterable)
  • iterable: Anything with a Symbol.iterator on it (including collections & arrays & strings).
  • regExp: An instanceof RegExp.
  • empty: An object matching {} -- which is also a literal.
  • literal: an object whose prototype is literally Object (as created by Object.create and the {} syntax). Also associative.
  • associative: An object which @browndragon/obj can get/set fields on. A Map or an object literal.

Finally, objects will try to call:

  • object: Any non-null js typeof(x)=='object'.

Finally-finally, anything will try to call:

  • default: Anything that wasn't otherwise matched.

switchType returns the result of the called method or else undefined.

Dependents (0)

Package Sidebar

Install

npm i @browndragon/func

Weekly Downloads

12

Version

0.0.13

License

MIT

Unpacked Size

10.1 kB

Total Files

8

Last publish

Collaborators

  • browndragon