-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.test.ts
More file actions
200 lines (174 loc) · 7.63 KB
/
index.test.ts
File metadata and controls
200 lines (174 loc) · 7.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { expect } from 'chai'
import { Basin } from '..'
describe('Basin', () => {
it('patch', () => {
const basin = new Basin<any>({
key: 'value',
})
expect(basin.applyPatches([
{ op: 'add', path: '/key', value: 'new value' },
])).to.deep.equal(['new value'])
expect(basin.applyPatches([
{ op: 'add', path: '/key', value: 'newer value' },
{ op: 'add', path: '/object', value: { list: [1] } },
{ op: 'add', path: '/object/list/1', value: 2 },
])).to.deep.equal(['newer value', { list: [1, 2] }])
expect(basin.applyPatches([
{ op: 'add', path: '/object/id', value: 'object id' },
])).to.deep.equal([{ id: 'object id', list: [1, 2] }])
expect(basin.applyPatches([
{ op: 'replace', path: '/key', value: 'newest value' },
{ op: 'replace', path: '/object/id', value: 'object id 2' },
])).to.deep.equal(['newest value', { id: 'object id 2', list: [1, 2] }])
expect(basin.items).to.deep
.equal({
key: 'newest value',
object: { id: 'object id 2', list: [1, 2] },
})
expect(basin.applyPatches([
{ op: 'add', path: '/weird~0~1~01key', value: 'val' },
])).to.deep.equal(['val'])
expect(basin.items['weird~/~1key']).to.equal('val')
})
it('string', () => {
const basin = new Basin<any>()
const key = 'key'
basin.setCursor({ jsonPath: '$[\'key\']' })
expect(basin.write('2')).to.equal('2')
expect(basin.items[key]).to.equal('2')
basin.setCursor({ jsonPath: 'key' })
basin.write('4')
expect(basin.items[key]).to.equal('4')
basin.setCursor({ jsonPath: '$.key', position: -1 })
basin.write('3')
expect(basin.items[key]).to.equal('43')
expect(basin.write('21')).to.equal('4321')
expect(basin.items[key]).to.equal('4321')
basin.setCursor({ jsonPath: 'key', position: 0 })
basin.write('76')
expect(basin.items[key]).to.equal('764321')
basin.write('5')
expect(basin.items[key]).to.equal('7654321')
delete basin.items[key]
expect(basin.items[key]).undefined
})
it('object', () => {
const basin = new Basin<any>()
const key = 'key'
basin.setCursor({ jsonPath: '$[\'key\']' })
basin.write({ a: 1 })
expect(basin.items[key]).to.deep.equal({ a: 1 })
basin.setCursor({ jsonPath: 'key.b' })
basin.write([{ t: 'h' }])
expect(basin.items[key]).to.deep.equal({ a: 1, b: [{ t: 'h' }] })
basin.setCursor({ jsonPath: '$[\'key\'].b[0].t', position: -1 })
basin.write('el')
expect(basin.write('lo')).to.deep.equal({ a: 1, b: [{ t: 'hello' }] })
expect(basin.items[key]).to.deep.equal({ a: 1, b: [{ t: 'hello' }] })
})
it('example', () => {
const basin = new Basin<any>()
basin.setCursor({ jsonPath: '$.message' })
expect(basin.write("ello")).to.equal("ello")
basin.setCursor({ jsonPath: 'message', position: -1 })
expect(basin.write(" World")).to.equal("ello World")
expect(basin.write("!")).to.equal("ello World!")
expect(basin.items).to.deep.equal({ message: "ello World!" })
basin.setCursor({ j: 'message', p: 0 })
expect(basin.write("H")).to.equal("Hello World!")
expect(basin.items).to.deep.equal({ message: "Hello World!" })
basin.setCursor({ jsonPath: 'message', p: -1 })
expect(basin.write(" It's")).to.equal("Hello World! It's")
expect(basin.write(" nice ")).to.equal("Hello World! It's nice ")
expect(basin.write("to stream")).to.equal("Hello World! It's nice to stream")
expect(basin.write(" to you.")).to.equal("Hello World! It's nice to stream to you.")
basin.setCursor({ jsonPath: '$.object' })
expect(basin.write({ list: ["item 1"] })).to.deep.equal({ list: ["item 1"] })
basin.setCursor({ jsonPath: '$.object.list', position: -1 })
expect(basin.write("item 2")).to.deep.equal({ list: ["item 1", "item 2"] })
expect(basin.items).to.deep.equal({ message: "Hello World! It's nice to stream to you.", object: { list: ['item 1', 'item 2'] } })
basin.setCursor({ jsonPath: 'object.list[1]', position: -1 })
expect(basin.write(" is the best")).to.deep.equal({ list: ['item 1', 'item 2 is the best'] })
expect(basin.items).to.deep.equal({
message: "Hello World! It's nice to stream to you.",
object: { list: ['item 1', 'item 2 is the best'] },
})
basin.setCursor({ jsonPath: 'object.list', position: 1 })
expect(basin.write("item 1.5")).to.deep.equal({ list: ['item 1', 'item 1.5', 'item 2 is the best'] })
basin.setCursor({ jsonPath: 'object.list[1]' })
expect(basin.write("item 1.33")).to.deep.equal({ list: ['item 1', 'item 1.33', 'item 2 is the best'] })
basin.setCursor({ jsonPath: 'object.list', p: -1 })
expect(basin.write('item 3')).to.deep.equal({ list: ['item 1', 'item 1.33', 'item 2 is the best', 'item 3'] })
expect(basin.write('item 4')).to.deep.equal({ list: ['item 1', 'item 1.33', 'item 2 is the best', 'item 3', 'item 4'] })
expect(basin.write('item 5')).to.deep.equal({ list: ['item 1', 'item 1.33', 'item 2 is the best', 'item 3', 'item 4', 'item 5'] })
basin.setCursor({ jsonPath: 'object.list', p: 0, deleteCount: 1 })
expect(basin.write(undefined)).to.deep.equal({ list: ['item 1.33', 'item 2 is the best', 'item 3', 'item 4', 'item 5'] })
basin.setCursor({ jsonPath: 'object.list', p: 1, d: 2 })
expect(basin.write()).to.deep.equal({ list: ['item 1.33', 'item 4', 'item 5'] })
basin.setCursor({ jsonPath: 'object.list[0]', p: 6, d: 3 })
expect(basin.write('!')).to.deep.equal({ list: ['item 1!', 'item 4', 'item 5'] })
})
it('list insert', () => {
const basin = new Basin<any>({ list: [] })
basin.setCursor({ jsonPath: 'list[0]' })
expect(basin.write('a')).to.deep.equal(['a'])
basin.setCursor({ jsonPath: 'list[1]' })
expect(basin.write('b')).to.deep.equal(['a', 'b'])
basin.setCursor({ jsonPath: 'list', position: -1 })
expect(basin.write('c')).to.deep.equal(['a', 'b', 'c'])
basin.setCursor({ jsonPath: '$.holder.list2' })
expect(basin.write([1])).to.deep.equal({ list2: [1] })
basin.setCursor({ jsonPath: 'holder.list2[1]' })
expect(basin.write(2)).to.deep.equal({ list2: [1, 2] })
basin.setCursor({ jsonPath: 'holder.list2', position: -1 })
expect(basin.write(3)).to.deep.equal({ list2: [1, 2, 3] })
basin.setCursor({ jsonPath: 'holder.list2[1]', position: -1 })
expect(basin.write(4)).to.deep.equal({ list2: [1, 2 + 4, 3] })
})
it('multi-cursor', () => {
const basin = new Basin<any>({ list: [
"Hello ",
"Hi ",
"Hey ",
] })
basin.setCursor({ jsonPath: 'list[0]', p: -1 })
basin.setCursor({ jsonPath: 'list[1]', p: -1 }, '1')
basin.setCursor({ jsonPath: 'list[2]', p: -1 }, '2')
expect(basin.write("there")).to.deep.equal(["Hello there", "Hi ", "Hey "])
expect(basin.write("guy", '1')).to.deep.equal(["Hello there", "Hi guy", "Hey "])
expect(basin.write("you", '2')).to.deep.equal(["Hello there", "Hi guy", "Hey you"])
expect(basin.write(".")).to.deep.equal(["Hello there.", "Hi guy", "Hey you"] )
expect(basin.write(".", '1')).to.deep.equal(["Hello there.", "Hi guy.", "Hey you"] )
expect(basin.write("!", '2')).to.deep.equal(["Hello there.", "Hi guy.", "Hey you!"])
expect(basin.items).to.deep.equal({ list: ["Hello there.", "Hi guy.", "Hey you!"] })
})
it('multi-cursor deeper', () => {
const items = {
key1: {
list: [
{
text: "Hello ",
wtv: 3,
},
],
},
key2: {
list: [
{
text: "Hi ",
wtv: 2,
},
],
},
}
const basin = new Basin<any>(items)
basin.setCursor({ jsonPath: '$[\'key1\'].list[0].text', p: -1 })
basin.setCursor({ jsonPath: '$[\'key2\'].list[0].text', p: -1 }, 'other')
expect(basin.write("there")).to.deep.equal({ list: [{ text: "Hello there", wtv: 3 }] })
expect(basin.write("guy", 'other')).to.deep.equal({ list: [{ text: "Hi guy", wtv: 2 }] })
expect(basin.items).to.deep.equal({
key1: { list: [{ text: "Hello there", wtv: 3 }] },
key2: { list: [{ text: "Hi guy", wtv: 2 }] },
})
})
})