@peerbit/document-interface 6.0.7-0691c73
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/src/id.d.ts +47 -0
- package/dist/src/id.d.ts.map +1 -0
- package/dist/src/id.js +217 -0
- package/dist/src/id.js.map +1 -0
- package/dist/src/index-engine.d.ts +45 -0
- package/dist/src/index-engine.d.ts.map +1 -0
- package/dist/src/index-engine.js +5 -0
- package/dist/src/index-engine.js.map +1 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +6 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/query.d.ts +173 -0
- package/dist/src/query.d.ts.map +1 -0
- package/dist/src/query.js +545 -0
- package/dist/src/query.js.map +1 -0
- package/dist/src/store.d.ts +7 -0
- package/dist/src/store.d.ts.map +1 -0
- package/dist/src/store.js +2 -0
- package/dist/src/store.js.map +1 -0
- package/dist/src/utils.d.ts +10 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +51 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +63 -0
- package/src/id.ts +209 -0
- package/src/index-engine.ts +57 -0
- package/src/index.ts +5 -0
- package/src/query.ts +478 -0
- package/src/store.ts +5 -0
- package/src/utils.ts +65 -0
package/src/query.ts
ADDED
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AbstractType,
|
|
3
|
+
deserialize,
|
|
4
|
+
field,
|
|
5
|
+
fixedArray,
|
|
6
|
+
serialize,
|
|
7
|
+
variant,
|
|
8
|
+
vec
|
|
9
|
+
} from "@dao-xyz/borsh";
|
|
10
|
+
|
|
11
|
+
import { randomBytes, sha256Base64Sync, toBase64 } from "@peerbit/crypto";
|
|
12
|
+
import {
|
|
13
|
+
BigUnsignedIntegerValue,
|
|
14
|
+
IntegerValue,
|
|
15
|
+
UnsignedIntegerValue
|
|
16
|
+
} from "./id.js";
|
|
17
|
+
|
|
18
|
+
export enum Compare {
|
|
19
|
+
Equal = 0,
|
|
20
|
+
Greater = 1,
|
|
21
|
+
GreaterOrEqual = 2,
|
|
22
|
+
Less = 3,
|
|
23
|
+
LessOrEqual = 4
|
|
24
|
+
}
|
|
25
|
+
export const compare = (
|
|
26
|
+
test: bigint | number,
|
|
27
|
+
compare: Compare,
|
|
28
|
+
value: bigint | number
|
|
29
|
+
) => {
|
|
30
|
+
switch (compare) {
|
|
31
|
+
case Compare.Equal:
|
|
32
|
+
return test == value; // == because with want bigint == number at some cases
|
|
33
|
+
case Compare.Greater:
|
|
34
|
+
return test > value;
|
|
35
|
+
case Compare.GreaterOrEqual:
|
|
36
|
+
return test >= value;
|
|
37
|
+
case Compare.Less:
|
|
38
|
+
return test < value;
|
|
39
|
+
case Compare.LessOrEqual:
|
|
40
|
+
return test <= value;
|
|
41
|
+
default:
|
|
42
|
+
console.warn("Unexpected compare");
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/// ----- QUERY -----
|
|
48
|
+
|
|
49
|
+
export abstract class Query {
|
|
50
|
+
clone() {
|
|
51
|
+
return deserialize(serialize(this), this.constructor) as this;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export enum SortDirection {
|
|
56
|
+
ASC = 0,
|
|
57
|
+
DESC = 1
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
@variant(0)
|
|
61
|
+
export class Sort {
|
|
62
|
+
@field({ type: vec("string") })
|
|
63
|
+
key: string[];
|
|
64
|
+
|
|
65
|
+
@field({ type: "u8" })
|
|
66
|
+
direction: SortDirection;
|
|
67
|
+
|
|
68
|
+
constructor(properties: {
|
|
69
|
+
key: string[] | string;
|
|
70
|
+
direction?: SortDirection;
|
|
71
|
+
}) {
|
|
72
|
+
this.key = Array.isArray(properties.key)
|
|
73
|
+
? properties.key
|
|
74
|
+
: [properties.key];
|
|
75
|
+
this.direction = properties.direction || SortDirection.ASC;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export abstract class AbstractSearchRequest { }
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Search with query and collect with sort conditionss
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
const toArray = <T>(arr: T | T[] | undefined) =>
|
|
86
|
+
(arr ? (Array.isArray(arr) ? arr : [arr]) : undefined) || [];
|
|
87
|
+
|
|
88
|
+
@variant(0)
|
|
89
|
+
export class SearchRequest extends AbstractSearchRequest {
|
|
90
|
+
@field({ type: fixedArray("u8", 32) })
|
|
91
|
+
id: Uint8Array; // Session id
|
|
92
|
+
|
|
93
|
+
@field({ type: vec(Query) })
|
|
94
|
+
query: Query[];
|
|
95
|
+
|
|
96
|
+
@field({ type: vec(Sort) })
|
|
97
|
+
sort: Sort[];
|
|
98
|
+
|
|
99
|
+
@field({ type: "u32" })
|
|
100
|
+
fetch: number;
|
|
101
|
+
|
|
102
|
+
constructor(props?: { query?: Query[] | Query; sort?: Sort[] | Sort }) {
|
|
103
|
+
super();
|
|
104
|
+
this.id = randomBytes(32);
|
|
105
|
+
this.query = toArray(props?.query);
|
|
106
|
+
this.sort = toArray(props?.sort);
|
|
107
|
+
this.fetch = 1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private _idString: string;
|
|
111
|
+
get idString(): string {
|
|
112
|
+
return this._idString || (this._idString = sha256Base64Sync(this.id));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Collect documents from peers using 'collect' session ids. This is used for distributed sorting internally
|
|
118
|
+
*/
|
|
119
|
+
@variant(2)
|
|
120
|
+
export class CollectNextRequest extends AbstractSearchRequest {
|
|
121
|
+
@field({ type: fixedArray("u8", 32) })
|
|
122
|
+
id: Uint8Array; // collect with id
|
|
123
|
+
|
|
124
|
+
@field({ type: "u32" })
|
|
125
|
+
amount: number; // number of documents to ask for
|
|
126
|
+
|
|
127
|
+
constructor(properties: { id: Uint8Array; amount: number }) {
|
|
128
|
+
super();
|
|
129
|
+
this.id = properties.id;
|
|
130
|
+
this.amount = properties.amount;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private _idString: string;
|
|
134
|
+
get idString(): string {
|
|
135
|
+
return this._idString || (this._idString = sha256Base64Sync(this.id));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@variant(3)
|
|
140
|
+
export class CloseIteratorRequest extends AbstractSearchRequest {
|
|
141
|
+
@field({ type: fixedArray("u8", 32) })
|
|
142
|
+
id: Uint8Array; // collect with id
|
|
143
|
+
|
|
144
|
+
constructor(properties: { id: Uint8Array }) {
|
|
145
|
+
super();
|
|
146
|
+
this.id = properties.id;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private _idString: string;
|
|
150
|
+
get idString(): string {
|
|
151
|
+
return this._idString || (this._idString = sha256Base64Sync(this.id));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
@variant(1)
|
|
156
|
+
export abstract class LogicalQuery extends Query { }
|
|
157
|
+
|
|
158
|
+
@variant(0)
|
|
159
|
+
export class And extends LogicalQuery {
|
|
160
|
+
@field({ type: vec(Query) })
|
|
161
|
+
and: Query[];
|
|
162
|
+
|
|
163
|
+
constructor(and: Query[]) {
|
|
164
|
+
super();
|
|
165
|
+
this.and = and;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
@variant(1)
|
|
170
|
+
export class Or extends LogicalQuery {
|
|
171
|
+
@field({ type: vec(Query) })
|
|
172
|
+
or: Query[];
|
|
173
|
+
|
|
174
|
+
constructor(or: Query[]) {
|
|
175
|
+
super();
|
|
176
|
+
this.or = or;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@variant(2)
|
|
181
|
+
export abstract class StateQuery extends Query { }
|
|
182
|
+
|
|
183
|
+
@variant(1)
|
|
184
|
+
export class StateFieldQuery extends StateQuery {
|
|
185
|
+
@field({ type: vec("string") })
|
|
186
|
+
key: string[];
|
|
187
|
+
|
|
188
|
+
constructor(props: { key: string[] | string }) {
|
|
189
|
+
super();
|
|
190
|
+
this.key = Array.isArray(props.key) ? props.key : [props.key];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
@variant(1)
|
|
195
|
+
export class ByteMatchQuery extends StateFieldQuery {
|
|
196
|
+
@field({ type: Uint8Array })
|
|
197
|
+
value: Uint8Array;
|
|
198
|
+
|
|
199
|
+
@field({ type: "u8" })
|
|
200
|
+
// @ts-ignore: unused
|
|
201
|
+
private _reserved: number; // Replcate MemoryCompare query with this?
|
|
202
|
+
|
|
203
|
+
constructor(props: { key: string[] | string; value: Uint8Array }) {
|
|
204
|
+
super(props);
|
|
205
|
+
this.value = props.value;
|
|
206
|
+
this._reserved = 0;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
_valueString: string;
|
|
210
|
+
/**
|
|
211
|
+
* value `asString`
|
|
212
|
+
*/
|
|
213
|
+
get valueString() {
|
|
214
|
+
return this._valueString ?? (this._valueString = toBase64(this.value));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export enum StringMatchMethod {
|
|
219
|
+
"exact" = 0,
|
|
220
|
+
"prefix" = 1,
|
|
221
|
+
"contains" = 2
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
@variant(2)
|
|
225
|
+
export class StringMatch extends StateFieldQuery {
|
|
226
|
+
@field({ type: "string" })
|
|
227
|
+
value: string;
|
|
228
|
+
|
|
229
|
+
@field({ type: "u8" })
|
|
230
|
+
method: StringMatchMethod;
|
|
231
|
+
|
|
232
|
+
@field({ type: "bool" })
|
|
233
|
+
caseInsensitive: boolean;
|
|
234
|
+
|
|
235
|
+
constructor(props: {
|
|
236
|
+
key: string[] | string;
|
|
237
|
+
value: string;
|
|
238
|
+
method?: StringMatchMethod;
|
|
239
|
+
caseInsensitive?: boolean;
|
|
240
|
+
}) {
|
|
241
|
+
super(props);
|
|
242
|
+
this.value = props.value;
|
|
243
|
+
this.method = props.method ?? StringMatchMethod.exact;
|
|
244
|
+
this.caseInsensitive = props.caseInsensitive ?? false;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
@variant(3)
|
|
249
|
+
export class IntegerCompare extends StateFieldQuery {
|
|
250
|
+
@field({ type: "u8" })
|
|
251
|
+
compare: Compare;
|
|
252
|
+
|
|
253
|
+
@field({ type: IntegerValue })
|
|
254
|
+
value: IntegerValue;
|
|
255
|
+
|
|
256
|
+
constructor(props: {
|
|
257
|
+
key: string[] | string;
|
|
258
|
+
value: bigint | number | IntegerValue;
|
|
259
|
+
compare: Compare;
|
|
260
|
+
}) {
|
|
261
|
+
super(props);
|
|
262
|
+
if (props.value instanceof IntegerValue) {
|
|
263
|
+
this.value = props.value;
|
|
264
|
+
} else {
|
|
265
|
+
if (typeof props.value === "bigint") {
|
|
266
|
+
this.value = new BigUnsignedIntegerValue(props.value);
|
|
267
|
+
} else {
|
|
268
|
+
this.value = new UnsignedIntegerValue(props.value);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
this.compare = props.compare;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
@variant(4)
|
|
277
|
+
export class MissingField extends StateFieldQuery {
|
|
278
|
+
constructor(props: { key: string[] | string }) {
|
|
279
|
+
super(props);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
@variant(5)
|
|
284
|
+
export class BoolQuery extends StateFieldQuery {
|
|
285
|
+
@field({ type: "bool" })
|
|
286
|
+
value: boolean;
|
|
287
|
+
|
|
288
|
+
constructor(props: { key: string[] | string; value: boolean }) {
|
|
289
|
+
super(props);
|
|
290
|
+
this.value = props.value;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// TODO MemoryCompareQuery can be replaces with ByteMatchQuery? Or Nesteed Queries + ByteMatchQuery?
|
|
295
|
+
/* @variant(0)
|
|
296
|
+
export class MemoryCompare {
|
|
297
|
+
@field({ type: Uint8Array })
|
|
298
|
+
bytes: Uint8Array;
|
|
299
|
+
|
|
300
|
+
@field({ type: "u64" })
|
|
301
|
+
offset: bigint;
|
|
302
|
+
|
|
303
|
+
constructor(opts?: { bytes: Uint8Array; offset: bigint }) {
|
|
304
|
+
if (opts) {
|
|
305
|
+
this.bytes = opts.bytes;
|
|
306
|
+
this.offset = opts.offset;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
@variant(4)
|
|
312
|
+
export class MemoryCompareQuery extends Query {
|
|
313
|
+
@field({ type: vec(MemoryCompare) })
|
|
314
|
+
compares: MemoryCompare[];
|
|
315
|
+
|
|
316
|
+
constructor(opts?: { compares: MemoryCompare[] }) {
|
|
317
|
+
super();
|
|
318
|
+
if (opts) {
|
|
319
|
+
this.compares = opts.compares;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} */
|
|
323
|
+
|
|
324
|
+
/// ----- RESULTS -----
|
|
325
|
+
|
|
326
|
+
export abstract class Result { }
|
|
327
|
+
|
|
328
|
+
@variant(0)
|
|
329
|
+
export class Context {
|
|
330
|
+
@field({ type: "u64" })
|
|
331
|
+
created: bigint;
|
|
332
|
+
|
|
333
|
+
@field({ type: "u64" })
|
|
334
|
+
modified: bigint;
|
|
335
|
+
|
|
336
|
+
@field({ type: "string" })
|
|
337
|
+
head: string;
|
|
338
|
+
|
|
339
|
+
@field({ type: "string" })
|
|
340
|
+
gid: string;
|
|
341
|
+
|
|
342
|
+
constructor(properties: {
|
|
343
|
+
created: bigint;
|
|
344
|
+
modified: bigint;
|
|
345
|
+
head: string;
|
|
346
|
+
gid: string;
|
|
347
|
+
}) {
|
|
348
|
+
this.created = properties.created;
|
|
349
|
+
this.modified = properties.modified;
|
|
350
|
+
this.head = properties.head;
|
|
351
|
+
this.gid = properties.gid;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
@variant(0)
|
|
356
|
+
export class ResultWithSource<T> extends Result {
|
|
357
|
+
@field({ type: Uint8Array })
|
|
358
|
+
_source: Uint8Array;
|
|
359
|
+
|
|
360
|
+
@field({ type: Context })
|
|
361
|
+
context: Context;
|
|
362
|
+
|
|
363
|
+
_type: AbstractType<T>;
|
|
364
|
+
constructor(opts: {
|
|
365
|
+
source: Uint8Array;
|
|
366
|
+
context: Context;
|
|
367
|
+
value?: T;
|
|
368
|
+
indexed?: Record<string, any>;
|
|
369
|
+
}) {
|
|
370
|
+
super();
|
|
371
|
+
this._source = opts.source;
|
|
372
|
+
this.context = opts.context;
|
|
373
|
+
this._value = opts.value;
|
|
374
|
+
this.indexed = opts.indexed;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
init(type: AbstractType<T>) {
|
|
378
|
+
this._type = type;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
_value?: T;
|
|
382
|
+
get value(): T {
|
|
383
|
+
if (this._value) {
|
|
384
|
+
return this._value;
|
|
385
|
+
}
|
|
386
|
+
if (!this._source) {
|
|
387
|
+
throw new Error("Missing source binary");
|
|
388
|
+
}
|
|
389
|
+
this._value = deserialize(this._source, this._type);
|
|
390
|
+
return this._value;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
indexed?: Record<string, any>;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export abstract class AbstractSearchResult<T> { }
|
|
397
|
+
|
|
398
|
+
@variant(0)
|
|
399
|
+
export class Results<T> extends AbstractSearchResult<T> {
|
|
400
|
+
@field({ type: vec(ResultWithSource) })
|
|
401
|
+
results: ResultWithSource<T>[];
|
|
402
|
+
|
|
403
|
+
@field({ type: "u64" })
|
|
404
|
+
kept: bigint; // how many results that were not sent, but can be collected later
|
|
405
|
+
|
|
406
|
+
constructor(properties: { results: ResultWithSource<T>[]; kept: bigint }) {
|
|
407
|
+
super();
|
|
408
|
+
this.kept = properties.kept;
|
|
409
|
+
this.results = properties.results;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
@variant(1)
|
|
414
|
+
export class NoAccess extends AbstractSearchResult<any> { }
|
|
415
|
+
|
|
416
|
+
/* @variant(5)
|
|
417
|
+
export class LogQuery extends Query { } */
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Find logs that can be decrypted by certain keys
|
|
421
|
+
*/
|
|
422
|
+
/*
|
|
423
|
+
@variant(0)
|
|
424
|
+
export class EntryEncryptedByQuery
|
|
425
|
+
extends LogQuery
|
|
426
|
+
implements
|
|
427
|
+
EntryEncryptionTemplate<
|
|
428
|
+
X25519PublicKey[],
|
|
429
|
+
X25519PublicKey[],
|
|
430
|
+
X25519PublicKey[],
|
|
431
|
+
X25519PublicKey[]
|
|
432
|
+
>
|
|
433
|
+
{
|
|
434
|
+
@field({ type: vec(X25519PublicKey) })
|
|
435
|
+
meta: X25519PublicKey[];
|
|
436
|
+
|
|
437
|
+
@field({ type: vec(X25519PublicKey) })
|
|
438
|
+
payload: X25519PublicKey[];
|
|
439
|
+
|
|
440
|
+
@field({ type: vec(X25519PublicKey) })
|
|
441
|
+
next: X25519PublicKey[];
|
|
442
|
+
|
|
443
|
+
@field({ type: vec(X25519PublicKey) })
|
|
444
|
+
signatures: X25519PublicKey[];
|
|
445
|
+
|
|
446
|
+
constructor(properties?: {
|
|
447
|
+
meta: X25519PublicKey[];
|
|
448
|
+
next: X25519PublicKey[];
|
|
449
|
+
payload: X25519PublicKey[];
|
|
450
|
+
signatures: X25519PublicKey[];
|
|
451
|
+
}) {
|
|
452
|
+
super();
|
|
453
|
+
if (properties) {
|
|
454
|
+
this.metadata = properties.metadata;
|
|
455
|
+
this.payload = properties.payload;
|
|
456
|
+
this.next = properties.next;
|
|
457
|
+
this.signatures = properties.signatures;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
@variant(1)
|
|
463
|
+
export class SignedByQuery extends LogQuery {
|
|
464
|
+
@field({ type: vec(PublicSignKey) })
|
|
465
|
+
_publicKeys: PublicSignKey[];
|
|
466
|
+
|
|
467
|
+
constructor(properties: { publicKeys: PublicSignKey[] }) {
|
|
468
|
+
super();
|
|
469
|
+
if (properties) {
|
|
470
|
+
this._publicKeys = properties.publicKeys;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
get publicKeys() {
|
|
475
|
+
return this._publicKeys;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
*/
|
package/src/store.ts
ADDED
package/src/utils.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Context, Sort, SortDirection } from "./query.js";
|
|
2
|
+
|
|
3
|
+
export const stringArraysEquals = (
|
|
4
|
+
a: string[] | string,
|
|
5
|
+
b: string[] | string
|
|
6
|
+
) => {
|
|
7
|
+
if (a.length !== b.length) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
for (let i = 0; i < a.length; i++) {
|
|
11
|
+
if (a[i] !== b[i]) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const resolvedSort = async <
|
|
19
|
+
Q extends { indexed: Record<string, any>; context: Context }
|
|
20
|
+
>(
|
|
21
|
+
arr: Q[],
|
|
22
|
+
sorts: Sort[]
|
|
23
|
+
) => {
|
|
24
|
+
arr.sort((a, b) => extractSortCompare(a.indexed, b.indexed, sorts));
|
|
25
|
+
return arr;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const extractSortCompare = (
|
|
29
|
+
a: Record<string, any>,
|
|
30
|
+
b: Record<string, any>,
|
|
31
|
+
sorts: Sort[]
|
|
32
|
+
) => {
|
|
33
|
+
for (const sort of sorts) {
|
|
34
|
+
const av = extractFieldValue(a, sort.key);
|
|
35
|
+
const bv = extractFieldValue(b, sort.key);
|
|
36
|
+
const cmp = sortCompare(av, bv);
|
|
37
|
+
if (cmp != 0) {
|
|
38
|
+
if (sort.direction === SortDirection.ASC) {
|
|
39
|
+
return cmp;
|
|
40
|
+
} else {
|
|
41
|
+
return -cmp;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return 0;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const sortCompare = (av: any, bv: any) => {
|
|
49
|
+
if (typeof av === "string" && typeof bv === "string") {
|
|
50
|
+
return av.localeCompare(bv);
|
|
51
|
+
}
|
|
52
|
+
if (av < bv) {
|
|
53
|
+
return -1;
|
|
54
|
+
} else if (av > bv) {
|
|
55
|
+
return 1;
|
|
56
|
+
}
|
|
57
|
+
return 0;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export const extractFieldValue = <T>(doc: any, path: string[]): T => {
|
|
61
|
+
for (let i = 0; i < path.length; i++) {
|
|
62
|
+
doc = doc[path[i]];
|
|
63
|
+
}
|
|
64
|
+
return doc;
|
|
65
|
+
};
|