xjs-common 8.0.3 → 8.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ export declare enum TimeUnit {
2
+ Year = 1,
3
+ Month = 2,
4
+ Day = 3,
5
+ Hour = 4,
6
+ Min = 5,
7
+ Sec = 6,
8
+ Msec = 7
9
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimeUnit = void 0;
4
+ var TimeUnit;
5
+ (function (TimeUnit) {
6
+ TimeUnit[TimeUnit["Year"] = 1] = "Year";
7
+ TimeUnit[TimeUnit["Month"] = 2] = "Month";
8
+ TimeUnit[TimeUnit["Day"] = 3] = "Day";
9
+ TimeUnit[TimeUnit["Hour"] = 4] = "Hour";
10
+ TimeUnit[TimeUnit["Min"] = 5] = "Min";
11
+ TimeUnit[TimeUnit["Sec"] = 6] = "Sec";
12
+ TimeUnit[TimeUnit["Msec"] = 7] = "Msec";
13
+ })(TimeUnit = exports.TimeUnit || (exports.TimeUnit = {}));
@@ -1,6 +1,17 @@
1
1
  export type IndexSignature = string | number | symbol;
2
2
  export type NormalRecord = Record<IndexSignature, any>;
3
3
  export type MaybeArray<T> = T | T[];
4
+ export type Loggable = {
5
+ log: (msg: any) => void;
6
+ warn: (msg: any) => void;
7
+ error: (msg: any) => void;
8
+ };
9
+ export type Unique<T = number> = {
10
+ id: T;
11
+ };
12
+ export type IdName<T = number> = {
13
+ name: string;
14
+ } & Unique<T>;
4
15
  export declare enum Type {
5
16
  string = "string",
6
17
  number = "number",
@@ -22,7 +22,10 @@ var UArray;
22
22
  if (op?.k)
23
23
  return Array.from((0, u_1.array2map)(array, e => e[op.k]).values()).map(a => op?.takeLast ? a.pop() : a.shift());
24
24
  const a = op?.takeLast ? [...array].reverse() : array;
25
- return a.filter((v, i) => a.findIndex(v2 => op?.predicate ? op?.predicate(v, v2) : v == v2) === i);
25
+ const p = op?.predicate ?? ((v1, v2) => v1 == v2);
26
+ const result = [a.shift()];
27
+ a.forEach(v => result.some(v2 => p(v, v2)) ? {} : result.push(v));
28
+ return result;
26
29
  }
27
30
  UArray.distinct = distinct;
28
31
  function chop(array, len) {
@@ -1,7 +1,16 @@
1
+ import { TimeUnit } from "../const/time-unit";
1
2
  export declare namespace UString {
2
3
  function eq(s1: string, s2: string): boolean;
3
- function simpleDate2sec(date?: Date): string;
4
- function simpleDate2day(date?: Date): string;
4
+ /**
5
+ * generate date time number as fixed length (depends on `unit`) string without separator charactor.
6
+ * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
7
+ * @param op.date Date object refered by this. default is `new Date()`.
8
+ * @param op.unit time unit. default is secound.
9
+ */
10
+ function simpleTime(op?: {
11
+ date?: Date;
12
+ unit?: TimeUnit;
13
+ }): string;
5
14
  function trimProps(obj: any): void;
6
15
  function generateRandomString(len: number): string;
7
16
  function idx2az(idx: number): string;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UString = void 0;
4
+ const time_unit_1 = require("../const/time-unit");
4
5
  const xjs_err_1 = require("../obj/xjs-err");
5
6
  const u_1 = require("./u");
6
7
  const u_type_1 = require("./u-type");
@@ -11,14 +12,19 @@ var UString;
11
12
  return !u_type_1.UType.isString(s1) || !u_type_1.UType.isString(s2) ? s1 === s2 : s1.trim() === s2.trim();
12
13
  }
13
14
  UString.eq = eq;
14
- function simpleDate2sec(date) {
15
- return (date ?? new Date()).toISOString().split(".")[0].replace(/[-T:]/g, "");
15
+ /**
16
+ * generate date time number as fixed length (depends on `unit`) string without separator charactor.
17
+ * For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
18
+ * @param op.date Date object refered by this. default is `new Date()`.
19
+ * @param op.unit time unit. default is secound.
20
+ */
21
+ function simpleTime(op) {
22
+ const t = (op?.date ?? new Date()).toISOString().split(".")[0].replace(/[-T:]/g, "");
23
+ if (op?.unit === time_unit_1.TimeUnit.Msec)
24
+ return t;
25
+ return t.substring(0, 14 - (6 - (op?.unit ?? time_unit_1.TimeUnit.Sec)) * 2);
16
26
  }
17
- UString.simpleDate2sec = simpleDate2sec;
18
- function simpleDate2day(date) {
19
- return simpleDate2sec(date).substring(0, 8);
20
- }
21
- UString.simpleDate2day = simpleDate2day;
27
+ UString.simpleTime = simpleTime;
22
28
  function trimProps(obj) {
23
29
  Object.keys(obj)
24
30
  .filter(k => typeof obj[k] === "string")
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./const/types";
2
+ export * from "./const/time-unit";
2
3
  export * from "./const/gender";
3
4
  export * from "./const/http-method";
4
5
  export * from "./func/u";
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.s_clientMode = exports.HttpResolver = exports.DType = void 0;
18
18
  __exportStar(require("./const/types"), exports);
19
+ __exportStar(require("./const/time-unit"), exports);
19
20
  __exportStar(require("./const/gender"), exports);
20
21
  __exportStar(require("./const/http-method"), exports);
21
22
  __exportStar(require("./func/u"), exports);
@@ -1,5 +1,6 @@
1
1
  import { ClientMode } from "./http-resolver";
2
2
  import { ClientOption, IHttpClient, RequestOption } from "./i-http-client";
3
+ import { Loggable } from "../../const/types";
3
4
  export declare const s_clientMode: Record<string, ClientMode>;
4
5
  export declare class HttpResolverContext implements IHttpClient {
5
6
  private readonly _cmv;
@@ -10,10 +11,7 @@ export declare class HttpResolverContext implements IHttpClient {
10
11
  private readonly _proxyConfig?;
11
12
  private readonly _chHeaders;
12
13
  private _cookies?;
13
- constructor(_cmv: number, op?: ClientOption, _l?: {
14
- log: (msg: any) => void;
15
- warn: (msg: any) => void;
16
- });
14
+ constructor(_cmv: number, op?: ClientOption, _l?: Loggable);
17
15
  /**
18
16
  * request GET to the url.
19
17
  * @param url target url.
@@ -1,3 +1,4 @@
1
+ import { Loggable } from "../../const/types";
1
2
  import { HttpResolverContext } from "./http-resolver-context";
2
3
  import { ClientOption, IHttpClient, RequestOption } from "./i-http-client";
3
4
  export interface ClientMode {
@@ -19,10 +20,7 @@ export declare class HttpResolver implements IHttpClient {
19
20
  * @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
20
21
  * @param _l custom logger. default is `console`.
21
22
  */
22
- constructor(_baseCmv: number, _l?: {
23
- log: (msg: any) => void;
24
- warn: (msg: any) => void;
25
- });
23
+ constructor(_baseCmv: number, _l?: Loggable);
26
24
  /**
27
25
  * create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
28
26
  * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-common",
3
- "version": "8.0.3",
3
+ "version": "8.1.0",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",