shelving 1.137.0 → 1.139.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 (85) hide show
  1. package/db/Provider.js +3 -3
  2. package/error/BaseError.d.ts +3 -1
  3. package/error/index.d.ts +0 -1
  4. package/error/index.js +0 -1
  5. package/markup/rule/link.js +3 -2
  6. package/package.json +1 -1
  7. package/schema/AllowSchema.js +4 -4
  8. package/schema/DateSchema.js +2 -1
  9. package/schema/EntitySchema.js +4 -4
  10. package/schema/NumberSchema.js +3 -2
  11. package/store/ArrayStore.js +5 -5
  12. package/util/array.d.ts +67 -66
  13. package/util/array.js +90 -30
  14. package/util/async.js +4 -4
  15. package/util/base64.d.ts +13 -8
  16. package/util/base64.js +82 -12
  17. package/util/boolean.d.ts +6 -5
  18. package/util/boolean.js +11 -11
  19. package/util/buffer.d.ts +5 -6
  20. package/util/buffer.js +5 -17
  21. package/util/bytes.d.ts +15 -0
  22. package/util/bytes.js +28 -0
  23. package/util/class.js +2 -2
  24. package/util/color.d.ts +4 -3
  25. package/util/color.js +5 -6
  26. package/util/crypto.d.ts +21 -0
  27. package/util/crypto.js +80 -0
  28. package/util/data.d.ts +6 -13
  29. package/util/data.js +8 -12
  30. package/util/date.d.ts +35 -27
  31. package/util/date.js +90 -82
  32. package/util/dictionary.d.ts +13 -7
  33. package/util/dictionary.js +12 -12
  34. package/util/entity.d.ts +8 -7
  35. package/util/entity.js +8 -8
  36. package/util/equal.d.ts +3 -2
  37. package/util/equal.js +5 -5
  38. package/util/file.d.ts +2 -1
  39. package/util/file.js +3 -3
  40. package/util/format.d.ts +51 -0
  41. package/util/format.js +113 -0
  42. package/util/function.js +2 -2
  43. package/util/index.d.ts +3 -0
  44. package/util/index.js +3 -0
  45. package/util/iterate.d.ts +0 -15
  46. package/util/iterate.js +0 -55
  47. package/util/jwt.d.ts +33 -0
  48. package/util/jwt.js +110 -0
  49. package/util/link.d.ts +26 -10
  50. package/util/link.js +34 -21
  51. package/util/map.d.ts +8 -7
  52. package/util/map.js +17 -18
  53. package/util/null.d.ts +7 -6
  54. package/util/null.js +13 -13
  55. package/util/number.d.ts +36 -42
  56. package/util/number.js +60 -82
  57. package/util/object.d.ts +0 -7
  58. package/util/object.js +4 -24
  59. package/util/optional.d.ts +2 -1
  60. package/util/optional.js +2 -2
  61. package/util/path.d.ts +7 -6
  62. package/util/path.js +10 -10
  63. package/util/query.js +3 -3
  64. package/util/random.js +2 -2
  65. package/util/regexp.js +2 -2
  66. package/util/set.d.ts +7 -6
  67. package/util/set.js +13 -13
  68. package/util/string.d.ts +26 -37
  69. package/util/string.js +40 -69
  70. package/util/template.d.ts +5 -4
  71. package/util/template.js +14 -13
  72. package/util/time.d.ts +7 -8
  73. package/util/time.js +14 -21
  74. package/util/transform.d.ts +3 -2
  75. package/util/transform.js +0 -1
  76. package/util/undefined.d.ts +4 -3
  77. package/util/undefined.js +8 -6
  78. package/util/units.d.ts +1 -2
  79. package/util/units.js +1 -1
  80. package/util/update.js +3 -3
  81. package/util/url.d.ts +6 -7
  82. package/util/url.js +7 -17
  83. package/util/validate.d.ts +2 -2
  84. package/error/AssertionError.d.ts +0 -8
  85. package/error/AssertionError.js +0 -12
package/db/Provider.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { RequiredError } from "../error/RequiredError.js";
2
2
  import { countArray } from "../util/array.js";
3
- import { getFirstItem } from "../util/iterate.js";
3
+ import { getFirst } from "../util/array.js";
4
4
  /** Provider with a fully synchronous interface */
5
5
  export class Provider {
6
6
  requireItem(collection, id) {
@@ -18,7 +18,7 @@ export class Provider {
18
18
  return countArray(this.getQuery(collection, query));
19
19
  }
20
20
  getFirst(collection, query) {
21
- return getFirstItem(this.getQuery(collection, { ...query, $limit: 1 }));
21
+ return getFirst(this.getQuery(collection, { ...query, $limit: 1 }));
22
22
  }
23
23
  requireFirst(collection, query) {
24
24
  const first = this.getFirst(collection, query);
@@ -49,7 +49,7 @@ export class AsyncProvider {
49
49
  return countArray(await this.getQuery(collection, query));
50
50
  }
51
51
  async getFirst(collection, query) {
52
- return getFirstItem(await this.getQuery(collection, { ...query, $limit: 1 }));
52
+ return getFirst(await this.getQuery(collection, { ...query, $limit: 1 }));
53
53
  }
54
54
  async requireFirst(collection, query) {
55
55
  const first = await this.getFirst(collection, query);
@@ -1,5 +1,7 @@
1
1
  import type { AnyConstructor } from "../util/class.js";
2
2
  import type { AnyFunction } from "../util/function.js";
3
+ /** Any calling function or constructor that can appear in a stack tracer. */
4
+ export type AnyCaller = AnyFunction | AnyConstructor;
3
5
  /** Options for `BaseError` that provide additional helpful error functionality. */
4
6
  export interface BaseErrorOptions extends ErrorOptions {
5
7
  /**
@@ -9,7 +11,7 @@ export interface BaseErrorOptions extends ErrorOptions {
9
11
  */
10
12
  [key: string]: unknown;
11
13
  /** Modify the stack to trim off lines after a certain calling function. */
12
- caller?: AnyFunction | AnyConstructor | undefined;
14
+ caller?: AnyCaller | undefined;
13
15
  }
14
16
  /** An error that provides additional helpful functionality. */
15
17
  export declare abstract class BaseError extends Error {
package/error/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./BaseError.js";
2
- export * from "./AssertionError.js";
3
2
  export * from "./NetworkError.js";
4
3
  export * from "./RequestError.js";
5
4
  export * from "./RequiredError.js";
package/error/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./BaseError.js";
2
- export * from "./AssertionError.js";
3
2
  export * from "./NetworkError.js";
4
3
  export * from "./RequestError.js";
5
4
  export * from "./RequiredError.js";
@@ -1,13 +1,14 @@
1
+ import { formatURL } from "../../util/format.js";
1
2
  import { getLink } from "../../util/link.js";
2
3
  import { getRegExp } from "../../util/regexp.js";
3
- import { formatURL, getOptionalURL } from "../../util/url.js";
4
+ import { getURL } from "../../util/url.js";
4
5
  import { renderMarkup } from "../render.js";
5
6
  import { REACT_ELEMENT_TYPE } from "../util/internal.js";
6
7
  import { getMarkupRule } from "../util/rule.js";
7
8
  /** Render `<a href="">` if the link is a valid one, or `<a>` (with no `href`) if it isn't. */
8
9
  function renderLinkMarkupRule({ groups: { title, href: unsafeHref } }, options, key) {
9
10
  const { base, schemes, hosts, rel } = options;
10
- const url = getOptionalURL(unsafeHref, base);
11
+ const url = getURL(unsafeHref, base);
11
12
  const href = getLink(url, base, schemes, hosts);
12
13
  const children = title ? renderMarkup(title, options, "link") : url ? formatURL(url) : "";
13
14
  return {
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "state-management",
12
12
  "query-builder"
13
13
  ],
14
- "version": "1.137.0",
14
+ "version": "1.139.0",
15
15
  "repository": "https://github.com/dhoulb/shelving",
16
16
  "author": "Dave Houlbrooke <dave@shax.com>",
17
17
  "license": "0BSD",
@@ -1,14 +1,14 @@
1
1
  import { ValueFeedback } from "../feedback/Feedback.js";
2
- import { requireFirstItem } from "../util/iterate.js";
2
+ import { requireFirst } from "../util/array.js";
3
+ import { formatValue } from "../util/format.js";
3
4
  import { getMap, isMapItem } from "../util/map.js";
4
- import { getString } from "../util/string.js";
5
5
  import { Schema } from "./Schema.js";
6
6
  /** Define a valid value from an allowed set of values. */
7
7
  export class AllowSchema extends Schema {
8
8
  allow;
9
9
  constructor(options) {
10
10
  const allow = getMap(options.allow);
11
- const value = requireFirstItem(allow.keys());
11
+ const value = requireFirst(allow.keys());
12
12
  super({ value, ...options });
13
13
  this.allow = allow;
14
14
  }
@@ -28,7 +28,7 @@ export class AllowStringSchema extends AllowSchema {
28
28
  super({ allow: getMap(allow), ...options });
29
29
  }
30
30
  validator(unsafeValue = this.value) {
31
- return super.validate(getString(unsafeValue));
31
+ return super.validate(formatValue(unsafeValue));
32
32
  }
33
33
  }
34
34
  /** Valid value from an allowed set of values. */
@@ -1,5 +1,6 @@
1
1
  import { ValueFeedback } from "../feedback/Feedback.js";
2
- import { formatDate, getDate, requireYMD } from "../util/date.js";
2
+ import { getDate, requireYMD } from "../util/date.js";
3
+ import { formatDate } from "../util/format.js";
3
4
  import { OPTIONAL } from "./OptionalSchema.js";
4
5
  import { Schema } from "./Schema.js";
5
6
  /** Define a valid date in YMD format, e.g. `2005-09-12` */
@@ -1,6 +1,6 @@
1
1
  import { ValueFeedback } from "../feedback/Feedback.js";
2
- import { isArrayItem } from "../util/array.js";
3
- import { splitOptionalEntity } from "../util/entity.js";
2
+ import { isItem } from "../util/array.js";
3
+ import { getEntity } from "../util/entity.js";
4
4
  import { OPTIONAL } from "./OptionalSchema.js";
5
5
  import { StringSchema } from "./StringSchema.js";
6
6
  /** Validate a file name matching one or more extensions. */
@@ -12,10 +12,10 @@ export class EntitySchema extends StringSchema {
12
12
  }
13
13
  validate(unsafeValue = this.value) {
14
14
  const entity = super.validate(unsafeValue);
15
- const [type] = splitOptionalEntity(entity);
15
+ const [type] = getEntity(entity);
16
16
  if (!type)
17
17
  throw new ValueFeedback("Must be entity", unsafeValue);
18
- if (this.types && !isArrayItem(this.types, type))
18
+ if (this.types && !isItem(this.types, type))
19
19
  throw new ValueFeedback("Invalid entity type", type);
20
20
  return entity;
21
21
  }
@@ -1,5 +1,6 @@
1
1
  import { ValueFeedback } from "../feedback/Feedback.js";
2
- import { formatNumber, getOptionalNumber, roundStep } from "../util/number.js";
2
+ import { formatNumber } from "../util/format.js";
3
+ import { getNumber, roundStep } from "../util/number.js";
3
4
  import { OPTIONAL } from "./OptionalSchema.js";
4
5
  import { Schema } from "./Schema.js";
5
6
  /** Schema that defines a valid number. */
@@ -14,7 +15,7 @@ export class NumberSchema extends Schema {
14
15
  this.step = step;
15
16
  }
16
17
  validate(unsafeValue = this.value) {
17
- const optionalNumber = getOptionalNumber(unsafeValue);
18
+ const optionalNumber = getNumber(unsafeValue);
18
19
  if (typeof optionalNumber !== "number")
19
20
  throw new ValueFeedback("Must be number", unsafeValue);
20
21
  const roundedNumber = typeof this.step === "number" ? roundStep(optionalNumber, this.step) : optionalNumber;
@@ -1,5 +1,5 @@
1
1
  import { omitArrayItems, toggleArrayItems, withArrayItems } from "../util/array.js";
2
- import { getFirstItem, getLastItem, requireFirstItem, requireLastItem } from "../util/iterate.js";
2
+ import { getFirst, getLast, requireFirst, requireLast } from "../util/array.js";
3
3
  import { Store } from "./Store.js";
4
4
  /** Store an array. */
5
5
  export class ArrayStore extends Store {
@@ -8,19 +8,19 @@ export class ArrayStore extends Store {
8
8
  }
9
9
  /** Get the first item in this store or `null` if this query has no items. */
10
10
  get optionalFirst() {
11
- return getFirstItem(this.value);
11
+ return getFirst(this.value);
12
12
  }
13
13
  /** Get the last item in this store or `null` if this query has no items. */
14
14
  get optionalLast() {
15
- return getLastItem(this.value);
15
+ return getLast(this.value);
16
16
  }
17
17
  /** Get the first item in this store. */
18
18
  get first() {
19
- return requireFirstItem(this.value);
19
+ return requireFirst(this.value);
20
20
  }
21
21
  /** Get the last item in this store. */
22
22
  get last() {
23
- return requireLastItem(this.value);
23
+ return requireLast(this.value);
24
24
  }
25
25
  /** Does the document have at least one result. */
26
26
  get exists() {
package/util/array.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { AnyCaller } from "../error/BaseError.js";
1
2
  /**
2
3
  * Mutable array: an array that can be changed.
3
4
  * - Consistency with `MutableObject<T>` and `ImmutableArray<T>`
@@ -12,16 +13,57 @@ export type ImmutableArray<T = unknown> = readonly T[];
12
13
  export type ArrayItem<T extends ImmutableArray> = T[number];
13
14
  /** Things that can be converted to arrays. */
14
15
  export type PossibleArray<T> = ImmutableArray<T> | Iterable<T>;
15
- /** Is an unknown value an array? */
16
- export declare function isArray(value: unknown): value is ImmutableArray;
17
- /** Assert that an unknown value is an array. */
18
- export declare function assertArray<T>(arr: unknown): asserts arr is ImmutableArray<T>;
19
- /** Is an unknown value an item in a specified array? */
20
- export declare function isArrayItem<T>(arr: ImmutableArray<T>, item: unknown): item is T;
21
- /** Assert that an unknown value is an item in a specified array. */
22
- export declare function assertArrayItem<T>(arr: ImmutableArray<T>, item: unknown): asserts item is T;
23
- /** Convert an iterable to an array (if its not already an array). */
16
+ /** Is an unknown value an array (optionally with specified min/max length). */
17
+ export declare function isArray<T>(arr: MutableArray<T>, min: 1, max: 1): arr is [T];
18
+ export declare function isArray<T>(arr: MutableArray<T>, min: 2, max: 2): arr is [T, T];
19
+ export declare function isArray<T>(arr: MutableArray<T>, min: 3, max: 3): arr is [T, T, T];
20
+ export declare function isArray<T>(arr: MutableArray<T>, min?: 1, max?: number): arr is [T, ...T[]];
21
+ export declare function isArray<T>(arr: MutableArray<T>, min: 2, max?: number): arr is [T, T, ...T[]];
22
+ export declare function isArray<T>(arr: MutableArray<T>, min: 3, max?: number): arr is [T, T, T, ...T[]];
23
+ export declare function isArray<T>(arr: MutableArray<T>, min?: number, max?: number): arr is MutableArray<T>;
24
+ export declare function isArray<T>(arr: ImmutableArray<T>, min: 1, max: 1): arr is readonly [T];
25
+ export declare function isArray<T>(arr: ImmutableArray<T>, min: 2, max: 2): arr is readonly [T, T];
26
+ export declare function isArray<T>(arr: ImmutableArray<T>, min: 3, max: 3): arr is readonly [T, T, T];
27
+ export declare function isArray<T>(arr: ImmutableArray<T>, min?: 1, max?: number): arr is readonly [T, ...T[]];
28
+ export declare function isArray<T>(arr: ImmutableArray<T>, min: 2, max?: number): arr is readonly [T, T, ...T[]];
29
+ export declare function isArray<T>(arr: ImmutableArray<T>, min: 3, max?: number): arr is readonly [T, T, T, ...T[]];
30
+ export declare function isArray<T>(value: unknown, min?: number, max?: number): value is ImmutableArray;
31
+ /** Assert that an unknown value is an array (optionally with specified min/max length). */
32
+ export declare function assertArray<T>(arr: MutableArray<T>, min: 1, max: 1, caller?: AnyCaller): asserts arr is [T];
33
+ export declare function assertArray<T>(arr: MutableArray<T>, min: 2, max: 2, caller?: AnyCaller): asserts arr is [T, T];
34
+ export declare function assertArray<T>(arr: MutableArray<T>, min: 3, max: 3, caller?: AnyCaller): asserts arr is [T, T, T];
35
+ export declare function assertArray<T>(arr: MutableArray<T>, min?: 1, max?: number, caller?: AnyCaller): asserts arr is [T, ...T[]];
36
+ export declare function assertArray<T>(arr: MutableArray<T>, min: 2, max?: number, caller?: AnyCaller): asserts arr is [T, T, ...T[]];
37
+ export declare function assertArray<T>(arr: MutableArray<T>, min: 3, max?: number, caller?: AnyCaller): asserts arr is [T, T, T, ...T[]];
38
+ export declare function assertArray<T>(arr: MutableArray<T>, min: number, max?: number, caller?: AnyCaller): asserts arr is MutableArray<T>;
39
+ export declare function assertArray<T>(arr: ImmutableArray<T>, min: 1, max: 1, caller?: AnyCaller): asserts arr is readonly [T];
40
+ export declare function assertArray<T>(arr: ImmutableArray<T>, min: 2, max: 2, caller?: AnyCaller): asserts arr is readonly [T, T];
41
+ export declare function assertArray<T>(arr: ImmutableArray<T>, min: 3, max: 3, caller?: AnyCaller): asserts arr is readonly [T, T, T];
42
+ export declare function assertArray<T>(arr: ImmutableArray<T>, min?: 1, max?: number, caller?: AnyCaller): asserts arr is readonly [T, ...T[]];
43
+ export declare function assertArray<T>(arr: ImmutableArray<T>, min: 2, max?: number, caller?: AnyCaller): asserts arr is readonly [T, T, ...T[]];
44
+ export declare function assertArray<T>(arr: ImmutableArray<T>, min: 3, max?: number, caller?: AnyCaller): asserts arr is readonly [T, T, T, ...T[]];
45
+ export declare function assertArray<T>(value: unknown, min?: number, max?: number, caller?: AnyCaller): asserts value is ImmutableArray<T>;
46
+ /** Convert a possible array to an array. */
24
47
  export declare function getArray<T>(list: PossibleArray<T>): ImmutableArray<T>;
48
+ /** Convert a possible array to an array (optionally with specified min/max length), or throw `RequiredError` if conversion fails. */
49
+ export declare function requireArray<T>(arr: MutableArray<T>, min: 1, max: 1, caller?: AnyCaller): [T];
50
+ export declare function requireArray<T>(arr: MutableArray<T>, min: 2, max: 2, caller?: AnyCaller): [T, T];
51
+ export declare function requireArray<T>(arr: MutableArray<T>, min: 3, max: 3, caller?: AnyCaller): [T, T, T];
52
+ export declare function requireArray<T>(arr: MutableArray<T>, min?: 1, max?: number, caller?: AnyCaller): [T, ...T[]];
53
+ export declare function requireArray<T>(arr: MutableArray<T>, min: 2, max?: number, caller?: AnyCaller): [T, T, ...T[]];
54
+ export declare function requireArray<T>(arr: MutableArray<T>, min: 3, max?: number, caller?: AnyCaller): [T, T, T, ...T[]];
55
+ export declare function requireArray<T>(arr: MutableArray<T>, min?: number, max?: number, caller?: AnyCaller): MutableArray<T>;
56
+ export declare function requireArray<T>(arr: ImmutableArray<T>, min: 1, max: 1, caller?: AnyCaller): readonly [T];
57
+ export declare function requireArray<T>(arr: ImmutableArray<T>, min: 2, max: 2, caller?: AnyCaller): readonly [T, T];
58
+ export declare function requireArray<T>(arr: ImmutableArray<T>, min: 3, max: 3, caller?: AnyCaller): readonly [T, T, T];
59
+ export declare function requireArray<T>(arr: ImmutableArray<T>, min?: 1, max?: number, caller?: AnyCaller): readonly [T, ...T[]];
60
+ export declare function requireArray<T>(arr: ImmutableArray<T>, min: 2, max?: number, caller?: AnyCaller): readonly [T, T, ...T[]];
61
+ export declare function requireArray<T>(arr: ImmutableArray<T>, min: 3, max?: number, caller?: AnyCaller): readonly [T, T, T, ...T[]];
62
+ export declare function requireArray<T>(list: PossibleArray<T>, min?: number, max?: number, caller?: AnyCaller): ImmutableArray<T>;
63
+ /** Is an unknown value an item in a specified array or iterable? */
64
+ export declare function isItem<T>(list: PossibleArray<T>, item: unknown): item is T;
65
+ /** Assert that an unknown value is an item in a specified array. */
66
+ export declare function assertItem<T>(arr: PossibleArray<T>, item: unknown, caller?: AnyCaller): asserts item is T;
25
67
  /** Add multiple items to an array (immutably) and return a new array with those items (or the same array if no changes were made). */
26
68
  export declare function withArrayItems<T>(list: PossibleArray<T>, ...add: T[]): ImmutableArray<T>;
27
69
  /** Pick multiple items from an array (immutably) and return a new array without those items (or the same array if no changes were made). */
@@ -53,60 +95,19 @@ export declare function getUniqueArray<T>(list: PossibleArray<T>): ImmutableArra
53
95
  export declare function limitArray<T>(list: PossibleArray<T>, limit: number): ImmutableArray<T>;
54
96
  /** Count the items in an array. */
55
97
  export declare function countArray<T>(arr: ImmutableArray<T>): number;
56
- /** Does an array have the specified minimum length. */
57
- export declare function isArrayLength<T>(arr: MutableArray<T>, min: 1, max: 1): arr is [T];
58
- export declare function isArrayLength<T>(arr: MutableArray<T>, min: 2, max: 2): arr is [T, T];
59
- export declare function isArrayLength<T>(arr: MutableArray<T>, min: 3, max: 3): arr is [T, T, T];
60
- export declare function isArrayLength<T>(arr: MutableArray<T>, min: 4, max: 4): arr is [T, T, T, T];
61
- export declare function isArrayLength<T>(arr: MutableArray<T>, min?: 1, max?: number): arr is [T, ...T[]];
62
- export declare function isArrayLength<T>(arr: MutableArray<T>, min: 2, max?: number): arr is [T, T, ...T[]];
63
- export declare function isArrayLength<T>(arr: MutableArray<T>, min: 3, max?: number): arr is [T, T, T, ...T[]];
64
- export declare function isArrayLength<T>(arr: MutableArray<T>, min: 4, max?: number): arr is [T, T, T, T, ...T[]];
65
- export declare function isArrayLength<T>(arr: MutableArray<T>, min?: number, max?: number): arr is MutableArray<T>;
66
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 1, max: 1): arr is readonly [T];
67
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 2, max: 2): arr is readonly [T, T];
68
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 3, max: 3): arr is readonly [T, T, T];
69
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 4, max: 4): arr is readonly [T, T, T, T];
70
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min?: 1, max?: number): arr is readonly [T, ...T[]];
71
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 2, max?: number): arr is readonly [T, T, ...T[]];
72
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 3, max?: number): arr is readonly [T, T, T, ...T[]];
73
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min: 4, max?: number): arr is readonly [T, T, T, T, ...T[]];
74
- export declare function isArrayLength<T>(arr: ImmutableArray<T>, min?: number, max?: number): boolean;
75
- /** Assert that an array has a specific length (or length is in a specific range). */
76
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: 1, max: 1): asserts arr is [T];
77
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: 2, max: 2): asserts arr is [T, T];
78
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: 3, max: 3): asserts arr is [T, T, T];
79
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: 4, max: 4): asserts arr is [T, T, T, T];
80
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min?: 1, max?: number): asserts arr is [T, ...T[]];
81
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: 2, max?: number): asserts arr is [T, T, ...T[]];
82
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: 3, max?: number): asserts arr is [T, T, T, ...T[]];
83
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: 4, max?: number): asserts arr is [T, T, T, T, ...T[]];
84
- export declare function assertArrayLength<T>(arr: MutableArray<T>, min: number, max?: number): asserts arr is MutableArray<T>;
85
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 1, max: 1): asserts arr is readonly [T];
86
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 2, max: 2): asserts arr is readonly [T, T];
87
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 3, max: 3): asserts arr is readonly [T, T, T];
88
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 4, max: 4): asserts arr is readonly [T, T, T, T];
89
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min?: 1, max?: number): asserts arr is readonly [T, ...T[]];
90
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 2, max?: number): asserts arr is readonly [T, T, ...T[]];
91
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 3, max?: number): asserts arr is readonly [T, T, T, ...T[]];
92
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: 4, max?: number): asserts arr is readonly [T, T, T, T, ...T[]];
93
- export declare function assertArrayLength<T>(arr: ImmutableArray<T>, min: number, max?: number): asserts arr is ImmutableArray<T>;
94
- /** Require that an array if has the specified minimum length, or throw `RequiredError` if is not. */
95
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min: 1, max: 1): [T];
96
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min: 2, max: 2): [T, T];
97
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min: 3, max: 3): [T, T, T];
98
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min: 4, max: 4): [T, T, T, T];
99
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min?: 1, max?: number): [T, ...T[]];
100
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min: 2, max?: number): [T, T, ...T[]];
101
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min: 3, max?: number): [T, T, T, ...T[]];
102
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min: 4, max?: number): [T, T, T, T, ...T[]];
103
- export declare function requireArrayLength<T>(arr: MutableArray<T>, min?: number, max?: number): MutableArray<T>;
104
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min: 1, max: 1): readonly [T];
105
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min: 2, max: 2): readonly [T, T];
106
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min: 3, max: 3): readonly [T, T, T];
107
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min: 4, max: 4): readonly [T, T, T, T];
108
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min?: 1, max?: number): readonly [T, ...T[]];
109
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min: 2, max?: number): readonly [T, T, ...T[]];
110
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min: 3, max?: number): readonly [T, T, T, ...T[]];
111
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min: 4, max?: number): readonly [T, T, T, T, ...T[]];
112
- export declare function requireArrayLength<T>(arr: ImmutableArray<T>, min?: number, max?: number): ImmutableArray<T>;
98
+ /** Get the first item from an array or iterable, or `undefined` if it didn't exist. */
99
+ export declare function getFirst<T>(items: PossibleArray<T>): T | undefined;
100
+ /** Get the first item from an array or iterable. */
101
+ export declare function requireFirst<T>(items: PossibleArray<T>, caller?: AnyCaller): T;
102
+ /** Get the last item from an array or iterable, or `undefined` if it didn't exist. */
103
+ export declare function getLast<T>(items: PossibleArray<T>): T | undefined;
104
+ /** Get the last item from an array or iterable. */
105
+ export declare function requireLast<T>(items: PossibleArray<T>, caller?: AnyCaller): T;
106
+ /** Get the next item in an array or iterable. */
107
+ export declare function getNext<T>(items: PossibleArray<T>, item: T): T | undefined;
108
+ /** Get the next item from an array or iterable. */
109
+ export declare function requireNext<T>(items: PossibleArray<T>, item: T, caller?: AnyCaller): T;
110
+ /** Get the previous item in an array or iterable. */
111
+ export declare function getPrev<T>(items: PossibleArray<T>, value: T): T | undefined;
112
+ /** Get the previous item from an array or iterable. */
113
+ export declare function requirePrev<T>(items: PossibleArray<T>, item: T, caller?: AnyCaller): T;
package/util/array.js CHANGED
@@ -1,28 +1,37 @@
1
- import { AssertionError } from "../error/AssertionError.js";
2
1
  import { RequiredError } from "../error/RequiredError.js";
3
2
  import { omitItems, pickItems } from "./iterate.js";
4
- import { formatRange } from "./number.js";
5
- /** Is an unknown value an array? */
6
- export function isArray(value) {
7
- return Array.isArray(value);
3
+ export function isArray(value, min = 0, max = Number.POSITIVE_INFINITY) {
4
+ return Array.isArray(value) && value.length >= min && value.length <= max;
8
5
  }
9
- /** Assert that an unknown value is an array. */
10
- export function assertArray(arr) {
11
- if (!isArray(arr))
12
- throw new AssertionError("Must be array", { received: arr, caller: assertArray });
6
+ export function assertArray(value, min, max, caller = assertArray) {
7
+ if (!isArray(value, min, max))
8
+ throw new RequiredError(`Must be array${min !== undefined || max !== undefined ? ` with ${min ?? 0} to ${max ?? "∞"} items` : ""}`, {
9
+ received: value,
10
+ caller,
11
+ });
13
12
  }
14
- /** Is an unknown value an item in a specified array? */
15
- export function isArrayItem(arr, item) {
16
- return arr.includes(item);
13
+ /** Convert a possible array to an array. */
14
+ export function getArray(list) {
15
+ return Array.isArray(list) ? list : Array.from(list);
17
16
  }
18
- /** Assert that an unknown value is an item in a specified array. */
19
- export function assertArrayItem(arr, item) {
20
- if (!isArrayItem(arr, item))
21
- throw new AssertionError("Item must exist in array", { item, array: arr, caller: assertArrayItem });
17
+ export function requireArray(list, min, max, caller = requireArray) {
18
+ const arr = getArray(list);
19
+ assertArray(arr, min, max, caller);
20
+ return arr;
22
21
  }
23
- /** Convert an iterable to an array (if its not already an array). */
24
- export function getArray(list) {
25
- return isArray(list) ? list : Array.from(list);
22
+ /** Is an unknown value an item in a specified array or iterable? */
23
+ export function isItem(list, item) {
24
+ if (isArray(list))
25
+ list.includes(item);
26
+ for (const i of list)
27
+ if (i === item)
28
+ return true;
29
+ return false;
30
+ }
31
+ /** Assert that an unknown value is an item in a specified array. */
32
+ export function assertItem(arr, item, caller = assertItem) {
33
+ if (!isItem(arr, item))
34
+ throw new RequiredError("Item must exist in array", { item, array: arr, caller });
26
35
  }
27
36
  /** Add multiple items to an array (immutably) and return a new array with those items (or the same array if no changes were made). */
28
37
  export function withArrayItems(list, ...add) {
@@ -55,7 +64,6 @@ export function shuffleArray(items) {
55
64
  const arr = Array.from(items);
56
65
  for (let i = arr.length - 1; i > 0; i--) {
57
66
  const j = Math.floor(Math.random() * (i + 1));
58
- // biome-ignore lint/style/noNonNullAssertion: We know these keys are set.
59
67
  [arr[i], arr[j]] = [arr[j], arr[i]];
60
68
  }
61
69
  return arr;
@@ -97,22 +105,74 @@ export function getUniqueArray(list) {
97
105
  }
98
106
  /** Apply a limit to an array. */
99
107
  export function limitArray(list, limit) {
100
- const arr = getArray(list);
108
+ const arr = requireArray(list, undefined, undefined, limitArray);
101
109
  return limit > arr.length ? arr : arr.slice(0, limit);
102
110
  }
103
111
  /** Count the items in an array. */
104
112
  export function countArray(arr) {
105
113
  return arr.length;
106
114
  }
107
- export function isArrayLength(arr, min = 1, max = Number.POSITIVE_INFINITY) {
108
- return arr.length >= min && arr.length <= max;
115
+ /** Get the first item from an array or iterable, or `undefined` if it didn't exist. */
116
+ export function getFirst(items) {
117
+ if (isArray(items))
118
+ return items[0];
119
+ for (const i of items)
120
+ return i;
121
+ }
122
+ /** Get the first item from an array or iterable. */
123
+ export function requireFirst(items, caller = requireFirst) {
124
+ const item = getFirst(items);
125
+ if (item === undefined)
126
+ throw new RequiredError("First item is required", { items: items, caller });
127
+ return item;
109
128
  }
110
- export function assertArrayLength(arr, min = 1, max = Number.POSITIVE_INFINITY) {
111
- if (!isArrayLength(arr, min, max))
112
- throw new AssertionError(`Must be array with length ${formatRange(min, max)}`, { received: arr, caller: assertArrayLength });
129
+ /** Get the last item from an array or iterable, or `undefined` if it didn't exist. */
130
+ export function getLast(items) {
131
+ if (isArray(items))
132
+ return items[items.length - 1];
133
+ let last;
134
+ for (const i of items) {
135
+ last = i;
136
+ }
137
+ return last;
113
138
  }
114
- export function requireArrayLength(arr, min = 1, max = Number.POSITIVE_INFINITY) {
115
- if (!isArrayLength(arr, min, max))
116
- throw new RequiredError(`Must be array with length ${formatRange(min, max)}`, { received: arr, caller: requireArrayLength });
117
- return arr;
139
+ /** Get the last item from an array or iterable. */
140
+ export function requireLast(items, caller = requireFirst) {
141
+ const item = getLast(items);
142
+ if (item === undefined)
143
+ throw new RequiredError("Last item is required", { items, caller });
144
+ return item;
145
+ }
146
+ /** Get the next item in an array or iterable. */
147
+ export function getNext(items, item) {
148
+ let found = false;
149
+ for (const i of items) {
150
+ if (found)
151
+ return i;
152
+ if (i === item)
153
+ found = true;
154
+ }
155
+ }
156
+ /** Get the next item from an array or iterable. */
157
+ export function requireNext(items, item, caller = requireNext) {
158
+ const next = getNext(items, item);
159
+ if (next === undefined)
160
+ throw new RequiredError("Next item is required", { item, items, caller });
161
+ return next;
162
+ }
163
+ /** Get the previous item in an array or iterable. */
164
+ export function getPrev(items, value) {
165
+ let last;
166
+ for (const i of items) {
167
+ if (i === value)
168
+ return last;
169
+ last = i;
170
+ }
171
+ }
172
+ /** Get the previous item from an array or iterable. */
173
+ export function requirePrev(items, item, caller = requirePrev) {
174
+ const prev = getPrev(items, item);
175
+ if (prev === undefined)
176
+ throw new RequiredError("Previous item is required", { item, items, caller });
177
+ return prev;
118
178
  }
package/util/async.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AssertionError } from "../error/AssertionError.js";
1
+ import { RequiredError } from "../error/RequiredError.js";
2
2
  /** Is a value an asynchronous value implementing a `then()` function. */
3
3
  export function isAsync(value) {
4
4
  return typeof value === "object" && value !== null && typeof value.then === "function";
@@ -20,17 +20,17 @@ export function throwAsync(value) {
20
20
  /** Assert an unknown value is synchronous (i.e. does not have a `.then()` method). */
21
21
  export function assertNotAsync(value) {
22
22
  if (isAsync(value))
23
- throw new AssertionError("Must be synchronous", { received: value, caller: assertNotAsync });
23
+ throw new RequiredError("Must be synchronous", { received: value, caller: assertNotAsync });
24
24
  }
25
25
  /** Assert an unknown value is asynchronous (i.e. has a `.then()` method). */
26
26
  export function assertAsync(value) {
27
27
  if (!isAsync(value))
28
- throw new AssertionError("Must be asynchronous", { received: value, caller: assertAsync });
28
+ throw new RequiredError("Must be asynchronous", { received: value, caller: assertAsync });
29
29
  }
30
30
  /** Assert that an unknown value is a `Promise` */
31
31
  export function assertPromise(value) {
32
32
  if (!(value instanceof Promise))
33
- throw new AssertionError("Must be promise", { received: value, caller: assertPromise });
33
+ throw new RequiredError("Must be promise", { received: value, caller: assertPromise });
34
34
  }
35
35
  /** Run any queued microtasks now. */
36
36
  export function runMicrotasks() {
package/util/base64.d.ts CHANGED
@@ -1,8 +1,13 @@
1
- /** Encode a string to Base64 (with no `=` padding on the end). */
2
- export declare function encodeBase64(str: string): string;
3
- /** Decode a string from Base64 (strips `=` padding on the end). */
4
- export declare function decodeBase64(b64: string): string;
5
- /** Encode a string to URL-safe Base64 */
6
- export declare function encodeBase64Url(str: string): string;
7
- /** Decode a string from URL-safe Base64. */
8
- export declare function decodeBase64Url(b64: string): string;
1
+ import { type PossibleBytes } from "./bytes.js";
2
+ /** Encode a string or binary data to Base64 string. */
3
+ export declare function encodeBase64(input: PossibleBytes, pad?: boolean): string;
4
+ /** Decode Base64 string to string (decodes Base64URL too). */
5
+ export declare function decodeBase64String(base64: string): string;
6
+ /** Decode URL-safe Base64 string to byte sequence (decodes Base64URL too). */
7
+ export declare function decodeBase64Bytes(base64: string): Uint8Array;
8
+ /** Encode a string or binary data to URL-safe Base64 */
9
+ export declare function encodeBase64URL(input: PossibleBytes, pad?: boolean): string;
10
+ /** Decode a string from URL-safe Base64 (decodes Base64 too). */
11
+ export declare function decodeBase64URLString(base64: string): string;
12
+ /** Decode URL-safe Base64 string to byte sequence (decodes Base64 too). */
13
+ export declare function decodeBase64URLBytes(base64: string): Uint8Array;