@tanwan/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.
Files changed (152) hide show
  1. package/dist/array/chunk.cjs +1 -1
  2. package/dist/array/chunk.d.cts +1 -1
  3. package/dist/array/chunk.d.ts +1 -1
  4. package/dist/array/chunk.js +1 -1
  5. package/dist/array/chunk.js.map +1 -1
  6. package/dist/array/flatten.cjs +1 -1
  7. package/dist/array/flatten.d.cts +1 -1
  8. package/dist/array/flatten.d.ts +1 -1
  9. package/dist/array/flatten.js +1 -1
  10. package/dist/array/flatten.js.map +1 -1
  11. package/dist/array/groupBy.cjs +36 -0
  12. package/dist/array/groupBy.d.cts +27 -0
  13. package/dist/array/groupBy.d.cts.map +1 -0
  14. package/dist/array/groupBy.d.ts +28 -0
  15. package/dist/array/groupBy.d.ts.map +1 -0
  16. package/dist/array/groupBy.js +38 -0
  17. package/dist/array/groupBy.js.map +1 -0
  18. package/dist/array/range.cjs +27 -0
  19. package/dist/array/range.d.cts +21 -0
  20. package/dist/array/range.d.cts.map +1 -0
  21. package/dist/array/range.d.ts +22 -0
  22. package/dist/array/range.d.ts.map +1 -0
  23. package/dist/array/range.js +29 -0
  24. package/dist/array/range.js.map +1 -0
  25. package/dist/array/shuffle.cjs +1 -1
  26. package/dist/array/shuffle.d.cts +1 -1
  27. package/dist/array/shuffle.d.ts +1 -1
  28. package/dist/array/shuffle.js +1 -1
  29. package/dist/array/shuffle.js.map +1 -1
  30. package/dist/array/unique.cjs +1 -1
  31. package/dist/array/unique.d.cts +1 -1
  32. package/dist/array/unique.d.ts +1 -1
  33. package/dist/array/unique.js +1 -1
  34. package/dist/array/unique.js.map +1 -1
  35. package/dist/base/deepClone.cjs +1 -1
  36. package/dist/base/deepClone.d.cts +1 -1
  37. package/dist/base/deepClone.d.ts +1 -1
  38. package/dist/base/deepClone.js +1 -1
  39. package/dist/base/deepClone.js.map +1 -1
  40. package/dist/base/isArray.cjs +1 -1
  41. package/dist/base/isArray.d.cts +1 -1
  42. package/dist/base/isArray.d.ts +1 -1
  43. package/dist/base/isArray.js +1 -1
  44. package/dist/base/isArray.js.map +1 -1
  45. package/dist/base/isEmpty.cjs +42 -0
  46. package/dist/base/isEmpty.d.cts +34 -0
  47. package/dist/base/isEmpty.d.cts.map +1 -0
  48. package/dist/base/isEmpty.d.ts +35 -0
  49. package/dist/base/isEmpty.d.ts.map +1 -0
  50. package/dist/base/isEmpty.js +44 -0
  51. package/dist/base/isEmpty.js.map +1 -0
  52. package/dist/base/isObject.cjs +1 -1
  53. package/dist/base/isObject.d.cts +1 -1
  54. package/dist/base/isObject.d.ts +1 -1
  55. package/dist/base/isObject.js +1 -1
  56. package/dist/base/isObject.js.map +1 -1
  57. package/dist/base/isString.cjs +1 -1
  58. package/dist/base/isString.d.cts +1 -1
  59. package/dist/base/isString.d.ts +1 -1
  60. package/dist/base/isString.js +1 -1
  61. package/dist/base/isString.js.map +1 -1
  62. package/dist/date/formatDate.cjs +1 -1
  63. package/dist/date/formatDate.d.cts +1 -1
  64. package/dist/date/formatDate.d.ts +1 -1
  65. package/dist/date/formatDate.js +1 -1
  66. package/dist/date/formatDate.js.map +1 -1
  67. package/dist/date/getRelativeTime.cjs +1 -1
  68. package/dist/date/getRelativeTime.d.cts +1 -1
  69. package/dist/date/getRelativeTime.d.ts +1 -1
  70. package/dist/date/getRelativeTime.js +1 -1
  71. package/dist/date/getRelativeTime.js.map +1 -1
  72. package/dist/function/debounce.cjs +1 -1
  73. package/dist/function/debounce.d.cts +1 -1
  74. package/dist/function/debounce.d.ts +1 -1
  75. package/dist/function/debounce.js +1 -1
  76. package/dist/function/debounce.js.map +1 -1
  77. package/dist/function/delay.cjs +8 -0
  78. package/dist/function/delay.d.cts +19 -0
  79. package/dist/function/delay.d.cts.map +1 -0
  80. package/dist/function/delay.d.ts +20 -0
  81. package/dist/function/delay.d.ts.map +1 -0
  82. package/dist/function/delay.js +10 -0
  83. package/dist/function/delay.js.map +1 -0
  84. package/dist/function/throttle.cjs +1 -1
  85. package/dist/function/throttle.d.cts +1 -1
  86. package/dist/function/throttle.d.ts +1 -1
  87. package/dist/function/throttle.js +1 -1
  88. package/dist/function/throttle.js.map +1 -1
  89. package/dist/index.cjs +18 -0
  90. package/dist/index.d.cts +10 -1
  91. package/dist/index.d.ts +10 -1
  92. package/dist/index.iife.js +1 -1
  93. package/dist/index.js +10 -1
  94. package/dist/number/clamp.cjs +1 -1
  95. package/dist/number/clamp.d.cts +1 -1
  96. package/dist/number/clamp.d.ts +1 -1
  97. package/dist/number/clamp.js +1 -1
  98. package/dist/number/clamp.js.map +1 -1
  99. package/dist/number/formatThousands.cjs +1 -1
  100. package/dist/number/formatThousands.d.cts +1 -1
  101. package/dist/number/formatThousands.d.ts +1 -1
  102. package/dist/number/formatThousands.js +1 -1
  103. package/dist/number/formatThousands.js.map +1 -1
  104. package/dist/number/randomInt.cjs +1 -1
  105. package/dist/number/randomInt.d.cts +1 -1
  106. package/dist/number/randomInt.d.ts +1 -1
  107. package/dist/number/randomInt.js +1 -1
  108. package/dist/number/randomInt.js.map +1 -1
  109. package/dist/object/omit.cjs +23 -0
  110. package/dist/object/omit.d.cts +18 -0
  111. package/dist/object/omit.d.cts.map +1 -0
  112. package/dist/object/omit.d.ts +19 -0
  113. package/dist/object/omit.d.ts.map +1 -0
  114. package/dist/object/omit.js +25 -0
  115. package/dist/object/omit.js.map +1 -0
  116. package/dist/object/pick.cjs +22 -0
  117. package/dist/object/pick.d.cts +18 -0
  118. package/dist/object/pick.d.cts.map +1 -0
  119. package/dist/object/pick.d.ts +19 -0
  120. package/dist/object/pick.d.ts.map +1 -0
  121. package/dist/object/pick.js +24 -0
  122. package/dist/object/pick.js.map +1 -0
  123. package/dist/string/camelCase.cjs +0 -1
  124. package/dist/string/camelCase.js +0 -1
  125. package/dist/string/camelCase.js.map +1 -1
  126. package/dist/string/capitalize.cjs +1 -1
  127. package/dist/string/capitalize.d.cts +1 -1
  128. package/dist/string/capitalize.d.ts +1 -1
  129. package/dist/string/capitalize.js +1 -1
  130. package/dist/string/capitalize.js.map +1 -1
  131. package/dist/string/uuid.cjs +23 -0
  132. package/dist/string/uuid.d.cts +17 -0
  133. package/dist/string/uuid.d.cts.map +1 -0
  134. package/dist/string/uuid.d.ts +18 -0
  135. package/dist/string/uuid.d.ts.map +1 -0
  136. package/dist/string/uuid.js +25 -0
  137. package/dist/string/uuid.js.map +1 -0
  138. package/dist/url/parseQueryString.cjs +48 -0
  139. package/dist/url/parseQueryString.d.cts +22 -0
  140. package/dist/url/parseQueryString.d.cts.map +1 -0
  141. package/dist/url/parseQueryString.d.ts +23 -0
  142. package/dist/url/parseQueryString.d.ts.map +1 -0
  143. package/dist/url/parseQueryString.js +50 -0
  144. package/dist/url/parseQueryString.js.map +1 -0
  145. package/dist/url/stringifyQueryString.cjs +38 -0
  146. package/dist/url/stringifyQueryString.d.cts +22 -0
  147. package/dist/url/stringifyQueryString.d.cts.map +1 -0
  148. package/dist/url/stringifyQueryString.d.ts +23 -0
  149. package/dist/url/stringifyQueryString.d.ts.map +1 -0
  150. package/dist/url/stringifyQueryString.js +40 -0
  151. package/dist/url/stringifyQueryString.js.map +1 -0
  152. package/package.json +39 -3
package/dist/index.js CHANGED
@@ -2,12 +2,18 @@ import isArray from "./base/isArray.js";
2
2
  import isString from "./base/isString.js";
3
3
  import isObject from "./base/isObject.js";
4
4
  import deepClone from "./base/deepClone.js";
5
+ import isEmpty from "./base/isEmpty.js";
6
+ import pick from "./object/pick.js";
7
+ import omit from "./object/omit.js";
5
8
  import debounce from "./function/debounce.js";
6
9
  import throttle from "./function/throttle.js";
10
+ import delay from "./function/delay.js";
7
11
  import unique from "./array/unique.js";
8
12
  import flatten from "./array/flatten.js";
9
13
  import chunk from "./array/chunk.js";
10
14
  import shuffle from "./array/shuffle.js";
15
+ import range from "./array/range.js";
16
+ import groupBy from "./array/groupBy.js";
11
17
  import randomInt from "./number/randomInt.js";
12
18
  import clamp from "./number/clamp.js";
13
19
  import formatThousands from "./number/formatThousands.js";
@@ -16,4 +22,7 @@ import getRelativeTime from "./date/getRelativeTime.js";
16
22
  import camelCase from "./string/camelCase.js";
17
23
  import kebabCase from "./string/kebabCase.js";
18
24
  import capitalize from "./string/capitalize.js";
19
- export { camelCase, capitalize, chunk, clamp, debounce, deepClone, flatten, formatDate, formatThousands, getRelativeTime, isArray, isObject, isString, kebabCase, randomInt, shuffle, throttle, unique };
25
+ import uuid from "./string/uuid.js";
26
+ import parseQueryString from "./url/parseQueryString.js";
27
+ import stringifyQueryString from "./url/stringifyQueryString.js";
28
+ export { camelCase, capitalize, chunk, clamp, debounce, deepClone, delay, flatten, formatDate, formatThousands, getRelativeTime, groupBy, isArray, isEmpty, isObject, isString, kebabCase, omit, parseQueryString, pick, randomInt, range, shuffle, stringifyQueryString, throttle, unique, uuid };
@@ -3,7 +3,7 @@
3
3
  * 将数值限制在指定范围内
4
4
  * @example
5
5
  * ```ts
6
- * import clamp from '@tw/utils/clamp'
6
+ * import clamp from '@tanwan/utils/clamp'
7
7
  * clamp(10, 0, 5) // 5
8
8
  * clamp(-1, 0, 5) // 0
9
9
  * clamp(3, 0, 5) // 3
@@ -3,7 +3,7 @@
3
3
  * 将数值限制在指定范围内
4
4
  * @example
5
5
  * ```ts
6
- * import clamp from '@tw/utils/clamp'
6
+ * import clamp from '@tanwan/utils/clamp'
7
7
  * clamp(10, 0, 5) // 5
8
8
  * clamp(-1, 0, 5) // 0
9
9
  * clamp(3, 0, 5) // 3
@@ -3,7 +3,7 @@
3
3
  * 将数值限制在指定范围内
4
4
  * @example
5
5
  * ```ts
6
- * import clamp from '@tw/utils/clamp'
6
+ * import clamp from '@tanwan/utils/clamp'
7
7
  * clamp(10, 0, 5) // 5
8
8
  * clamp(-1, 0, 5) // 0
9
9
  * clamp(3, 0, 5) // 3
@@ -3,7 +3,7 @@
3
3
  * 将数值限制在指定范围内
4
4
  * @example
5
5
  * ```ts
6
- * import clamp from '@tw/utils/clamp'
6
+ * import clamp from '@tanwan/utils/clamp'
7
7
  * clamp(10, 0, 5) // 5
8
8
  * clamp(-1, 0, 5) // 0
9
9
  * clamp(3, 0, 5) // 3
@@ -1 +1 @@
1
- {"version":3,"file":"clamp.js","names":[],"sources":["../../src/number/clamp/index.ts"],"sourcesContent":["/**\n * 将数值限制在指定范围内\n * @example\n * ```ts\n * import clamp from '@tw/utils/clamp'\n * clamp(10, 0, 5) // 5\n * clamp(-1, 0, 5) // 0\n * clamp(3, 0, 5) // 3\n * ```\n */\nexport default function clamp(value: number, min: number, max: number): number {\n return Math.min(Math.max(value, min), max);\n}\n"],"mappings":";;;;;;;;;;;AAUA,SAAwB,MAAM,OAAe,KAAa,KAAqB;AAC7E,QAAO,KAAK,IAAI,KAAK,IAAI,OAAO,IAAI,EAAE,IAAI"}
1
+ {"version":3,"file":"clamp.js","names":[],"sources":["../../src/number/clamp/index.ts"],"sourcesContent":["/**\n * 将数值限制在指定范围内\n * @example\n * ```ts\n * import clamp from '@tanwan/utils/clamp'\n * clamp(10, 0, 5) // 5\n * clamp(-1, 0, 5) // 0\n * clamp(3, 0, 5) // 3\n * ```\n */\nexport default function clamp(value: number, min: number, max: number): number {\n return Math.min(Math.max(value, min), max);\n}\n"],"mappings":";;;;;;;;;;;AAUA,SAAwB,MAAM,OAAe,KAAa,KAAqB;AAC7E,QAAO,KAAK,IAAI,KAAK,IAAI,OAAO,IAAI,EAAE,IAAI"}
@@ -3,7 +3,7 @@
3
3
  * 千分位格式化数字
4
4
  * @example
5
5
  * ```ts
6
- * import formatThousands from '@tw/utils/formatThousands'
6
+ * import formatThousands from '@tanwan/utils/formatThousands'
7
7
  * formatThousands(1234567) // '1,234,567'
8
8
  * formatThousands(1234567.89) // '1,234,567.89'
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 千分位格式化数字
4
4
  * @example
5
5
  * ```ts
6
- * import formatThousands from '@tw/utils/formatThousands'
6
+ * import formatThousands from '@tanwan/utils/formatThousands'
7
7
  * formatThousands(1234567) // '1,234,567'
8
8
  * formatThousands(1234567.89) // '1,234,567.89'
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 千分位格式化数字
4
4
  * @example
5
5
  * ```ts
6
- * import formatThousands from '@tw/utils/formatThousands'
6
+ * import formatThousands from '@tanwan/utils/formatThousands'
7
7
  * formatThousands(1234567) // '1,234,567'
8
8
  * formatThousands(1234567.89) // '1,234,567.89'
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 千分位格式化数字
4
4
  * @example
5
5
  * ```ts
6
- * import formatThousands from '@tw/utils/formatThousands'
6
+ * import formatThousands from '@tanwan/utils/formatThousands'
7
7
  * formatThousands(1234567) // '1,234,567'
8
8
  * formatThousands(1234567.89) // '1,234,567.89'
9
9
  * ```
@@ -1 +1 @@
1
- {"version":3,"file":"formatThousands.js","names":[],"sources":["../../src/number/formatThousands/index.ts"],"sourcesContent":["/**\n * 千分位格式化数字\n * @example\n * ```ts\n * import formatThousands from '@tw/utils/formatThousands'\n * formatThousands(1234567) // '1,234,567'\n * formatThousands(1234567.89) // '1,234,567.89'\n * ```\n */\nexport default function formatThousands(num: number | string): string {\n const parts = String(num).split('.');\n parts[0] = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');\n return parts.join('.');\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,gBAAgB,KAA8B;CACpE,MAAM,QAAQ,OAAO,IAAI,CAAC,MAAM,IAAI;AACpC,OAAM,KAAK,MAAM,GAAG,QAAQ,yBAAyB,IAAI;AACzD,QAAO,MAAM,KAAK,IAAI"}
1
+ {"version":3,"file":"formatThousands.js","names":[],"sources":["../../src/number/formatThousands/index.ts"],"sourcesContent":["/**\n * 千分位格式化数字\n * @example\n * ```ts\n * import formatThousands from '@tanwan/utils/formatThousands'\n * formatThousands(1234567) // '1,234,567'\n * formatThousands(1234567.89) // '1,234,567.89'\n * ```\n */\nexport default function formatThousands(num: number | string): string {\n const parts = String(num).split('.');\n parts[0] = parts[0].replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');\n return parts.join('.');\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,gBAAgB,KAA8B;CACpE,MAAM,QAAQ,OAAO,IAAI,CAAC,MAAM,IAAI;AACpC,OAAM,KAAK,MAAM,GAAG,QAAQ,yBAAyB,IAAI;AACzD,QAAO,MAAM,KAAK,IAAI"}
@@ -3,7 +3,7 @@
3
3
  * 生成指定范围内的随机整数
4
4
  * @example
5
5
  * ```ts
6
- * import randomInt from '@tw/utils/randomInt'
6
+ * import randomInt from '@tanwan/utils/randomInt'
7
7
  * randomInt(1, 10) // 3(介于 1-10 之间)
8
8
  * randomInt(0, 1) // 0 或 1
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 生成指定范围内的随机整数
4
4
  * @example
5
5
  * ```ts
6
- * import randomInt from '@tw/utils/randomInt'
6
+ * import randomInt from '@tanwan/utils/randomInt'
7
7
  * randomInt(1, 10) // 3(介于 1-10 之间)
8
8
  * randomInt(0, 1) // 0 或 1
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 生成指定范围内的随机整数
4
4
  * @example
5
5
  * ```ts
6
- * import randomInt from '@tw/utils/randomInt'
6
+ * import randomInt from '@tanwan/utils/randomInt'
7
7
  * randomInt(1, 10) // 3(介于 1-10 之间)
8
8
  * randomInt(0, 1) // 0 或 1
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 生成指定范围内的随机整数
4
4
  * @example
5
5
  * ```ts
6
- * import randomInt from '@tw/utils/randomInt'
6
+ * import randomInt from '@tanwan/utils/randomInt'
7
7
  * randomInt(1, 10) // 3(介于 1-10 之间)
8
8
  * randomInt(0, 1) // 0 或 1
9
9
  * ```
@@ -1 +1 @@
1
- {"version":3,"file":"randomInt.js","names":[],"sources":["../../src/number/randomInt/index.ts"],"sourcesContent":["/**\n * 生成指定范围内的随机整数\n * @example\n * ```ts\n * import randomInt from '@tw/utils/randomInt'\n * randomInt(1, 10) // 3(介于 1-10 之间)\n * randomInt(0, 1) // 0 或 1\n * ```\n */\nexport default function randomInt(min: number, max: number): number {\n return Math.floor(Math.random() * (max - min + 1)) + min;\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,UAAU,KAAa,KAAqB;AAClE,QAAO,KAAK,MAAM,KAAK,QAAQ,IAAI,MAAM,MAAM,GAAG,GAAG"}
1
+ {"version":3,"file":"randomInt.js","names":[],"sources":["../../src/number/randomInt/index.ts"],"sourcesContent":["/**\n * 生成指定范围内的随机整数\n * @example\n * ```ts\n * import randomInt from '@tanwan/utils/randomInt'\n * randomInt(1, 10) // 3(介于 1-10 之间)\n * randomInt(0, 1) // 0 或 1\n * ```\n */\nexport default function randomInt(min: number, max: number): number {\n return Math.floor(Math.random() * (max - min + 1)) + min;\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,UAAU,KAAa,KAAqB;AAClE,QAAO,KAAK,MAAM,KAAK,QAAQ,IAAI,MAAM,MAAM,GAAG,GAAG"}
@@ -0,0 +1,23 @@
1
+ //#region src/object/omit/index.ts
2
+ /**
3
+ * 从对象中排除指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要排除的 key 数组
7
+ * @returns 排除指定 key 后的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import omit from '@tanwan/utils/omit'
12
+ * omit({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { b: 2 }
13
+ * omit({ x: 'foo' }, []) // { x: 'foo' }
14
+ * ```
15
+ */
16
+ function omit(obj, keys) {
17
+ const keySet = new Set(keys);
18
+ const result = {};
19
+ for (const key in obj) if (Object.prototype.hasOwnProperty.call(obj, key) && !keySet.has(key)) result[key] = obj[key];
20
+ return result;
21
+ }
22
+ //#endregion
23
+ module.exports = omit;
@@ -0,0 +1,18 @@
1
+ //#region src/object/omit/index.d.ts
2
+ /**
3
+ * 从对象中排除指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要排除的 key 数组
7
+ * @returns 排除指定 key 后的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import omit from '@tanwan/utils/omit'
12
+ * omit({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { b: 2 }
13
+ * omit({ x: 'foo' }, []) // { x: 'foo' }
14
+ * ```
15
+ */
16
+ declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
17
+ export = omit;
18
+ //# sourceMappingURL=omit.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"omit.d.cts","names":[],"sources":["../../src/object/omit/index.ts"],"mappings":";;;;;;;;;;;;;;;iBAcwB,IAAA,WAAe,MAAA,mCAAyC,CAAA,CAAA,CAAG,GAAA,EAAK,CAAA,EAAG,IAAA,EAAM,CAAA,KAAM,IAAA,CAAK,CAAA,EAAG,CAAA;AAAA"}
@@ -0,0 +1,19 @@
1
+ //#region src/object/omit/index.d.ts
2
+ /**
3
+ * 从对象中排除指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要排除的 key 数组
7
+ * @returns 排除指定 key 后的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import omit from '@tanwan/utils/omit'
12
+ * omit({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { b: 2 }
13
+ * omit({ x: 'foo' }, []) // { x: 'foo' }
14
+ * ```
15
+ */
16
+ declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
17
+ //#endregion
18
+ export { omit as default };
19
+ //# sourceMappingURL=omit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"omit.d.ts","names":[],"sources":["../../src/object/omit/index.ts"],"mappings":";;;;;;;;;;;;;;;iBAcwB,IAAA,WAAe,MAAA,mCAAyC,CAAA,CAAA,CAAG,GAAA,EAAK,CAAA,EAAG,IAAA,EAAM,CAAA,KAAM,IAAA,CAAK,CAAA,EAAG,CAAA"}
@@ -0,0 +1,25 @@
1
+ //#region src/object/omit/index.ts
2
+ /**
3
+ * 从对象中排除指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要排除的 key 数组
7
+ * @returns 排除指定 key 后的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import omit from '@tanwan/utils/omit'
12
+ * omit({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { b: 2 }
13
+ * omit({ x: 'foo' }, []) // { x: 'foo' }
14
+ * ```
15
+ */
16
+ function omit(obj, keys) {
17
+ const keySet = new Set(keys);
18
+ const result = {};
19
+ for (const key in obj) if (Object.prototype.hasOwnProperty.call(obj, key) && !keySet.has(key)) result[key] = obj[key];
20
+ return result;
21
+ }
22
+ //#endregion
23
+ export { omit as default };
24
+
25
+ //# sourceMappingURL=omit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"omit.js","names":[],"sources":["../../src/object/omit/index.ts"],"sourcesContent":["/**\n * 从对象中排除指定 key,返回新对象\n *\n * @param obj - 源对象\n * @param keys - 要排除的 key 数组\n * @returns 排除指定 key 后的新对象\n *\n * @example\n * ```ts\n * import omit from '@tanwan/utils/omit'\n * omit({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { b: 2 }\n * omit({ x: 'foo' }, []) // { x: 'foo' }\n * ```\n */\nexport default function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Omit<T, K> {\n const keySet = new Set<K>(keys);\n const result = {} as Record<string, unknown>;\n for (const key in obj) {\n if (Object.prototype.hasOwnProperty.call(obj, key) && !keySet.has(key as unknown as K)) {\n result[key] = obj[key];\n }\n }\n return result as Omit<T, K>;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,SAAwB,KAA2D,KAAQ,MAAuB;CAChH,MAAM,SAAS,IAAI,IAAO,KAAK;CAC/B,MAAM,SAAS,EAAE;AACjB,MAAK,MAAM,OAAO,IAChB,KAAI,OAAO,UAAU,eAAe,KAAK,KAAK,IAAI,IAAI,CAAC,OAAO,IAAI,IAAoB,CACpF,QAAO,OAAO,IAAI;AAGtB,QAAO"}
@@ -0,0 +1,22 @@
1
+ //#region src/object/pick/index.ts
2
+ /**
3
+ * 从对象中选取指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要选取的 key 数组
7
+ * @returns 包含指定 key 的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import pick from '@tanwan/utils/pick'
12
+ * pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { a: 1, c: 3 }
13
+ * pick({ x: 'foo' }, ['y']) // {}
14
+ * ```
15
+ */
16
+ function pick(obj, keys) {
17
+ const result = {};
18
+ for (const key of keys) if (key in obj) result[key] = obj[key];
19
+ return result;
20
+ }
21
+ //#endregion
22
+ module.exports = pick;
@@ -0,0 +1,18 @@
1
+ //#region src/object/pick/index.d.ts
2
+ /**
3
+ * 从对象中选取指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要选取的 key 数组
7
+ * @returns 包含指定 key 的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import pick from '@tanwan/utils/pick'
12
+ * pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { a: 1, c: 3 }
13
+ * pick({ x: 'foo' }, ['y']) // {}
14
+ * ```
15
+ */
16
+ declare function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
17
+ export = pick;
18
+ //# sourceMappingURL=pick.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pick.d.cts","names":[],"sources":["../../src/object/pick/index.ts"],"mappings":";;;;;;;;;;;;;;;iBAcwB,IAAA,WAAe,MAAA,mCAAyC,CAAA,CAAA,CAAG,GAAA,EAAK,CAAA,EAAG,IAAA,EAAM,CAAA,KAAM,IAAA,CAAK,CAAA,EAAG,CAAA;AAAA"}
@@ -0,0 +1,19 @@
1
+ //#region src/object/pick/index.d.ts
2
+ /**
3
+ * 从对象中选取指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要选取的 key 数组
7
+ * @returns 包含指定 key 的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import pick from '@tanwan/utils/pick'
12
+ * pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { a: 1, c: 3 }
13
+ * pick({ x: 'foo' }, ['y']) // {}
14
+ * ```
15
+ */
16
+ declare function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
17
+ //#endregion
18
+ export { pick as default };
19
+ //# sourceMappingURL=pick.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pick.d.ts","names":[],"sources":["../../src/object/pick/index.ts"],"mappings":";;;;;;;;;;;;;;;iBAcwB,IAAA,WAAe,MAAA,mCAAyC,CAAA,CAAA,CAAG,GAAA,EAAK,CAAA,EAAG,IAAA,EAAM,CAAA,KAAM,IAAA,CAAK,CAAA,EAAG,CAAA"}
@@ -0,0 +1,24 @@
1
+ //#region src/object/pick/index.ts
2
+ /**
3
+ * 从对象中选取指定 key,返回新对象
4
+ *
5
+ * @param obj - 源对象
6
+ * @param keys - 要选取的 key 数组
7
+ * @returns 包含指定 key 的新对象
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import pick from '@tanwan/utils/pick'
12
+ * pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { a: 1, c: 3 }
13
+ * pick({ x: 'foo' }, ['y']) // {}
14
+ * ```
15
+ */
16
+ function pick(obj, keys) {
17
+ const result = {};
18
+ for (const key of keys) if (key in obj) result[key] = obj[key];
19
+ return result;
20
+ }
21
+ //#endregion
22
+ export { pick as default };
23
+
24
+ //# sourceMappingURL=pick.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pick.js","names":[],"sources":["../../src/object/pick/index.ts"],"sourcesContent":["/**\n * 从对象中选取指定 key,返回新对象\n *\n * @param obj - 源对象\n * @param keys - 要选取的 key 数组\n * @returns 包含指定 key 的新对象\n *\n * @example\n * ```ts\n * import pick from '@tanwan/utils/pick'\n * pick({ a: 1, b: 2, c: 3 }, ['a', 'c']) // { a: 1, c: 3 }\n * pick({ x: 'foo' }, ['y']) // {}\n * ```\n */\nexport default function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {\n const result = {} as Pick<T, K>;\n for (const key of keys) {\n if (key in obj) {\n result[key] = obj[key];\n }\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,SAAwB,KAA2D,KAAQ,MAAuB;CAChH,MAAM,SAAS,EAAE;AACjB,MAAK,MAAM,OAAO,KAChB,KAAI,OAAO,IACT,QAAO,OAAO,IAAI;AAGtB,QAAO"}
@@ -5,7 +5,6 @@
5
5
  function camelCase(str, capital = false) {
6
6
  let result = str.replace(/[-_.\s]+(\w|$)/g, (_, c) => c.toUpperCase());
7
7
  if (capital && result.length > 0) result = result[0].toUpperCase() + result.slice(1);
8
- console.log(result);
9
8
  return result;
10
9
  }
11
10
  //#endregion
@@ -5,7 +5,6 @@
5
5
  function camelCase(str, capital = false) {
6
6
  let result = str.replace(/[-_.\s]+(\w|$)/g, (_, c) => c.toUpperCase());
7
7
  if (capital && result.length > 0) result = result[0].toUpperCase() + result.slice(1);
8
- console.log(result);
9
8
  return result;
10
9
  }
11
10
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"camelCase.js","names":[],"sources":["../../src/string/camelCase/index.ts"],"sourcesContent":["/**\n * 将字符串转换为驼峰命名\n */\nexport default function camelCase(str: string, capital = false): string {\n const reg = /[-_.\\s]+(\\w|$)/g;\n let result = str.replace(reg, (_, c) => c.toUpperCase());\n\n if (capital && result.length > 0) {\n result = result[0].toUpperCase() + result.slice(1);\n }\n console.log(result);\n return result;\n}\n"],"mappings":";;;;AAGA,SAAwB,UAAU,KAAa,UAAU,OAAe;CAEtE,IAAI,SAAS,IAAI,QAAQ,oBAAM,GAAG,MAAM,EAAE,aAAa,CAAC;AAExD,KAAI,WAAW,OAAO,SAAS,EAC7B,UAAS,OAAO,GAAG,aAAa,GAAG,OAAO,MAAM,EAAE;AAEpD,SAAQ,IAAI,OAAO;AACnB,QAAO"}
1
+ {"version":3,"file":"camelCase.js","names":[],"sources":["../../src/string/camelCase/index.ts"],"sourcesContent":["/**\n * 将字符串转换为驼峰命名\n */\nexport default function camelCase(str: string, capital = false): string {\n const reg = /[-_.\\s]+(\\w|$)/g;\n let result = str.replace(reg, (_, c) => c.toUpperCase());\n\n if (capital && result.length > 0) {\n result = result[0].toUpperCase() + result.slice(1);\n }\n return result;\n}\n"],"mappings":";;;;AAGA,SAAwB,UAAU,KAAa,UAAU,OAAe;CAEtE,IAAI,SAAS,IAAI,QAAQ,oBAAM,GAAG,MAAM,EAAE,aAAa,CAAC;AAExD,KAAI,WAAW,OAAO,SAAS,EAC7B,UAAS,OAAO,GAAG,aAAa,GAAG,OAAO,MAAM,EAAE;AAEpD,QAAO"}
@@ -3,7 +3,7 @@
3
3
  * 首字母大写
4
4
  * @example
5
5
  * ```ts
6
- * import capitalize from '@tw/utils/capitalize'
6
+ * import capitalize from '@tanwan/utils/capitalize'
7
7
  * capitalize('hello') // 'Hello'
8
8
  * capitalize('hello world') // 'Hello world'
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 首字母大写
4
4
  * @example
5
5
  * ```ts
6
- * import capitalize from '@tw/utils/capitalize'
6
+ * import capitalize from '@tanwan/utils/capitalize'
7
7
  * capitalize('hello') // 'Hello'
8
8
  * capitalize('hello world') // 'Hello world'
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 首字母大写
4
4
  * @example
5
5
  * ```ts
6
- * import capitalize from '@tw/utils/capitalize'
6
+ * import capitalize from '@tanwan/utils/capitalize'
7
7
  * capitalize('hello') // 'Hello'
8
8
  * capitalize('hello world') // 'Hello world'
9
9
  * ```
@@ -3,7 +3,7 @@
3
3
  * 首字母大写
4
4
  * @example
5
5
  * ```ts
6
- * import capitalize from '@tw/utils/capitalize'
6
+ * import capitalize from '@tanwan/utils/capitalize'
7
7
  * capitalize('hello') // 'Hello'
8
8
  * capitalize('hello world') // 'Hello world'
9
9
  * ```
@@ -1 +1 @@
1
- {"version":3,"file":"capitalize.js","names":[],"sources":["../../src/string/capitalize/index.ts"],"sourcesContent":["/**\n * 首字母大写\n * @example\n * ```ts\n * import capitalize from '@tw/utils/capitalize'\n * capitalize('hello') // 'Hello'\n * capitalize('hello world') // 'Hello world'\n * ```\n */\nexport default function capitalize(str: string): string {\n if (!str) return str;\n return str.charAt(0).toUpperCase() + str.slice(1);\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,WAAW,KAAqB;AACtD,KAAI,CAAC,IAAK,QAAO;AACjB,QAAO,IAAI,OAAO,EAAE,CAAC,aAAa,GAAG,IAAI,MAAM,EAAE"}
1
+ {"version":3,"file":"capitalize.js","names":[],"sources":["../../src/string/capitalize/index.ts"],"sourcesContent":["/**\n * 首字母大写\n * @example\n * ```ts\n * import capitalize from '@tanwan/utils/capitalize'\n * capitalize('hello') // 'Hello'\n * capitalize('hello world') // 'Hello world'\n * ```\n */\nexport default function capitalize(str: string): string {\n if (!str) return str;\n return str.charAt(0).toUpperCase() + str.slice(1);\n}\n"],"mappings":";;;;;;;;;;AASA,SAAwB,WAAW,KAAqB;AACtD,KAAI,CAAC,IAAK,QAAO;AACjB,QAAO,IAAI,OAAO,EAAE,CAAC,aAAa,GAAG,IAAI,MAAM,EAAE"}
@@ -0,0 +1,23 @@
1
+ //#region src/string/uuid/index.ts
2
+ /**
3
+ * 生成 UUID v4
4
+ *
5
+ * 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
6
+ *
7
+ * @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import uuid from '@tanwan/utils/uuid'
12
+ * uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
13
+ * ```
14
+ */
15
+ function uuid() {
16
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID();
17
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
18
+ const r = Math.random() * 16 | 0;
19
+ return (c === "x" ? r : r & 3 | 8).toString(16);
20
+ });
21
+ }
22
+ //#endregion
23
+ module.exports = uuid;
@@ -0,0 +1,17 @@
1
+ //#region src/string/uuid/index.d.ts
2
+ /**
3
+ * 生成 UUID v4
4
+ *
5
+ * 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
6
+ *
7
+ * @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import uuid from '@tanwan/utils/uuid'
12
+ * uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
13
+ * ```
14
+ */
15
+ declare function uuid(): string;
16
+ export = uuid;
17
+ //# sourceMappingURL=uuid.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.d.cts","names":[],"sources":["../../src/string/uuid/index.ts"],"mappings":";;;;;;;;;;;;;;iBAawB,IAAA,CAAA;AAAA"}
@@ -0,0 +1,18 @@
1
+ //#region src/string/uuid/index.d.ts
2
+ /**
3
+ * 生成 UUID v4
4
+ *
5
+ * 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
6
+ *
7
+ * @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import uuid from '@tanwan/utils/uuid'
12
+ * uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
13
+ * ```
14
+ */
15
+ declare function uuid(): string;
16
+ //#endregion
17
+ export { uuid as default };
18
+ //# sourceMappingURL=uuid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.d.ts","names":[],"sources":["../../src/string/uuid/index.ts"],"mappings":";;;;;;;;;;;;;;iBAawB,IAAA,CAAA"}
@@ -0,0 +1,25 @@
1
+ //#region src/string/uuid/index.ts
2
+ /**
3
+ * 生成 UUID v4
4
+ *
5
+ * 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案
6
+ *
7
+ * @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import uuid from '@tanwan/utils/uuid'
12
+ * uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'
13
+ * ```
14
+ */
15
+ function uuid() {
16
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return crypto.randomUUID();
17
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
18
+ const r = Math.random() * 16 | 0;
19
+ return (c === "x" ? r : r & 3 | 8).toString(16);
20
+ });
21
+ }
22
+ //#endregion
23
+ export { uuid as default };
24
+
25
+ //# sourceMappingURL=uuid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.js","names":[],"sources":["../../src/string/uuid/index.ts"],"sourcesContent":["/**\n * 生成 UUID v4\n *\n * 优先使用 `crypto.randomUUID()`,不支持时自动降级为手动生成方案\n *\n * @returns UUID v4 格式字符串,例如 `'550e8400-e29b-41d4-a716-446655440000'`\n *\n * @example\n * ```ts\n * import uuid from '@tanwan/utils/uuid'\n * uuid() // 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'\n * ```\n */\nexport default function uuid(): string {\n if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {\n return crypto.randomUUID();\n }\n\n // fallback:手动生成 UUID v4\n return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {\n const r = (Math.random() * 16) | 0;\n const v = c === 'x' ? r : (r & 0x3) | 0x8;\n return v.toString(16);\n });\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,SAAwB,OAAe;AACrC,KAAI,OAAO,WAAW,eAAe,OAAO,OAAO,eAAe,WAChE,QAAO,OAAO,YAAY;AAI5B,QAAO,uCAAuC,QAAQ,UAAU,MAAM;EACpE,MAAM,IAAK,KAAK,QAAQ,GAAG,KAAM;AAEjC,UADU,MAAM,MAAM,IAAK,IAAI,IAAO,GAC7B,SAAS,GAAG;GACrB"}
@@ -0,0 +1,48 @@
1
+ //#region src/url/parseQueryString/index.ts
2
+ /**
3
+ * 解析 URL 查询字符串为对象
4
+ *
5
+ * 支持重复 key 解析为数组,数字字符串自动转为 number,布尔字符串自动转为 boolean
6
+ *
7
+ * @param query - 查询字符串(可带或不带前导 `?`),默认为当前页面 `location.search`
8
+ * @returns 解析后的键值对对象
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import parseQueryString from '@tanwan/utils/parseQueryString'
13
+ * parseQueryString('?name=John&age=25') // { name: 'John', age: 25 }
14
+ * parseQueryString('name=John&age=25') // { name: 'John', age: 25 }
15
+ * parseQueryString('?ids=1&ids=2') // { ids: [1, 2] }
16
+ * parseQueryString('?active=true&count=0') // { active: true, count: 0 }
17
+ * parseQueryString('') // {}
18
+ * ```
19
+ */
20
+ function parseQueryString(query) {
21
+ const qs = query ?? (typeof location !== "undefined" ? location.search : "");
22
+ if (!qs) return {};
23
+ const str = qs.startsWith("?") ? qs.slice(1) : qs;
24
+ if (!str) return {};
25
+ const result = {};
26
+ for (const part of str.split("&")) {
27
+ if (!part) continue;
28
+ const [rawKey, rawValue = ""] = part.split("=");
29
+ const key = decodeURIComponent(rawKey);
30
+ const parsed = parseValue(decodeURIComponent(rawValue));
31
+ if (key in result) {
32
+ const existing = result[key];
33
+ if (Array.isArray(existing)) existing.push(parsed);
34
+ else result[key] = [existing, parsed];
35
+ } else result[key] = parsed;
36
+ }
37
+ return result;
38
+ }
39
+ function parseValue(value) {
40
+ if (value === "true") return true;
41
+ if (value === "false") return false;
42
+ if (value === "") return "";
43
+ const num = Number(value);
44
+ if (!Number.isNaN(num) && String(num) === value) return num;
45
+ return value;
46
+ }
47
+ //#endregion
48
+ module.exports = parseQueryString;
@@ -0,0 +1,22 @@
1
+ //#region src/url/parseQueryString/index.d.ts
2
+ /**
3
+ * 解析 URL 查询字符串为对象
4
+ *
5
+ * 支持重复 key 解析为数组,数字字符串自动转为 number,布尔字符串自动转为 boolean
6
+ *
7
+ * @param query - 查询字符串(可带或不带前导 `?`),默认为当前页面 `location.search`
8
+ * @returns 解析后的键值对对象
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import parseQueryString from '@tanwan/utils/parseQueryString'
13
+ * parseQueryString('?name=John&age=25') // { name: 'John', age: 25 }
14
+ * parseQueryString('name=John&age=25') // { name: 'John', age: 25 }
15
+ * parseQueryString('?ids=1&ids=2') // { ids: [1, 2] }
16
+ * parseQueryString('?active=true&count=0') // { active: true, count: 0 }
17
+ * parseQueryString('') // {}
18
+ * ```
19
+ */
20
+ declare function parseQueryString(query?: string): Record<string, string | number | boolean | (string | number | boolean)[]>;
21
+ export = parseQueryString;
22
+ //# sourceMappingURL=parseQueryString.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parseQueryString.d.cts","names":[],"sources":["../../src/url/parseQueryString/index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;iBAkBwB,gBAAA,CACtB,KAAA,YACC,MAAA;AAAA"}