npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@whoj/utils-core

v2.3.1

Published

Core collection of common JS / TS helpers/utils by @whoj

Readme

@whoj/utils-core

Install

# ✨ Auto-detect
npx nypm install @whoj/utils-core

# npm
npm install @whoj/utils-core

# yarn
yarn add @whoj/utils-core

# pnpm
pnpm install @whoj/utils-core

# bun
bun install @whoj/utils-core

# deno
deno install @whoj/utils-core

Available Functions

Array

at(array, index)

clampArrayRange(n, arr)

Clamp a number to the index ranage of an array

flattenArrayable(array?)

Convert Arrayable<T> to Array<T> and flatten it

flattenDeepArray(array?, key?)

Convert Arrayable<T> to Array<T> and flatten object's array by key

Example: const payload = [ { child: [{name: 'John Doe'}] }, { child: [ {age: 23} ] } ]; flattenDeepArray(payload, 'child'); Result: [{name: 'John Doe'}, {age: 23}]

last(array)

mergeArrayable()

Use rest arguments to merge arrays

move(arr, from, to)

Move element in an Array

partition(array)

range()

remove(array, value)

Remove an item from Array

sample(arr, count)

Get random items from an array

shuffle(array)

Shuffle an array. This function mutates the array.

toArray(array?)

Convert Arrayable<T> to Array<T>

uniq(array)

Unique an Array

Function

batchInvoke(functions)

Call every function in an array

invoke(fn)

Call the function

tap(val, callback)

Pass the value through the callback, and return the value

Example:

function createUser(name: string): User {
  return tap(new User, user => {
    user.name = name
  })
}

Globals

getDocument()

If document is defined, return document, otherwise return undefined.

getGlobal(prop?)

If we're on the server, return the global object, otherwise return the window object

getWindow()

If we're on the server, return undefined, otherwise return the window object.

isClient()

If the window object exists, then we're in the browser, otherwise we're in Node.

isServer()

If the type of window is undefined, then we're on the server.

scrollToElement(element)

Guard

isTruthy(v)

Type guard to filter out falsy values

Example:

array.filter(isTruthy)

noNull(v)

Type guard to filter out null values

Example:

array.filter(noNull)

notNullish(v)

Type guard to filter out null-ish values

Example:

array.filter(notNullish)

notUndefined(v)

Type guard to filter out null-ish values

Example:

array.filter(notUndefined)

Math

clamp(n, min, max)

Clamp any number

sum()

Sum any amount of number

Object

clearUndefined(obj)

Clear undefined fields from an object. It mutates the object

deepMerge(target)

Deep merge :P

fromPairs(pairs)

isKeyOf(obj, k)

Type guard for any key, k. Marks k as a key of T if k is in obj.

objectEntries(obj)

Strict typed Object.entries

objectKeys(obj)

Strict typed Object.keys

objectMap(obj, fn)

Map key/value pairs for an object, and construct a new one

Example:

objectMap({ a: 1, b: 2 }, (k, v) => [k.toString().toUpperCase(), v.toString()])
// { A: '1', B: '2' }

Swap key/value:

Example:

objectMap({ a: 1, b: 2 }, (k, v) => [v, k])
// { 1: 'a', 2: 'b' }

Filter keys:

Example:

objectMap({ a: 1, b: 2 }, (k, v) => k === 'a' ? undefined : [k, v])
// { b: 2 }

objectPick(obj, keys, omitUndefined)

Create a new subset object by giving keys

toPairs(obj)

Promise

createControlledPromise()

Return a Promise with resolve and reject methods

Example:

const promise = createControlledPromise()

await promise

// in anther context:
promise.resolve(data)

createPromiseLock()

Create a promise lock

Example:

const lock = createPromiseLock()

lock.run(async () => {
  await doSomething()
})

// in anther context:
await lock.wait() // it will wait all tasking finished

createSingletonPromise(fn)

Create singleton promise function

parallel(tasks, fn)

Parallel Promise

parallelSafe(tasks, fn)

Safe Parallel Promise

serial(tasks, fn)

Serial Promise

sleep(ms, callback?)

Promised setTimeout

String

capitalize()

divideStr(str, separator, position)

ensurePrefix(prefix, str)

Ensure prefix of a string

ensureSuffix(suffix, str)

Ensure suffix of a string

slash(str)

Replace backslash to slash

template(str)

Dead simple template engine, just like Python's .format()

Example:

const result = template(
  'Hello {0}! My name is {1}.',
  'John',
  'B.'
) // Hello John! My name is B..

Timer

stoppableTimeOut(cb, interval)

timestamp()

Timestamp value

Others

assert(condition, message)

Assert

noop()

Noop

hasConsole

  • Type: boolean
  • Default: true

isArray(val)

Check if is Array

isBoolean(val)

Check if value is boolean

isBrowser

  • Type: boolean
  • Default: false

isDef(val?)

isFunction(val)

Check if value is Function

isGlobPattern(pattern?, options?)

Check if it has glob pattern

isNumber(val)

Check if value is Number

isNumberish(val)

Check if value is Numberish

isObject(val)

Check if value is Object

isString(val)

Check if value is String

isWindow(val)

Check if window defined

MIT License © 2022 Jonson B.