# @car_han/utils

> JS开发工具包

Latest version **1.1.11** (published 2024-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @car_han/utils
pnpm add @car_han/utils
yarn add @car_han/utils
bun add @car_han/utils
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.11 |
| Published | 2024-03-11 |
| First published | 2022-10-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | hsx |
| Maintainers | car_han |
| Keywords | typescript, ts, express, log, typeof, timer |

## Links

- npm: https://www.npmjs.com/package/@car_han/utils
- Repository: https://github.com/hands0meX/npm-utils
- Issues: https://github.com/hands0meX/npm-utils/issues
- npm.io page: https://npm.io/package/@car_han/utils

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.1.11 (latest) — 2024-03-11
- 1.1.7 — 2024-03-11
- 1.1.6 — 2024-03-11
- 1.1.5 — 2024-03-10
- 1.1.4 — 2024-03-10
- 1.1.3 — 2024-03-10
- 1.1.2 — 2024-03-10
- 1.1.1 — 2024-03-05
- 1.1.0 — 2024-03-03
- 1.0.21 — 2024-03-03
- 1.0.20 — 2023-02-08
- 1.0.19 — 2023-02-08
- 1.0.17 — 2023-02-08
- 1.0.16 — 2022-11-30
- 1.0.15 — 2022-11-29
- … 15 more at https://npm.io/package/@car_han/utils/versions

## README

[license-image]: https://img.shields.io/npm/l/%40car_han%2Futils
[license-url]: https://github.com/hands0meX/npm-utils/blob/main/LICENSE

<header style="display:flex;align-items:center; justify-content:center">
    <img style="width:100px;height: 80px" src="./imgs/bag.png"/>
    <h1>npm utils package</h1>
</header>
<p style="text-align:center">
    <img src="https://img.shields.io/npm/v/%40car_han%2Futils"/>
    <img src="https://img.shields.io/npm/dy/%40car_han%2Futils"/>
    <a herf="https://github.com/hands0meX/npm-utils/blob/main/LICENSE">
        <img src="https://img.shields.io/npm/l/%40car_han%2Futils"/>
    </a>
</p>



## Usage

-   [LOG](#LOG)
-   [TYPE_TEST](#type_test)
    -   [is\*](#is)
    -   [typeOf](#typeof)
-   [Time](#time)
-   [Timer](#timer)
-   [DataUtils](#dataUtils)
-   [HttpHelper](#httphelper)
-   [ProjHelper](#projecthelper)
-   [Reg](#reg)

> Try it in the **example** directory.

### LOG
```ts
import { Console } from "@car_han/utils";
Console.DIR = process.cwd(); //Set the root directory path of your project

Console.log(123);
// res => at ${FnName} (/examples/test.ts:4:9)

Console.table(["foo", "bar"]);
// ┌─────────┬────────┐
// │ (index) │ Values │
// ├─────────┼────────┤
// │    0    │ 'foo'  │
// │    1    │ 'bar'  │
// └─────────┴────────┘
// at ${FnName} (/examples/test.ts:4:9)
```

>

### TYPE_TEST

#### is\*

> Determine the type of determination

```ts
// _ TYPE_TEST
const fn = () => {};
const a = 1;
const b = {};
const c = [];
const d = [1];
const e = { foo: "bar" };
const f = "";
const g = "123";
Console.log(T.isFn(fn));
Console.log(T.isNum(a));
Console.log(T.isObj(b));
Console.log(T.isArray(c));
Console.log(T.isValidArray(d));
Console.log(T.isValidObj(e));
Console.log(T.isStr(f));
Console.log(T.isValidStr(g));
// true at Object.<anonymous> (/examples/test.ts:23:9)
// true at Object.<anonymous> (/examples/test.ts:24:9)
// true at Object.<anonymous> (/examples/test.ts:25:9)
// true at Object.<anonymous> (/examples/test.ts:26:9)
// true at Object.<anonymous> (/examples/test.ts:27:9)
// true at Object.<anonymous> (/examples/test.ts:28:9)
// true at Object.<anonymous> (/examples/test.ts:29:9)
// true at Object.<anonymous> (/examples/test.ts:30:9)
```

#### typeOf

> When you are not sure about the type, determine the type it belongs to
> **includes**

-   string
-   object
-   array
-   number
-   null
-   undefined
-   regex
-   date

```ts
// typeOf
Console.log(T.typeOf(Symbol("a")));
Console.log(T.typeOf(new Date()));
Console.log(T.typeOf(undefined));
Console.log(T.typeOf(null));
// symbol at Object.<anonymous> (/examples/test.ts:39:9)
// date at Object.<anonymous> (/examples/test.ts:40:9)
// undefined at Object.<anonymous> (/examples/test.ts:41:9)
// null at Object.<anonymous> (/examples/test.ts:42:9)
```

### Time

> Y => Year, M => Month, D => Date, h => hour, m: minutes, s: seconds.

```ts
console.log(Time.datefmt(new Date())); // default
// 2022/11/14 15:42:20
console.log(Time.datefmt(new Date(), "D-m")); // change fmtPattern
// 14/45
console.log(Time.datefmt(new Date(), undefined, ":*")); // change fmt modifer
// 2022:11:14 15*47*13
```

### Timer

```ts
let bubbleCount = 0;
const timerFn = (...args) => {
    Console.log("bubble count", ++bubbleCount);
    Console.log("params", args);
};

let testTimer: undefined | NodeJS.Timer;
const NOW = Date.now();
const debounceImplFn = Timer.debounce(timerFn, 1000);
const throttleImplFn = Timer.throttle(timerFn, 600);
testTimer = setInterval(() => {
    if (Date.now() - NOW > 3000) {
        clearInterval(testTimer);
        testTimer = undefined;
    }
    debounceImplFn("debounce", "foo");
    throttleImplFn("throttle", "bar");
}, 16);

// bubble count 1 at timerFn (/examples/test.ts:41:10)
// params [ 'throttle', 'bar' ] at timerFn (/examples/test.ts:42:10)
// bubble count 2 at timerFn (/examples/test.ts:41:10)
// params [ 'throttle', 'bar' ] at timerFn (/examples/test.ts:42:10)
// bubble count 3 at timerFn (/examples/test.ts:41:10)
// params [ 'throttle', 'bar' ] at timerFn (/examples/test.ts:42:10)
// bubble count 4 at timerFn (/examples/test.ts:41:10)
// params [ 'throttle', 'bar' ] at timerFn (/examples/test.ts:42:10)
// bubble count 5 at timerFn (/examples/test.ts:41:10)
// params [ 'throttle', 'bar' ] at timerFn (/examples/test.ts:42:10)
// bubble count 6 at timerFn (/examples/test.ts:41:10)
// params [ 'debounce', 'foo' ] at timerFn (/examples/test.ts:42:10)
```

### DataUtils

-   String
    generate random string, default len: 16

```ts
Console.log(Str.rollingRandomString(32));
// bbplbmjpdzmnlujnfvdpufdnhthewkzz at Object.<anonymous> (/examples/test.ts:68:9)
```

### HttpHelper

-   uri
    -   obj2query

```ts
const res = HttpHelper.obj2Query({
    test: 13,
    hh: "tt",
});

Console.log(res);
//?test=13&hh=tt at Object.<anonymous> (/src/dev/HttpHelper.ts:27:9)
```

### ProjectHelper

> Use the publish-subscribe model to publish and subscribe to the message bus
```ts
const emitter = new EventBus();
emitter.on("foo", (payload) => {
    Console.log(payload);
});
emitter.fire("foo", 123);
emitter.fire("foo", 321);
// 123 at /src/dev/ProjectUtils/EventBus.ts:38:13
// 321 at /src/dev/ProjectUtils/EventBus.ts:38:13
```

### Reg
> Some commonly used regular matches.
- isPhone
- isEmail
- isIdCard
- isPassword
- isUrl
- isIp
- isPostalCode
- isChinese

---
_Source: https://npm.io/package/@car_han/utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
