generated from shgysk8zer0/npm-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.js
More file actions
52 lines (43 loc) · 1.1 KB
/
list.js
File metadata and controls
52 lines (43 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { DisposableState, DisposableComputed } from './disposable.js';
import { ZERO_WIDTH_SPACE } from './consts.js';
export class ListComputed extends DisposableComputed {
static REF_PREFIX = '__list_signal';
constructor(callback, config) {
super(() => {
const value = callback();
if (Array.isArray(value)) {
return value;
} else if (typeof value[Symbol.iterator] === 'function') {
return Array.from(value);
} else {
return [value];
}
}, config);
}
map(cb) {
return new ListComputed(cb);
}
toString() {
return `${ZERO_WIDTH_SPACE}<!--${this.ref}-->`;
}
}
export class ListState extends DisposableState {
constructor(value, config) {
if (Array.isArray(value)) {
super(value, config);
} else if (typeof value[Symbol.iterator] === 'function') {
super(Array.from(value), config);
} else {
super([value], config);
}
}
map(cb) {
return new ListComputed(cb);
}
toString() {
return `${ZERO_WIDTH_SPACE}<!--${this.ref}-->`;
}
}
export const $list = (val, config) => typeof val === 'function'
? new ListComputed(val, config)
: new ListState(val, config);