@bytebury/toolkit 1.8.0 → 2.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.
@@ -177,3 +177,83 @@ export function average(nums) {
177
177
  return 0;
178
178
  return sum(nums) / nums.length;
179
179
  }
180
+ /**
181
+ * Clamps a number between a minimum and maximum value.
182
+ *
183
+ * @example
184
+ * ```ts
185
+ * clamp(5, 0, 10); // 5
186
+ * clamp(-1, 0, 10); // 0
187
+ * clamp(15, 0, 10); // 10
188
+ * ```
189
+ */
190
+ export function clamp(num, min, max) {
191
+ return Math.min(Math.max(num, min), max);
192
+ }
193
+ /**
194
+ * Returns the median value from a list of numbers.
195
+ *
196
+ * @remarks
197
+ * If you pass an empty list, this will return `0`.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * median([]); // 0
202
+ * median([1, 2, 3]); // 2
203
+ * median([1, 2, 3, 4]); // 2.5
204
+ * ```
205
+ */
206
+ export function median(nums) {
207
+ if (isEmpty(nums))
208
+ return 0;
209
+ const sorted = [...nums].sort((a, b) => a - b);
210
+ const mid = Math.floor(sorted.length / 2);
211
+ return sorted.length % 2 === 0
212
+ ? (sorted[mid - 1] + sorted[mid]) / 2
213
+ : sorted[mid];
214
+ }
215
+ /**
216
+ * Rounds a number to the given number of decimal places.
217
+ *
218
+ * @remarks
219
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * roundTo(1.235, 2); // 1.24
224
+ * roundTo(1.5, 0); // 2
225
+ * roundTo(1234.5, -2); // 1200
226
+ * ```
227
+ */
228
+ export function roundTo(num, decimals) {
229
+ const factor = Math.pow(10, decimals);
230
+ return Math.round((num || 0) * factor) / factor;
231
+ }
232
+ /**
233
+ * Gives a random number in the given range. The first parameter is inclusive
234
+ * and the second one is exclusive. Therefore, it will work with lists out of
235
+ * the box.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * random(0, 10); // 0 -> 9
240
+ * random(3, 7); // 3 -> 6
241
+ * ```
242
+ */
243
+ export function random(start, end) {
244
+ return Math.floor(Math.random() * (end - start)) + start;
245
+ }
246
+ /**
247
+ * Returns true if the given value is within the given range (inclusive on both ends).
248
+ *
249
+ * @example
250
+ * ```ts
251
+ * inRange(5, 0, 10); // true
252
+ * inRange(0, 0, 10); // true
253
+ * inRange(10, 0, 10); // true
254
+ * inRange(11, 0, 10); // false
255
+ * ```
256
+ */
257
+ export function inRange(value, min, max) {
258
+ return value >= min && value <= max;
259
+ }
@@ -0,0 +1,90 @@
1
+ import type { None } from "./utility_types.js";
2
+ /**
3
+ * Returns a strongly-typed array of an object's own enumerable keys.
4
+ *
5
+ * @remarks
6
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty array.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * keys({ a: 1, b: 2 }); // ["a", "b"]
11
+ * keys({}); // []
12
+ * keys(null); // []
13
+ * ```
14
+ */
15
+ export declare function keys<T extends Record<string, unknown>>(obj: T | None): (keyof T)[];
16
+ /**
17
+ * Returns a strongly-typed array of an object's own enumerable `[key, value]` pairs.
18
+ *
19
+ * @remarks
20
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty array.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * entries({ a: 1, b: 2 }); // [["a", 1], ["b", 2]]
25
+ * entries({}); // []
26
+ * entries(null); // []
27
+ * ```
28
+ */
29
+ export declare function entries<T extends Record<string, unknown>>(obj: T | None): [keyof T, T[keyof T]][];
30
+ /**
31
+ * Returns a new object containing only the specified keys. Keys that are not
32
+ * present on the source object are silently ignored.
33
+ *
34
+ * @remarks
35
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * pick({ a: 1, b: 2, c: 3 }, ["a", "c"]); // { a: 1, c: 3 }
40
+ * pick({ a: 1, b: 2 }, []); // {}
41
+ * pick(null, ["a"]); // {}
42
+ * ```
43
+ */
44
+ export declare function pick<T extends Record<string, unknown>, K extends keyof T>(obj: T | None, keys: K[]): Pick<T, K>;
45
+ /**
46
+ * Returns a new object with the specified keys excluded.
47
+ *
48
+ * @remarks
49
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * omit({ a: 1, b: 2, c: 3 }, ["a"]); // { b: 2, c: 3 }
54
+ * omit({ a: 1, b: 2 }, []); // { a: 1, b: 2 }
55
+ * omit(null, ["a"]); // {}
56
+ * ```
57
+ */
58
+ export declare function omit<T extends Record<string, unknown>, K extends keyof T>(obj: T | None, keys: K[]): Omit<T, K>;
59
+ /**
60
+ * Returns a new object with the same keys, where each value is transformed by `fn`.
61
+ *
62
+ * @remarks
63
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * mapValues({ a: 1, b: 2 }, (v) => v * 2); // { a: 2, b: 4 }
68
+ * mapValues({ a: 1, b: 2 }, (v, k) => `${k}=${v}`); // { a: "a=1", b: "b=2" }
69
+ * mapValues(null, (v) => v); // {}
70
+ * ```
71
+ */
72
+ export declare function mapValues<T extends Record<string, unknown>, V>(obj: T | None, fn: (value: T[keyof T], key: keyof T) => V): {
73
+ [K in keyof T]: V;
74
+ };
75
+ /**
76
+ * Returns a new object with each key transformed by `fn`. Values are unchanged.
77
+ * If `fn` produces duplicate keys, later entries overwrite earlier ones.
78
+ *
79
+ * @remarks
80
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * mapKeys({ a: 1, b: 2 }, (k) => k.toUpperCase()); // { A: 1, B: 2 }
85
+ * mapKeys({ first: "Bob", last: "Lee" }, (k) => `user_${k}`); // { user_first: "Bob", user_last: "Lee" }
86
+ * mapKeys(null, (k) => k); // {}
87
+ * ```
88
+ */
89
+ export declare function mapKeys<T extends Record<string, unknown>>(obj: T | None, fn: (key: keyof T, value: T[keyof T]) => string): Record<string, T[keyof T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E,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,GACzC;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC;CAAE,CAOvB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACvD,GAAG,EAAE,CAAC,GAAG,IAAI,EACb,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,MAAM,GAC9C,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAO5B"}
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Returns a strongly-typed array of an object's own enumerable keys.
3
+ *
4
+ * @remarks
5
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty array.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * keys({ a: 1, b: 2 }); // ["a", "b"]
10
+ * keys({}); // []
11
+ * keys(null); // []
12
+ * ```
13
+ */
14
+ export function keys(obj) {
15
+ if (obj == null)
16
+ return [];
17
+ return Object.keys(obj);
18
+ }
19
+ /**
20
+ * Returns a strongly-typed array of an object's own enumerable `[key, value]` pairs.
21
+ *
22
+ * @remarks
23
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty array.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * entries({ a: 1, b: 2 }); // [["a", 1], ["b", 2]]
28
+ * entries({}); // []
29
+ * entries(null); // []
30
+ * ```
31
+ */
32
+ export function entries(obj) {
33
+ if (obj == null)
34
+ return [];
35
+ return Object.entries(obj);
36
+ }
37
+ /**
38
+ * Returns a new object containing only the specified keys. Keys that are not
39
+ * present on the source object are silently ignored.
40
+ *
41
+ * @remarks
42
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * pick({ a: 1, b: 2, c: 3 }, ["a", "c"]); // { a: 1, c: 3 }
47
+ * pick({ a: 1, b: 2 }, []); // {}
48
+ * pick(null, ["a"]); // {}
49
+ * ```
50
+ */
51
+ export function pick(obj, keys) {
52
+ if (obj == null)
53
+ return {};
54
+ const result = {};
55
+ for (const key of keys) {
56
+ if (key in obj)
57
+ result[key] = obj[key];
58
+ }
59
+ return result;
60
+ }
61
+ /**
62
+ * Returns a new object with the specified keys excluded.
63
+ *
64
+ * @remarks
65
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * omit({ a: 1, b: 2, c: 3 }, ["a"]); // { b: 2, c: 3 }
70
+ * omit({ a: 1, b: 2 }, []); // { a: 1, b: 2 }
71
+ * omit(null, ["a"]); // {}
72
+ * ```
73
+ */
74
+ export function omit(obj, keys) {
75
+ if (obj == null)
76
+ return {};
77
+ const excluded = new Set(keys);
78
+ const result = {};
79
+ for (const key of Object.keys(obj)) {
80
+ if (!excluded.has(key))
81
+ result[key] = obj[key];
82
+ }
83
+ return result;
84
+ }
85
+ /**
86
+ * Returns a new object with the same keys, where each value is transformed by `fn`.
87
+ *
88
+ * @remarks
89
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * mapValues({ a: 1, b: 2 }, (v) => v * 2); // { a: 2, b: 4 }
94
+ * mapValues({ a: 1, b: 2 }, (v, k) => `${k}=${v}`); // { a: "a=1", b: "b=2" }
95
+ * mapValues(null, (v) => v); // {}
96
+ * ```
97
+ */
98
+ export function mapValues(obj, fn) {
99
+ if (obj == null)
100
+ return {};
101
+ const result = {};
102
+ for (const key of Object.keys(obj)) {
103
+ result[key] = fn(obj[key], key);
104
+ }
105
+ return result;
106
+ }
107
+ /**
108
+ * Returns a new object with each key transformed by `fn`. Values are unchanged.
109
+ * If `fn` produces duplicate keys, later entries overwrite earlier ones.
110
+ *
111
+ * @remarks
112
+ * This is `None` safe. If you pass `null | undefined` then this will return an empty object.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * mapKeys({ a: 1, b: 2 }, (k) => k.toUpperCase()); // { A: 1, B: 2 }
117
+ * mapKeys({ first: "Bob", last: "Lee" }, (k) => `user_${k}`); // { user_first: "Bob", user_last: "Lee" }
118
+ * mapKeys(null, (k) => k); // {}
119
+ * ```
120
+ */
121
+ export function mapKeys(obj, fn) {
122
+ if (obj == null)
123
+ return {};
124
+ const result = {};
125
+ for (const key of Object.keys(obj)) {
126
+ result[fn(key, obj[key])] = obj[key];
127
+ }
128
+ return result;
129
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"objects_test.d.ts","sourceRoot":"","sources":["../../src/src/objects_test.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,CAAC"}
@@ -163,4 +163,66 @@ export declare function keepAlphanumeric(text: string): string;
163
163
  * ```
164
164
  */
165
165
  export declare function keepNumeric(text: string): string;
166
+ /**
167
+ * Converts the string to `camelCase` by removing punctuation, trimming extra
168
+ * spaces, and lowercasing the first word with subsequent words capitalized.
169
+ *
170
+ * @remarks
171
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * camel(null); // ""
176
+ * camel("Hello World"); // "helloWorld"
177
+ * camel("user_profile_page"); // "userProfilePage"
178
+ * camel("HELLO-WORLD"); // "helloWorld"
179
+ * ```
180
+ */
181
+ export declare function camel(text: string): string;
182
+ /**
183
+ * Converts the string to `PascalCase` by removing punctuation, trimming extra
184
+ * spaces, and capitalizing every word.
185
+ *
186
+ * @remarks
187
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * pascal(null); // ""
192
+ * pascal("hello world"); // "HelloWorld"
193
+ * pascal("user_profile_page"); // "UserProfilePage"
194
+ * ```
195
+ */
196
+ export declare function pascal(text: string): string;
197
+ /**
198
+ * Truncates a string to the given length. If truncation occurs, the suffix is
199
+ * appended and counted toward the total length.
200
+ *
201
+ * @remarks
202
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
203
+ *
204
+ * @example
205
+ * ```ts
206
+ * truncate(null, 10); // ""
207
+ * truncate("Hello World", 5); // "He..."
208
+ * truncate("Hello World", 20); // "Hello World"
209
+ * truncate("Hello World", 8, "…"); // "Hello W…"
210
+ * ```
211
+ */
212
+ export declare function truncate(text: string, length: number, suffix?: string): string;
213
+ /**
214
+ * Converts a string to a URL-safe slug. Diacritics are stripped, punctuation is
215
+ * removed, and whitespace is replaced with hyphens.
216
+ *
217
+ * @remarks
218
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * slugify(null); // ""
223
+ * slugify("Hello World!"); // "hello-world"
224
+ * slugify("Café à la Carte"); // "cafe-a-la-carte"
225
+ * ```
226
+ */
227
+ export declare function slugify(text: string): string;
166
228
  //# sourceMappingURL=strings.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../src/src/strings.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAElD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM1C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAErD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MA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AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAK1C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM3C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,SAAQ,GAAG,MAAM,CAI7E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE5C"}
@@ -189,6 +189,87 @@ export function keepAlphanumeric(text) {
189
189
  export function keepNumeric(text) {
190
190
  return (text || "").replace(/[^\d]/g, "");
191
191
  }
192
+ /**
193
+ * Converts the string to `camelCase` by removing punctuation, trimming extra
194
+ * spaces, and lowercasing the first word with subsequent words capitalized.
195
+ *
196
+ * @remarks
197
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
198
+ *
199
+ * @example
200
+ * ```ts
201
+ * camel(null); // ""
202
+ * camel("Hello World"); // "helloWorld"
203
+ * camel("user_profile_page"); // "userProfilePage"
204
+ * camel("HELLO-WORLD"); // "helloWorld"
205
+ * ```
206
+ */
207
+ export function camel(text) {
208
+ const parts = kebab(text).split("-").filter(Boolean);
209
+ if (parts.length === 0)
210
+ return "";
211
+ return parts[0] +
212
+ parts.slice(1).map((p) => upper(p[0]) + p.slice(1)).join("");
213
+ }
214
+ /**
215
+ * Converts the string to `PascalCase` by removing punctuation, trimming extra
216
+ * spaces, and capitalizing every word.
217
+ *
218
+ * @remarks
219
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * pascal(null); // ""
224
+ * pascal("hello world"); // "HelloWorld"
225
+ * pascal("user_profile_page"); // "UserProfilePage"
226
+ * ```
227
+ */
228
+ export function pascal(text) {
229
+ return kebab(text)
230
+ .split("-")
231
+ .filter(Boolean)
232
+ .map((p) => upper(p[0]) + p.slice(1))
233
+ .join("");
234
+ }
235
+ /**
236
+ * Truncates a string to the given length. If truncation occurs, the suffix is
237
+ * appended and counted toward the total length.
238
+ *
239
+ * @remarks
240
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
241
+ *
242
+ * @example
243
+ * ```ts
244
+ * truncate(null, 10); // ""
245
+ * truncate("Hello World", 5); // "He..."
246
+ * truncate("Hello World", 20); // "Hello World"
247
+ * truncate("Hello World", 8, "…"); // "Hello W…"
248
+ * ```
249
+ */
250
+ export function truncate(text, length, suffix = "...") {
251
+ const t = text || "";
252
+ if (t.length <= length)
253
+ return t;
254
+ return t.slice(0, Math.max(0, length - suffix.length)) + suffix;
255
+ }
256
+ /**
257
+ * Converts a string to a URL-safe slug. Diacritics are stripped, punctuation is
258
+ * removed, and whitespace is replaced with hyphens.
259
+ *
260
+ * @remarks
261
+ * This is `None` safe. If you pass `null | undefined` then this will return an Empty String.
262
+ *
263
+ * @example
264
+ * ```ts
265
+ * slugify(null); // ""
266
+ * slugify("Hello World!"); // "hello-world"
267
+ * slugify("Café à la Carte"); // "cafe-a-la-carte"
268
+ * ```
269
+ */
270
+ export function slugify(text) {
271
+ return kebab(text);
272
+ }
192
273
  function removePunctuation(text) {
193
274
  return text
194
275
  .normalize("NFKD")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytebury/toolkit",
3
- "version": "1.8.0",
3
+ "version": "2.0.0",
4
4
  "description": "TypeScript utility library to help energize your projects with useful functions for any size project. Save yourself some time and focus on shipping features.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,12 +13,41 @@
13
13
  ".": {
14
14
  "import": "./esm/mod.js",
15
15
  "require": "./script/mod.js"
16
+ },
17
+ "./core": {
18
+ "import": "./esm/src/core.js",
19
+ "require": "./script/src/core.js"
20
+ },
21
+ "./dates": {
22
+ "import": "./esm/src/dates.js",
23
+ "require": "./script/src/dates.js"
24
+ },
25
+ "./duration": {
26
+ "import": "./esm/src/duration.js",
27
+ "require": "./script/src/duration.js"
28
+ },
29
+ "./numbers": {
30
+ "import": "./esm/src/numbers.js",
31
+ "require": "./script/src/numbers.js"
32
+ },
33
+ "./objects": {
34
+ "import": "./esm/src/objects.js",
35
+ "require": "./script/src/objects.js"
36
+ },
37
+ "./strings": {
38
+ "import": "./esm/src/strings.js",
39
+ "require": "./script/src/strings.js"
40
+ },
41
+ "./utility-types": {
42
+ "import": "./esm/src/utility_types.js",
43
+ "require": "./script/src/utility_types.js"
16
44
  }
17
45
  },
18
46
  "scripts": {
19
47
  "test": "node test_runner.js"
20
48
  },
21
49
  "private": false,
50
+ "sideEffects": false,
22
51
  "devDependencies": {
23
52
  "@types/node": "^20.9.0",
24
53
  "picocolors": "^1.0.0",
package/script/mod.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./src/core.js";
2
2
  export * from "./src/dates.js";
3
3
  export * from "./src/duration.js";
4
4
  export * from "./src/numbers.js";
5
+ export * from "./src/objects.js";
5
6
  export * from "./src/strings.js";
6
7
  export * from "./src/utility_types.js";
7
8
  //# sourceMappingURL=mod.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC"}
1
+ {"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../src/mod.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,wBAAwB,CAAC"}
package/script/mod.js CHANGED
@@ -18,5 +18,6 @@ __exportStar(require("./src/core.js"), exports);
18
18
  __exportStar(require("./src/dates.js"), exports);
19
19
  __exportStar(require("./src/duration.js"), exports);
20
20
  __exportStar(require("./src/numbers.js"), exports);
21
+ __exportStar(require("./src/objects.js"), exports);
21
22
  __exportStar(require("./src/strings.js"), exports);
22
23
  __exportStar(require("./src/utility_types.js"), exports);