1.7.7 • Published 1 year ago

@zero-dependency/utils v1.7.7

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@zero-dependency/utils

npm version npm bundle size (scoped) npm license

Installation

npm install @zero-dependency/utils
yarn add @zero-dependency/utils
pnpm add @zero-dependency/utils

Usage

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'
1.7.7

1 year ago

1.7.6

1 year ago

1.7.5

1 year ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.7.4

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.2.2

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.0.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.4

2 years ago

1.2.1

2 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago