@tb-dev/utils 1.3.3 → 1.4.1

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/array.cjs ADDED
@@ -0,0 +1,32 @@
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
+ return array;
11
+ }
12
+ function splitWhitespace(value) {
13
+ if (!value)
14
+ return [];
15
+ if (Array.isArray(value)) {
16
+ const array = value.map((i) => splitWhitespace(i));
17
+ return array.flat(Number.POSITIVE_INFINITY);
18
+ }
19
+ value = value.trim().split(/\s/);
20
+ return trimArray(value);
21
+ }
22
+ function toArray(item) {
23
+ const array = item ?? [];
24
+ return Array.isArray(array) ? array : [array];
25
+ }
26
+ function trimArray(array) {
27
+ return array.map((i) => i.trim()).filter(Boolean);
28
+ }
29
+ exports.splitWhitespace = splitWhitespace;
30
+ exports.toArray = toArray;
31
+ exports.trimArray = trimArray;
32
+ exports.upsert = upsert;
@@ -0,0 +1,5 @@
1
+ import type { MaybeArray, Nullish } from '@tb-dev/utility-types';
2
+ export declare function upsert<T>(array: T[], item: T, predicate: (item: T) => boolean): T[];
3
+ export declare function splitWhitespace(value: Nullish<string | string[]>): string[];
4
+ export declare function toArray<T>(item?: Nullish<MaybeArray<T>>): T[];
5
+ export declare function trimArray(array: string[]): string[];
package/dist/array.js ADDED
@@ -0,0 +1,32 @@
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
+ return array;
9
+ }
10
+ function splitWhitespace(value) {
11
+ if (!value)
12
+ return [];
13
+ if (Array.isArray(value)) {
14
+ const array = value.map((i) => splitWhitespace(i));
15
+ return array.flat(Number.POSITIVE_INFINITY);
16
+ }
17
+ value = value.trim().split(/\s/);
18
+ return trimArray(value);
19
+ }
20
+ function toArray(item) {
21
+ const array = item ?? [];
22
+ return Array.isArray(array) ? array : [array];
23
+ }
24
+ function trimArray(array) {
25
+ return array.map((i) => i.trim()).filter(Boolean);
26
+ }
27
+ export {
28
+ splitWhitespace,
29
+ toArray,
30
+ trimArray,
31
+ upsert
32
+ };
package/dist/index.cjs CHANGED
@@ -1,55 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function splitWhitespace(value) {
4
- if (!value)
5
- return [];
6
- if (Array.isArray(value)) {
7
- const array = value.map((i) => splitWhitespace(i));
8
- return array.flat(Number.POSITIVE_INFINITY);
9
- }
10
- value = value.trim().split(/\s/);
11
- return trimArray(value);
12
- }
13
- function toArray(item) {
14
- const array = item ?? [];
15
- return Array.isArray(array) ? array : [array];
16
- }
17
- function trimArray(array) {
18
- return array.map((i) => i.trim()).filter(Boolean);
19
- }
20
- function isEmpty(value) {
21
- if (Array.isArray(value) || typeof value === "string") {
22
- return value.length === 0;
23
- }
24
- if (value instanceof Map || value instanceof Set) {
25
- return value.size === 0;
26
- }
27
- return isNullish(value);
28
- }
3
+ const regex = require("./regex.cjs");
4
+ const array = require("./array.cjs");
29
5
  function isNullish(value) {
30
6
  return value === null || value === void 0;
31
7
  }
32
- function repeat(amount, fn) {
33
- let n = Math.trunc(amount);
34
- if (n < 1) {
35
- n = 0;
36
- } else if (n > Number.MAX_SAFE_INTEGER) {
37
- n = Number.MAX_SAFE_INTEGER;
38
- }
39
- for (let i = 0; i < n; i++) {
40
- fn(i);
41
- }
42
- }
43
8
  function toPixel(value) {
44
- if (typeof value === "number" || typeof value === "string" && /^\d+(?:\.(\d+))?$/.test(value)) {
9
+ if (typeof value === "number" || typeof value === "string" && regex.float.test(value)) {
45
10
  return `${value}px`;
46
11
  }
47
12
  return value;
48
13
  }
49
- exports.isEmpty = isEmpty;
14
+ exports.splitWhitespace = array.splitWhitespace;
15
+ exports.toArray = array.toArray;
16
+ exports.trimArray = array.trimArray;
17
+ exports.upsert = array.upsert;
50
18
  exports.isNullish = isNullish;
51
- exports.repeat = repeat;
52
- exports.splitWhitespace = splitWhitespace;
53
- exports.toArray = toArray;
54
19
  exports.toPixel = toPixel;
55
- exports.trimArray = trimArray;
package/dist/index.d.ts CHANGED
@@ -1,30 +1,7 @@
1
- import type { MaybeArray } from '@tb-dev/utility-types';
2
- import type { Nullish } from '@tb-dev/utility-types';
3
-
4
- export declare function isEmpty(value?: Nullish<string>): boolean;
5
-
6
- export declare function isEmpty<T>(value?: Nullish<T[]>): boolean;
7
-
8
- export declare function isEmpty<T>(value?: Nullish<readonly T[]>): boolean;
9
-
10
- export declare function isEmpty<K>(value?: Nullish<Set<K>>): boolean;
11
-
12
- export declare function isEmpty<K>(value?: Nullish<ReadonlySet<K>>): boolean;
13
-
14
- export declare function isEmpty<K, V>(value?: Nullish<Map<K, V>>): boolean;
15
-
16
- export declare function isEmpty<K, V>(value?: Nullish<ReadonlyMap<K, V>>): boolean;
17
-
18
- export declare function isNullish(value: unknown): boolean;
19
-
20
- export declare function repeat(amount: number, fn: (current: number) => void): void;
21
-
22
- export declare function splitWhitespace(value: Nullish<string | string[]>): string[];
23
-
24
- export declare function toArray<T>(item?: Nullish<MaybeArray<T>>): T[];
25
-
26
- export declare function toPixel(value: string | number): string;
27
-
28
- export declare function trimArray(array: string[]): string[];
29
-
30
- export { }
1
+ export { splitWhitespace, toArray, trimArray, upsert } from './array';
2
+ export declare function isNullish(value: unknown): value is null | undefined;
3
+ /**
4
+ * Add the pixel unit to a value.
5
+ * If the value is a string, it is returned unchanged.
6
+ */
7
+ export declare function toPixel(value: string | number): string;
package/dist/index.js CHANGED
@@ -1,55 +1,19 @@
1
- function splitWhitespace(value) {
2
- if (!value)
3
- return [];
4
- if (Array.isArray(value)) {
5
- const array = value.map((i) => splitWhitespace(i));
6
- return array.flat(Number.POSITIVE_INFINITY);
7
- }
8
- value = value.trim().split(/\s/);
9
- return trimArray(value);
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
- function isEmpty(value) {
19
- if (Array.isArray(value) || typeof value === "string") {
20
- return value.length === 0;
21
- }
22
- if (value instanceof Map || value instanceof Set) {
23
- return value.size === 0;
24
- }
25
- return isNullish(value);
26
- }
1
+ import { float } from "./regex.js";
2
+ import { splitWhitespace, toArray, trimArray, upsert } from "./array.js";
27
3
  function isNullish(value) {
28
4
  return value === null || value === void 0;
29
5
  }
30
- function repeat(amount, fn) {
31
- let n = Math.trunc(amount);
32
- if (n < 1) {
33
- n = 0;
34
- } else if (n > Number.MAX_SAFE_INTEGER) {
35
- n = Number.MAX_SAFE_INTEGER;
36
- }
37
- for (let i = 0; i < n; i++) {
38
- fn(i);
39
- }
40
- }
41
6
  function toPixel(value) {
42
- if (typeof value === "number" || typeof value === "string" && /^\d+(?:\.(\d+))?$/.test(value)) {
7
+ if (typeof value === "number" || typeof value === "string" && float.test(value)) {
43
8
  return `${value}px`;
44
9
  }
45
10
  return value;
46
11
  }
47
12
  export {
48
- isEmpty,
49
13
  isNullish,
50
- repeat,
51
14
  splitWhitespace,
52
15
  toArray,
53
16
  toPixel,
54
- trimArray
17
+ trimArray,
18
+ upsert
55
19
  };
package/dist/regex.cjs ADDED
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const float = /^\d+(?:\.(\d+))?$/;
4
+ exports.float = float;
@@ -0,0 +1 @@
1
+ export declare const float: RegExp;
package/dist/regex.js ADDED
@@ -0,0 +1,4 @@
1
+ const float = /^\d+(?:\.(\d+))?$/;
2
+ export {
3
+ float
4
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/utils",
3
- "version": "1.3.3",
3
+ "version": "1.4.1",
4
4
  "description": "TypeScript utils",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -28,10 +28,10 @@
28
28
  "*.{?(c|m)@(j|t)s,vue}": "pnpm run lint"
29
29
  },
30
30
  "dependencies": {
31
- "@tb-dev/utility-types": "^1.2.3"
31
+ "@tb-dev/utility-types": "^1.2.4"
32
32
  },
33
33
  "devDependencies": {
34
- "@tb-dev/eslint-config": "^3.2.2",
34
+ "@tb-dev/eslint-config": "^3.3.0",
35
35
  "@types/node": "^20.11.30",
36
36
  "eslint": "^8.57.0",
37
37
  "husky": "^9.0.11",
@@ -41,7 +41,7 @@
41
41
  "typedoc": "^0.25.12",
42
42
  "typedoc-plugin-mdn-links": "^3.1.18",
43
43
  "typescript": "^5.4.3",
44
- "vite": "^5.2.3",
44
+ "vite": "^5.2.6",
45
45
  "vite-plugin-dts": "^3.7.3"
46
46
  },
47
47
  "files": [
@@ -55,6 +55,16 @@
55
55
  "types": "./dist/index.d.ts",
56
56
  "import": "./dist/index.js",
57
57
  "require": "./dist/index.cjs"
58
+ },
59
+ "./array": {
60
+ "types": "./dist/array.d.ts",
61
+ "import": "./dist/array.js",
62
+ "require": "./dist/array.cjs"
63
+ },
64
+ "./regex": {
65
+ "types": "./dist/regex.d.ts",
66
+ "import": "./dist/regex.js",
67
+ "require": "./dist/regex.cjs"
58
68
  }
59
69
  },
60
70
  "scripts": {
@@ -64,6 +74,7 @@
64
74
  "format-check": "prettier . --check",
65
75
  "lint": "eslint . --config eslint.config.js --cache",
66
76
  "release": "pnpm run build && pnpm publish",
67
- "type-check": "tsc --noEmit"
77
+ "type-check": "tsc --noEmit",
78
+ "update": "miho update major -t"
68
79
  }
69
80
  }