@bodil/core 0.5.7 → 0.5.8

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 (78) hide show
  1. package/dist/assert.js.map +1 -1
  2. package/dist/assert.test.js.map +1 -1
  3. package/dist/async/abortable-promise.d.ts +1 -1
  4. package/dist/async/abortable-promise.js +1 -1
  5. package/dist/async/abortable-promise.js.map +1 -1
  6. package/dist/async/abortable-promise.test.js +1 -1
  7. package/dist/async/abortable-promise.test.js.map +1 -1
  8. package/dist/async/emitter.test.js +1 -1
  9. package/dist/async/emitter.test.js.map +1 -1
  10. package/dist/collections/lfu-cache.js +1 -1
  11. package/dist/collections/lfu-cache.js.map +1 -1
  12. package/dist/collections/lru-cache.js +1 -1
  13. package/dist/collections/lru-cache.js.map +1 -1
  14. package/dist/decorators.test.js.map +1 -1
  15. package/dist/disposable.js.map +1 -1
  16. package/dist/disposable.test.js +1 -1
  17. package/dist/disposable.test.js.map +1 -1
  18. package/dist/index.d.ts +2 -1
  19. package/dist/index.js +2 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/logger.d.ts +19 -0
  22. package/dist/logger.js +26 -0
  23. package/dist/logger.js.map +1 -0
  24. package/dist/order.js.map +1 -1
  25. package/dist/order.test.js +1 -1
  26. package/dist/order.test.js.map +1 -1
  27. package/dist/time.test.js.map +1 -1
  28. package/package.json +29 -1
  29. package/src/assert.test.ts +1 -0
  30. package/src/assert.ts +9 -7
  31. package/src/async/abortable-promise.test.ts +2 -1
  32. package/src/async/abortable-promise.ts +17 -17
  33. package/src/async/emitter.test.ts +2 -1
  34. package/src/collections/lfu-cache.ts +1 -1
  35. package/src/collections/linked-list.ts +1 -1
  36. package/src/collections/lru-cache.ts +2 -2
  37. package/src/decorators.test.ts +1 -0
  38. package/src/decorators.ts +1 -1
  39. package/src/disposable.test.ts +4 -2
  40. package/src/disposable.ts +2 -1
  41. package/src/index.ts +2 -1
  42. package/src/logger.ts +38 -0
  43. package/src/order.test.ts +2 -1
  44. package/src/order.ts +1 -1
  45. package/src/time.test.ts +1 -0
  46. package/src/time.ts +4 -4
  47. package/dist/assert.d.ts +0 -93
  48. package/dist/assert.test.d.ts +0 -1
  49. package/dist/async/abortable-promise.test.d.ts +0 -1
  50. package/dist/async/emitter.d.ts +0 -10
  51. package/dist/async/emitter.js +0 -22
  52. package/dist/async/emitter.js.map +0 -1
  53. package/dist/async/emitter.test.d.ts +0 -1
  54. package/dist/async.d.ts +0 -28
  55. package/dist/async.js +0 -53
  56. package/dist/async.js.map +0 -1
  57. package/dist/collections/lfu-cache.d.ts +0 -45
  58. package/dist/collections/lfu-cache.test.d.ts +0 -1
  59. package/dist/collections/linked-list.d.ts +0 -47
  60. package/dist/collections/lru-cache.d.ts +0 -45
  61. package/dist/collections/lru-cache.test.d.ts +0 -1
  62. package/dist/collections.d.ts +0 -7
  63. package/dist/collections.js +0 -8
  64. package/dist/collections.js.map +0 -1
  65. package/dist/decorators.d.ts +0 -12
  66. package/dist/decorators.test.d.ts +0 -1
  67. package/dist/disposable.d.ts +0 -122
  68. package/dist/disposable.test.d.ts +0 -1
  69. package/dist/fun.d.ts +0 -40
  70. package/dist/fun.js +0 -40
  71. package/dist/fun.js.map +0 -1
  72. package/dist/order.d.ts +0 -55
  73. package/dist/order.test.d.ts +0 -1
  74. package/dist/time.d.ts +0 -80
  75. package/dist/time.test.d.ts +0 -1
  76. package/dist/types.d.ts +0 -17
  77. package/dist/types.js +0 -6
  78. package/dist/types.js.map +0 -1
@@ -1,45 +0,0 @@
1
- import { type Option } from "@bodil/opt";
2
- /**
3
- * Configuration for the {@link LRUCache}.
4
- */
5
- export type LRUCacheOptions<V> = {
6
- /**
7
- * The maximum size of the cache.
8
- */
9
- maxLength?: number;
10
- /**
11
- * An optional dispose function to be called when a value is removed from
12
- * the cache. Defaults to disposing of values which implement
13
- * {@link Disposable}, and doing nothing otherwise.
14
- */
15
- disposeFunction?: (value: V) => void;
16
- };
17
- /**
18
- * A Least Recently Used cache.
19
- *
20
- * When the cache needs to drop a value to stay within its bounds, it chooses
21
- * the value that was accessed the least recently.
22
- */
23
- export declare class LRUCache<K, V> implements Disposable {
24
- #private;
25
- constructor(options?: LRUCacheOptions<V>);
26
- [Symbol.dispose](): void;
27
- /**
28
- * Get the current size of the cache.
29
- */
30
- get size(): number;
31
- /**
32
- * Test if the cache contains a given key.
33
- *
34
- * This does not count as an access towards cache invalidation.
35
- */
36
- has(key: K): boolean;
37
- /**
38
- * Get the value associated with the given key, if it exists in the cache.
39
- */
40
- get(key: K): Option<V>;
41
- /**
42
- * Add a key/value pair to the cache.
43
- */
44
- set(key: K, value: V): void;
45
- }
@@ -1 +0,0 @@
1
- import "@bodil/opt-vitest";
@@ -1,7 +0,0 @@
1
- /**
2
- * Some useful data structures.
3
- * @module
4
- */
5
- export { LinkedList, type ListNode } from "./collections/linked-list";
6
- export { LFUCache, type LFUCacheOptions } from "./collections/lfu-cache";
7
- export { LRUCache, type LRUCacheOptions } from "./collections/lru-cache";
@@ -1,8 +0,0 @@
1
- /**
2
- * Some useful data structures.
3
- * @module
4
- */
5
- export { LinkedList } from "./collections/linked-list";
6
- export { LFUCache } from "./collections/lfu-cache";
7
- export { LRUCache } from "./collections/lru-cache";
8
- //# sourceMappingURL=collections.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAiB,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAwB,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAE,QAAQ,EAAwB,MAAM,yBAAyB,CAAC"}
@@ -1,12 +0,0 @@
1
- /**
2
- * Assorted decorators.
3
- * @module
4
- */
5
- /**
6
- * Decorate a getter to cache its result.
7
- *
8
- * Useful for deferring initialisation of a property until first access.
9
- * Instead of `myProperty = new Thing();`,
10
- * use `@cache get myProperty() { return new Thing(); }`.
11
- */
12
- export declare function cache<C extends object, T>(getter: (this: C) => T, context: ClassGetterDecoratorContext<C, T>): (this: C) => T;
@@ -1 +0,0 @@
1
- export {};
@@ -1,122 +0,0 @@
1
- /**
2
- * Tools for working with {@link Disposable}s and things that should have been
3
- * {@link Disposable}.
4
- *
5
- * @module
6
- */
7
- import type { Constructor } from "type-fest";
8
- import type { Unsubscribable } from "type-fest/globals";
9
- /**
10
- * Test whether something implements {@link Disposable}.
11
- */
12
- export declare function isDisposable(value: unknown): value is Disposable;
13
- /**
14
- * Type for something that can be turned into a {@link Disposable}.
15
- *
16
- * Includes things that already implement {@link Disposable}, objects with an
17
- * `unsubscribe()` method (an {@link Unsubscribable}), and functions
18
- * with no arguments or return values which are assumed to be unsubscribe
19
- * functions, as popularised by React.
20
- */
21
- export type Disposifiable = (() => void) | Unsubscribable | Disposable;
22
- /**
23
- * Construct a new {@link Disposable} out of any {@link Disposifiable}.
24
- *
25
- * This constructs a new object which implements {@link Disposable}, and nothing
26
- * else. The new object's dispose method will dispose of the original
27
- * {@link Disposifiable}, and ensure that this happens only once, regardless of
28
- * how many times the dispose method is called.
29
- */
30
- export declare function toDisposable(value: Disposifiable): Disposable;
31
- /**
32
- * Test whether something is {@link Disposifiable}.
33
- *
34
- * This is true if that something implements either of {@link Disposable} or
35
- * {@link Unsubscribable}, or if it is a function.
36
- */
37
- export declare function isDisposifiable(value: unknown): value is Disposifiable;
38
- /**
39
- * Call {@link setTimeout} with the provided callback and return a
40
- * {@link Disposable} which will cancel the timeout when disposed.
41
- */
42
- export declare function timeout(fn: () => void, timeout: number | Temporal.Duration): Disposable;
43
- /**
44
- * Call {@link setInterval} with the provided callback and return a
45
- * {@link Disposable} which will cancel the interval when disposed.
46
- */
47
- export declare function interval(fn: () => void, interval: number | Temporal.Duration): Disposable;
48
- export declare function idleCallback(callback: IdleRequestCallback, options?: IdleRequestOptions): Disposable;
49
- /**
50
- * An implementation of {@link AbortController} which also implements
51
- * {@link Disposable}.
52
- *
53
- * Disposing the controller automatically triggers the abort signal.
54
- */
55
- export declare class DisposableAbortController implements AbortController, Disposable {
56
- #private;
57
- get signal(): AbortSignal;
58
- abort(reason?: any): void;
59
- [Symbol.dispose](): void;
60
- }
61
- /**
62
- * Create an {@link AbortController} which also implements {@link Disposable}.
63
- *
64
- * Disposing the controller automatically triggers the abort signal.
65
- *
66
- * @returns A {@link DisposableAbortController}.
67
- */
68
- export declare function abortController(): DisposableAbortController;
69
- export interface DisposableContext extends Disposable {
70
- /**
71
- * Disposes the resources currently held by this context.
72
- */
73
- dispose(): void;
74
- /**
75
- * Register a {@link Disposifiable} resource with this context, to be
76
- * disposed along with it.
77
- *
78
- * @returns The {@link Disposable} that was registered with the context.
79
- */
80
- use(resource: null): null;
81
- use(resource: undefined): undefined;
82
- use(resource: Disposifiable): Disposable;
83
- use(resource: Disposifiable | undefined): Disposable | undefined;
84
- use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
85
- adopt<A>(value: A, onDispose: (value: A) => void): A;
86
- }
87
- /**
88
- * A reentrant version of {@link DisposableStack}.
89
- *
90
- * This maintains a list of {@link Disposable}s, added with the
91
- * {@link DisposableContext.use} method, which will all be disposed when you
92
- * dispose of the {@link DisposableContext} itself.
93
- *
94
- * Where this differs from {@link DisposableStack} is that disposing it only
95
- * resets it after disposing of its dependents, and you can reuse it as many
96
- * times as you like. This is very handy for managing lifecycle resources, eg. a
97
- * web component can allocate resources in its `connectedCallback` method and
98
- * during its connected lifecycle phase which can all be disposed in
99
- * `disconnectedCallback` with a single call to
100
- * {@link DisposableContext.dispose}, and it's instantly ready to go again on
101
- * reconnect.
102
- *
103
- * @class
104
- */
105
- export declare const DisposableContext: Constructor<DisposableContext>;
106
- /**
107
- * A class mixin which provides a {@link Disposable} implementation.
108
- *
109
- * The `use(resource)` method can be used to register {@link Disposifiable}
110
- * resources which will be disposed when the object's dispose method is called.
111
- */
112
- export declare const AutoDisposable: <C extends Constructor<object>>(superclass: C) => {
113
- new (...arguments_: any[]): {
114
- __context?: DisposableContext;
115
- use(resource: null): null;
116
- use(resource: undefined): undefined;
117
- use(resource: Disposifiable): Disposable;
118
- use(resource: Disposifiable | undefined): Disposable | undefined;
119
- use(resource: Disposifiable | null | undefined): Disposable | null | undefined;
120
- [Symbol.dispose](): void;
121
- };
122
- } & C;
@@ -1 +0,0 @@
1
- export {};
package/dist/fun.d.ts DELETED
@@ -1,40 +0,0 @@
1
- /**
2
- * The functional programmer's toolkit.
3
- * @module
4
- */
5
- /**
6
- * Create a function that always returns `value`.
7
- *
8
- * @example
9
- * ```ts @import.meta.vitest
10
- * const alwaysFive: () => number = always(5);
11
- * expect(alwaysFive()).toEqual(5);
12
- * ```
13
- */
14
- export declare function always<A>(value: A): () => A;
15
- /**
16
- * The identity function: it just returns its argument unchanged.
17
- *
18
- * @example
19
- * ```ts @import.meta.vitest
20
- * expect(id(1337)).toEqual(1337);
21
- * expect(id("Hello Joe!")).toEqual("Hello Joe!");
22
- * ```
23
- */
24
- export declare function id<A>(value: A): A;
25
- /**
26
- * A function which does nothing and returns `void`.
27
- */
28
- export declare function nop(): void;
29
- /**
30
- * A function which applies the `+` operator to its arguments.
31
- *
32
- * @example
33
- * ```ts @import.meta.vitest
34
- * expect(sum(2, 3)).toEqual(5);
35
- * expect(sum("foo", "bar")).toEqual("foobar");
36
- * ```
37
- */
38
- export declare function sum(a: number, b: number): number;
39
- export declare function sum(a: bigint, b: bigint): bigint;
40
- export declare function sum(a: string, b: string): string;
package/dist/fun.js DELETED
@@ -1,40 +0,0 @@
1
- /**
2
- * The functional programmer's toolkit.
3
- * @module
4
- */
5
- /**
6
- * Create a function that always returns `value`.
7
- *
8
- * @example
9
- * ```ts @import.meta.vitest
10
- * const alwaysFive: () => number = always(5);
11
- * expect(alwaysFive()).toEqual(5);
12
- * ```
13
- */
14
- export function always(value) {
15
- return () => value;
16
- }
17
- /**
18
- * The identity function: it just returns its argument unchanged.
19
- *
20
- * @example
21
- * ```ts @import.meta.vitest
22
- * expect(id(1337)).toEqual(1337);
23
- * expect(id("Hello Joe!")).toEqual("Hello Joe!");
24
- * ```
25
- */
26
- export function id(value) {
27
- return value;
28
- }
29
- /**
30
- * A function which does nothing and returns `void`.
31
- */
32
- export function nop() {
33
- // nop
34
- }
35
- export function sum(a, b) {
36
- // @ts-ignore
37
- // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
38
- return a + b;
39
- }
40
- //# sourceMappingURL=fun.js.map
package/dist/fun.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"fun.js","sourceRoot":"","sources":["../src/fun.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAI,KAAQ;IAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACvB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC1B,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,GAAG;IACf,MAAM;AACV,CAAC;AAcD,MAAM,UAAU,GAAG,CAAqC,CAAI,EAAE,CAAI;IAC9D,aAAa;IACb,qEAAqE;IACrE,OAAO,CAAC,GAAG,CAAC,CAAC;AACjB,CAAC"}
package/dist/order.d.ts DELETED
@@ -1,55 +0,0 @@
1
- /**
2
- * Sorting and ordering.
3
- * @module
4
- */
5
- import { type Option } from "@bodil/opt";
6
- export declare enum Cmp {
7
- /** Less than. */
8
- LT = -1,
9
- /** Equal to. */
10
- EQ = 0,
11
- /** Greater than. */
12
- GT = 1
13
- }
14
- /**
15
- * An order function is a function which is used to compare two items, returning
16
- * a {@link Cmp} to describe the first item in relation to the second, eg.
17
- * {@link Cmp.GT} says the first item is greater than the second item.
18
- */
19
- export type OrderFn<A> = (left: A, right: A) => Cmp;
20
- /**
21
- * The set of types which can be compared using the `<` and `>` operators.
22
- */
23
- export type OperatorComparable = number | string | bigint | Date;
24
- /**
25
- * An order function which sorts {@link OperatorComparable} items in ascending
26
- * order.
27
- */
28
- export declare function ascending<A extends OperatorComparable>(left: A, right: A): Cmp;
29
- /**
30
- * An order function which sorts {@link OperatorComparable} items in descending
31
- * order.
32
- */
33
- export declare function descending<A extends OperatorComparable>(left: A, right: A): Cmp;
34
- /**
35
- * Take an order function and invert its order.
36
- */
37
- export declare function reverse<T>(fn: OrderFn<T>): OrderFn<T>;
38
- /**
39
- * Given a value, a sorted array and a comparison function by which the array
40
- * has been sorted, find the lowest index at which the value could be inserted
41
- * while maintaining sort order.
42
- */
43
- export declare function bisectLow<A>(cmp: OrderFn<A>, array: Array<A>, key: A): number;
44
- /**
45
- * Given a value, a sorted array and a comparison function by which the array
46
- * has been sorted, find the highest index at which the value could be inserted
47
- * while maintaining sort order.
48
- */
49
- export declare function bisectHigh<A>(cmp: OrderFn<A>, array: Array<A>, key: A): number;
50
- /**
51
- * Given a value, a sorted array and a comparison function by which the array
52
- * has been sorted, find the index of the specific value if it exists in the
53
- * array.
54
- */
55
- export declare function find<A>(cmp: OrderFn<A>, array: Array<A>, key: A): Option<number>;
@@ -1 +0,0 @@
1
- import "@bodil/opt-vitest";
package/dist/time.d.ts DELETED
@@ -1,80 +0,0 @@
1
- /**
2
- * Utilities for working with {@link Temporal}.
3
- * @module
4
- */
5
- /**
6
- * Test if the given {@link Temporal.Instant} comes before a certain moment
7
- * in time.
8
- *
9
- * If `other` is unspecified, compare with the current time.
10
- *
11
- * If `other` is a {@link Temporal.Duration}, compare with that duration
12
- * subtracted from the current time.
13
- *
14
- * If `other` is a {@link Temporal.Instant}, compare with that instant.
15
- *
16
- * @example
17
- * time.isBefore(instant) // is `instant` in the past?
18
- * time.isBefore(instant, otherInstant) // is `instant` before `otherInstant`?
19
- * time.isBefore(instant, duration) // is `instant` more than `duration` ago?
20
- */
21
- export declare function isBefore(instant: Temporal.Instant, other?: Temporal.Instant | Temporal.Duration): boolean;
22
- /**
23
- * Test if the given {@link Temporal.Instant} comes after a certain moment in
24
- * time.
25
- *
26
- * If `other` is unspecified, compare with the current time.
27
- *
28
- * If `other` is a {@link Temporal.Duration}, compare with that duration added
29
- * to the current time.
30
- *
31
- * If `other` is a {@link Temporal.Instant}, compare with that instant.
32
- *
33
- * @example
34
- * time.isAfter(instant) // is `instant` in the future?
35
- * time.isAfter(instant, otherInstant) // is `instant` after `otherInstant`?
36
- * time.isAfter(instant, duration) // is `instant` more than `duration` into the future?
37
- */
38
- export declare function isAfter(instant: Temporal.Instant, other?: Temporal.Instant | Temporal.Duration): boolean;
39
- /**
40
- * Find the shortest duration or earliest time given a series of
41
- * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
42
- */
43
- export declare function min<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T;
44
- /**
45
- * Find the longest duration or latest time given a series of
46
- * {@link Temporal.Duration}s or {@link Temporal.Instant}s.
47
- */
48
- export declare function max<T extends Temporal.Duration | Temporal.Instant>(...rest: Array<T>): T;
49
- /**
50
- * The current time as a {@link Temporal.Instant}.
51
- *
52
- * A shorthand for `Temporal.Now.instant()`.
53
- */
54
- export declare function now(): Temporal.Instant;
55
- /**
56
- * The current time as a {@link Temporal.ZonedDateTime}.
57
- *
58
- * A shorthand for `Temporal.Now.zonedDateTimeISO()`.
59
- */
60
- export declare function nowZ(): Temporal.ZonedDateTime;
61
- /**
62
- * Construct a duration of the given number of days.
63
- */
64
- export declare function days(days: number): Temporal.Duration;
65
- /**
66
- * Construct a duration of the given number of hours.
67
- */
68
- export declare function hours(hours: number): Temporal.Duration;
69
- /**
70
- * Construct a duration of the given number of minutes.
71
- */
72
- export declare function minutes(minutes: number): Temporal.Duration;
73
- /**
74
- * Construct a duration of the given number of seconds.
75
- */
76
- export declare function seconds(seconds: number): Temporal.Duration;
77
- /**
78
- * Construct a duration of the given number of milliseconds.
79
- */
80
- export declare function millis(milliseconds: number): Temporal.Duration;
@@ -1 +0,0 @@
1
- import "temporal-polyfill/global";
package/dist/types.d.ts DELETED
@@ -1,17 +0,0 @@
1
- /**
2
- * Utility types.
3
- * @module
4
- */
5
- /**
6
- * Constructs a type by excluding `undefined` from `T`.
7
- *
8
- * This is like {@link NonNullable} but it only removes `undefined`, not `null`.
9
- */
10
- export type Present<T> = T extends undefined ? never : T;
11
- /**
12
- * Interface for classes which implement equality checking.
13
- */
14
- export interface Equals {
15
- /** Tests whether a given value is equal to this value. */
16
- equals(other: unknown): boolean;
17
- }
package/dist/types.js DELETED
@@ -1,6 +0,0 @@
1
- /**
2
- * Utility types.
3
- * @module
4
- */
5
- export {};
6
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}