Skip to content

petite-utilsDocs


petite-utils

A collection of small util function in JavaScript.

features:

  • small and fast.
  • node.js and browser support.
  • tree-shakable.
  • high unit test coverage.

Installation

bash
# npm
npm i petite-utils
# yarn
yarn add petite-utils
# pnpm
pnpm add petite-utils

用法:

js
// cjs module
const { randomStr } = require('petite-utils')
// ES module
import { randomStr } from 'petite-utils'

use esm in html by cdn

html
<!-- load ES module -->
<script type="module">
  import { randomStr } from 'https://unpkg.com/petite-utils/dist/index.js'

  const result = randomStr(20, 50)
  console.log(result)
  console.log('test in browser using esm')
</script>

use umd in html by cdn

html
<!-- load umd -->
<script src="https://unpkg.com/petite-utils"></script>
<!-- then use function from PU object -->
<script>
  const str = PU.randomStr()
</script>

isNumber

check if the value is a number.

js
import { isNumber, repeatRun } from 'petite-utils'

isNumber(1) // true
isNumber('1') // true
isNumber(4.917736942280289e-10) // true
isNumber(BigInt('0b11111111111111111111111111111111111111111111111111111')) // true
isNumber(NaN) // false
isNumber('a') // false

repeatRun(
  (time, stop) => {
    if (time === 10) {
      stop()
    }
    console.log('repeat run me!')
  },
  { interval: 1000 },
)

unit test coverage

functioncoverage
debounce100%
throttle100%
randomNum100%
randomStr100%
repeatRun100%
chunk100%
uuid100%
isNull100%
isUndefined100%
isNullish100%
isObject100%
isArray100%
isBoolean100%
isDate100%
isError100%
isNumber100%
isNumerical100%
isString100%
isSymbol100%
isRegExp100%
isPrimitive100%
isFalsy100%
isTruthy100%
isFunction100%
isEmptyStr100%
isEmpty100%

more usages see petite-utils -- document

Released under the MIT License.