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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@well-balanced/utils

v0.1.5

Published

JavaScript / TypeScript utils

Downloads

479

Readme

utils

NPM Version

JavaScript / TypeScript utils

Table of Contents

Installation

$ npm i @well-balanced/utils
$ pnpm add @well-balanced/utils
$ yarn add @well-balanced/utils

Usage

cleanObject

If the object has fields with an undefined value, clean it.

example

const user = { id: 'id', name: 'name', age: 20, gender: undefined }
const cleaned = cleanObject(user) // { id: 'id', name: 'name', age: 20}

reduceSeries

Execute a function on each element of an array, sequentially.

example

import { reduceSeries } from '@well-balanced/utils'

const array = [1, 2, 3, 4, 5]
const sum = await reduceSeries(array, async (prev, curr) => prev + curr, 0)
console.log(sum) // 15

source

test

mapSeries

Map over an array, applying an asynchronous function sequentially.

example

import { mapSeries } from '@well-balanced/utils'

const array = [1, 2, 3, 4, 5]
const results = await mapSeries(array, async (item) => item * 2)

console.log(results) // [2, 4, 6, 8, 10]

source

test

readdirRecursive

Read a directory recursively and return a list of file paths.

example

import { readdirRecursive } from '@well-balanced/utils'

const files = await readdirRecursive('/path/to/directory')
console.log(files)

source

test

setPropertyRecursive

Set a nested property on an object, creating intermediate objects as necessary.

example

import { setPropertyRecursive } from '@well-balanced/utils'

const obj = { hello: 'world' }
const updated = setPropertyRecursive(
  ['key1', 'key2', 'key3'],
  'hello world',
  obj,
)

console.log(updated) // { key1: { key2: { key3: 'hello world' } }, hello: 'world' }

source

test

cursor

Encode and decode cursors for pagination.

example

import { encodeCursor, decodeCursor } from '@well-balanced/utils'

const obj = { page: 1, limit: 10 }
const cursor = encodeCursor(obj)
console.log(cursor) // Encoded cursor string

const decoded = decodeCursor(cursor)
console.log(decoded) // { page: 1, limit: 10 }

source

test

getFulfilledResult

Extract fulfilled results from an array of settled promises.

example

import { getFulfilledResult } from '@well-balanced/utils'

const promises = [
  Promise.resolve(1),
  Promise.reject(new Error('Failure')),
  Promise.resolve(3),
]

const settled = await Promise.allSettled(promises)
const results = getFulfilledResult(settled)

console.log(results) // [1, 3]

source

test

msleep

Sleep for a given number of milliseconds.

example

import { msleep } from '@well-balanced/utils'

await msleep(1000) // Sleep for 1 second
console.log('1 second later')

source

kst

Get the current date and time in Korea Standard Time.

example

import { kst } from '@well-balanced/utils'

const koreanTime = kst()
console.log(koreanTime)

formatKst

Format a date object to a string in Korea Standard Time.

example

import { formatKst } from '@well-balanced/utils'

const date = new Date()
const formattedDate = formatKst(date, 'yyyy-MM-dd HH:mm:ss')
console.log(formattedDate)

filterOut

Filter out elements from an array based on a comparator function.

example

import { filterOut } from '@well-balanced/utils'

const array = [1, 2, 3, 4, 5]
const valuesToRemove = [2, 4]
const comparator = (a: number, b: number) => a === b

const result = filterOut(array, valuesToRemove, comparator)
console.log(result) // [1, 3, 5]

hashIndex

Generate a hash index from a string.

example

import { hashIndex } from '@well-balanced/utils'

const index = hashIndex('example', 10)
console.log(index)

notEmpty

Check if a value is not null or undefined.

example

import { notEmpty } from '@well-balanced/utils'

const value = 'example'
if (notEmpty(value)) {
  console.log('Value is not empty')
}

randomInt

Generate a random integer between a given range.

example

import { randomInt } from '@well-balanced/utils'

const randomValue = randomInt(1, 10)
console.log(randomValue)

safeSample

Get a random element from an array excluding certain values.

example

import { safeSample } from '@well-balanced/utils'

const array = [1, 2, 3, 4, 5]
const valuesToExclude = [2, 4]
const comparator = (a: number, b: number) => a === b

const result = safeSample(array, valuesToExclude, comparator)
console.log(result)

sample

Get a random element from an array.

example

import { sample } from '@well-balanced/utils'

const array = [1, 2, 3, 4, 5]
const result = sample(array)
console.log(result)

License

MIT