1.1.11 • Published 9 months ago

@car_han/utils v1.1.11

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Usage

Try it in the example directory.

LOG

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

// _ 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
// 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.

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

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
Console.log(Str.rollingRandomString(32));
// bbplbmjpdzmnlujnfvdpufdnhthewkzz at Object.<anonymous> (/examples/test.ts:68:9)

HttpHelper

  • uri
    • obj2query
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

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
1.1.7

9 months ago

1.1.6

9 months ago

1.1.11

9 months ago

1.1.5

9 months ago

1.1.4

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.21

9 months ago

1.0.19

2 years ago

1.0.17

2 years ago

1.0.20

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago