npm.io
1.52.0 • Published 1 month ago

@hairy/utils

Licence
MIT
Version
1.52.0
Deps
3
Size
535 kB
Vulns
0
Weekly
0
Stars
12

@hairy/utils

npm version npm downloads bundle size license

Core utility functions shared across all Hairylib packages.

Install

# ✨ Auto-detect
npx nypm install @hairy/utils

# npm
npm install @hairy/utils

# yarn
yarn add @hairy/utils

# pnpm
pnpm add @hairy/utils

# bun
bun install @hairy/utils

# deno
deno install npm:@hairy/utils

API

arange()
average(array, options?)
BIG_INTS
b
d
  • Type: number
  • Default: 10
n
  • Type: string
  • Default: "b"
v
  • Type: number
  • Default: 1000000000
k
d
  • Type: number
  • Default: 4
n
  • Type: string
  • Default: "k"
v
  • Type: number
  • Default: 1000
m
d
  • Type: number
  • Default: 7
n
  • Type: string
  • Default: "m"
v
  • Type: number
  • Default: 1000000
t
d
  • Type: number
  • Default: 13
n
  • Type: string
  • Default: "t"
v
  • Type: number
  • Default: 1000000000000
Bignumber()
bignumber(n, base?)
call(fn)
camelCase()
capitalCase()
chunk()
clone()
cloneDeep()
cloneDeepWith()
cloneWith()
compose()
concat()
constantCase()
cover(value, mode, symbol)

Intercept front and back characters, hide middle characters

debounce()
decimal(value, n)

retain n decimal places

DEFAULT_BIGNUM_CONFIG
DECIMAL_PLACES
  • Type: number
  • Default: 6
ROUNDING_MODE
  • Type: number
  • Default: 0
Deferred()
delay(ms)

Delay for a given number of milliseconds.

Example:

delay(1000).then(() => { console.log('1 second') })
dialsPhone(phoneNumber)
divide(array, options?)
dotCase()
downloadBlobFile(data, name)

Generate Blob | string file and download it

downloadNetworkFile(url, name?)

Download network files

downloadUrlFile(url, name?)

Download network files

ensurePrefix(prefix, str)

Ensure prefix of a string

ensureSuffix(suffix, str)

Ensure suffix of a string

find()
formatNumeric(value, options?)

format number thousand separator and unit

formdataToObject(formData)

formData to object

get()
ghost(strictMessage?)
groupBy()
gt(a, b)
gte(a, b)
integer(value)

format as a positive integer

isAndroid()
isArguments()
isArray()
isArrayBuffer()
isArrayLike()
isArrayLikeObject()
isBoolean()
isBrowser()
isBuffer()
isChrome()
isDate()
isEdge()
isElement()
isEmpty()
isEqual()
isEqualWith()
isError()
isFF()
isFormData(value)
isFunction()
isIE()
isIE11()
isIE9()
isInteger()
isIOS()
isMap()
isMatch()
isMatchWith()
isMobile()
isNaN()
isNative()
isNull()
isNumber()
isObject()
isObjectLike()
isPhantomJS()
isPlainObject()
isRegexp()
isSet()
isString()
isSymbol()
isTruthy(value)
isUndefined()
isWeakMap()
isWeakSet()
isWeex()
isWindow(value)
join()
kebabCase()
keyBy()
keys()
loop()
lt(a, b)
lte(a, b)
max()
maxBy()
merge()
mergeWith()
min()
minBy()
multiply(array, options?)
noCase()
nonnanable(value)

Check if a value is not NaN.

noop()
numberify(value)

Convert a value to a number.

numberish(value)

Convert a value to a numberish value.

objectToFormdata(object)

Object to formData

off(obj)
omit()
omitBy()
on(obj)
once()
openFilePicker(option)

eslint-disable prefer-promise-reject-errors

Select multiple files

openImagePicker(options)

Select multiple images

parseNumeric(num, delimiters)
pascalCase()
pascalSnakeCase()
pathCase()
percentage(total, count, options?)

calculate percentage

pick()
pickBy()
pipe()
plus(array, options?)
proxy(initObject?, initExtend?, options?: { strictMessage? })

eslint-disable ts/no-empty-object-type

Creates a proxy object that updates the original object when the proxy is updated.

Example:

const obj = proxy({ name: 'John' })
console.log(obj.name) // John

obj.proxy.update({ name: 'Jane' })
console.log(obj.name) // Jane

const obj2 = proxy()

obj2.any // Error: Proxy not updated. Call object.proxy.update() to update the proxy.

obj2.proxy.source // undefined
obj2.update({ name: 'John' })

// get the original object
obj2.proxy.source // { name: 'John' }
randomItem(array)

Get a random item from an array.

Example:

randomItem(['a', 'b', 'c']) // 'a' | 'b' | 'c'
randomNumber(min, max)

Get a random number between a minimum and maximum value.

Example:

randomNumber(0, 100) // 0-100
randomString(size, chars)

Get a random string of a given size.

Example:

randomString() // 10 characters long
randomString(20) // 20 characters long
randomString(20, 'abcdefghijklmnopqrstuvwxyz') // 20 characters long
range()
readFileReader(formType, file)

Read File file

redirectTo(url, target)
riposte()

Select a value based on a condition.

Example:

select(
 [condition1, value],
 [condition2, value2],
// default value
[true, value3],
 ...
) // value
select()

Select a value based on a condition.

Example:

select(
 [condition1, value],
 [condition2, value2],
// default value
[true, value3],
 ...
) // value
selectImages(options)

Select multiple images

sentenceCase()
set()
shortenId(value, startWith, endWith)

Shortens an identifier string by showing only the beginning and end portions, with ellipsis in the middle. Suitable for various types of identifiers like IPFS CID, transaction hashes, EVM addresses, user IDs, etc.

showOpenFilePicker(option)

eslint-disable prefer-promise-reject-errors

Select multiple files

showOpenImagePicker(options)

Select multiple images

size(dimension, unit?)
slash(str)

Replace backslash to slash

snakeCase()
stringify(value)

Convert a value to a string.

template(str)
to(promise, error?)

Convert a promise to a tuple of [error, data].

Example:

to(Promise.resolve('data')) // Promise<[null, 'data']>
to(Promise.reject(new Error('error'))) // Promise<[Error, undefined]>
toArray(value?, required)

Convert a value to an array.

Example:

toArray(arrorOrItemOrUndefined) // item[] | undefined
toArray(arrayOrItemOrUndefined, true) // item[]
trainCase()
truncate()
tryParseJson(text)
unindent(str)

Remove common leading whitespace from a template string. Will also remove empty lines at the beginning and end.

Example:

const str = unindent`
  if (a) {
    b()
  }
`
uniq()
uniqBy()
uniqWith()
unit(value, unit)
unwrap(value)

Unwrap a value or a function that returns a value.

Example:

unwrap({ name: 'John' }) // { name: 'John' }
unwrap(() => { return { name: 'John' } }) // { name: 'John' }
values()
whenever(value, callback)

Call a callback if a value is not null or undefined.

Example:

whenever(value, (value) => { return 'value' }) // value
zerofill(value, n, type)

leading zeros

zeroRemove(value, convert)

Directory structure

├── src/
│   ├── browser/
│   │   ├── file.ts
│   │   ├── index.ts
│   │   └── util.ts
│   ├── is/
│   │   └── index.ts
│   ├── module/
│   │   ├── change-case.ts
│   │   ├── index.ts
│   │   └── lodash-es.ts
│   ├── number/
│   │   └── index.ts
│   ├── string/
│   │   └── index.ts
│   ├── typings/
│   │   ├── atom.ts
│   │   ├── deep.ts
│   │   ├── index.ts
│   │   └── util.ts
│   ├── util/
│   │   ├── compose-promise.ts
│   │   ├── compose.ts
│   │   ├── deferred.ts
│   │   ├── delay.ts
│   │   ├── ghost.ts
│   │   ├── index.ts
│   │   ├── json.ts
│   │   ├── loop.ts
│   │   ├── map-deep.ts
│   │   ├── noop.ts
│   │   ├── pipe-promise.ts
│   │   ├── pipe.ts
│   │   ├── proxy.ts
│   │   ├── random.ts
│   │   ├── serialized.ts
│   │   ├── to-array.ts
│   │   ├── to.ts
│   │   ├── typeof.ts
│   │   ├── unit.ts
│   │   └── util.ts
│   ├── index.ts
│   └── special.ts
├── test/
│   └── index.test.ts
├── package.json
├── README.md
└── tsdown.config.ts

Source

export * from './browser'
export * from './is'
export * from './module'
export * from './number'
export * from './string'
export * from './typings'
export * from './util'

Contributors

Published under the MIT license. Made by @Hairyf and community

License

MIT License

Copyright (c) 2025-PRESENT Hairyf https://github.com/hairyf

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


auto updated with automd