ctx-core 2.1.0 → 2.2.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/be/index.d.ts +5 -0
- package/be/index.js +5 -0
- package/be/index.test.ts +251 -0
- package/package.json +3 -2
package/be/index.d.ts
ADDED
package/be/index.js
ADDED
package/be/index.test.ts
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { test } from 'uvu'
|
|
2
|
+
import { equal } from 'uvu/assert'
|
|
3
|
+
import {
|
|
4
|
+
be_,
|
|
5
|
+
be__delete,
|
|
6
|
+
be__has_,
|
|
7
|
+
be__has__ctx_,
|
|
8
|
+
be__set,
|
|
9
|
+
be__val_,
|
|
10
|
+
type Ctx,
|
|
11
|
+
ctx__delete,
|
|
12
|
+
ctx__new,
|
|
13
|
+
ctx__set,
|
|
14
|
+
type MapCtx
|
|
15
|
+
} from '../be/index.js'
|
|
16
|
+
test('be_|Map', ()=>{
|
|
17
|
+
const ctx = ctx__new()
|
|
18
|
+
let incrementer_num = 0
|
|
19
|
+
const incrementer = ()=>++incrementer_num
|
|
20
|
+
const root_ = be_('root_', ()=>incrementer())
|
|
21
|
+
const child_ = be_('child_', ctx=>root_(ctx) + incrementer())
|
|
22
|
+
const child1_ = be_('child1_', ctx=>root_(ctx) + child_(ctx))
|
|
23
|
+
equal(root_(ctx), 1)
|
|
24
|
+
equal(ctx.get('root_'), 1)
|
|
25
|
+
equal(child_(ctx), 3)
|
|
26
|
+
equal(ctx.get('child_'), 3)
|
|
27
|
+
equal(child1_(ctx), 4)
|
|
28
|
+
equal(ctx.get('child1_'), 4)
|
|
29
|
+
})
|
|
30
|
+
test('be_|simple array', ()=>{
|
|
31
|
+
const ctx0 = ctx__new()
|
|
32
|
+
const ctx1 = ctx__new()
|
|
33
|
+
const ctx = [ctx0, ctx1]
|
|
34
|
+
const root_ = be_('root_', ()=>1)
|
|
35
|
+
equal(root_(ctx1), 1)
|
|
36
|
+
equal(root_(ctx), 1)
|
|
37
|
+
equal(ctx0.has(root_), false)
|
|
38
|
+
equal(ctx1.has(root_), true)
|
|
39
|
+
const child_ = be_('child_', ctx=>root_(ctx) + 1)
|
|
40
|
+
equal(child_(ctx), 2)
|
|
41
|
+
equal(ctx0.has(child_), true)
|
|
42
|
+
equal(ctx1.has(child_), false)
|
|
43
|
+
})
|
|
44
|
+
test('be_|nested array', ()=>{
|
|
45
|
+
const ctx0 = ctx__new()
|
|
46
|
+
const ctx1 = ctx__new()
|
|
47
|
+
const ctx2 = ctx__new()
|
|
48
|
+
const ctx3 = ctx__new()
|
|
49
|
+
const ctx = [[[ctx0], ctx1], [ctx2, ctx3]]
|
|
50
|
+
const root_ = be_('root_', ()=>1)
|
|
51
|
+
equal(root_(ctx3), 1)
|
|
52
|
+
equal(root_(ctx), 1)
|
|
53
|
+
equal(ctx0.has(root_), false)
|
|
54
|
+
equal(ctx1.has(root_), false)
|
|
55
|
+
equal(ctx2.has(root_), false)
|
|
56
|
+
equal(ctx3.has(root_), true)
|
|
57
|
+
const child_ = be_('child_', ctx=>root_(ctx) + 1)
|
|
58
|
+
equal(child_(ctx), 2)
|
|
59
|
+
equal(ctx0.has(child_), true)
|
|
60
|
+
equal(ctx1.has(child_), false)
|
|
61
|
+
equal(ctx2.has(child_), false)
|
|
62
|
+
equal(ctx3.has(child_), false)
|
|
63
|
+
})
|
|
64
|
+
test('be_|is_source_', ()=>{
|
|
65
|
+
const ctx0 = ctx__new()
|
|
66
|
+
const ctx1 = ctx__new()
|
|
67
|
+
ctx1.set('matching', true)
|
|
68
|
+
const ctx = [ctx0, ctx1]
|
|
69
|
+
const be__ctx_a:Ctx[] = []
|
|
70
|
+
const root_ = be_('root_', ctx=>{
|
|
71
|
+
be__ctx_a.push(ctx)
|
|
72
|
+
return 1
|
|
73
|
+
}, {
|
|
74
|
+
is_source_(map_ctx) {
|
|
75
|
+
return !!map_ctx.get('matching')
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
equal(root_(ctx), 1)
|
|
79
|
+
equal(be__ctx_a, [[ctx0, ctx1]])
|
|
80
|
+
equal(ctx0.has(root_), false)
|
|
81
|
+
equal(ctx1.has(root_), true)
|
|
82
|
+
})
|
|
83
|
+
test('be_|Ctx generic type', ()=>{
|
|
84
|
+
const valid_ctx = ctx__new() as test_ctx_T
|
|
85
|
+
const val_ = be_<boolean, test_ctx_T>('val_', ()=>true)
|
|
86
|
+
val_(valid_ctx)
|
|
87
|
+
// val_(ctx_()) // type error
|
|
88
|
+
})
|
|
89
|
+
test('be_|Ctx|NestedMapCtx', ()=>{
|
|
90
|
+
const ctx0 = ctx__new()
|
|
91
|
+
const ctx1 = ctx__new()
|
|
92
|
+
ctx1.set('matching', true)
|
|
93
|
+
const ctx = [ctx0, ctx1]
|
|
94
|
+
const nested__ctx_ = be_<Ctx>('nested__ctx_', ctx=>[ctx])
|
|
95
|
+
equal(nested__ctx_(ctx), [[ctx0, ctx1]])
|
|
96
|
+
})
|
|
97
|
+
test('be__set', ()=>{
|
|
98
|
+
const ctx0 = ctx__new()
|
|
99
|
+
const val_ = be_<number|undefined>('val_', ()=>undefined, {
|
|
100
|
+
is_source_: map_ctx=>map_ctx === ctx0
|
|
101
|
+
})
|
|
102
|
+
be__set(val_, ctx0, 1)
|
|
103
|
+
equal(val_(ctx0), 1)
|
|
104
|
+
const ctx1 = ctx__new()
|
|
105
|
+
const ctx_a = [ctx1, ctx0]
|
|
106
|
+
be__set(val_, ctx_a, 2)
|
|
107
|
+
equal(val_(ctx0), 2)
|
|
108
|
+
equal(val_(ctx_a), 2)
|
|
109
|
+
equal(ctx1.has(val_), false)
|
|
110
|
+
})
|
|
111
|
+
test('ctx__set', ()=>{
|
|
112
|
+
const ctx0 = ctx__new()
|
|
113
|
+
ctx__set(ctx0, 'key', 1)
|
|
114
|
+
equal(ctx0.get('key'), 1)
|
|
115
|
+
const ctx1 = ctx__new()
|
|
116
|
+
const ctx_a = [ctx1, ctx0]
|
|
117
|
+
ctx__set(ctx_a, 'key', 2,
|
|
118
|
+
(map_ctx:MapCtx)=>map_ctx.get('key') != null)
|
|
119
|
+
equal(ctx0.get('key'), 2)
|
|
120
|
+
equal(ctx1.has('key'), false)
|
|
121
|
+
})
|
|
122
|
+
test('be__delete', ()=>{
|
|
123
|
+
const ctx0 = ctx__new()
|
|
124
|
+
const val_ = be_<boolean>('val_', ()=>true)
|
|
125
|
+
be__delete(val_, ctx0)
|
|
126
|
+
equal(ctx0.has(val_), false)
|
|
127
|
+
equal(ctx0.has('val_'), false)
|
|
128
|
+
equal(val_(ctx0), true)
|
|
129
|
+
equal(ctx0.get(val_), true)
|
|
130
|
+
equal(ctx0.get('val_'), true)
|
|
131
|
+
be__delete(val_, ctx0)
|
|
132
|
+
equal(ctx0.has(val_), false)
|
|
133
|
+
equal(ctx0.has('val_'), false)
|
|
134
|
+
const ctx1 = ctx__new()
|
|
135
|
+
const nested__ctx = [ctx0, ctx1]
|
|
136
|
+
equal(val_(ctx0), true)
|
|
137
|
+
equal(val_(ctx1), true)
|
|
138
|
+
equal(val_(nested__ctx), true)
|
|
139
|
+
equal(ctx0.get('val_'), true)
|
|
140
|
+
equal(ctx1.get('val_'), true)
|
|
141
|
+
be__delete(val_, nested__ctx)
|
|
142
|
+
equal(ctx0.has(val_), false)
|
|
143
|
+
equal(ctx0.has('val_'), false)
|
|
144
|
+
equal(ctx1.has(val_), false)
|
|
145
|
+
equal(ctx1.has('val_'), false)
|
|
146
|
+
})
|
|
147
|
+
test('ctx__delete|id', ()=>{
|
|
148
|
+
const ctx0 = ctx__new()
|
|
149
|
+
ctx__delete(ctx0, 'key')
|
|
150
|
+
equal(ctx0.has('key'), false)
|
|
151
|
+
ctx0.set('key', true)
|
|
152
|
+
equal(ctx0.get('key'), true)
|
|
153
|
+
ctx__delete(ctx0, 'key')
|
|
154
|
+
equal(ctx0.has('key'), false)
|
|
155
|
+
const ctx1 = ctx__new()
|
|
156
|
+
const nested__ctx = [ctx0, ctx1]
|
|
157
|
+
ctx0.set('key', true)
|
|
158
|
+
ctx1.set('key', true)
|
|
159
|
+
equal(ctx0.get('key'), true)
|
|
160
|
+
equal(ctx1.get('key'), true)
|
|
161
|
+
ctx__delete(nested__ctx, 'key')
|
|
162
|
+
equal(ctx0.has('key'), false)
|
|
163
|
+
equal(ctx1.has('key'), false)
|
|
164
|
+
})
|
|
165
|
+
test('ctx__delete|be', ()=>{
|
|
166
|
+
const ctx0 = ctx__new()
|
|
167
|
+
const num_ = be_(()=>1)
|
|
168
|
+
ctx__delete(ctx0, num_)
|
|
169
|
+
equal(ctx0.has(num_), false)
|
|
170
|
+
num_(ctx0)
|
|
171
|
+
equal(ctx0.has(num_), true)
|
|
172
|
+
ctx__delete(ctx0, num_)
|
|
173
|
+
equal(ctx0.has(num_), false)
|
|
174
|
+
num_(ctx0)
|
|
175
|
+
equal(ctx0.has(num_), true)
|
|
176
|
+
ctx__delete(ctx0, num_)
|
|
177
|
+
equal(ctx0.has(num_), false)
|
|
178
|
+
const ctx1 = ctx__new()
|
|
179
|
+
ctx1.set('ctx1', true)
|
|
180
|
+
const nested__ctx = [ctx0, ctx1]
|
|
181
|
+
num_(ctx0)
|
|
182
|
+
num_(ctx1)
|
|
183
|
+
equal(ctx0.has(num_), true)
|
|
184
|
+
equal(ctx1.has(num_), true)
|
|
185
|
+
ctx__delete(nested__ctx, num_)
|
|
186
|
+
equal(ctx0.has(num_), false)
|
|
187
|
+
equal(ctx1.has(num_), false)
|
|
188
|
+
num_(ctx0)
|
|
189
|
+
num_(ctx1)
|
|
190
|
+
equal(ctx0.has(num_), true)
|
|
191
|
+
equal(ctx1.has(num_), true)
|
|
192
|
+
ctx__delete(nested__ctx, num_)
|
|
193
|
+
equal(ctx0.has(num_), false)
|
|
194
|
+
equal(ctx1.has(num_), false)
|
|
195
|
+
const is_source__num_ =
|
|
196
|
+
be_(()=>1,
|
|
197
|
+
{ is_source_: ctx=>!!ctx.get('ctx1') })
|
|
198
|
+
is_source__num_(nested__ctx)
|
|
199
|
+
equal(ctx0.has(is_source__num_), false)
|
|
200
|
+
equal(ctx1.has(is_source__num_), true)
|
|
201
|
+
ctx__delete(nested__ctx, is_source__num_)
|
|
202
|
+
equal(ctx0.has(is_source__num_), false)
|
|
203
|
+
equal(ctx1.has(is_source__num_), false)
|
|
204
|
+
is_source__num_(nested__ctx)
|
|
205
|
+
equal(ctx0.has(is_source__num_), false)
|
|
206
|
+
equal(ctx1.has(is_source__num_), true)
|
|
207
|
+
ctx__delete(nested__ctx, is_source__num_)
|
|
208
|
+
equal(ctx0.has(is_source__num_), false)
|
|
209
|
+
equal(ctx1.has(is_source__num_), false)
|
|
210
|
+
})
|
|
211
|
+
test('be__has_', ()=>{
|
|
212
|
+
const ctx0 = ctx__new()
|
|
213
|
+
ctx__delete(ctx0, 'key')
|
|
214
|
+
equal(be__has_('key', ctx0), false)
|
|
215
|
+
ctx0.set('key', true)
|
|
216
|
+
equal(be__has_('key', ctx0), true)
|
|
217
|
+
ctx__delete(ctx0, 'key')
|
|
218
|
+
equal(be__has_('key', ctx0), false)
|
|
219
|
+
const ctx1 = ctx__new()
|
|
220
|
+
const nested__ctx = [ctx0, ctx1]
|
|
221
|
+
ctx1.set('key', true)
|
|
222
|
+
equal(be__has_('key', nested__ctx), true)
|
|
223
|
+
})
|
|
224
|
+
test('be__has__ctx_', ()=>{
|
|
225
|
+
const ctx0 = ctx__new()
|
|
226
|
+
ctx__delete(ctx0, 'key')
|
|
227
|
+
equal(be__has__ctx_('key', ctx0), null)
|
|
228
|
+
ctx0.set('key', true)
|
|
229
|
+
equal(be__has__ctx_('key', ctx0), ctx0)
|
|
230
|
+
ctx__delete(ctx0, 'key')
|
|
231
|
+
equal(be__has__ctx_('key', ctx0), null)
|
|
232
|
+
const ctx1 = ctx__new()
|
|
233
|
+
const nested__ctx = [ctx0, ctx1]
|
|
234
|
+
ctx1.set('key', true)
|
|
235
|
+
equal(be__has__ctx_('key', nested__ctx), ctx1)
|
|
236
|
+
})
|
|
237
|
+
test('be__val_', ()=>{
|
|
238
|
+
const ctx = ctx__new()
|
|
239
|
+
const val_ = be_<boolean>('val_', ()=>true)
|
|
240
|
+
equal(val_(ctx), true)
|
|
241
|
+
equal(ctx.get(val_), true)
|
|
242
|
+
equal(be__val_(val_, ctx), true)
|
|
243
|
+
be__set(val_, ctx, false)
|
|
244
|
+
equal(val_(ctx), false)
|
|
245
|
+
equal(be__val_(val_, ctx), false)
|
|
246
|
+
})
|
|
247
|
+
test.run()
|
|
248
|
+
declare const test_ctx_sym:unique symbol
|
|
249
|
+
type test_ctx_T = Ctx&{
|
|
250
|
+
[test_ctx_sym]:any
|
|
251
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ctx-core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "ctx-core core library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ctx-core",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"array",
|
|
27
27
|
"atob",
|
|
28
28
|
"base16",
|
|
29
|
+
"be",
|
|
29
30
|
"btoa",
|
|
30
31
|
"buffer",
|
|
31
32
|
"chain",
|
|
@@ -103,7 +104,7 @@
|
|
|
103
104
|
"check-dts": "^0.7.2",
|
|
104
105
|
"sinon": "^17.0.1",
|
|
105
106
|
"ts-node": "^10.9.1",
|
|
106
|
-
"tsx": "^4.
|
|
107
|
+
"tsx": "^4.1.2",
|
|
107
108
|
"typescript": "next",
|
|
108
109
|
"uvu": "^0.5.6"
|
|
109
110
|
},
|