
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@charpeni/one-of
Advanced tools
Type-safe utilities to return a random element from an array or random entry from an object
Type-safe and tested utilities to return a random element from an array or random entry from an object.
npm install @charpeni/one-of
oneOf
is a function accepting either an array or an object and returning a random element or entry from it.
It was built mostly to be used in tests or for generating random data (it can also be used safely for any other type of behaviors, including production apps), e.g.:
<Card color={oneOf(['red', 'blue', 'green'])}>{/* ... */}</Card>
[!NOTE]
oneOf
is a type-safe function. It will infer the type of the array or object passed as an argument and return the corresponding type to ensure the return type is a union of possibilities rather than a primitive type (1 | 2
instead ofnumber[]
).If the input is passed inlined, the
const
modifier will be applied and therefore the type will be inferred asReadonlyArray
or readonlyRecord
. Otherwise, usingas const
is suggested.
import { oneOf } from '@charpeni/one-of';
// Returns a random element from the array
const element = oneOf([1, 2, 3]); // 2
// ^? const element: 1 | 2 | 3
// Returns a random entry from the object
const entry = oneOf({ a: 1, b: 2, c: 3 }); // ['b', 2]
// ^? const entry: ['a', 1] | ['b', 2] | ['c', 3]
Specific functions are also available: oneElementOf
and oneEntryOf
, that's what oneOf
uses under the hood:
import { oneElementOf, oneEntryOf } from '@charpeni/one-of';
// Returns a random element from the array
const element = oneElementOf([1, 2, 3]); // 2
// ^? const element: 1 | 2 | 3
// Returns a random entry from the object
const entry = oneEntryOf({ a: 1, b: 2, c: 3 }); // ['b', 2]
// ^? const entry: ['a', 1] | ['b', 2] | ['c', 3]
@charpeni/one-of is MIT licensed.
FAQs
Type-safe utilities to return a random element from an array or random entry from an object
The npm package @charpeni/one-of receives a total of 320 weekly downloads. As such, @charpeni/one-of popularity was classified as not popular.
We found that @charpeni/one-of demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.