Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→util-array-object-or-bothnext

util-array-object-or-both5.2.1

Validate and normalise user choice: array, object or both?

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • 2022 UPDATE
  • PURPOSE
  • API — ARROBJORBOTH…
  • API — DEFAULTS
  • API — VERSION
  • API — TYPES
  • USE
  • Changelog

Installation

Quick Take

Examples

  • Normalise the accepted array aliases
  • Customise the thrown error's context
  • Normalise aliases that allow either arrays or objects
  • Case and surrounding whitespace are normalised
  • Normalise the accepted object aliases

2022 Update

This is one of our early, pre-TypeScript libraries. Today, I’d just offer one of three values to pick and call it a day. There’s very little practical benefit from giving user a variety of values to pick from. User will have to look at documentation anyway. Not to mention, TypeScript Intellisense will suggest the values.

Purpose

It standardises the input strings a user has given:

Turned into "array"Turned into "object"Turned into "any"
array
arrayobjectany
arrays
arraysobjectsall
arr
arrobjeverything
aray
arayobboth
arr
arroeither
a
aeach
whatever
whatever
e
e

API — arrObjOrBoth()

The main function arrObjOrBoth() is imported like this:

It’s a function which takes two input arguments:

Input argumentTypeObligatoryDescription
input
Type: String
Obligatory: yes
inputStringyesLet users choose from variations of “array”, “object” or “both”. See above.
opts
Type: Plain object
Obligatory: no
optsPlain objectnoOptional Options Object. See below for its API.

Options object lets you customise the thrown error message:

KeyTypeObligatoryDefaultDescription
msg
Type: String
Obligatory: no
Default: Empty string
msgStringnoEmpty stringAppend the message in front of the thrown error.
optsVarName
Type: String
Obligatory: no
Default: given variable
optsVarNameStringnogiven variableThe name of the variable being checked.

For example, set optsVarName to opts.only and set msg to ast-delete-key/deleteKey(): [THROW_ID_01] and the error message thrown if user misconfigures the setting will be, for example:

`ast-delete-key/deleteKey(): [THROW_ID_01] The variable "opts.only" was customised to an unrecognised value: sweetcarrots. Please check it against the API documentation.`;

Here are all defaults in one place for copying:

Function will return one of three possible string values: "array" or "object" or "any".

API — defaults

You can import defaults:

It's a plain object:

The main function calculates the options to be used by merging the options you passed with these defaults.

API — version

You can import version:

API — types

This package is written in TypeScript and exports the following types:

TypeDescription
Opts
Type: Opts
OptsThe Optional Options Object of arrObjOrBoth(), documented above.
ArrayObjectOrBoth
Type: ArrayObjectOrBoth
ArrayObjectOrBothWhat arrObjOrBoth() returns — "array", "object" or "any".
import type { ArrayObjectOrBoth, Opts } from "util-array-object-or-both";

Use

// require this library:
import { arrObjOrBoth } from 'util-array-object-or-both';
// and friends:
import clone from 'lodash.clonedeep'
import {checkTypes} from 'check-types-mini'
// let's say you have a function:
function myFunc (input, opts) {
  // now you want to check your options object, is it still valid after users have laid their sticky paws on it:
  // define defaults:
  let defaults = {
    lalala: null,
    only: 'object' // <<< this is the value to validate, is it `array`|`object`|`any`
  };
  // clone the defaults to safeguard it, and then, object-assign onto defaults.
  // basically you fill missing values with default-ones
  const resolvedOpts = {...defaults, ...opts};
  // now, use "check-types-mini" to validate the types:
  checkTypes(resolvedOpts, defaults,
    {
      // give a meaningful message in case it throws,
      // customise the library `check-types-mini`:
      msg: 'my-library/myPrecious(): [THROW_ID_01]',
      optsVarName: 'opts',
      schema: {
        lalala: ['null', 'string'],
        only: ['null', 'string']
      }
    }
  )
  // by this point, opts.only is guaranteed to be either `null` or `string`.
  // if it's a `string`, let's validate is its values among accepted-ones:
  resolvedOpts.only = arrObjOrBoth(opts.only, {
    msg: 'my-library/myPrecious(): [THROW_ID_02]',
    optsVarName: 'opts.only'
  })
  // now it's guaranteed to be either falsy (undefined or null) OR:
  //   - `object`
  //   - `array`
  //   - `any`

  // now you can use `opts.only` in your function safely.
  ...
  // rest of the function...
}

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab