npm package: @focme/create-util
Description: a tool lib for @focme/create-app
Author: focme
Latest version: 4.0.0

# @focme/create-util

a tool lib for @focme/create-app

## functions  

### is  

**`is.nil`**  
param  

* target: `any`  

returns `boolean`  

determine `target` is `undefined` or `null`

```javascript
import { is } from "@focme/rsk-util"

is.nil(undefined) // true
is.nil(null) // true
is.nil("") // false
is.nil(0) // false
is.nil(true) // false
is.nil({}) // false
is.nil([]) // false
```

**`is.nilEmpty`**  
param  

* target: `any`  

returns `boolean`  

determine `target` is `nil` or empty string  

```javascript
import { is } from "@focme/rsk-util"

is.nilEmpty(undefined) // true
is.nilEmpty(null) // true
is.nilEmpty("") // true
is.nil(0) // false
is.nil(true) // false
is.nil({}) // false
is.nil([]) // false
```

### createFill  

**`createFill`**  
param  

* option: `{ target, length, fill, position }`  
    > `length: number` length of the result  
    > `fill: string` the item filled into target  
    > `position: start | end | around` filling position default value `end`  

returns `string`  

```javascript
import { createFill } from "@focme/rsk-util"

const fillEnd = createFill({ length: 2, fill: "0" })
fillEnd("6") // "60"

const fillStart = createFill({ length: 2, fill: "0", position: "start" })
fillEnd("6") // "06"
```

### createCurrent  

**`createCurrent`**  
param  

* init: `any`  

returns `{ current: any }`  

a copy of `React.useRef` hook  

```javascript
import { createCurrent } from "@focme/rsk-util"

const current = createCurrent(0)
console.log(current) // { current: 0 }
```

### createJoin  

**`createJoin`**  
param  

* separator: `string`

returns `(...arg[]: any[]) => string`  

a `Array.prototyp.join.call` function  

```javascript
const { createJoin } from "@focme/rsk-util"

const join = createJoin()
join([1, 3, 5, () => 7]) // `1357`
join("-") // `1-3-5-7`
```

```javascript
const { createJoin } from "@focme/rsk-util"

const join = createJoin("-")
join([1, 3, 5, () => 7]) // `1-3-5-7`
```

**`createJoin.from`**  
param  

* ...arg[]: `any[]`

returns `(separator: string) => string`  

a `Array.prototyp.join.call` function  

```javascript
const { createJoin } from "@focme/rsk-util"

const join = createJoin.from([1, 3, 5, () => 7])
join() // `1357`
join("-") // `1-3-5-7`
```

### createTimer  

**`createTimer`**  
returns `{ check, record, records, format }`  
    > `check()` add a record  
    > `record(index: number)` get a record  
    > `records()` get all records  
    > `format(template: string, index?: number)` format a record to string  

record durations  

```javascript
const createTimer from "@focme/rsk-util"

const timer = createTimer()
timer.record() // 0
setTimeout(() => {
    timer.check()
    timer.record() // may be 10
    timer.record(-1) // may be 10
    timer.record(0) // 0
    timer.records() // may be [0, 10]
    timer.format("ss\\s") // may be 10s
    timer.format("ss\\s", 0) // 0s
}, 10)
```

**format option**  

|option|value       |example(984224415)|
|------|------------|------------------|
|SSSS  |duration    |"984224415"       |
|SSS   |milliseconds|"415"             |
|SS    |full seconds|"984224"          |
|ss    |seconds     |"44"              |
|MM    |full minutes|"16403"           |
|mm    |minutes     |"23"              |
|HH    |full hours  |"273"             |
|hh    |hours       |"9"               |
|dd    |days        |"11"              |

### createDater  

**`createDater`**  
param  

* date: `Date`  

returns `{ current, format }`  

```javascript
const { createDater } from "@focme/rsk-util"

const dater = createDater(new Date(1695019785071))
console.log(dater.current) // { YYYY, YY, ... }
dater.format("YYYY") // 2023
dater.format("YYYY-MM-DD HH:mm:ss") // "2023-09-18 14:49:45"
```

**format option**  

|option|value                                |example(1695019785071)|
|------|-------------------------------------|----------------------|
|YYYY  |Four-digit year                      |"2023"                |
|YY    |Two-digit year                       |"23"                  |
|MMMM  |The full month name                  |"September"           |
|MMM   |The abbreviated month name           |"Sep"                 |
|MM    |The month, 2-digits                  |"09"                  |
|M     |The month, beginning at 1            |"9"                   |
|DD    |The day of the month, 2-digits       |"18"                  |
|D     |The day of the month                 |"18"                  |
|ddd   |The name of the day of the week      |"Monday"              |
|dd    |The short name of the day of the week|"Mon"                 |
|d     |The day of the week, with Sunday as 0|"1"                   |
|HH    |The hour, 2-digits                   |"14"                  |
|H     |The hour                             |"14"                  |
|hh    |The hour, 12-hour clock, 2-digits    |"02"                  |
|h     |The hour, 12-hour clock              |"2"                   |
|mm    |The minute, 2-digits                 |"49"                  |
|m     |The minute                           |"49"                  |
|SSS   |The millisecond, 3-digits            |"071"                 |
|ss    |The second, 2-digits                 |"45"                  |
|s     |The second                           |"45"                  |
|A     |                                     |"PM"                  |
|a     |                                     |"pm"                  |
|ZZ    |The offset from UTC, ±HHmm           |"+0800"               |
|Z     |The offset from UTC, ±HH:mm          |"+08:00"              |

### createLimit  

promise limiter

**`createLimit`**  
param  

* limit: `number`  

returns `{ run, append, remove, on, off }`  

**`Limit.run`**  
param

* delay: `number`

returns `number`  

```javascript
const limit = createLimit(4)
const list = []
for (let i = 0; i < 10; i++>) {
    limit.append(() => {
        const target = new Promise(resolve => {
            setTimeout(() => {
                resolve(i)
            }, 1000)
        })
        list.push(target)
        return target
    })
}
limit.on("finish", () => {
    console.log("done")
    Promise.all(list).then(res => {
        console.log(res)
    })
})
limit.run()
```