@oscarpalmer/mora 0.14.0 → 0.15.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.
package/dist/mora.full.js CHANGED
@@ -337,15 +337,45 @@ class ReactiveArray extends Reactive {
337
337
  }));
338
338
  this.#size.set(value.length);
339
339
  }
340
+ /**
341
+ * Get an indexed item
342
+ */
343
+ at(index) {
344
+ return this.get().at(index);
345
+ }
340
346
  /**
341
347
  * @inheritdoc
342
348
  */
343
349
  get() {
344
350
  return getValue(this.state);
345
351
  }
352
+ /**
353
+ * Create a computed, filtered array
354
+ */
355
+ filter(callback) {
356
+ return computed(() => this.get().filter(callback));
357
+ }
358
+ /**
359
+ * Create a computed, mapped array
360
+ */
361
+ map(callback) {
362
+ return computed(() => this.get().map(callback));
363
+ }
346
364
  peek(length) {
347
365
  return length === true ? this.#size.peek() : [...this.state.value];
348
366
  }
367
+ /**
368
+ * Remove and return the last item of the array
369
+ */
370
+ pop() {
371
+ return this.state.value.pop();
372
+ }
373
+ /**
374
+ * Add items to the end of the array
375
+ */
376
+ push(...items) {
377
+ return this.state.value.push(...items);
378
+ }
349
379
  set(first, second) {
350
380
  if (Array.isArray(first)) {
351
381
  this.state.value.splice(0, this.state.value.length, ...first);
@@ -357,6 +387,24 @@ class ReactiveArray extends Reactive {
357
387
  this.state.value[first] = second;
358
388
  }
359
389
  }
390
+ /**
391
+ * Remove and return the first item of the array
392
+ */
393
+ shift() {
394
+ return this.state.value.shift();
395
+ }
396
+ /**
397
+ * Remove and return items from the array _(and optionally add new items)_
398
+ */
399
+ splice(from, to, ...items) {
400
+ return this.state.value.splice(from, to ?? this.state.value.length, ...items);
401
+ }
402
+ /**
403
+ * Add items to the beginning of the array
404
+ */
405
+ unshift(...items) {
406
+ return this.state.value.unshift(...items);
407
+ }
360
408
  }
361
409
  /**
362
410
  * Create a reactive array
@@ -10,6 +10,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
10
10
  const helpers_emit = require("../helpers/emit.cjs");
11
11
  const helpers_is = require("../helpers/is.cjs");
12
12
  const helpers_value = require("../helpers/value.cjs");
13
+ const value_computed = require("./computed.cjs");
13
14
  const value_reactive = require("./reactive.cjs");
14
15
  const value_signal = require("./signal.cjs");
15
16
  class ReactiveArray extends value_reactive.Reactive {
@@ -38,15 +39,45 @@ class ReactiveArray extends value_reactive.Reactive {
38
39
  this.get().length = value;
39
40
  }
40
41
  }
42
+ /**
43
+ * Get an indexed item
44
+ */
45
+ at(index) {
46
+ return this.get().at(index);
47
+ }
41
48
  /**
42
49
  * @inheritdoc
43
50
  */
44
51
  get() {
45
52
  return helpers_value.getValue(this.state);
46
53
  }
54
+ /**
55
+ * Create a computed, filtered array
56
+ */
57
+ filter(callback) {
58
+ return value_computed.computed(() => this.get().filter(callback));
59
+ }
60
+ /**
61
+ * Create a computed, mapped array
62
+ */
63
+ map(callback) {
64
+ return value_computed.computed(() => this.get().map(callback));
65
+ }
47
66
  peek(length) {
48
67
  return length === true ? __privateGet(this, _size).peek() : [...this.state.value];
49
68
  }
69
+ /**
70
+ * Remove and return the last item of the array
71
+ */
72
+ pop() {
73
+ return this.state.value.pop();
74
+ }
75
+ /**
76
+ * Add items to the end of the array
77
+ */
78
+ push(...items) {
79
+ return this.state.value.push(...items);
80
+ }
50
81
  set(first, second) {
51
82
  if (Array.isArray(first)) {
52
83
  this.state.value.splice(0, this.state.value.length, ...first);
@@ -56,6 +87,28 @@ class ReactiveArray extends value_reactive.Reactive {
56
87
  this.state.value[first] = second;
57
88
  }
58
89
  }
90
+ /**
91
+ * Remove and return the first item of the array
92
+ */
93
+ shift() {
94
+ return this.state.value.shift();
95
+ }
96
+ /**
97
+ * Remove and return items from the array _(and optionally add new items)_
98
+ */
99
+ splice(from, to, ...items) {
100
+ return this.state.value.splice(
101
+ from,
102
+ to ?? this.state.value.length,
103
+ ...items
104
+ );
105
+ }
106
+ /**
107
+ * Add items to the beginning of the array
108
+ */
109
+ unshift(...items) {
110
+ return this.state.value.unshift(...items);
111
+ }
59
112
  }
60
113
  _size = new WeakMap();
61
114
  function array(value) {
@@ -8,6 +8,7 @@ var _size;
8
8
  import { emitValue, emitArrayChanges } from "../helpers/emit.js";
9
9
  import { arrayName } from "../helpers/is.js";
10
10
  import { getValue } from "../helpers/value.js";
11
+ import { computed } from "./computed.js";
11
12
  import { Reactive } from "./reactive.js";
12
13
  import { signal } from "./signal.js";
13
14
  class ReactiveArray extends Reactive {
@@ -36,15 +37,45 @@ class ReactiveArray extends Reactive {
36
37
  this.get().length = value;
37
38
  }
38
39
  }
40
+ /**
41
+ * Get an indexed item
42
+ */
43
+ at(index) {
44
+ return this.get().at(index);
45
+ }
39
46
  /**
40
47
  * @inheritdoc
41
48
  */
42
49
  get() {
43
50
  return getValue(this.state);
44
51
  }
52
+ /**
53
+ * Create a computed, filtered array
54
+ */
55
+ filter(callback) {
56
+ return computed(() => this.get().filter(callback));
57
+ }
58
+ /**
59
+ * Create a computed, mapped array
60
+ */
61
+ map(callback) {
62
+ return computed(() => this.get().map(callback));
63
+ }
45
64
  peek(length) {
46
65
  return length === true ? __privateGet(this, _size).peek() : [...this.state.value];
47
66
  }
67
+ /**
68
+ * Remove and return the last item of the array
69
+ */
70
+ pop() {
71
+ return this.state.value.pop();
72
+ }
73
+ /**
74
+ * Add items to the end of the array
75
+ */
76
+ push(...items) {
77
+ return this.state.value.push(...items);
78
+ }
48
79
  set(first, second) {
49
80
  if (Array.isArray(first)) {
50
81
  this.state.value.splice(0, this.state.value.length, ...first);
@@ -54,6 +85,28 @@ class ReactiveArray extends Reactive {
54
85
  this.state.value[first] = second;
55
86
  }
56
87
  }
88
+ /**
89
+ * Remove and return the first item of the array
90
+ */
91
+ shift() {
92
+ return this.state.value.shift();
93
+ }
94
+ /**
95
+ * Remove and return items from the array _(and optionally add new items)_
96
+ */
97
+ splice(from, to, ...items) {
98
+ return this.state.value.splice(
99
+ from,
100
+ to ?? this.state.value.length,
101
+ ...items
102
+ );
103
+ }
104
+ /**
105
+ * Add items to the beginning of the array
106
+ */
107
+ unshift(...items) {
108
+ return this.state.value.unshift(...items);
109
+ }
57
110
  }
58
111
  _size = new WeakMap();
59
112
  function array(value) {
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  },
55
55
  "type": "module",
56
56
  "types": "./types/index.d.ts",
57
- "version": "0.14.0"
57
+ "version": "0.15.0"
58
58
  }
@@ -1,6 +1,7 @@
1
1
  import {emitArrayChanges, emitValue} from '../helpers/emit';
2
2
  import {arrayName} from '../helpers/is';
3
3
  import {getValue} from '../helpers/value';
4
+ import {type Computed, computed} from './computed';
4
5
  import {Reactive, type ReactiveState} from './reactive';
5
6
  import {type Signal, signal} from './signal';
6
7
 
@@ -43,6 +44,13 @@ export class ReactiveArray<Item> extends Reactive<Item[]> {
43
44
  this.#size.set(value.length);
44
45
  }
45
46
 
47
+ /**
48
+ * Get an indexed item
49
+ */
50
+ at(index: number): Item | undefined {
51
+ return this.get().at(index);
52
+ }
53
+
46
54
  /**
47
55
  * @inheritdoc
48
56
  */
@@ -50,6 +58,24 @@ export class ReactiveArray<Item> extends Reactive<Item[]> {
50
58
  return getValue(this.state);
51
59
  }
52
60
 
61
+ /**
62
+ * Create a computed, filtered array
63
+ */
64
+ filter(
65
+ callback: (item: Item, index: number, array: Item[]) => boolean,
66
+ ): Computed<Item[]> {
67
+ return computed(() => this.get().filter(callback));
68
+ }
69
+
70
+ /**
71
+ * Create a computed, mapped array
72
+ */
73
+ map<Mapped>(
74
+ callback: (item: Item, index: number, array: Item[]) => Mapped,
75
+ ): Computed<Mapped[]> {
76
+ return computed(() => this.get().map(callback));
77
+ }
78
+
53
79
  /**
54
80
  * @inheritdoc
55
81
  */
@@ -64,6 +90,20 @@ export class ReactiveArray<Item> extends Reactive<Item[]> {
64
90
  return length === true ? this.#size.peek() : [...this.state.value];
65
91
  }
66
92
 
93
+ /**
94
+ * Remove and return the last item of the array
95
+ */
96
+ pop(): Item | undefined {
97
+ return this.state.value.pop();
98
+ }
99
+
100
+ /**
101
+ * Add items to the end of the array
102
+ */
103
+ push(...items: Item[]): number {
104
+ return this.state.value.push(...items);
105
+ }
106
+
67
107
  /**
68
108
  * Set the value
69
109
  */
@@ -88,6 +128,31 @@ export class ReactiveArray<Item> extends Reactive<Item[]> {
88
128
  this.state.value[first] = second as Item;
89
129
  }
90
130
  }
131
+
132
+ /**
133
+ * Remove and return the first item of the array
134
+ */
135
+ shift(): Item | undefined {
136
+ return this.state.value.shift();
137
+ }
138
+
139
+ /**
140
+ * Remove and return items from the array _(and optionally add new items)_
141
+ */
142
+ splice(from: number, to?: number, ...items: Item[]): Item[] {
143
+ return this.state.value.splice(
144
+ from,
145
+ to ?? this.state.value.length,
146
+ ...items,
147
+ );
148
+ }
149
+
150
+ /**
151
+ * Add items to the beginning of the array
152
+ */
153
+ unshift(...items: Item[]): number {
154
+ return this.state.value.unshift(...items);
155
+ }
91
156
  }
92
157
 
93
158
  /**
@@ -68,10 +68,22 @@ declare class ReactiveArray<Item> extends Reactive<Item[]> {
68
68
  */
69
69
  set length(value: number);
70
70
  constructor(value: Item[]);
71
+ /**
72
+ * Get an indexed item
73
+ */
74
+ at(index: number): Item | undefined;
71
75
  /**
72
76
  * @inheritdoc
73
77
  */
74
78
  get(): Item[];
79
+ /**
80
+ * Create a computed, filtered array
81
+ */
82
+ filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
83
+ /**
84
+ * Create a computed, mapped array
85
+ */
86
+ map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
75
87
  /**
76
88
  * @inheritdoc
77
89
  */
@@ -80,6 +92,14 @@ declare class ReactiveArray<Item> extends Reactive<Item[]> {
80
92
  * Get the length of the array _(without reactivity)_
81
93
  */
82
94
  peek(length: true): number;
95
+ /**
96
+ * Remove and return the last item of the array
97
+ */
98
+ pop(): Item | undefined;
99
+ /**
100
+ * Add items to the end of the array
101
+ */
102
+ push(...items: Item[]): number;
83
103
  /**
84
104
  * Set the value
85
105
  */
@@ -92,6 +112,18 @@ declare class ReactiveArray<Item> extends Reactive<Item[]> {
92
112
  * Set the length of the array
93
113
  */
94
114
  set(property: "length", value: number): void;
115
+ /**
116
+ * Remove and return the first item of the array
117
+ */
118
+ shift(): Item | undefined;
119
+ /**
120
+ * Remove and return items from the array _(and optionally add new items)_
121
+ */
122
+ splice(from: number, to?: number, ...items: Item[]): Item[];
123
+ /**
124
+ * Add items to the beginning of the array
125
+ */
126
+ unshift(...items: Item[]): number;
95
127
  }
96
128
  declare class Signal<Value> extends Reactive<Value> {
97
129
  constructor(value: Value);
package/types/index.d.cts CHANGED
@@ -84,10 +84,22 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
84
84
  */
85
85
  set length(value: number);
86
86
  constructor(value: Item[]);
87
+ /**
88
+ * Get an indexed item
89
+ */
90
+ at(index: number): Item | undefined;
87
91
  /**
88
92
  * @inheritdoc
89
93
  */
90
94
  get(): Item[];
95
+ /**
96
+ * Create a computed, filtered array
97
+ */
98
+ filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
99
+ /**
100
+ * Create a computed, mapped array
101
+ */
102
+ map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
91
103
  /**
92
104
  * @inheritdoc
93
105
  */
@@ -96,6 +108,14 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
96
108
  * Get the length of the array _(without reactivity)_
97
109
  */
98
110
  peek(length: true): number;
111
+ /**
112
+ * Remove and return the last item of the array
113
+ */
114
+ pop(): Item | undefined;
115
+ /**
116
+ * Add items to the end of the array
117
+ */
118
+ push(...items: Item[]): number;
99
119
  /**
100
120
  * Set the value
101
121
  */
@@ -108,6 +128,18 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
108
128
  * Set the length of the array
109
129
  */
110
130
  set(property: "length", value: number): void;
131
+ /**
132
+ * Remove and return the first item of the array
133
+ */
134
+ shift(): Item | undefined;
135
+ /**
136
+ * Remove and return items from the array _(and optionally add new items)_
137
+ */
138
+ splice(from: number, to?: number, ...items: Item[]): Item[];
139
+ /**
140
+ * Add items to the beginning of the array
141
+ */
142
+ unshift(...items: Item[]): number;
111
143
  }
112
144
  /**
113
145
  * Create a reactive array
@@ -68,10 +68,22 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
68
68
  */
69
69
  set length(value: number);
70
70
  constructor(value: Item[]);
71
+ /**
72
+ * Get an indexed item
73
+ */
74
+ at(index: number): Item | undefined;
71
75
  /**
72
76
  * @inheritdoc
73
77
  */
74
78
  get(): Item[];
79
+ /**
80
+ * Create a computed, filtered array
81
+ */
82
+ filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
83
+ /**
84
+ * Create a computed, mapped array
85
+ */
86
+ map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
75
87
  /**
76
88
  * @inheritdoc
77
89
  */
@@ -80,6 +92,14 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
80
92
  * Get the length of the array _(without reactivity)_
81
93
  */
82
94
  peek(length: true): number;
95
+ /**
96
+ * Remove and return the last item of the array
97
+ */
98
+ pop(): Item | undefined;
99
+ /**
100
+ * Add items to the end of the array
101
+ */
102
+ push(...items: Item[]): number;
83
103
  /**
84
104
  * Set the value
85
105
  */
@@ -92,6 +112,18 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
92
112
  * Set the length of the array
93
113
  */
94
114
  set(property: "length", value: number): void;
115
+ /**
116
+ * Remove and return the first item of the array
117
+ */
118
+ shift(): Item | undefined;
119
+ /**
120
+ * Remove and return items from the array _(and optionally add new items)_
121
+ */
122
+ splice(from: number, to?: number, ...items: Item[]): Item[];
123
+ /**
124
+ * Add items to the beginning of the array
125
+ */
126
+ unshift(...items: Item[]): number;
95
127
  }
96
128
  /**
97
129
  * Create a reactive array
@@ -1,3 +1,4 @@
1
+ import { type Computed } from './computed';
1
2
  import { Reactive } from './reactive';
2
3
  export declare class ReactiveArray<Item> extends Reactive<Item[]> {
3
4
  #private;
@@ -10,10 +11,22 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
10
11
  */
11
12
  set length(value: number);
12
13
  constructor(value: Item[]);
14
+ /**
15
+ * Get an indexed item
16
+ */
17
+ at(index: number): Item | undefined;
13
18
  /**
14
19
  * @inheritdoc
15
20
  */
16
21
  get(): Item[];
22
+ /**
23
+ * Create a computed, filtered array
24
+ */
25
+ filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
26
+ /**
27
+ * Create a computed, mapped array
28
+ */
29
+ map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
17
30
  /**
18
31
  * @inheritdoc
19
32
  */
@@ -22,6 +35,14 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
22
35
  * Get the length of the array _(without reactivity)_
23
36
  */
24
37
  peek(length: true): number;
38
+ /**
39
+ * Remove and return the last item of the array
40
+ */
41
+ pop(): Item | undefined;
42
+ /**
43
+ * Add items to the end of the array
44
+ */
45
+ push(...items: Item[]): number;
25
46
  /**
26
47
  * Set the value
27
48
  */
@@ -34,6 +55,18 @@ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
34
55
  * Set the length of the array
35
56
  */
36
57
  set(property: 'length', value: number): void;
58
+ /**
59
+ * Remove and return the first item of the array
60
+ */
61
+ shift(): Item | undefined;
62
+ /**
63
+ * Remove and return items from the array _(and optionally add new items)_
64
+ */
65
+ splice(from: number, to?: number, ...items: Item[]): Item[];
66
+ /**
67
+ * Add items to the beginning of the array
68
+ */
69
+ unshift(...items: Item[]): number;
37
70
  }
38
71
  /**
39
72
  * Create a reactive array