Skip to content

Commit e531d1f

Browse files
committed
refactor: organize operators.js by category
1 parent 97e7bb3 commit e531d1f

File tree

1 file changed

+85
-71
lines changed

1 file changed

+85
-71
lines changed

operators.js

+85-71
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,28 @@ const pullAwaitable = require('pull-awaitable')
77
const cat = require('pull-cat')
88
const { safeFilename } = require('./files')
99

10-
function query(...cbs) {
11-
let res = cbs[0]
12-
for (let i = 1, n = cbs.length; i < n; i++) if (cbs[i]) res = cbs[i](res)
13-
return res
14-
}
10+
//#region Helper functions and util operators
1511

16-
function fromDB(db) {
17-
return {
18-
meta: { db },
12+
function copyMeta(orig, dest) {
13+
if (orig.meta) {
14+
dest.meta = orig.meta
1915
}
2016
}
2117

22-
function toBufferOrFalsy(value) {
23-
if (!value) return value
24-
return Buffer.isBuffer(value) ? value : Buffer.from(value)
25-
}
26-
27-
function seqs(values) {
28-
return {
29-
type: 'SEQS',
30-
seqs: values,
31-
}
18+
function updateMeta(orig, key, value) {
19+
const res = Object.assign({}, orig)
20+
res.meta[key] = value
21+
return res
3222
}
3323

34-
function liveSeqs(pullStream) {
35-
return {
36-
type: 'LIVESEQS',
37-
stream: pullStream,
38-
}
24+
function extractMeta(orig) {
25+
const meta = orig.meta
26+
return meta
3927
}
4028

41-
function offsets(values) {
42-
return {
43-
type: 'OFFSETS',
44-
offsets: values,
45-
}
29+
function toBufferOrFalsy(value) {
30+
if (!value) return value
31+
return Buffer.isBuffer(value) ? value : Buffer.from(value)
4632
}
4733

4834
function seekFromDesc(desc) {
@@ -58,6 +44,40 @@ function seekFromDesc(desc) {
5844
}
5945
}
6046

47+
function query(...cbs) {
48+
let res = cbs[0]
49+
for (let i = 1, n = cbs.length; i < n; i++) if (cbs[i]) res = cbs[i](res)
50+
return res
51+
}
52+
53+
function debug() {
54+
return (ops) => {
55+
const meta = JSON.stringify(ops.meta, (key, val) =>
56+
key === 'db' ? void 0 : val
57+
)
58+
console.log(
59+
'debug',
60+
JSON.stringify(
61+
ops,
62+
(key, val) => {
63+
if (key === 'meta') return void 0
64+
else if (key === 'task' && typeof val === 'function')
65+
return '[Function]'
66+
else if (key === 'value' && val.type === 'Buffer')
67+
return Buffer.from(val.data).toString()
68+
else return val
69+
},
70+
2
71+
),
72+
meta === '{}' ? '' : 'meta: ' + meta
73+
)
74+
return ops
75+
}
76+
}
77+
78+
//#endregion
79+
//#region "Unit operators": they create objects that JITDB interprets
80+
6181
function slowEqual(seekDesc, target, opts) {
6282
opts = opts || {}
6383
const seek = seekFromDesc(seekDesc)
@@ -150,13 +170,6 @@ function includes(seek, target, opts) {
150170
}
151171
}
152172

153-
function not(ops) {
154-
return {
155-
type: 'NOT',
156-
data: [ops],
157-
}
158-
}
159-
160173
function gt(value, indexName) {
161174
if (typeof value !== 'number') throw new Error('gt() needs a number arg')
162175
return {
@@ -201,53 +214,42 @@ function lte(value, indexName) {
201214
}
202215
}
203216

204-
function deferred(task) {
217+
function seqs(values) {
205218
return {
206-
type: 'DEFERRED',
207-
task,
219+
type: 'SEQS',
220+
seqs: values,
208221
}
209222
}
210223

211-
function debug() {
212-
return (ops) => {
213-
const meta = JSON.stringify(ops.meta, (key, val) =>
214-
key === 'db' ? void 0 : val
215-
)
216-
console.log(
217-
'debug',
218-
JSON.stringify(
219-
ops,
220-
(key, val) => {
221-
if (key === 'meta') return void 0
222-
else if (key === 'task' && typeof val === 'function')
223-
return '[Function]'
224-
else if (key === 'value' && val.type === 'Buffer')
225-
return Buffer.from(val.data).toString()
226-
else return val
227-
},
228-
2
229-
),
230-
meta === '{}' ? '' : 'meta: ' + meta
231-
)
232-
return ops
224+
function liveSeqs(pullStream) {
225+
return {
226+
type: 'LIVESEQS',
227+
stream: pullStream,
233228
}
234229
}
235230

236-
function copyMeta(orig, dest) {
237-
if (orig.meta) {
238-
dest.meta = orig.meta
231+
function offsets(values) {
232+
return {
233+
type: 'OFFSETS',
234+
offsets: values,
239235
}
240236
}
241237

242-
function updateMeta(orig, key, value) {
243-
const res = Object.assign({}, orig)
244-
res.meta[key] = value
245-
return res
238+
function deferred(task) {
239+
return {
240+
type: 'DEFERRED',
241+
task,
242+
}
246243
}
247244

248-
function extractMeta(orig) {
249-
const meta = orig.meta
250-
return meta
245+
//#endregion
246+
//#region "Combinator operators": they build composite operations
247+
248+
function not(ops) {
249+
return {
250+
type: 'NOT',
251+
data: [ops],
252+
}
251253
}
252254

253255
function and(...args) {
@@ -294,7 +296,14 @@ function or(...args) {
294296
}
295297
}
296298

297-
// The following are "special operators": they only update meta
299+
//#endregion
300+
//#region "Special operators": they only update meta
301+
302+
function fromDB(db) {
303+
return {
304+
meta: { db },
305+
}
306+
}
298307

299308
function live(opts) {
300309
if (opts && opts.old) return (ops) => updateMeta(ops, 'live', 'liveAndOld')
@@ -313,6 +322,9 @@ function paginate(pageSize) {
313322
return (ops) => updateMeta(ops, 'pageSize', pageSize)
314323
}
315324

325+
//#endregion
326+
//#region "Consumer operators": they execute the query tree
327+
316328
async function executeDeferredOps(ops, meta) {
317329
// Collect all deferred tasks and their object-traversal paths
318330
const allDeferred = []
@@ -422,6 +434,8 @@ function toAsyncIter() {
422434
}
423435
}
424436

437+
//#endregion
438+
425439
module.exports = {
426440
fromDB,
427441
query,

0 commit comments

Comments
 (0)