@tb-dev/utils 5.1.5 → 6.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.
@@ -1,3 +1,3 @@
1
- export { upsert } from './upsert';
2
- export { toArray } from './to-array';
3
- export { trimArray, type TrimArrayOptions } from './trim-array';
1
+ export * from './to-array';
2
+ export * from './trim-array';
3
+ export * from './upsert';
package/dist/index.d.ts CHANGED
@@ -1,20 +1,10 @@
1
- export * from './nil';
2
1
  export * from './array';
2
+ export * from './function';
3
+ export * from './nil';
4
+ export * from './option';
3
5
  export * from './panic';
4
- export * from './regex';
5
- export * from './types';
6
- export * from './string';
7
6
  export * from './promise';
8
- export * from './function';
9
- /**
10
- * Checks if a value is empty.
11
- *
12
- * Arrays and strings are considered empty if they have a length of 0.
13
- * Map and Sets are considered empty if they have a size of 0.
14
- *
15
- * This function does not check for empty objects.
16
- * Any other value is considered empty only if it is nullish.
17
- */
18
- export declare function isEmpty(value?: unknown): boolean;
19
- /** Adds the pixel unit to a value. */
7
+ export * from './string';
8
+ export type * from './types';
9
+ /** Adds the pixel unit to a value if it is a number. */
20
10
  export declare function toPixel(value: string | number): string;
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
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 isNil(value) {
9
- return value === void 0 || value === null;
4
+ function toArray(item) {
5
+ const array = item ?? [];
6
+ return Array.isArray(array) ? array : [array];
7
+ }
8
+ function trimArray(array, options = {}) {
9
+ const _array = array.map((it) => it.trim());
10
+ return options.allowEmpty ? _array : _array.filter(Boolean);
10
11
  }
11
- const isNullish = isNil;
12
12
  function upsert(array, item, predicate) {
13
13
  const index = array.findIndex(predicate ?? ((value) => value === item));
14
14
  if (index === -1) {
@@ -17,14 +17,12 @@ function upsert(array, item, predicate) {
17
17
  array[index] = item;
18
18
  }
19
19
  }
20
- function toArray(item) {
21
- const array = item ?? [];
22
- return Array.isArray(array) ? array : [array];
23
- }
24
- function trimArray(array, options = {}) {
25
- const _array = array.map((it) => it.trim());
26
- return options.allowEmpty ? _array : _array.filter(Boolean);
20
+ const noop = () => {
21
+ };
22
+ function isNil(value) {
23
+ return value === void 0 || value === null;
27
24
  }
25
+ const isNullish = isNil;
28
26
  function panic(...args) {
29
27
  throw new Error(args.join(" "));
30
28
  }
@@ -44,17 +42,14 @@ function format(base, args) {
44
42
  }
45
43
  return message;
46
44
  }
47
- function splitWhitespace(value, options = {}) {
48
- if (!value) return [];
49
- if (Array.isArray(value)) {
50
- const array = value.map((it) => splitWhitespace(it, options));
51
- return array.flat(Number.POSITIVE_INFINITY);
52
- }
53
- value = value.trim().split(/\s/);
54
- return trimArray(value, options);
45
+ function unwrap(value, message = "`unwrap` called with a nil value") {
46
+ return isNil(value) ? panic(message) : value;
55
47
  }
56
- function sleep(ms) {
57
- return new Promise((resolve) => void setTimeout(resolve, ms));
48
+ function unwrapOr(value, other) {
49
+ return isNil(value) ? other : value;
50
+ }
51
+ function unwrapOrElse(value, fn) {
52
+ return isNil(value) ? fn() : value;
58
53
  }
59
54
  function flushPromises() {
60
55
  return new Promise((resolve) => void setTimeout(resolve, 0));
@@ -83,35 +78,31 @@ class PromiseSet {
83
78
  return set;
84
79
  }
85
80
  }
86
- const noop = () => {
87
- };
88
- function isEmpty(value) {
89
- if (isNullish(value)) {
90
- return true;
91
- }
92
- if (Array.isArray(value) || typeof value === "string") {
93
- return value.length === 0;
94
- }
95
- if (value instanceof Map || value instanceof Set) {
96
- return value.size === 0;
81
+ function sleep(ms) {
82
+ return new Promise((resolve) => void setTimeout(resolve, ms));
83
+ }
84
+ function splitWhitespace(value, options = {}) {
85
+ if (value) {
86
+ if (Array.isArray(value)) {
87
+ const array = value.map((it) => splitWhitespace(it, options));
88
+ return array.flat(Number.POSITIVE_INFINITY);
89
+ }
90
+ value = value.trim().split(/\s/);
91
+ return trimArray(value, options);
97
92
  }
98
- return false;
93
+ return [];
99
94
  }
100
95
  function toPixel(value) {
101
- if (typeof value === "number" || typeof value === "string" && regex.float.test(value)) {
102
- return `${value}px`;
103
- }
96
+ if (typeof value === "number") return `${value}px`;
104
97
  return value;
105
98
  }
106
99
  export {
107
100
  PromiseSet,
108
101
  flushPromises,
109
- isEmpty,
110
102
  isNil,
111
103
  isNullish,
112
104
  noop,
113
105
  panic,
114
- regex,
115
106
  sleep,
116
107
  splitWhitespace,
117
108
  toArray,
@@ -120,5 +111,8 @@ export {
120
111
  trimArray,
121
112
  unimplemented,
122
113
  unreachable,
114
+ unwrap,
115
+ unwrapOr,
116
+ unwrapOrElse,
123
117
  upsert
124
118
  };
@@ -1 +1 @@
1
- export { isNil, isNullish } from './is-nil';
1
+ export * from './is-nil';
@@ -0,0 +1 @@
1
+ export * from './unwrap';
@@ -0,0 +1,4 @@
1
+ import { Option } from '../types';
2
+ export declare function unwrap<T>(value: Option<T>, message?: string): T;
3
+ export declare function unwrapOr<T>(value: Option<T>, other: T): T;
4
+ export declare function unwrapOrElse<T>(value: Option<T>, fn: () => T): T;
@@ -1,2 +1,2 @@
1
- /** Flushes all pending promises. */
1
+ /** Flushes pending promises. */
2
2
  export declare function flushPromises(): Promise<void>;
@@ -1,3 +1,3 @@
1
- export { sleep } from './sleep';
2
- export { PromiseSet } from './promise-set';
3
- export { flushPromises } from './flush-promises';
1
+ export * from './flush-promises';
2
+ export * from './promise-set';
3
+ export * from './sleep';
@@ -1 +1 @@
1
- export { splitWhitespace, type SplitWhitespaceOptions } from './split-whitespace';
1
+ export * from './split-whitespace';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/utils",
3
- "version": "5.1.5",
3
+ "version": "6.0.0",
4
4
  "description": "TypeScript utils",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/dist/regex.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export declare const regex: {
2
- readonly float: RegExp;
3
- };
File without changes
File without changes