ph-utils 1.0.0 → 1.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.
package/lib/browser.d.ts CHANGED
@@ -192,6 +192,17 @@ declare function isVisible(el: HTMLElement, parent?: HTMLElement | null | undefi
192
192
  * @returns {boolean} 如果设备是移动设备,则返回true;否则返回false。
193
193
  */
194
194
  declare function isMobile(): boolean;
195
+ type ClsxValue = string | number | boolean | undefined | null | ClsxValue[] | Record<string, boolean | string | number | undefined | null>;
196
+ /**
197
+ * 条件化类名拼接工具,支持任意嵌套的字符串、数组、对象参数。
198
+ * - 字符串/数字:直接拼接
199
+ * - 数组:递归处理每个元素
200
+ * - 对象:值为真时拼接键名
201
+ * - falsy 值(false、null、undefined、0、""):跳过
202
+ * @param args - 任意数量的类名参数
203
+ * @returns 拼接后的类名字符串
204
+ */
205
+ declare function clsx(...args: ClsxValue[]): string;
195
206
  /**
196
207
  * 格式化类名,支持数组和对象两种形式。
197
208
  * - 数组形式:数组中的每个元素代表一个类名,非空元素将被添加到结果字符串中。
@@ -200,6 +211,18 @@ declare function isMobile(): boolean;
200
211
  * @returns 格式化后的类名字符串
201
212
  */
202
213
  declare function formatClass(classObj: (boolean | string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string): string;
214
+ type StyxBaseValue = string | boolean | undefined | null;
215
+ type StyxValue = StyxBaseValue | StyxBaseValue[] | Record<string, StyxBaseValue | number>;
216
+ /**
217
+ * 条件化样式拼接工具,支持任意嵌套的字符串、数组、对象参数。
218
+ * - 字符串:直接拼接(自动补 `;`)
219
+ * - 数组:递归处理每个元素
220
+ * - 对象:拼接为 `key:value;` 格式
221
+ * - falsy 值(false、null、undefined、0、""):跳过
222
+ * @param args - 任意数量的样式参数
223
+ * @returns 拼接后的样式字符串
224
+ */
225
+ declare function styx(...args: StyxValue[]): string;
203
226
  /**
204
227
  * 将样式对象格式化为 CSS 样式字符串。
205
228
  * @param styleObj - 样式对象,可以是字符串数组或键值对对象。
@@ -413,4 +436,4 @@ declare function debounce<R extends unknown[], T>(fn: (...args: R) => T, interva
413
436
  cancel: () => void;
414
437
  };
415
438
  //#endregion
416
- export { $, $$, $one, addClass, applyColorTheme, applyTheme, attr, calcuteCursorPosition, clear, copy, create, debounce, elem, formJson, formatClass, formatStyle, get, getAttr, getColorTheme, getSystemTheme, getTheme, hasClass, html, initColorTheme, initTheme, isMobile, isVisible, iterate, off, on, parent, query, queryHideNodeSize, remove, removeClass, replaceClass, set, shouldEventNext, text, throttle, toggleClass, toggleColorTheme, toggleTheme, transition };
439
+ export { $, $$, $one, addClass, applyColorTheme, applyTheme, attr, calcuteCursorPosition, clear, clsx, copy, create, debounce, elem, formJson, formatClass, formatStyle, get, getAttr, getColorTheme, getSystemTheme, getTheme, hasClass, html, initColorTheme, initTheme, isMobile, isVisible, iterate, off, on, parent, query, queryHideNodeSize, remove, removeClass, replaceClass, set, shouldEventNext, styx, text, throttle, toggleClass, toggleColorTheme, toggleTheme, transition };
package/lib/browser.js CHANGED
@@ -314,6 +314,30 @@ function isMobile() {
314
314
  return isMobile || screenWidth <= 800 || screenHeight <= 600;
315
315
  }
316
316
  /**
317
+ * 条件化类名拼接工具,支持任意嵌套的字符串、数组、对象参数。
318
+ * - 字符串/数字:直接拼接
319
+ * - 数组:递归处理每个元素
320
+ * - 对象:值为真时拼接键名
321
+ * - falsy 值(false、null、undefined、0、""):跳过
322
+ * @param args - 任意数量的类名参数
323
+ * @returns 拼接后的类名字符串
324
+ */
325
+ function clsx(...args) {
326
+ let classes = "";
327
+ for (let i = 0, len = args.length; i < len; i++) {
328
+ const arg = args[i];
329
+ if (!arg) continue;
330
+ if (typeof arg === "string" || typeof arg === "number") classes += arg + " ";
331
+ else if (Array.isArray(arg)) {
332
+ const inner = clsx(...arg);
333
+ if (inner) classes += inner + " ";
334
+ } else if (typeof arg === "object") {
335
+ for (const key in arg) if (arg[key]) classes += key + " ";
336
+ }
337
+ }
338
+ return classes.trim();
339
+ }
340
+ /**
317
341
  * 格式化类名,支持数组和对象两种形式。
318
342
  * - 数组形式:数组中的每个元素代表一个类名,非空元素将被添加到结果字符串中。
319
343
  * - 对象形式:对象的键代表类名,值为真(非空、非undefined、非null)时,键将被添加到结果字符串中。
@@ -321,14 +345,32 @@ function isMobile() {
321
345
  * @returns 格式化后的类名字符串
322
346
  */
323
347
  function formatClass(classObj) {
324
- let classes = "";
325
- if (Array.isArray(classObj)) for (let i = 0, len = classObj.length; i < len; i++) {
326
- const item = classObj[i];
327
- if (item) classes += `${item} `;
348
+ return clsx(classObj);
349
+ }
350
+ /**
351
+ * 条件化样式拼接工具,支持任意嵌套的字符串、数组、对象参数。
352
+ * - 字符串:直接拼接(自动补 `;`)
353
+ * - 数组:递归处理每个元素
354
+ * - 对象:拼接为 `key:value;` 格式
355
+ * - falsy 值(false、null、undefined、0、""):跳过
356
+ * @param args - 任意数量的样式参数
357
+ * @returns 拼接后的样式字符串
358
+ */
359
+ function styx(...args) {
360
+ let styleStr = "";
361
+ for (let i = 0, len = args.length; i < len; i++) {
362
+ const arg = args[i];
363
+ if (!arg) continue;
364
+ if (typeof arg === "string") styleStr += arg.endsWith(";") ? arg : arg + ";";
365
+ else if (Array.isArray(arg)) {
366
+ const inner = styx(...arg);
367
+ if (inner) styleStr += inner;
368
+ } else if (typeof arg === "object") for (const key in arg) {
369
+ const value = arg[key];
370
+ if (value || value === 0) styleStr += `${key}:${value};`;
371
+ }
328
372
  }
329
- else if (typeof classObj === "string") classes = classObj;
330
- else for (const key in classObj) if (classObj[key]) classes += `${key} `;
331
- return classes.trim();
373
+ return styleStr;
332
374
  }
333
375
  /**
334
376
  * 将样式对象格式化为 CSS 样式字符串。
@@ -336,17 +378,7 @@ function formatClass(classObj) {
336
378
  * @returns 格式化后的 CSS 样式字符串。
337
379
  */
338
380
  function formatStyle(styleObj) {
339
- let styleStr = "";
340
- if (Array.isArray(styleObj)) for (let i = 0, len = styleObj.length; i < len; i++) {
341
- const item = styleObj[i];
342
- if (item) styleStr += `${item};`;
343
- }
344
- else if (typeof styleObj === "string") styleStr = styleObj;
345
- else for (const key in styleObj) {
346
- const value = styleObj[key];
347
- if (value) styleStr += `${key}:${value};`;
348
- }
349
- return styleStr;
381
+ return styx(styleObj);
350
382
  }
351
383
  function toggleCssProperty(el, properties, method = "set") {
352
384
  for (let i = 0, len = properties.length; i < len; i++) {
@@ -855,4 +887,4 @@ function debounce(fn, interval = 500) {
855
887
  return debounced;
856
888
  }
857
889
  //#endregion
858
- export { $, $$, $one, addClass, applyColorTheme, applyTheme, attr, calcuteCursorPosition, clear, copy, create, debounce, elem, formJson, formatClass, formatStyle, get, getAttr, getColorTheme, getSystemTheme, getTheme, hasClass, html, initColorTheme, initTheme, isMobile, isVisible, iterate, off, on, parent, query, queryHideNodeSize, remove, removeClass, replaceClass, set, shouldEventNext, text, throttle, toggleClass, toggleColorTheme, toggleTheme, transition };
890
+ export { $, $$, $one, addClass, applyColorTheme, applyTheme, attr, calcuteCursorPosition, clear, clsx, copy, create, debounce, elem, formJson, formatClass, formatStyle, get, getAttr, getColorTheme, getSystemTheme, getTheme, hasClass, html, initColorTheme, initTheme, isMobile, isVisible, iterate, off, on, parent, query, queryHideNodeSize, remove, removeClass, replaceClass, set, shouldEventNext, styx, text, throttle, toggleClass, toggleColorTheme, toggleTheme, transition };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ph-utils",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "js 开发工具集,前后端都可以使用(commonjs和es module)",
5
5
  "keywords": [
6
6
  "date",