@t007/utils 0.0.8 → 0.0.10

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.cjs CHANGED
@@ -27,9 +27,20 @@ __export(index_exports, {
27
27
  createEl: () => createEl,
28
28
  guardAllMethods: () => guardAllMethods,
29
29
  guardMethod: () => guardMethod,
30
+ inBoolArrOpt: () => inBoolArrOpt,
30
31
  initScrollAssist: () => initScrollAssist,
31
32
  initVScrollerator: () => initVScrollerator,
33
+ isArr: () => isArr,
34
+ isBool: () => isBool,
35
+ isDef: () => import_utils.isDef,
36
+ isFunc: () => isFunc,
37
+ isIter: () => isIter,
38
+ isNum: () => isNum,
39
+ isObj: () => import_utils2.isObj,
32
40
  isSameURL: () => isSameURL,
41
+ isStr: () => isStr,
42
+ isStrictObj: () => import_utils2.isStrictObj,
43
+ isSym: () => isSym,
33
44
  loadResource: () => loadResource,
34
45
  onAllMethods: () => onAllMethods,
35
46
  removeScrollAssist: () => removeScrollAssist,
@@ -37,12 +48,40 @@ __export(index_exports, {
37
48
  });
38
49
  module.exports = __toCommonJS(index_exports);
39
50
 
51
+ // src/core/obj.ts
52
+ var import_utils = require("sia-reactor/utils");
53
+ var import_utils2 = require("sia-reactor/utils");
54
+ function isSym(val) {
55
+ return "symbol" === typeof val;
56
+ }
57
+ function isBool(val) {
58
+ return "boolean" === typeof val;
59
+ }
60
+ function isNum(val) {
61
+ return "number" === typeof val;
62
+ }
63
+ function isStr(val) {
64
+ return "string" === typeof val;
65
+ }
66
+ function isArr(obj) {
67
+ return Array.isArray(obj);
68
+ }
69
+ function isIter(obj) {
70
+ return obj != null && "function" === typeof obj[Symbol.iterator];
71
+ }
72
+ function isFunc(val) {
73
+ return "function" === typeof val;
74
+ }
75
+ function inBoolArrOpt(opt, str) {
76
+ return opt?.includes?.(str) ?? opt;
77
+ }
78
+
40
79
  // src/core/str.ts
41
80
  function uid(prefix = "") {
42
81
  return prefix + Date.now().toString(36) + "_" + performance.now().toString(36).replace(".", "") + "_" + Math.random().toString(36).slice(2);
43
82
  }
44
83
  function isSameURL(src1, src2) {
45
- if (typeof src1 !== "string" || typeof src2 !== "string" || !src1 || !src2) return false;
84
+ if (!isStr(src1) || !isStr(src2) || !src1 || !src2) return false;
46
85
  try {
47
86
  const u1 = new URL(src1, window.location.href), u2 = new URL(src2, window.location.href);
48
87
  return decodeURIComponent(u1.origin + u1.pathname) === decodeURIComponent(u2.origin + u2.pathname);
@@ -70,7 +109,7 @@ function assignEl(el, props, dataset, styles) {
70
109
  var VIRTUAL_RESOURCE = /* @__PURE__ */ Symbol.for("T007_VIRTUAL_RESOURCE");
71
110
  function loadResource(req, type = "style", { module: module2, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
72
111
  w.t007 ??= {}, w.t007._resourceCache ??= {};
73
- if (req === VIRTUAL_RESOURCE || "symbol" === typeof req) return Promise.resolve();
112
+ if (req === VIRTUAL_RESOURCE || isSym(req)) return Promise.resolve();
74
113
  const src = req;
75
114
  if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
76
115
  const existing = type === "script" ? Array.prototype.find.call(w.document.scripts, (s) => isSameURL(s.src, src)) : type === "style" ? Array.prototype.find.call(w.document.styleSheets, (s) => isSameURL(s.href, src)) : null;
@@ -107,7 +146,7 @@ function onAllMethods(owner, callback) {
107
146
  while (proto && proto !== Object.prototype) {
108
147
  for (const method of Object.getOwnPropertyNames(proto)) {
109
148
  if (method === "constructor") continue;
110
- if ("function" !== typeof Object.getOwnPropertyDescriptor(proto, method)?.value) continue;
149
+ if (isFunc(Object.getOwnPropertyDescriptor(proto, method)?.value)) continue;
111
150
  callback(method, owner);
112
151
  }
113
152
  proto = Object.getPrototypeOf(proto);
@@ -223,7 +262,7 @@ function initScrollAssist(el, { pxPerSecond = 80, assistClassName = "tmg-video-c
223
262
  var removeScrollAssist = (el) => t007._scrollers.get(el)?.destroy();
224
263
 
225
264
  // src/index.ts
226
- if (typeof window !== "undefined") {
265
+ if ((0, import_utils.isDef)(window)) {
227
266
  window.t007 ??= {};
228
267
  t007.VIRTUAL_RESOURCE = VIRTUAL_RESOURCE;
229
268
  window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
@@ -242,9 +281,20 @@ if (typeof window !== "undefined") {
242
281
  createEl,
243
282
  guardAllMethods,
244
283
  guardMethod,
284
+ inBoolArrOpt,
245
285
  initScrollAssist,
246
286
  initVScrollerator,
287
+ isArr,
288
+ isBool,
289
+ isDef,
290
+ isFunc,
291
+ isIter,
292
+ isNum,
293
+ isObj,
247
294
  isSameURL,
295
+ isStr,
296
+ isStrictObj,
297
+ isSym,
248
298
  loadResource,
249
299
  onAllMethods,
250
300
  removeScrollAssist,
package/dist/index.d.cts CHANGED
@@ -1,3 +1,5 @@
1
+ export { isDef, isObj, isStrictObj } from 'sia-reactor/utils';
2
+
1
3
  interface ScrolleratorOptions {
2
4
  baseSpeed?: number;
3
5
  maxSpeed?: number;
@@ -50,6 +52,16 @@ declare global {
50
52
  var t007: T007Namespace;
51
53
  }
52
54
 
55
+ declare function isSym<T extends symbol = symbol>(val: any): val is T;
56
+ declare function isBool<T extends boolean = boolean>(val: any): val is T;
57
+ declare function isNum<T extends number = number>(val: any): val is T;
58
+ declare function isStr<T extends string = string>(val: any): val is T;
59
+ declare function isArr<T = unknown>(obj: any): obj is T[];
60
+
61
+ declare function isIter<T = unknown>(obj: any): obj is Iterable<T>;
62
+ declare function isFunc<T extends Function = Function>(val: any): val is T;
63
+ declare function inBoolArrOpt(opt: any, str: string): boolean;
64
+
53
65
  declare function clamp(min: number | undefined, val: number, max?: number): number;
54
66
 
55
67
  declare function uid(prefix?: string): string;
@@ -81,4 +93,4 @@ declare function bindAllMethods(owner: any): void;
81
93
  declare function guardAllMethods(owner: any, guardFn?: (fn: Function) => Function, bound?: boolean): void;
82
94
  declare function guardMethod<T extends Function>(fn: T, onError?: (e: any) => void): T;
83
95
 
84
- export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, clamp, createEl, guardAllMethods, guardMethod, initScrollAssist, initVScrollerator, isSameURL, loadResource, onAllMethods, removeScrollAssist, uid };
96
+ export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, clamp, createEl, guardAllMethods, guardMethod, inBoolArrOpt, initScrollAssist, initVScrollerator, isArr, isBool, isFunc, isIter, isNum, isSameURL, isStr, isSym, loadResource, onAllMethods, removeScrollAssist, uid };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export { isDef, isObj, isStrictObj } from 'sia-reactor/utils';
2
+
1
3
  interface ScrolleratorOptions {
2
4
  baseSpeed?: number;
3
5
  maxSpeed?: number;
@@ -50,6 +52,16 @@ declare global {
50
52
  var t007: T007Namespace;
51
53
  }
52
54
 
55
+ declare function isSym<T extends symbol = symbol>(val: any): val is T;
56
+ declare function isBool<T extends boolean = boolean>(val: any): val is T;
57
+ declare function isNum<T extends number = number>(val: any): val is T;
58
+ declare function isStr<T extends string = string>(val: any): val is T;
59
+ declare function isArr<T = unknown>(obj: any): obj is T[];
60
+
61
+ declare function isIter<T = unknown>(obj: any): obj is Iterable<T>;
62
+ declare function isFunc<T extends Function = Function>(val: any): val is T;
63
+ declare function inBoolArrOpt(opt: any, str: string): boolean;
64
+
53
65
  declare function clamp(min: number | undefined, val: number, max?: number): number;
54
66
 
55
67
  declare function uid(prefix?: string): string;
@@ -81,4 +93,4 @@ declare function bindAllMethods(owner: any): void;
81
93
  declare function guardAllMethods(owner: any, guardFn?: (fn: Function) => Function, bound?: boolean): void;
82
94
  declare function guardMethod<T extends Function>(fn: T, onError?: (e: any) => void): T;
83
95
 
84
- export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, clamp, createEl, guardAllMethods, guardMethod, initScrollAssist, initVScrollerator, isSameURL, loadResource, onAllMethods, removeScrollAssist, uid };
96
+ export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, clamp, createEl, guardAllMethods, guardMethod, inBoolArrOpt, initScrollAssist, initVScrollerator, isArr, isBool, isFunc, isIter, isNum, isSameURL, isStr, isSym, loadResource, onAllMethods, removeScrollAssist, uid };
package/dist/index.js CHANGED
@@ -1,9 +1,37 @@
1
+ // src/core/obj.ts
2
+ import { isDef } from "sia-reactor/utils";
3
+ import { isObj, isStrictObj } from "sia-reactor/utils";
4
+ function isSym(val) {
5
+ return "symbol" === typeof val;
6
+ }
7
+ function isBool(val) {
8
+ return "boolean" === typeof val;
9
+ }
10
+ function isNum(val) {
11
+ return "number" === typeof val;
12
+ }
13
+ function isStr(val) {
14
+ return "string" === typeof val;
15
+ }
16
+ function isArr(obj) {
17
+ return Array.isArray(obj);
18
+ }
19
+ function isIter(obj) {
20
+ return obj != null && "function" === typeof obj[Symbol.iterator];
21
+ }
22
+ function isFunc(val) {
23
+ return "function" === typeof val;
24
+ }
25
+ function inBoolArrOpt(opt, str) {
26
+ return opt?.includes?.(str) ?? opt;
27
+ }
28
+
1
29
  // src/core/str.ts
2
30
  function uid(prefix = "") {
3
31
  return prefix + Date.now().toString(36) + "_" + performance.now().toString(36).replace(".", "") + "_" + Math.random().toString(36).slice(2);
4
32
  }
5
33
  function isSameURL(src1, src2) {
6
- if (typeof src1 !== "string" || typeof src2 !== "string" || !src1 || !src2) return false;
34
+ if (!isStr(src1) || !isStr(src2) || !src1 || !src2) return false;
7
35
  try {
8
36
  const u1 = new URL(src1, window.location.href), u2 = new URL(src2, window.location.href);
9
37
  return decodeURIComponent(u1.origin + u1.pathname) === decodeURIComponent(u2.origin + u2.pathname);
@@ -31,7 +59,7 @@ function assignEl(el, props, dataset, styles) {
31
59
  var VIRTUAL_RESOURCE = /* @__PURE__ */ Symbol.for("T007_VIRTUAL_RESOURCE");
32
60
  function loadResource(req, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
33
61
  w.t007 ??= {}, w.t007._resourceCache ??= {};
34
- if (req === VIRTUAL_RESOURCE || "symbol" === typeof req) return Promise.resolve();
62
+ if (req === VIRTUAL_RESOURCE || isSym(req)) return Promise.resolve();
35
63
  const src = req;
36
64
  if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
37
65
  const existing = type === "script" ? Array.prototype.find.call(w.document.scripts, (s) => isSameURL(s.src, src)) : type === "style" ? Array.prototype.find.call(w.document.styleSheets, (s) => isSameURL(s.href, src)) : null;
@@ -68,7 +96,7 @@ function onAllMethods(owner, callback) {
68
96
  while (proto && proto !== Object.prototype) {
69
97
  for (const method of Object.getOwnPropertyNames(proto)) {
70
98
  if (method === "constructor") continue;
71
- if ("function" !== typeof Object.getOwnPropertyDescriptor(proto, method)?.value) continue;
99
+ if (isFunc(Object.getOwnPropertyDescriptor(proto, method)?.value)) continue;
72
100
  callback(method, owner);
73
101
  }
74
102
  proto = Object.getPrototypeOf(proto);
@@ -184,7 +212,7 @@ function initScrollAssist(el, { pxPerSecond = 80, assistClassName = "tmg-video-c
184
212
  var removeScrollAssist = (el) => t007._scrollers.get(el)?.destroy();
185
213
 
186
214
  // src/index.ts
187
- if (typeof window !== "undefined") {
215
+ if (isDef(window)) {
188
216
  window.t007 ??= {};
189
217
  t007.VIRTUAL_RESOURCE = VIRTUAL_RESOURCE;
190
218
  window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
@@ -202,9 +230,20 @@ export {
202
230
  createEl,
203
231
  guardAllMethods,
204
232
  guardMethod,
233
+ inBoolArrOpt,
205
234
  initScrollAssist,
206
235
  initVScrollerator,
236
+ isArr,
237
+ isBool,
238
+ isDef,
239
+ isFunc,
240
+ isIter,
241
+ isNum,
242
+ isObj,
207
243
  isSameURL,
244
+ isStr,
245
+ isStrictObj,
246
+ isSym,
208
247
  loadResource,
209
248
  onAllMethods,
210
249
  removeScrollAssist,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t007/utils",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "High-performance, zero-dependency utility functions for the t007 ecosystem.",
5
5
  "author": "Oketade Oluwatobiloba <tobioketade007@gmail.com>",
6
6
  "license": "MIT",
@@ -43,5 +43,8 @@
43
43
  "dom-manipulation",
44
44
  "shared-logic",
45
45
  "performance"
46
- ]
46
+ ],
47
+ "dependencies": {
48
+ "sia-reactor": "^0.0.12"
49
+ }
47
50
  }