@tb-dev/utils 2.1.1 → 3.0.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.
@@ -0,0 +1,3 @@
1
+ export { upsert } from './upsert';
2
+ export { toArray } from './to-array';
3
+ export { trimArray } from './trim-array';
@@ -0,0 +1,4 @@
1
+ import { MaybeArray, Nullish } from '../types';
2
+
3
+ /** Converts the item to an array if it isn't already. */
4
+ export declare function toArray<T>(item?: Nullish<MaybeArray<T>>): T[];
@@ -0,0 +1,2 @@
1
+ /** Trims each string in the array, removing any empty strings. */
2
+ export declare function trimArray(array: string[]): string[];
@@ -0,0 +1,2 @@
1
+ /** Pushes an item to the array if it doesn't exist, otherwise updates it. */
2
+ export declare function upsert<T>(array: T[], item: T, predicate?: (value: T) => boolean): void;
package/dist/index.cjs CHANGED
@@ -1,10 +1,77 @@
1
1
  "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
2
5
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const regex = require("./regex.cjs");
4
- const array = require("./array.cjs");
5
- const panic = require("./panic.cjs");
6
- const string = require("./string.cjs");
7
- const promise = require("./promise.cjs");
6
+ const float = /^\d+(?:\.(\d+))?$/;
7
+ const regex = {
8
+ float
9
+ };
10
+ function upsert(array, item, predicate) {
11
+ const index = array.findIndex(predicate ?? ((it) => it === item));
12
+ if (index === -1) {
13
+ array.push(item);
14
+ } else {
15
+ array[index] = item;
16
+ }
17
+ }
18
+ function toArray(item) {
19
+ const array = item ?? [];
20
+ return Array.isArray(array) ? array : [array];
21
+ }
22
+ function trimArray(array) {
23
+ return array.map((it) => it.trim()).filter(Boolean);
24
+ }
25
+ function panic(...args) {
26
+ throw new Error(args.join(" "));
27
+ }
28
+ function todo(...args) {
29
+ panic(format("TODO", args));
30
+ }
31
+ function unimplemented(...args) {
32
+ panic(format("not implemented", args));
33
+ }
34
+ function unreachable(...args) {
35
+ panic(format("unreachable", args));
36
+ }
37
+ function format(base, args) {
38
+ let message = base;
39
+ if (args.length > 0) {
40
+ message = `${message}: ${args.join(" ")}`;
41
+ }
42
+ return message;
43
+ }
44
+ function splitWhitespace(value) {
45
+ if (!value) return [];
46
+ if (Array.isArray(value)) {
47
+ const array = value.map((it) => splitWhitespace(it));
48
+ return array.flat(Number.POSITIVE_INFINITY);
49
+ }
50
+ value = value.trim().split(/\s/);
51
+ return trimArray(value);
52
+ }
53
+ function sleep(ms) {
54
+ return new Promise((resolve) => void setTimeout(resolve, ms));
55
+ }
56
+ class PromiseChain {
57
+ constructor() {
58
+ __publicField(this, "promises", /* @__PURE__ */ new Set());
59
+ }
60
+ chain(promise) {
61
+ this.promises.add(promise);
62
+ return this;
63
+ }
64
+ async join() {
65
+ await Promise.all(this.promises);
66
+ this.promises.clear();
67
+ }
68
+ static create() {
69
+ return new PromiseChain();
70
+ }
71
+ }
72
+ function flushPromises() {
73
+ return new Promise((resolve) => void setTimeout(resolve, 0));
74
+ }
8
75
  function isEmpty(value) {
9
76
  if (isNullish(value)) {
10
77
  return true;
@@ -23,23 +90,24 @@ function isNullish(value) {
23
90
  const noop = () => {
24
91
  };
25
92
  function toPixel(value) {
26
- if (typeof value === "number" || typeof value === "string" && regex.regex.float.test(value)) {
93
+ if (typeof value === "number" || typeof value === "string" && regex.float.test(value)) {
27
94
  return `${value}px`;
28
95
  }
29
96
  return value;
30
97
  }
31
- exports.regex = regex.regex;
32
- exports.toArray = array.toArray;
33
- exports.trimArray = array.trimArray;
34
- exports.upsert = array.upsert;
35
- exports.panic = panic.panic;
36
- exports.todo = panic.todo;
37
- exports.unimplemented = panic.unimplemented;
38
- exports.unreachable = panic.unreachable;
39
- exports.splitWhitespace = string.splitWhitespace;
40
- exports.flushPromises = promise.flushPromises;
41
- exports.sleep = promise.sleep;
98
+ exports.PromiseChain = PromiseChain;
99
+ exports.flushPromises = flushPromises;
42
100
  exports.isEmpty = isEmpty;
43
101
  exports.isNullish = isNullish;
44
102
  exports.noop = noop;
103
+ exports.panic = panic;
104
+ exports.regex = regex;
105
+ exports.sleep = sleep;
106
+ exports.splitWhitespace = splitWhitespace;
107
+ exports.toArray = toArray;
45
108
  exports.toPixel = toPixel;
109
+ exports.todo = todo;
110
+ exports.trimArray = trimArray;
111
+ exports.unimplemented = unimplemented;
112
+ exports.unreachable = unreachable;
113
+ exports.upsert = upsert;
package/dist/index.js CHANGED
@@ -1,8 +1,75 @@
1
- import { regex } from "./regex.js";
2
- import { toArray, trimArray, upsert } from "./array.js";
3
- import { panic, todo, unimplemented, unreachable } from "./panic.js";
4
- import { splitWhitespace } from "./string.js";
5
- import { flushPromises, sleep } from "./promise.js";
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ const float = /^\d+(?:\.(\d+))?$/;
5
+ const regex = {
6
+ float
7
+ };
8
+ function upsert(array, item, predicate) {
9
+ const index = array.findIndex(predicate ?? ((it) => it === item));
10
+ if (index === -1) {
11
+ array.push(item);
12
+ } else {
13
+ array[index] = item;
14
+ }
15
+ }
16
+ function toArray(item) {
17
+ const array = item ?? [];
18
+ return Array.isArray(array) ? array : [array];
19
+ }
20
+ function trimArray(array) {
21
+ return array.map((it) => it.trim()).filter(Boolean);
22
+ }
23
+ function panic(...args) {
24
+ throw new Error(args.join(" "));
25
+ }
26
+ function todo(...args) {
27
+ panic(format("TODO", args));
28
+ }
29
+ function unimplemented(...args) {
30
+ panic(format("not implemented", args));
31
+ }
32
+ function unreachable(...args) {
33
+ panic(format("unreachable", args));
34
+ }
35
+ function format(base, args) {
36
+ let message = base;
37
+ if (args.length > 0) {
38
+ message = `${message}: ${args.join(" ")}`;
39
+ }
40
+ return message;
41
+ }
42
+ function splitWhitespace(value) {
43
+ if (!value) return [];
44
+ if (Array.isArray(value)) {
45
+ const array = value.map((it) => splitWhitespace(it));
46
+ return array.flat(Number.POSITIVE_INFINITY);
47
+ }
48
+ value = value.trim().split(/\s/);
49
+ return trimArray(value);
50
+ }
51
+ function sleep(ms) {
52
+ return new Promise((resolve) => void setTimeout(resolve, ms));
53
+ }
54
+ class PromiseChain {
55
+ constructor() {
56
+ __publicField(this, "promises", /* @__PURE__ */ new Set());
57
+ }
58
+ chain(promise) {
59
+ this.promises.add(promise);
60
+ return this;
61
+ }
62
+ async join() {
63
+ await Promise.all(this.promises);
64
+ this.promises.clear();
65
+ }
66
+ static create() {
67
+ return new PromiseChain();
68
+ }
69
+ }
70
+ function flushPromises() {
71
+ return new Promise((resolve) => void setTimeout(resolve, 0));
72
+ }
6
73
  function isEmpty(value) {
7
74
  if (isNullish(value)) {
8
75
  return true;
@@ -27,6 +94,7 @@ function toPixel(value) {
27
94
  return value;
28
95
  }
29
96
  export {
97
+ PromiseChain,
30
98
  flushPromises,
31
99
  isEmpty,
32
100
  isNullish,
@@ -1,3 +1,2 @@
1
1
  /** Flushes all pending promises. */
2
2
  export declare function flushPromises(): Promise<void>;
3
- export declare function sleep(ms: number): Promise<void>;
@@ -0,0 +1,3 @@
1
+ export { sleep } from './sleep';
2
+ export { PromiseChain } from './promise-chain';
3
+ export { flushPromises } from './flush-promises';
@@ -0,0 +1,6 @@
1
+ export declare class PromiseChain {
2
+ private readonly promises;
3
+ chain(promise: Promise<any>): this;
4
+ join(): Promise<void>;
5
+ static create(): PromiseChain;
6
+ }
@@ -0,0 +1 @@
1
+ export declare function sleep(ms: number): Promise<void>;
@@ -0,0 +1 @@
1
+ export { splitWhitespace } from './split-whitespace';
@@ -1,4 +1,4 @@
1
- import { Nullish } from './types';
1
+ import { Nullish } from '../types';
2
2
 
3
3
  /**
4
4
  * Splits a string or an array of strings (recursively) by whitespace.
package/dist/types.d.ts CHANGED
@@ -27,10 +27,6 @@ export type PickByValue<T, V> = {
27
27
  export type PickPartial<T, K extends keyof T> = Pick<Partial<T>, K>;
28
28
  /** Constructs a type by picking the set of properties `K` from a required version of `T`. */
29
29
  export type PickRequired<T, K extends keyof T> = Pick<Required<T>, K>;
30
- /** Constructs a type where all properties of `T` are strings. */
31
- export type StringifyObject<T> = {
32
- [P in keyof T]: string;
33
- };
34
30
  /** Constructs a type consisting of some properties of T set to partial. */
35
31
  export type WithPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
36
32
  /** Constructs a type consisting of some properties of T set to partial. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/utils",
3
- "version": "2.1.1",
3
+ "version": "3.0.0",
4
4
  "description": "TypeScript utils",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,12 +23,12 @@
23
23
  "typescript"
24
24
  ],
25
25
  "lint-staged": {
26
- "*.{?(c|m)@(j|t)s,css,vue,md,json}": "prettier --write",
27
- "*.{?(c|m)@(j|t)s,vue}": "pnpm run lint"
26
+ "*.{?(c|m)@(j|t)s,css,vue,md,json}": "prettier --write"
28
27
  },
29
28
  "devDependencies": {
30
29
  "@tb-dev/eslint-config": "^5.2.0",
31
30
  "@types/node": "^22.2.0",
31
+ "@vitest/ui": "^2.0.5",
32
32
  "eslint": "^9.9.0",
33
33
  "husky": "^9.1.4",
34
34
  "lint-staged": "^15.2.8",
@@ -38,7 +38,8 @@
38
38
  "typedoc-plugin-mdn-links": "^3.2.8",
39
39
  "typescript": "^5.5.4",
40
40
  "vite": "^5.4.0",
41
- "vite-plugin-dts": "^3.9.1"
41
+ "vite-plugin-dts": "^3.9.1",
42
+ "vitest": "^2.0.5"
42
43
  },
43
44
  "files": [
44
45
  "dist"
@@ -51,36 +52,6 @@
51
52
  "types": "./dist/index.d.ts",
52
53
  "import": "./dist/index.js",
53
54
  "require": "./dist/index.cjs"
54
- },
55
- "./array": {
56
- "types": "./dist/array.d.ts",
57
- "import": "./dist/array.js",
58
- "require": "./dist/array.cjs"
59
- },
60
- "./panic": {
61
- "types": "./dist/panic.d.ts",
62
- "import": "./dist/panic.js",
63
- "require": "./dist/panic.cjs"
64
- },
65
- "./promise": {
66
- "types": "./dist/promise.d.ts",
67
- "import": "./dist/promise.js",
68
- "require": "./dist/promise.cjs"
69
- },
70
- "./regex": {
71
- "types": "./dist/regex.d.ts",
72
- "import": "./dist/regex.js",
73
- "require": "./dist/regex.cjs"
74
- },
75
- "./string": {
76
- "types": "./dist/string.d.ts",
77
- "import": "./dist/string.js",
78
- "require": "./dist/string.cjs"
79
- },
80
- "./types": {
81
- "types": "./dist/types.d.ts",
82
- "import": "./dist/types.js",
83
- "require": "./dist/types.cjs"
84
55
  }
85
56
  },
86
57
  "scripts": {
@@ -91,6 +62,8 @@
91
62
  "lint": "eslint . --config eslint.config.js --cache",
92
63
  "lint-fix": "eslint . --config eslint.config.js --fix",
93
64
  "release": "pnpm run build && pnpm publish",
65
+ "test": "vitest",
66
+ "test:ui": "vitest --ui --watch",
94
67
  "type-check": "tsc --noEmit",
95
68
  "update": "miho update major -t"
96
69
  }
package/dist/array.cjs DELETED
@@ -1,20 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function upsert(array, item, predicate) {
4
- const index = array.findIndex(predicate);
5
- if (index === -1) {
6
- array.push(item);
7
- } else {
8
- array[index] = item;
9
- }
10
- }
11
- function toArray(item) {
12
- const array = item ?? [];
13
- return Array.isArray(array) ? array : [array];
14
- }
15
- function trimArray(array) {
16
- return array.map((i) => i.trim()).filter(Boolean);
17
- }
18
- exports.toArray = toArray;
19
- exports.trimArray = trimArray;
20
- exports.upsert = upsert;
package/dist/array.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import { MaybeArray, Nullish } from './types';
2
-
3
- /** Pushes an item to the array if it doesn't exist, otherwise updates it. */
4
- export declare function upsert<T>(array: T[], item: T, predicate: (value: T) => boolean): void;
5
- /** Converts the item to an array if it isn't already. */
6
- export declare function toArray<T>(item?: Nullish<MaybeArray<T>>): T[];
7
- /** Trims each string in the array, removing any empty strings. */
8
- export declare function trimArray(array: string[]): string[];
package/dist/array.js DELETED
@@ -1,20 +0,0 @@
1
- function upsert(array, item, predicate) {
2
- const index = array.findIndex(predicate);
3
- if (index === -1) {
4
- array.push(item);
5
- } else {
6
- array[index] = item;
7
- }
8
- }
9
- function toArray(item) {
10
- const array = item ?? [];
11
- return Array.isArray(array) ? array : [array];
12
- }
13
- function trimArray(array) {
14
- return array.map((i) => i.trim()).filter(Boolean);
15
- }
16
- export {
17
- toArray,
18
- trimArray,
19
- upsert
20
- };
package/dist/panic.cjs DELETED
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function panic(...args) {
4
- throw new Error(args.join(" "));
5
- }
6
- function todo(...args) {
7
- panic(format("TODO", args));
8
- }
9
- function unimplemented(...args) {
10
- panic(format("not implemented", args));
11
- }
12
- function unreachable(...args) {
13
- panic(format("unreachable", args));
14
- }
15
- function format(base, args) {
16
- let message = base;
17
- if (args.length > 0) {
18
- message = `${message}: ${args.join(" ")}`;
19
- }
20
- return message;
21
- }
22
- exports.panic = panic;
23
- exports.todo = todo;
24
- exports.unimplemented = unimplemented;
25
- exports.unreachable = unreachable;
package/dist/panic.js DELETED
@@ -1,25 +0,0 @@
1
- function panic(...args) {
2
- throw new Error(args.join(" "));
3
- }
4
- function todo(...args) {
5
- panic(format("TODO", args));
6
- }
7
- function unimplemented(...args) {
8
- panic(format("not implemented", args));
9
- }
10
- function unreachable(...args) {
11
- panic(format("unreachable", args));
12
- }
13
- function format(base, args) {
14
- let message = base;
15
- if (args.length > 0) {
16
- message = `${message}: ${args.join(" ")}`;
17
- }
18
- return message;
19
- }
20
- export {
21
- panic,
22
- todo,
23
- unimplemented,
24
- unreachable
25
- };
package/dist/promise.cjs DELETED
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const scheduler = typeof setImmediate === "function" ? setImmediate : setTimeout;
4
- function flushPromises() {
5
- return new Promise((resolve) => void scheduler(resolve, 0));
6
- }
7
- function sleep(ms) {
8
- return new Promise((resolve) => void setTimeout(resolve, ms));
9
- }
10
- exports.flushPromises = flushPromises;
11
- exports.sleep = sleep;
package/dist/promise.js DELETED
@@ -1,11 +0,0 @@
1
- const scheduler = typeof setImmediate === "function" ? setImmediate : setTimeout;
2
- function flushPromises() {
3
- return new Promise((resolve) => void scheduler(resolve, 0));
4
- }
5
- function sleep(ms) {
6
- return new Promise((resolve) => void setTimeout(resolve, ms));
7
- }
8
- export {
9
- flushPromises,
10
- sleep
11
- };
package/dist/regex.cjs DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const float = /^\d+(?:\.(\d+))?$/;
4
- const regex = {
5
- float
6
- };
7
- exports.regex = regex;
package/dist/regex.js DELETED
@@ -1,7 +0,0 @@
1
- const float = /^\d+(?:\.(\d+))?$/;
2
- const regex = {
3
- float
4
- };
5
- export {
6
- regex
7
- };
package/dist/string.cjs DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const array = require("./array.cjs");
4
- function splitWhitespace(value) {
5
- if (!value) return [];
6
- if (Array.isArray(value)) {
7
- const array2 = value.map((it) => splitWhitespace(it));
8
- return array2.flat(Number.POSITIVE_INFINITY);
9
- }
10
- value = value.trim().split(/\s/);
11
- return array.trimArray(value);
12
- }
13
- exports.splitWhitespace = splitWhitespace;
package/dist/string.js DELETED
@@ -1,13 +0,0 @@
1
- import { trimArray } from "./array.js";
2
- function splitWhitespace(value) {
3
- if (!value) return [];
4
- if (Array.isArray(value)) {
5
- const array = value.map((it) => splitWhitespace(it));
6
- return array.flat(Number.POSITIVE_INFINITY);
7
- }
8
- value = value.trim().split(/\s/);
9
- return trimArray(value);
10
- }
11
- export {
12
- splitWhitespace
13
- };
package/dist/types.cjs DELETED
@@ -1 +0,0 @@
1
- "use strict";
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
-