@ntnyq/utils 0.6.2 → 0.6.4

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/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  [![CI](https://github.com/ntnyq/utils/workflows/CI/badge.svg)](https://github.com/ntnyq/utils/actions)
4
4
  [![NPM VERSION](https://img.shields.io/npm/v/@ntnyq/utils.svg)](https://www.npmjs.com/package/@ntnyq/utils)
5
5
  [![NPM DOWNLOADS](https://img.shields.io/npm/dy/@ntnyq/utils.svg)](https://www.npmjs.com/package/@ntnyq/utils)
6
- [![CODECOV](https://codecov.io/github/ntnyq/utils/branch/main/graph/badge.svg)](https://codecov.io/github/ntnyq/utils)
7
6
  [![LICENSE](https://img.shields.io/github/license/ntnyq/utils.svg)](https://github.com/ntnyq/utils/blob/main/LICENSE)
8
7
 
9
8
  > Common used utils.
package/dist/index.cjs CHANGED
@@ -25,6 +25,7 @@ __export(index_exports, {
25
25
  RE_BLOCK_COMMENT: () => RE_BLOCK_COMMENT,
26
26
  RE_COMMENTS: () => RE_COMMENTS,
27
27
  RE_LINE_COMMENT: () => RE_LINE_COMMENT,
28
+ SPECIAL_CHAR: () => SPECIAL_CHAR,
28
29
  at: () => at,
29
30
  cAF: () => cAF,
30
31
  chunk: () => chunk,
@@ -58,6 +59,7 @@ __export(index_exports, {
58
59
  isEmptyStringOrWhitespace: () => isEmptyStringOrWhitespace,
59
60
  isError: () => isError,
60
61
  isFunction: () => isFunction,
62
+ isHTMLElement: () => isHTMLElement,
61
63
  isInteger: () => isInteger,
62
64
  isIterable: () => isIterable,
63
65
  isMap: () => isMap,
@@ -86,6 +88,7 @@ __export(index_exports, {
86
88
  noop: () => noop,
87
89
  omit: () => omit,
88
90
  once: () => once,
91
+ openExternalURL: () => openExternalURL,
89
92
  pick: () => pick,
90
93
  rAF: () => rAF,
91
94
  randomHexColor: () => randomHexColor,
@@ -129,35 +132,12 @@ function once(func) {
129
132
  };
130
133
  }
131
134
 
132
- // src/is/isDeepEqual.ts
133
- function isDeepEqual(value1, value2) {
134
- const type1 = getObjectType(value1);
135
- const type2 = getObjectType(value2);
136
- if (type1 !== type2) {
137
- return false;
138
- }
139
- if (isArray(value1) && isArray(value2)) {
140
- if (value1.length !== value2.length) {
141
- return false;
142
- }
143
- return value1.every((item, index) => isDeepEqual(item, value2[index]));
144
- }
145
- if (isObject(value1) && isObject(value2)) {
146
- const keys = Object.keys(value1);
147
- if (keys.length !== Object.keys(value2).length) {
148
- return false;
149
- }
150
- return keys.every(
151
- (key) => isDeepEqual(
152
- value1[key],
153
- value2[key]
154
- )
155
- );
156
- }
157
- return Object.is(value1, value2);
135
+ // src/is/dom.ts
136
+ function isHTMLElement(value) {
137
+ return typeof value === "object" && value !== null && "nodeType" in value && value.nodeType === Node.ELEMENT_NODE && value instanceof HTMLElement;
158
138
  }
159
139
 
160
- // src/is/index.ts
140
+ // src/is/core.ts
161
141
  function getObjectType(value) {
162
142
  return Object.prototype.toString.call(value).slice(8, -1);
163
143
  }
@@ -262,6 +242,34 @@ function isIterable(value) {
262
242
  return isFunction(value?.[Symbol.iterator]);
263
243
  }
264
244
 
245
+ // src/is/isDeepEqual.ts
246
+ function isDeepEqual(value1, value2) {
247
+ const type1 = getObjectType(value1);
248
+ const type2 = getObjectType(value2);
249
+ if (type1 !== type2) {
250
+ return false;
251
+ }
252
+ if (isArray(value1) && isArray(value2)) {
253
+ if (value1.length !== value2.length) {
254
+ return false;
255
+ }
256
+ return value1.every((item, index) => isDeepEqual(item, value2[index]));
257
+ }
258
+ if (isObject(value1) && isObject(value2)) {
259
+ const keys = Object.keys(value1);
260
+ if (keys.length !== Object.keys(value2).length) {
261
+ return false;
262
+ }
263
+ return keys.every(
264
+ (key) => isDeepEqual(
265
+ value1[key],
266
+ value2[key]
267
+ )
268
+ );
269
+ }
270
+ return Object.is(value1, value2);
271
+ }
272
+
265
273
  // src/dom/scrollIntoView.ts
266
274
  function scrollElementIntoView(element, options = {}) {
267
275
  const body = document.body;
@@ -279,6 +287,13 @@ function scrollElementIntoView(element, options = {}) {
279
287
  }
280
288
  }
281
289
 
290
+ // src/dom/openExternalURL.ts
291
+ function openExternalURL(url, options = {}) {
292
+ const { target = "_blank" } = options;
293
+ const proxy = window.open(url, target);
294
+ return proxy;
295
+ }
296
+
282
297
  // src/dom/isVisibleInViewport.ts
283
298
  function isElementVisibleInViewport(element, targetWindow = window) {
284
299
  const { top, left, bottom, right } = element.getBoundingClientRect();
@@ -808,6 +823,11 @@ function sortObject(obj, options = {}) {
808
823
  return sortKeys(obj);
809
824
  }
810
825
 
826
+ // src/constants/char.ts
827
+ var SPECIAL_CHAR = {
828
+ newline: "\n"
829
+ };
830
+
811
831
  // src/constants/regexp.ts
812
832
  var RE_COMMENTS = /(?:<!--|\/\*)([\s\S]*?)(?:-->|\*\/)/g;
813
833
  var RE_LINE_COMMENT = /\/\/.*/;
@@ -819,6 +839,7 @@ var RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
819
839
  RE_BLOCK_COMMENT,
820
840
  RE_COMMENTS,
821
841
  RE_LINE_COMMENT,
842
+ SPECIAL_CHAR,
822
843
  at,
823
844
  cAF,
824
845
  chunk,
@@ -852,6 +873,7 @@ var RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
852
873
  isEmptyStringOrWhitespace,
853
874
  isError,
854
875
  isFunction,
876
+ isHTMLElement,
855
877
  isInteger,
856
878
  isIterable,
857
879
  isMap,
@@ -880,6 +902,7 @@ var RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
880
902
  noop,
881
903
  omit,
882
904
  once,
905
+ openExternalURL,
883
906
  pick,
884
907
  rAF,
885
908
  randomHexColor,
package/dist/index.d.cts CHANGED
@@ -10,9 +10,14 @@ declare const NOOP: typeof noop;
10
10
  declare function once<T extends unknown[]>(func: (...args: T) => void): (this: unknown, ...args: T) => boolean;
11
11
 
12
12
  /**
13
- * check if two values are deeply equal
13
+ * @file is/dom.ts
14
14
  */
15
- declare function isDeepEqual(value1: any, value2: any): boolean;
15
+ /**
16
+ * Check if given value is an HTMLElement
17
+ * @param value - The value to check
18
+ * @returns True if the value is an HTMLElement, false otherwise
19
+ */
20
+ declare function isHTMLElement(value: unknown): value is HTMLElement;
16
21
 
17
22
  /**
18
23
  * @file is utils
@@ -57,6 +62,11 @@ declare function isNativePromise<T = unknown>(value: unknown): value is Promise<
57
62
  declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
58
63
  declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
59
64
 
65
+ /**
66
+ * check if two values are deeply equal
67
+ */
68
+ declare function isDeepEqual(value1: any, value2: any): boolean;
69
+
60
70
  interface Options extends ScrollIntoViewOptions {
61
71
  /**
62
72
  * @default `document.body`
@@ -71,6 +81,83 @@ interface Options extends ScrollIntoViewOptions {
71
81
  */
72
82
  declare function scrollElementIntoView(element: HTMLElement, options?: Options): void;
73
83
 
84
+ type JsonArray = JsonValue[] | readonly JsonValue[];
85
+ type JsonObject = {
86
+ [Key in string]: JsonValue;
87
+ } & {
88
+ [Key in string]?: JsonValue | undefined;
89
+ };
90
+ type JsonPrimitive = boolean | number | string | null;
91
+ /**
92
+ * @copyright {@link https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts}
93
+ */
94
+ type JsonValue = JsonArray | JsonObject | JsonPrimitive;
95
+
96
+ /**
97
+ * A literal type that supports custom further strings but preserves autocompletion in IDEs.
98
+ *
99
+ * @see https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
100
+ */
101
+ type LiteralUnion<Union extends Base, Base = string> = Union | (Base & {
102
+ zz_IGNORE_ME?: never;
103
+ });
104
+ /**
105
+ * Non empty object `{}`
106
+ */
107
+ type NonEmptyObject<T> = T extends Record<string, never> ? never : T;
108
+
109
+ /**
110
+ * interop module
111
+ */
112
+ type InteropModuleDefault<T> = T extends {
113
+ default: infer U;
114
+ } ? U : T;
115
+
116
+ type AnyFn<T = any, R = any> = (...args: T[]) => R;
117
+ type Arrayable<T> = T | T[];
118
+ type Awaitable<T> = Promise<T> | T;
119
+ type Callable<T> = AnyFn<any, T> | T;
120
+ type MayBe<T> = T | undefined;
121
+ type Nullable<T> = T | null;
122
+ /**
123
+ * Overwrite some keys type
124
+ */
125
+ type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
126
+ /**
127
+ * Prettify object type
128
+ */
129
+ type Prettify<T> = {
130
+ [K in keyof T]: T[K];
131
+ } & {};
132
+ type PrettifyV2<T> = Omit<T, never>;
133
+ type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
134
+ /**
135
+ * Resolve `boolean | Record<string, any>` to `Record<string, any>`
136
+ */
137
+ type ResolvedOptions<T> = T extends boolean ? never : NonNullable<T>;
138
+
139
+ interface OpenExternalURLOptions {
140
+ /**
141
+ * open target
142
+ *
143
+ * @default `_blank`
144
+ */
145
+ target?: LiteralUnion<'_self' | '_top' | '_blank' | '_parent'>;
146
+ }
147
+ /**
148
+ * Open external url
149
+ * @param url - URL to open
150
+ * @param options - open options
151
+ * @returns window proxy
152
+ */
153
+ declare function openExternalURL(url: string | URL, options?: OpenExternalURLOptions): WindowProxy | null;
154
+
155
+ /**
156
+ * Check if element is in viewport
157
+ * @param element - checked element
158
+ * @param targetWindow - window
159
+ * @returns true if element is in viewport, false otherwise
160
+ */
74
161
  declare function isElementVisibleInViewport(element: HTMLElement, targetWindow?: Window): boolean;
75
162
 
76
163
  /**
@@ -208,61 +295,6 @@ declare function unique<T>(array: T[]): T[];
208
295
  */
209
296
  declare function uniqueBy<T>(array: T[], equalFn: (a: T, b: T) => boolean): T[];
210
297
 
211
- type JsonArray = JsonValue[] | readonly JsonValue[];
212
- type JsonObject = {
213
- [Key in string]: JsonValue;
214
- } & {
215
- [Key in string]?: JsonValue | undefined;
216
- };
217
- type JsonPrimitive = boolean | number | string | null;
218
- /**
219
- * @copyright {@link https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts}
220
- */
221
- type JsonValue = JsonArray | JsonObject | JsonPrimitive;
222
-
223
- /**
224
- * A literal type that supports custom further strings but preserves autocompletion in IDEs.
225
- *
226
- * @see https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
227
- */
228
- type LiteralUnion<Union extends Base, Base = string> = Union | (Base & {
229
- zz_IGNORE_ME?: never;
230
- });
231
- /**
232
- * Non empty object `{}`
233
- */
234
- type NonEmptyObject<T> = T extends Record<string, never> ? never : T;
235
-
236
- /**
237
- * interop module
238
- */
239
- type InteropModuleDefault<T> = T extends {
240
- default: infer U;
241
- } ? U : T;
242
-
243
- type AnyFn<T = any, R = any> = (...args: T[]) => R;
244
- type Arrayable<T> = T | T[];
245
- type Awaitable<T> = Promise<T> | T;
246
- type Callable<T> = AnyFn<any, T> | T;
247
- type MayBe<T> = T | undefined;
248
- type Nullable<T> = T | null;
249
- /**
250
- * Overwrite some keys type
251
- */
252
- type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
253
- /**
254
- * Prettify object type
255
- */
256
- type Prettify<T> = {
257
- [K in keyof T]: T[K];
258
- } & {};
259
- type PrettifyV2<T> = Omit<T, never>;
260
- type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
261
- /**
262
- * Resolve `boolean | Record<string, any>` to `Record<string, any>`
263
- */
264
- type ResolvedOptions<T> = T extends boolean ? never : NonNullable<T>;
265
-
266
298
  /**
267
299
  * Converts a value to an array.
268
300
  * @param array - The value to convert.
@@ -564,6 +596,13 @@ declare function ensurePrefix(input: string, prefix: string): string;
564
596
 
565
597
  declare function ensureSuffix(input: string, suffix: string): string;
566
598
 
599
+ /**
600
+ * Special chars
601
+ */
602
+ declare const SPECIAL_CHAR: {
603
+ newline: string;
604
+ };
605
+
567
606
  /**
568
607
  * 注释正则
569
608
  *
@@ -579,4 +618,4 @@ declare const RE_LINE_COMMENT: RegExp;
579
618
  */
580
619
  declare const RE_BLOCK_COMMENT: RegExp;
581
620
 
582
- export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
621
+ export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type OpenExternalURLOptions, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, type RamdomNumberOptions, type ResolvedOptions, SPECIAL_CHAR, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.d.ts CHANGED
@@ -10,9 +10,14 @@ declare const NOOP: typeof noop;
10
10
  declare function once<T extends unknown[]>(func: (...args: T) => void): (this: unknown, ...args: T) => boolean;
11
11
 
12
12
  /**
13
- * check if two values are deeply equal
13
+ * @file is/dom.ts
14
14
  */
15
- declare function isDeepEqual(value1: any, value2: any): boolean;
15
+ /**
16
+ * Check if given value is an HTMLElement
17
+ * @param value - The value to check
18
+ * @returns True if the value is an HTMLElement, false otherwise
19
+ */
20
+ declare function isHTMLElement(value: unknown): value is HTMLElement;
16
21
 
17
22
  /**
18
23
  * @file is utils
@@ -57,6 +62,11 @@ declare function isNativePromise<T = unknown>(value: unknown): value is Promise<
57
62
  declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
58
63
  declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
59
64
 
65
+ /**
66
+ * check if two values are deeply equal
67
+ */
68
+ declare function isDeepEqual(value1: any, value2: any): boolean;
69
+
60
70
  interface Options extends ScrollIntoViewOptions {
61
71
  /**
62
72
  * @default `document.body`
@@ -71,6 +81,83 @@ interface Options extends ScrollIntoViewOptions {
71
81
  */
72
82
  declare function scrollElementIntoView(element: HTMLElement, options?: Options): void;
73
83
 
84
+ type JsonArray = JsonValue[] | readonly JsonValue[];
85
+ type JsonObject = {
86
+ [Key in string]: JsonValue;
87
+ } & {
88
+ [Key in string]?: JsonValue | undefined;
89
+ };
90
+ type JsonPrimitive = boolean | number | string | null;
91
+ /**
92
+ * @copyright {@link https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts}
93
+ */
94
+ type JsonValue = JsonArray | JsonObject | JsonPrimitive;
95
+
96
+ /**
97
+ * A literal type that supports custom further strings but preserves autocompletion in IDEs.
98
+ *
99
+ * @see https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
100
+ */
101
+ type LiteralUnion<Union extends Base, Base = string> = Union | (Base & {
102
+ zz_IGNORE_ME?: never;
103
+ });
104
+ /**
105
+ * Non empty object `{}`
106
+ */
107
+ type NonEmptyObject<T> = T extends Record<string, never> ? never : T;
108
+
109
+ /**
110
+ * interop module
111
+ */
112
+ type InteropModuleDefault<T> = T extends {
113
+ default: infer U;
114
+ } ? U : T;
115
+
116
+ type AnyFn<T = any, R = any> = (...args: T[]) => R;
117
+ type Arrayable<T> = T | T[];
118
+ type Awaitable<T> = Promise<T> | T;
119
+ type Callable<T> = AnyFn<any, T> | T;
120
+ type MayBe<T> = T | undefined;
121
+ type Nullable<T> = T | null;
122
+ /**
123
+ * Overwrite some keys type
124
+ */
125
+ type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
126
+ /**
127
+ * Prettify object type
128
+ */
129
+ type Prettify<T> = {
130
+ [K in keyof T]: T[K];
131
+ } & {};
132
+ type PrettifyV2<T> = Omit<T, never>;
133
+ type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
134
+ /**
135
+ * Resolve `boolean | Record<string, any>` to `Record<string, any>`
136
+ */
137
+ type ResolvedOptions<T> = T extends boolean ? never : NonNullable<T>;
138
+
139
+ interface OpenExternalURLOptions {
140
+ /**
141
+ * open target
142
+ *
143
+ * @default `_blank`
144
+ */
145
+ target?: LiteralUnion<'_self' | '_top' | '_blank' | '_parent'>;
146
+ }
147
+ /**
148
+ * Open external url
149
+ * @param url - URL to open
150
+ * @param options - open options
151
+ * @returns window proxy
152
+ */
153
+ declare function openExternalURL(url: string | URL, options?: OpenExternalURLOptions): WindowProxy | null;
154
+
155
+ /**
156
+ * Check if element is in viewport
157
+ * @param element - checked element
158
+ * @param targetWindow - window
159
+ * @returns true if element is in viewport, false otherwise
160
+ */
74
161
  declare function isElementVisibleInViewport(element: HTMLElement, targetWindow?: Window): boolean;
75
162
 
76
163
  /**
@@ -208,61 +295,6 @@ declare function unique<T>(array: T[]): T[];
208
295
  */
209
296
  declare function uniqueBy<T>(array: T[], equalFn: (a: T, b: T) => boolean): T[];
210
297
 
211
- type JsonArray = JsonValue[] | readonly JsonValue[];
212
- type JsonObject = {
213
- [Key in string]: JsonValue;
214
- } & {
215
- [Key in string]?: JsonValue | undefined;
216
- };
217
- type JsonPrimitive = boolean | number | string | null;
218
- /**
219
- * @copyright {@link https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts}
220
- */
221
- type JsonValue = JsonArray | JsonObject | JsonPrimitive;
222
-
223
- /**
224
- * A literal type that supports custom further strings but preserves autocompletion in IDEs.
225
- *
226
- * @see https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
227
- */
228
- type LiteralUnion<Union extends Base, Base = string> = Union | (Base & {
229
- zz_IGNORE_ME?: never;
230
- });
231
- /**
232
- * Non empty object `{}`
233
- */
234
- type NonEmptyObject<T> = T extends Record<string, never> ? never : T;
235
-
236
- /**
237
- * interop module
238
- */
239
- type InteropModuleDefault<T> = T extends {
240
- default: infer U;
241
- } ? U : T;
242
-
243
- type AnyFn<T = any, R = any> = (...args: T[]) => R;
244
- type Arrayable<T> = T | T[];
245
- type Awaitable<T> = Promise<T> | T;
246
- type Callable<T> = AnyFn<any, T> | T;
247
- type MayBe<T> = T | undefined;
248
- type Nullable<T> = T | null;
249
- /**
250
- * Overwrite some keys type
251
- */
252
- type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
253
- /**
254
- * Prettify object type
255
- */
256
- type Prettify<T> = {
257
- [K in keyof T]: T[K];
258
- } & {};
259
- type PrettifyV2<T> = Omit<T, never>;
260
- type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
261
- /**
262
- * Resolve `boolean | Record<string, any>` to `Record<string, any>`
263
- */
264
- type ResolvedOptions<T> = T extends boolean ? never : NonNullable<T>;
265
-
266
298
  /**
267
299
  * Converts a value to an array.
268
300
  * @param array - The value to convert.
@@ -564,6 +596,13 @@ declare function ensurePrefix(input: string, prefix: string): string;
564
596
 
565
597
  declare function ensureSuffix(input: string, suffix: string): string;
566
598
 
599
+ /**
600
+ * Special chars
601
+ */
602
+ declare const SPECIAL_CHAR: {
603
+ newline: string;
604
+ };
605
+
567
606
  /**
568
607
  * 注释正则
569
608
  *
@@ -579,4 +618,4 @@ declare const RE_LINE_COMMENT: RegExp;
579
618
  */
580
619
  declare const RE_BLOCK_COMMENT: RegExp;
581
620
 
582
- export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
621
+ export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type OpenExternalURLOptions, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, type RamdomNumberOptions, type ResolvedOptions, SPECIAL_CHAR, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
package/dist/index.js CHANGED
@@ -16,35 +16,12 @@ function once(func) {
16
16
  };
17
17
  }
18
18
 
19
- // src/is/isDeepEqual.ts
20
- function isDeepEqual(value1, value2) {
21
- const type1 = getObjectType(value1);
22
- const type2 = getObjectType(value2);
23
- if (type1 !== type2) {
24
- return false;
25
- }
26
- if (isArray(value1) && isArray(value2)) {
27
- if (value1.length !== value2.length) {
28
- return false;
29
- }
30
- return value1.every((item, index) => isDeepEqual(item, value2[index]));
31
- }
32
- if (isObject(value1) && isObject(value2)) {
33
- const keys = Object.keys(value1);
34
- if (keys.length !== Object.keys(value2).length) {
35
- return false;
36
- }
37
- return keys.every(
38
- (key) => isDeepEqual(
39
- value1[key],
40
- value2[key]
41
- )
42
- );
43
- }
44
- return Object.is(value1, value2);
19
+ // src/is/dom.ts
20
+ function isHTMLElement(value) {
21
+ return typeof value === "object" && value !== null && "nodeType" in value && value.nodeType === Node.ELEMENT_NODE && value instanceof HTMLElement;
45
22
  }
46
23
 
47
- // src/is/index.ts
24
+ // src/is/core.ts
48
25
  function getObjectType(value) {
49
26
  return Object.prototype.toString.call(value).slice(8, -1);
50
27
  }
@@ -149,6 +126,34 @@ function isIterable(value) {
149
126
  return isFunction(value?.[Symbol.iterator]);
150
127
  }
151
128
 
129
+ // src/is/isDeepEqual.ts
130
+ function isDeepEqual(value1, value2) {
131
+ const type1 = getObjectType(value1);
132
+ const type2 = getObjectType(value2);
133
+ if (type1 !== type2) {
134
+ return false;
135
+ }
136
+ if (isArray(value1) && isArray(value2)) {
137
+ if (value1.length !== value2.length) {
138
+ return false;
139
+ }
140
+ return value1.every((item, index) => isDeepEqual(item, value2[index]));
141
+ }
142
+ if (isObject(value1) && isObject(value2)) {
143
+ const keys = Object.keys(value1);
144
+ if (keys.length !== Object.keys(value2).length) {
145
+ return false;
146
+ }
147
+ return keys.every(
148
+ (key) => isDeepEqual(
149
+ value1[key],
150
+ value2[key]
151
+ )
152
+ );
153
+ }
154
+ return Object.is(value1, value2);
155
+ }
156
+
152
157
  // src/dom/scrollIntoView.ts
153
158
  function scrollElementIntoView(element, options = {}) {
154
159
  const body = document.body;
@@ -166,6 +171,13 @@ function scrollElementIntoView(element, options = {}) {
166
171
  }
167
172
  }
168
173
 
174
+ // src/dom/openExternalURL.ts
175
+ function openExternalURL(url, options = {}) {
176
+ const { target = "_blank" } = options;
177
+ const proxy = window.open(url, target);
178
+ return proxy;
179
+ }
180
+
169
181
  // src/dom/isVisibleInViewport.ts
170
182
  function isElementVisibleInViewport(element, targetWindow = window) {
171
183
  const { top, left, bottom, right } = element.getBoundingClientRect();
@@ -695,6 +707,11 @@ function sortObject(obj, options = {}) {
695
707
  return sortKeys(obj);
696
708
  }
697
709
 
710
+ // src/constants/char.ts
711
+ var SPECIAL_CHAR = {
712
+ newline: "\n"
713
+ };
714
+
698
715
  // src/constants/regexp.ts
699
716
  var RE_COMMENTS = /(?:<!--|\/\*)([\s\S]*?)(?:-->|\*\/)/g;
700
717
  var RE_LINE_COMMENT = /\/\/.*/;
@@ -705,6 +722,7 @@ export {
705
722
  RE_BLOCK_COMMENT,
706
723
  RE_COMMENTS,
707
724
  RE_LINE_COMMENT,
725
+ SPECIAL_CHAR,
708
726
  at,
709
727
  cAF,
710
728
  chunk,
@@ -738,6 +756,7 @@ export {
738
756
  isEmptyStringOrWhitespace,
739
757
  isError,
740
758
  isFunction,
759
+ isHTMLElement,
741
760
  isInteger,
742
761
  isIterable,
743
762
  isMap,
@@ -766,6 +785,7 @@ export {
766
785
  noop,
767
786
  omit,
768
787
  once,
788
+ openExternalURL,
769
789
  pick,
770
790
  rAF,
771
791
  randomHexColor,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ntnyq/utils",
3
3
  "type": "module",
4
- "version": "0.6.2",
4
+ "version": "0.6.4",
5
5
  "description": "Common used utils.",
6
6
  "keywords": [
7
7
  "utils"
@@ -37,28 +37,26 @@
37
37
  ],
38
38
  "sideEffects": false,
39
39
  "devDependencies": {
40
- "@ntnyq/eslint-config": "^4.0.2",
40
+ "@ntnyq/eslint-config": "^4.0.3",
41
41
  "@ntnyq/prettier-config": "^2.0.0",
42
- "@vitest/coverage-v8": "^3.0.9",
43
42
  "bumpp": "^10.1.0",
44
- "eslint": "^9.23.0",
43
+ "eslint": "^9.24.0",
45
44
  "husky": "^9.1.7",
46
45
  "nano-staged": "^0.8.0",
47
46
  "npm-run-all2": "^7.0.2",
48
47
  "prettier": "^3.5.3",
49
48
  "tsup": "^8.4.0",
50
- "typescript": "^5.8.2",
51
- "vitest": "^3.0.9"
49
+ "typescript": "^5.8.3",
50
+ "vitest": "^3.1.1"
52
51
  },
53
52
  "engines": {
54
53
  "node": ">=18.18.0"
55
54
  },
56
55
  "nano-staged": {
57
- "*.{js,ts,mjs,cjs,md,yml,yaml,json}": "eslint --fix"
56
+ "*.{js,ts,mjs,cjs,md,yml,yaml,toml,json}": "eslint --fix"
58
57
  },
59
58
  "scripts": {
60
59
  "build": "tsup",
61
- "coverage": "vitest --coverage",
62
60
  "dev": "tsup --watch src",
63
61
  "lint": "eslint",
64
62
  "release": "run-s release:check release:version release:publish",