https://github.com/zero-dependency/utils
π Additional utilities
https://github.com/zero-dependency/utils
Last synced: about 1 year ago
JSON representation
π Additional utilities
- Host: GitHub
- URL: https://github.com/zero-dependency/utils
- Owner: zero-dependency
- License: mit
- Created: 2022-10-14T13:01:58.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-09T14:04:31.000Z (almost 2 years ago)
- Last Synced: 2025-01-27T04:18:30.376Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 121 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @zero-dependency/utils
[](https://npm.im/@zero-dependency/utils)
[](https://bundlephobia.com/package/@zero-dependency/utils@latest)

## Installation
```sh
npm install @zero-dependency/utils
```
```sh
yarn add @zero-dependency/utils
```
```sh
pnpm add @zero-dependency/utils
```
## Usage
```ts
import {
hexToRgb,
rgbToHex,
isHexColor,
debounce,
throttle,
toNumber,
addZero,
entries,
pick,
omit,
pluralize,
randomNum,
randomToken,
generateChars,
capitalize
wait,
match
} from '@zero-dependency/utils'
// hex
console.log(hexToRgb('#000')) // { r: 0, g: 0, b: 0 }
console.log(rgbToHex({ r: 0, g: 0, b: 0 })) // #000000
console.log(isHexColor('#000')) // RegExpExecArray
console.log(isHexColor('wrong')) // null
// debounce
const debounced = debounce((msg) => console.log(msg), 1000)
// throttle
const throttled = throttle((msg) => console.log(msg), 1000)
// number
console.log(toNumber('1')) // 1
console.log(addZero(1)) // '01'
console.log(randomNum(1, 10))
// object
console.log(entries({ a: 1, b: 2 })) // [['a', 1], ['b', 2]]
console.log(pick({ a: 1, b: 2 }, ['a'])) // { a: 1 }
console.log(omit({ a: 1, b: 2 }, ['a'])) // { b: 2 }
// pluralize
const tasksPluralize = pluralize({
one: 'Π·Π°Π΄Π°Π½ΠΈΠ΅',
two: 'Π·Π°Π΄Π°Π½ΠΈΡ',
few: 'Π·Π°Π΄Π°Π½ΠΈΠΉ',
prefix: true
})
console.log(tasksPluralize(1)) // '1 Π·Π°Π΄Π°Π½ΠΈΠ΅'
console.log(tasksPluralize(3)) // '3 Π·Π°Π΄Π°Π½ΠΈΡ'
console.log(tasksPluralize(5)) // '5 Π·Π°Π΄Π°Π½ΠΈΠΉ'
console.log(tasksPluralize(999)) // '999 Π·Π°Π΄Π°Π½ΠΈΠΉ'
// string
console.log(randomToken()) // 'vpxi4hpzmy'
console.log(generateChars('a', 'd')) // ['a', 'b', 'c', 'd']
console.log(capitalize('hello')) // 'Hello'
// wait
await wait(1000)
console.log('resolve after 1s')
// pattern matching
const matcher = match<[string, string], string>((test) => ({
[test((firstName) => !firstName)]: 'User not found',
[test((firstName) => firstName.length < 8)]: (firstName, lastName) => `${firstName} ${lastName}`,
[test((firstName) => firstName.length >= 8)]: (firstName) => firstName
}))
matcher('', 'Doe') // 'User not found'
matcher('John', 'Doe') // 'John Doe'
```