@jiakun-zhao/utils 1.4.0 → 1.4.2

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.
package/dist/index.d.mts CHANGED
@@ -1,10 +1,81 @@
1
- //#region src/add-ons/assert.d.ts
1
+ //#region src/addons/assert.d.ts
2
2
  declare function assert(condition: boolean, message: string): asserts condition;
3
3
  //#endregion
4
- //#region src/add-ons/natural-compare.d.ts
4
+ //#region src/addons/deep-equal.d.ts
5
+ declare function isDeepEqual(a: any, b: any): boolean;
6
+ //#endregion
7
+ //#region src/addons/http-status-code.d.ts
8
+ declare const status: {
9
+ readonly 100: "Continue";
10
+ readonly 101: "Switching Protocols";
11
+ readonly 102: "Processing";
12
+ readonly 103: "Early Hints";
13
+ readonly 200: "OK";
14
+ readonly 201: "Created";
15
+ readonly 202: "Accepted";
16
+ readonly 203: "Non-Authoritative Information";
17
+ readonly 204: "No Content";
18
+ readonly 205: "Reset Content";
19
+ readonly 206: "Partial Content";
20
+ readonly 207: "Multi-Status";
21
+ readonly 208: "Already Reported";
22
+ readonly 226: "IM Used";
23
+ readonly 300: "Multiple Choices";
24
+ readonly 301: "Moved Permanently";
25
+ readonly 302: "Found";
26
+ readonly 303: "See Other";
27
+ readonly 304: "Not Modified";
28
+ readonly 305: "Use Proxy";
29
+ readonly 307: "Temporary Redirect";
30
+ readonly 308: "Permanent Redirect";
31
+ readonly 400: "Bad Request";
32
+ readonly 401: "Unauthorized";
33
+ readonly 402: "Payment Required";
34
+ readonly 403: "Forbidden";
35
+ readonly 404: "Not Found";
36
+ readonly 405: "Method Not Allowed";
37
+ readonly 406: "Not Acceptable";
38
+ readonly 407: "Proxy Authentication Required";
39
+ readonly 408: "Request Timeout";
40
+ readonly 409: "Conflict";
41
+ readonly 410: "Gone";
42
+ readonly 411: "Length Required";
43
+ readonly 412: "Precondition Failed";
44
+ readonly 413: "Payload Too Large";
45
+ readonly 414: "URI Too Long";
46
+ readonly 415: "Unsupported Media Type";
47
+ readonly 416: "Range Not Satisfiable";
48
+ readonly 417: "Expectation Failed";
49
+ readonly 418: "I'm a Teapot";
50
+ readonly 421: "Misdirected Request";
51
+ readonly 422: "Unprocessable Entity";
52
+ readonly 423: "Locked";
53
+ readonly 424: "Failed Dependency";
54
+ readonly 425: "Too Early";
55
+ readonly 426: "Upgrade Required";
56
+ readonly 428: "Precondition Required";
57
+ readonly 429: "Too Many Requests";
58
+ readonly 431: "Request Header Fields Too Large";
59
+ readonly 451: "Unavailable For Legal Reasons";
60
+ readonly 500: "Internal Server Error";
61
+ readonly 501: "Not Implemented";
62
+ readonly 502: "Bad Gateway";
63
+ readonly 503: "Service Unavailable";
64
+ readonly 504: "Gateway Timeout";
65
+ readonly 505: "HTTP Version Not Supported";
66
+ readonly 506: "Variant Also Negotiates";
67
+ readonly 507: "Insufficient Storage";
68
+ readonly 508: "Loop Detected";
69
+ readonly 509: "Bandwidth Limit Exceeded";
70
+ readonly 510: "Not Extended";
71
+ readonly 511: "Network Authentication Required";
72
+ };
73
+ declare function getHttpStatusMessage(code: keyof typeof status): "Continue" | "Switching Protocols" | "Processing" | "Early Hints" | "OK" | "Created" | "Accepted" | "Non-Authoritative Information" | "No Content" | "Reset Content" | "Partial Content" | "Multi-Status" | "Already Reported" | "IM Used" | "Multiple Choices" | "Moved Permanently" | "Found" | "See Other" | "Not Modified" | "Use Proxy" | "Temporary Redirect" | "Permanent Redirect" | "Bad Request" | "Unauthorized" | "Payment Required" | "Forbidden" | "Not Found" | "Method Not Allowed" | "Not Acceptable" | "Proxy Authentication Required" | "Request Timeout" | "Conflict" | "Gone" | "Length Required" | "Precondition Failed" | "Payload Too Large" | "URI Too Long" | "Unsupported Media Type" | "Range Not Satisfiable" | "Expectation Failed" | "I'm a Teapot" | "Misdirected Request" | "Unprocessable Entity" | "Locked" | "Failed Dependency" | "Too Early" | "Upgrade Required" | "Precondition Required" | "Too Many Requests" | "Request Header Fields Too Large" | "Unavailable For Legal Reasons" | "Internal Server Error" | "Not Implemented" | "Bad Gateway" | "Service Unavailable" | "Gateway Timeout" | "HTTP Version Not Supported" | "Variant Also Negotiates" | "Insufficient Storage" | "Loop Detected" | "Bandwidth Limit Exceeded" | "Not Extended" | "Network Authentication Required";
74
+ //#endregion
75
+ //#region src/addons/natural-compare.d.ts
5
76
  declare function naturalCompare<T extends string | number>(a: T, b: T): -1 | 0 | 1;
6
77
  //#endregion
7
- //#region src/add-ons/promise.d.ts
78
+ //#region src/addons/promise.d.ts
8
79
  type Awaitable<T> = T | PromiseLike<T>;
9
80
  //#endregion
10
81
  //#region src/base/array.d.ts
@@ -25,9 +96,8 @@ declare const isBoolean: (val: any) => val is boolean;
25
96
  //#region src/base/date.d.ts
26
97
  declare const isDate: (val: any) => val is Date;
27
98
  declare const timestamp: () => number;
28
- type Unit = 'year' | 'month' | 'dayOfMonth' | 'hour' | 'minute' | 'second' | 'millisecond' | 'dayOfWeek';
29
- declare function isSameDay(dateA: Date, dateB?: Date): boolean;
30
- declare function dateCalculate(date: Date, value: number, unit?: Unit): Date;
99
+ declare const isToDay: (date: Date) => boolean;
100
+ declare function isSameDay(dateA: Date, dateB: Date): boolean;
31
101
  //#endregion
32
102
  //#region src/base/function.d.ts
33
103
  type Fn<T = void> = () => T;
@@ -65,8 +135,8 @@ declare const isRegExp: (val: any) => val is RegExp;
65
135
  declare const toString: (val: any) => string;
66
136
  declare const isString: (val: unknown) => val is string;
67
137
  declare const slash: (str: string) => string;
68
- declare const ensurePrefix: (prefix: string, str: string) => string;
69
- declare const ensureSuffix: (suffix: string, str: string) => string;
138
+ declare const ensurePrefix: (str: string, prefix: string) => string;
139
+ declare const ensureSuffix: (str: string, suffix: string) => string;
70
140
  declare const capitalize: (str: string) => string;
71
141
  //#endregion
72
- export { Arrayable, Awaitable, ElementOf, Fn, Nullable, assert, capitalize, clamp, clearUndefined, createArray, dateCalculate, ensurePrefix, ensureSuffix, isArray, isBoolean, isDate, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, timestamp, toArray, toString, transform, uniq };
142
+ export { Arrayable, Awaitable, ElementOf, Fn, Nullable, assert, capitalize, clamp, clearUndefined, createArray, ensurePrefix, ensureSuffix, getHttpStatusMessage, isArray, isBoolean, isDate, isDeepEqual, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isToDay, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, status, timestamp, toArray, toString, transform, uniq };
package/dist/index.mjs CHANGED
@@ -1,10 +1,98 @@
1
- //#region src/add-ons/assert.ts
1
+ //#region src/addons/assert.ts
2
2
  function assert(condition, message) {
3
3
  if (!condition) throw new Error(message);
4
4
  }
5
5
 
6
6
  //#endregion
7
- //#region src/add-ons/natural-compare.ts
7
+ //#region src/addons/deep-equal.ts
8
+ function isDeepEqual(a, b) {
9
+ if (a === b) return true;
10
+ const constructor = a?.constructor;
11
+ if (constructor !== b?.constructor) return false;
12
+ if (constructor === Array) return a.length === b.length && a.every((item, i) => isDeepEqual(item, b[i]));
13
+ if (constructor === Object) {
14
+ const aKeys = Object.keys(a);
15
+ const bKeys = Object.keys(b);
16
+ return aKeys.length === bKeys.length && aKeys.every((key) => isDeepEqual(a[key], b[key]));
17
+ }
18
+ if (constructor === RegExp) return a.toString() === b.toString();
19
+ if (constructor === Date) return a.getTime() === b.getTime();
20
+ return Object.is(a, b);
21
+ }
22
+
23
+ //#endregion
24
+ //#region src/addons/http-status-code.ts
25
+ const status = {
26
+ 100: "Continue",
27
+ 101: "Switching Protocols",
28
+ 102: "Processing",
29
+ 103: "Early Hints",
30
+ 200: "OK",
31
+ 201: "Created",
32
+ 202: "Accepted",
33
+ 203: "Non-Authoritative Information",
34
+ 204: "No Content",
35
+ 205: "Reset Content",
36
+ 206: "Partial Content",
37
+ 207: "Multi-Status",
38
+ 208: "Already Reported",
39
+ 226: "IM Used",
40
+ 300: "Multiple Choices",
41
+ 301: "Moved Permanently",
42
+ 302: "Found",
43
+ 303: "See Other",
44
+ 304: "Not Modified",
45
+ 305: "Use Proxy",
46
+ 307: "Temporary Redirect",
47
+ 308: "Permanent Redirect",
48
+ 400: "Bad Request",
49
+ 401: "Unauthorized",
50
+ 402: "Payment Required",
51
+ 403: "Forbidden",
52
+ 404: "Not Found",
53
+ 405: "Method Not Allowed",
54
+ 406: "Not Acceptable",
55
+ 407: "Proxy Authentication Required",
56
+ 408: "Request Timeout",
57
+ 409: "Conflict",
58
+ 410: "Gone",
59
+ 411: "Length Required",
60
+ 412: "Precondition Failed",
61
+ 413: "Payload Too Large",
62
+ 414: "URI Too Long",
63
+ 415: "Unsupported Media Type",
64
+ 416: "Range Not Satisfiable",
65
+ 417: "Expectation Failed",
66
+ 418: "I'm a Teapot",
67
+ 421: "Misdirected Request",
68
+ 422: "Unprocessable Entity",
69
+ 423: "Locked",
70
+ 424: "Failed Dependency",
71
+ 425: "Too Early",
72
+ 426: "Upgrade Required",
73
+ 428: "Precondition Required",
74
+ 429: "Too Many Requests",
75
+ 431: "Request Header Fields Too Large",
76
+ 451: "Unavailable For Legal Reasons",
77
+ 500: "Internal Server Error",
78
+ 501: "Not Implemented",
79
+ 502: "Bad Gateway",
80
+ 503: "Service Unavailable",
81
+ 504: "Gateway Timeout",
82
+ 505: "HTTP Version Not Supported",
83
+ 506: "Variant Also Negotiates",
84
+ 507: "Insufficient Storage",
85
+ 508: "Loop Detected",
86
+ 509: "Bandwidth Limit Exceeded",
87
+ 510: "Not Extended",
88
+ 511: "Network Authentication Required"
89
+ };
90
+ function getHttpStatusMessage(code) {
91
+ return status[code];
92
+ }
93
+
94
+ //#endregion
95
+ //#region src/addons/natural-compare.ts
8
96
  function naturalCompare(a, b) {
9
97
  var i, codeA, codeB = 1, posA = 0, posB = 0, alphabet = String.alphabet;
10
98
  function getCode(str, pos, code) {
@@ -61,48 +149,18 @@ const isBoolean = (val) => typeof val === "boolean";
61
149
  const toString = (val) => Object.prototype.toString.call(val);
62
150
  const isString = (val) => typeof val === "string";
63
151
  const slash = (str) => str.replace(/\\/g, "/");
64
- const ensurePrefix = (prefix, str) => str.startsWith(prefix) ? str : prefix + str;
65
- const ensureSuffix = (suffix, str) => str.endsWith(suffix) ? str : str + suffix;
152
+ const ensurePrefix = (str, prefix) => str.startsWith(prefix) ? str : prefix + str;
153
+ const ensureSuffix = (str, suffix) => str.endsWith(suffix) ? str : str + suffix;
66
154
  const capitalize = (str) => str[0].toUpperCase() + str.slice(1).toLowerCase();
67
155
 
68
156
  //#endregion
69
157
  //#region src/base/date.ts
70
158
  const isDate = (val) => toString(val) === "[object Date]";
71
159
  const timestamp = () => +Date.now();
72
- function isSameDay(dateA, dateB = /* @__PURE__ */ new Date()) {
160
+ const isToDay = (date) => isSameDay(date, /* @__PURE__ */ new Date());
161
+ function isSameDay(dateA, dateB) {
73
162
  return dateA.getDate() === dateB.getDate() && dateA.getMonth() === dateB.getMonth() && dateA.getFullYear() === dateB.getFullYear();
74
163
  }
75
- function dateCalculate(date, value, unit = "dayOfMonth") {
76
- const result = new Date(date);
77
- switch (unit) {
78
- case "year":
79
- result.setFullYear(date.getFullYear() + value);
80
- break;
81
- case "month":
82
- result.setMonth(date.getMonth() + value);
83
- break;
84
- case "dayOfMonth":
85
- result.setDate(date.getDate() + value);
86
- break;
87
- case "dayOfWeek":
88
- result.setDate(date.getDay() + value);
89
- break;
90
- case "hour":
91
- result.setHours(date.getHours() + value);
92
- break;
93
- case "minute":
94
- result.setMinutes(date.getMinutes() + value);
95
- break;
96
- case "second":
97
- result.setSeconds(date.getSeconds() + value);
98
- break;
99
- case "millisecond":
100
- result.setMilliseconds(date.getMilliseconds() + value);
101
- break;
102
- default: return date;
103
- }
104
- return result;
105
- }
106
164
 
107
165
  //#endregion
108
166
  //#region src/base/function.ts
@@ -151,4 +209,4 @@ function clearUndefined(obj) {
151
209
  const isRegExp = (val) => toString(val) === "[object RegExp]";
152
210
 
153
211
  //#endregion
154
- export { assert, capitalize, clamp, clearUndefined, createArray, dateCalculate, ensurePrefix, ensureSuffix, isArray, isBoolean, isDate, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, timestamp, toArray, toString, transform, uniq };
212
+ export { assert, capitalize, clamp, clearUndefined, createArray, ensurePrefix, ensureSuffix, getHttpStatusMessage, isArray, isBoolean, isDate, isDeepEqual, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isToDay, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, status, timestamp, toArray, toString, transform, uniq };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jiakun-zhao/utils",
3
3
  "type": "module",
4
- "version": "1.4.0",
4
+ "version": "1.4.2",
5
5
  "description": "Utils.",
6
6
  "author": "Jiakun Zhao <hi@zhaojiakun.com>",
7
7
  "license": "MIT",