
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
util-array-object-or-both
Advanced tools
Validate and normalise user choice: array, object or both?
npm i util-array-object-or-both
// consume via a CommonJS require:
const arrObjOrBoth = require("util-array-object-or-both");
// or as an ES Module:
import arrObjOrBoth from "util-array-object-or-both";
Here's what you'll get:
Type | Key in package.json | Path | Size |
---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports | main | dist/util-array-object-or-both.cjs.js | 3 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . | module | dist/util-array-object-or-both.esm.js | 2 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in | browser | dist/util-array-object-or-both.umd.js | 17 KB |
When I give the user ability to choose their preference out of: array
, object
or any
, I want to:
array
, I also want to accept: Arrays
, add
, ARR
, a
. Similar thing goes for options object
and any
.array
, object
or any
. This is necessary because we want set values to use in our programs. You can't have five values for array
in an IF statement, for example.throw
a meaningful error message. Technically this will be achieved using an options object.Assumed to be an array-type | object-type | either type | |
---|---|---|---|
Input string: | array | object | any |
arrays | objects | all | |
arr | obj | everything | |
aray | ob | both | |
arr | o | either | |
a | each | ||
whatever | |||
e | |||
---- | ---- | ---- | |
Output string: | array | object | any |
API is simple - just pass your value through this library's function. If it's valid, it will be normalised to either array
or object
or any
. If it's not valid, an error will be thrown.
Input argument | Type | Obligatory? | Description |
---|---|---|---|
input | String | yes | Let users choose from variations of "array", "object" or "both". See above. |
opts | Plain object | no | Optional Options Object. See below for its API. |
Options object lets you customise the throw
n error message. It's format is the following:
${opts.msg}The ${opts.optsVarName} was customised to an unrecognised value: ${str}. Please check it against the API documentation.
options object's key | Type | Obligatory? | Default | Description |
---|---|---|---|---|
{ | ||||
msg | String | no | `` | Append the message in front of the thrown error. |
optsVarName | String | no | given variable | The name of the variable we are checking here. |
} |
For example, set optsVarName
to opts.only
and set msg
to ast-delete-key/deleteKey(): [THROW_ID_01]
and the error message throw
n 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.
// require this library:
const arrObjOrBoth = require('util-array-object-or-both')
// and friends:
const clone = require('lodash.clonedeep')
const checkTypes = require('check-types-mini')
const objectAssign = require('object-assign')
// let's say you have a function:
function myPrecious (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 we're particularly keen 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
opts = objectAssign(clone(defaults), opts)
// now, use "check-types-mini" to validate the types:
checkTypes(opts, 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, we can guarantee that opts.only is either `null` or `string`.
// if it's a `string`, let's validate is its values among accepted-ones:
opts.only = arrObjOrBoth(opts.only, {
msg: 'my-library/myPrecious(): [THROW_ID_02]',
optsVarName: 'opts.only'
})
// now we can guarantee that it's either falsey (undefined or null) OR:
// - `object`
// - `array`
// - `any`
// now you can use `opts.only` in your function safely.
...
// rest of the function...
}
You may ask, why on Earth you would need a package for such thing? It's not very universal to be useful for society, is it?
Actually, it is.
I discovered that when working with AST's, you often need to tell your tools to process (traverse, delete, and so on) EITHER objects OR arrays or both. That's where this library comes in: standardise the choice (from three options) and relieve the user from the need to remember the exact value.
I think the API should accept a very wide spectrum of values, so users would not even need to check the API documentation - they'd just describe what they want, in plain English.
I'm going to use it in:
and others. So, it's not that niche as it might seem!
If you want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.
If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.
If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.
MIT License (MIT)
Copyright © 2018 Codsen Ltd, Roy Revelt
FAQs
Validate and normalise user choice: array, object or both?
The npm package util-array-object-or-both receives a total of 192 weekly downloads. As such, util-array-object-or-both popularity was classified as not popular.
We found that util-array-object-or-both demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.