@verdant-web/common 1.15.2 → 1.15.3-next.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.
Files changed (163) hide show
  1. package/dist/cjs/EventSubscriber.d.ts +17 -17
  2. package/dist/cjs/EventSubscriber.js +66 -66
  3. package/dist/cjs/baseline.d.ts +5 -5
  4. package/dist/cjs/baseline.js +2 -2
  5. package/dist/cjs/batching.d.ts +42 -42
  6. package/dist/cjs/batching.js +91 -91
  7. package/dist/cjs/files.d.ts +19 -19
  8. package/dist/cjs/files.js +18 -18
  9. package/dist/cjs/files.test.d.ts +1 -0
  10. package/dist/cjs/files.test.js +81 -0
  11. package/dist/cjs/files.test.js.map +1 -0
  12. package/dist/cjs/index.d.ts +19 -18
  13. package/dist/cjs/index.js +41 -36
  14. package/dist/cjs/index.js.map +1 -1
  15. package/dist/cjs/indexes.d.ts +17 -17
  16. package/dist/cjs/indexes.js +142 -142
  17. package/dist/cjs/indexes.test.d.ts +1 -1
  18. package/dist/cjs/indexes.test.js +127 -127
  19. package/dist/cjs/migration.d.ts +172 -172
  20. package/dist/cjs/migration.js +301 -301
  21. package/dist/cjs/oids.d.ts +100 -96
  22. package/dist/cjs/oids.js +457 -439
  23. package/dist/cjs/oids.js.map +1 -1
  24. package/dist/cjs/oids.test.d.ts +1 -1
  25. package/dist/cjs/oids.test.js +348 -272
  26. package/dist/cjs/oids.test.js.map +1 -1
  27. package/dist/cjs/operation.d.ts +139 -139
  28. package/dist/cjs/operation.js +545 -545
  29. package/dist/cjs/operation.test.d.ts +1 -1
  30. package/dist/cjs/operation.test.js +529 -529
  31. package/dist/cjs/patch.d.ts +26 -26
  32. package/dist/cjs/patch.js +226 -226
  33. package/dist/cjs/presence.d.ts +27 -27
  34. package/dist/cjs/presence.js +2 -2
  35. package/dist/cjs/protocol.d.ts +145 -145
  36. package/dist/cjs/protocol.js +3 -3
  37. package/dist/cjs/refs.d.ts +7 -5
  38. package/dist/cjs/refs.js +29 -21
  39. package/dist/cjs/refs.js.map +1 -1
  40. package/dist/cjs/replica.d.ts +28 -28
  41. package/dist/cjs/replica.js +28 -28
  42. package/dist/cjs/schema/defaults.d.ts +4 -0
  43. package/dist/cjs/schema/defaults.js +72 -0
  44. package/dist/cjs/schema/defaults.js.map +1 -0
  45. package/dist/cjs/schema/defaults.test.d.ts +1 -1
  46. package/dist/cjs/schema/defaults.test.js +20 -20
  47. package/dist/cjs/schema/fields.d.ts +9 -9
  48. package/dist/cjs/schema/fields.js +144 -144
  49. package/dist/cjs/schema/index.d.ts +8 -8
  50. package/dist/cjs/schema/index.js +51 -51
  51. package/dist/cjs/schema/indexFilters.d.ts +6 -6
  52. package/dist/cjs/schema/indexFilters.js +30 -30
  53. package/dist/cjs/schema/types/collection.d.ts +23 -23
  54. package/dist/cjs/schema/types/collection.js +2 -2
  55. package/dist/cjs/schema/types/compounds.d.ts +11 -11
  56. package/dist/cjs/schema/types/compounds.js +2 -2
  57. package/dist/cjs/schema/types/fields.d.ts +75 -75
  58. package/dist/cjs/schema/types/fields.js +2 -2
  59. package/dist/cjs/schema/types/filters.d.ts +29 -29
  60. package/dist/cjs/schema/types/filters.js +2 -2
  61. package/dist/cjs/schema/types/shapes.d.ts +21 -21
  62. package/dist/cjs/schema/types/shapes.js +2 -2
  63. package/dist/cjs/schema/types/synthetics.d.ts +37 -37
  64. package/dist/cjs/schema/types/synthetics.js +2 -2
  65. package/dist/cjs/schema/types.d.ts +18 -18
  66. package/dist/cjs/schema/types.js +22 -22
  67. package/dist/cjs/schema/validation.d.ts +3 -3
  68. package/dist/cjs/schema/validation.js +86 -86
  69. package/dist/cjs/timestamp.d.ts +50 -50
  70. package/dist/cjs/timestamp.js +251 -251
  71. package/dist/cjs/timestamp.test.d.ts +1 -1
  72. package/dist/cjs/timestamp.test.js +104 -104
  73. package/dist/cjs/undo.d.ts +3 -3
  74. package/dist/cjs/undo.js +198 -198
  75. package/dist/cjs/utils.d.ts +20 -20
  76. package/dist/cjs/utils.js +145 -145
  77. package/dist/cjs/utils.test.d.ts +1 -1
  78. package/dist/cjs/utils.test.js +36 -36
  79. package/dist/esm/EventSubscriber.d.ts +17 -17
  80. package/dist/esm/EventSubscriber.js +62 -62
  81. package/dist/esm/baseline.d.ts +5 -5
  82. package/dist/esm/baseline.js +1 -1
  83. package/dist/esm/batching.d.ts +42 -42
  84. package/dist/esm/batching.js +86 -86
  85. package/dist/esm/files.d.ts +19 -19
  86. package/dist/esm/files.js +12 -12
  87. package/dist/esm/files.test.d.ts +1 -0
  88. package/dist/esm/files.test.js +79 -0
  89. package/dist/esm/files.test.js.map +1 -0
  90. package/dist/esm/index.d.ts +19 -18
  91. package/dist/esm/index.js +16 -15
  92. package/dist/esm/index.js.map +1 -1
  93. package/dist/esm/indexes.d.ts +17 -17
  94. package/dist/esm/indexes.js +130 -130
  95. package/dist/esm/indexes.test.d.ts +1 -1
  96. package/dist/esm/indexes.test.js +125 -125
  97. package/dist/esm/migration.d.ts +172 -172
  98. package/dist/esm/migration.js +294 -294
  99. package/dist/esm/oids.d.ts +100 -96
  100. package/dist/esm/oids.js +422 -407
  101. package/dist/esm/oids.js.map +1 -1
  102. package/dist/esm/oids.test.d.ts +1 -1
  103. package/dist/esm/oids.test.js +346 -270
  104. package/dist/esm/oids.test.js.map +1 -1
  105. package/dist/esm/operation.d.ts +139 -139
  106. package/dist/esm/operation.js +530 -530
  107. package/dist/esm/operation.test.d.ts +1 -1
  108. package/dist/esm/operation.test.js +527 -527
  109. package/dist/esm/patch.d.ts +26 -26
  110. package/dist/esm/patch.js +222 -222
  111. package/dist/esm/presence.d.ts +27 -27
  112. package/dist/esm/presence.js +1 -1
  113. package/dist/esm/protocol.d.ts +145 -145
  114. package/dist/esm/protocol.js +2 -2
  115. package/dist/esm/refs.d.ts +7 -5
  116. package/dist/esm/refs.js +22 -16
  117. package/dist/esm/refs.js.map +1 -1
  118. package/dist/esm/replica.d.ts +28 -28
  119. package/dist/esm/replica.js +25 -25
  120. package/dist/esm/schema/defaults.d.ts +4 -0
  121. package/dist/esm/schema/defaults.js +66 -0
  122. package/dist/esm/schema/defaults.js.map +1 -0
  123. package/dist/esm/schema/defaults.test.d.ts +1 -1
  124. package/dist/esm/schema/defaults.test.js +18 -18
  125. package/dist/esm/schema/fields.d.ts +9 -9
  126. package/dist/esm/schema/fields.js +133 -133
  127. package/dist/esm/schema/index.d.ts +8 -8
  128. package/dist/esm/schema/index.js +32 -32
  129. package/dist/esm/schema/indexFilters.d.ts +6 -6
  130. package/dist/esm/schema/indexFilters.js +22 -22
  131. package/dist/esm/schema/types/collection.d.ts +23 -23
  132. package/dist/esm/schema/types/collection.js +1 -1
  133. package/dist/esm/schema/types/compounds.d.ts +11 -11
  134. package/dist/esm/schema/types/compounds.js +1 -1
  135. package/dist/esm/schema/types/fields.d.ts +75 -75
  136. package/dist/esm/schema/types/fields.js +1 -1
  137. package/dist/esm/schema/types/filters.d.ts +29 -29
  138. package/dist/esm/schema/types/filters.js +1 -1
  139. package/dist/esm/schema/types/shapes.d.ts +21 -21
  140. package/dist/esm/schema/types/shapes.js +1 -1
  141. package/dist/esm/schema/types/synthetics.d.ts +37 -37
  142. package/dist/esm/schema/types/synthetics.js +1 -1
  143. package/dist/esm/schema/types.d.ts +18 -18
  144. package/dist/esm/schema/types.js +6 -6
  145. package/dist/esm/schema/validation.d.ts +3 -3
  146. package/dist/esm/schema/validation.js +81 -81
  147. package/dist/esm/timestamp.d.ts +50 -50
  148. package/dist/esm/timestamp.js +234 -234
  149. package/dist/esm/timestamp.test.d.ts +1 -1
  150. package/dist/esm/timestamp.test.js +102 -102
  151. package/dist/esm/undo.d.ts +3 -3
  152. package/dist/esm/undo.js +194 -194
  153. package/dist/esm/utils.d.ts +20 -20
  154. package/dist/esm/utils.js +127 -127
  155. package/dist/esm/utils.test.d.ts +1 -1
  156. package/dist/esm/utils.test.js +34 -34
  157. package/dist/tsconfig-cjs.tsbuildinfo +1 -1
  158. package/dist/tsconfig.tsbuildinfo +1 -1
  159. package/package.json +1 -1
  160. package/src/index.ts +1 -0
  161. package/src/oids.test.ts +82 -0
  162. package/src/oids.ts +17 -0
  163. package/src/refs.ts +8 -0
@@ -1,131 +1,131 @@
1
- import { isIndexed, } from './index.js';
2
- // unlikely to be used unicode character
3
- export const COMPOUND_INDEX_SEPARATOR = '\uFFFFFE';
4
- // 1 lower than separator
5
- export const COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = '\u0000';
6
- // 1 higher than separator
7
- export const COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = '\uFFFFFF';
8
- export function createCompoundIndexValue(...fields) {
9
- const value = expandArrayIndex(fields);
10
- if (value.length === 1) {
11
- return value[0];
12
- }
13
- return value;
14
- }
15
- export function createUpperBoundIndexValue(...fields) {
16
- return (fields.join(COMPOUND_INDEX_SEPARATOR) +
17
- `${COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`);
18
- }
19
- export function createLowerBoundIndexValue(...fields) {
20
- return (fields.join(COMPOUND_INDEX_SEPARATOR) +
21
- `${COMPOUND_INDEX_SEPARATOR}${COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`);
22
- }
23
- /**
24
- * Whenever an array value is included in a compound index, we have to expand
25
- * the value to include all permutations of values in the array.
26
- * For example if we had an index id + tags on a document
27
- *
28
- * {
29
- * id: '1',
30
- * tags: ['a', 'b']
31
- * }
32
- *
33
- * we want to create an index:
34
- *
35
- * id_tags: ['1#a', '1#b']
36
- *
37
- * If multiple arrays are indexed we have to exponentially expand...
38
- *
39
- * id_tags_tags2: ['1#a#a', '1#a#b', '1#b#a', '1#b#b']
40
- *
41
- * To generalize this we construct a 2-level array of strings.
42
- * Iterating over indexed values, we expand each item into N items if
43
- * the current value is an array.
44
- *
45
- * Then we combine all nested arrays into compound index values.
46
- * This will produce an array of 1 element if none of the indexed values
47
- * were arrays. The caller should unwrap that.
48
- *
49
- * This function also deduplicates values.
50
- */
51
- function expandArrayIndex(fields) {
52
- let value = [[]];
53
- for (const field of fields) {
54
- if (Array.isArray(field)) {
55
- const newValue = [];
56
- for (const previousValue of value) {
57
- for (const fieldValue of field) {
58
- newValue.push(previousValue.concat(fieldValue));
59
- }
60
- }
61
- value = newValue;
62
- }
63
- else {
64
- for (const item of value) {
65
- item.push(`${field}`);
66
- }
67
- }
68
- }
69
- return Array.from(new Set(value.map((item) => {
70
- return item.join(COMPOUND_INDEX_SEPARATOR);
71
- })));
72
- }
73
- export function isDirectSynthetic(index) {
74
- return !!index.field;
75
- }
76
- export function computeSynthetics(schema, obj) {
77
- const result = {};
78
- for (const [name, property] of Object.entries(schema.indexes || {})) {
79
- const index = property;
80
- if (isDirectSynthetic(index)) {
81
- result[name] = sanitizeIndexValue(obj[index.field]);
82
- }
83
- else {
84
- result[name] = sanitizeIndexValue(index.compute(obj));
85
- }
86
- }
87
- return result;
88
- }
89
- export function computeCompoundIndices(schema, doc) {
90
- return Object.entries(schema.compounds || {}).reduce((acc, [indexKey, index]) => {
91
- acc[indexKey] = createCompoundIndexValue(...index.of.map((key) => doc[key]));
92
- return acc;
93
- }, {});
94
- }
95
- function computeIndexedFields(schema, doc) {
96
- return Object.entries(schema.fields).reduce((acc, [key, field]) => {
97
- if (isIndexed(field)) {
98
- acc[key] = sanitizeIndexValue(doc[key]);
99
- }
100
- return acc;
101
- }, {});
102
- }
103
- export function getIndexValues(schema, doc) {
104
- return Object.assign(Object.assign(Object.assign(Object.assign({ [schema.primaryKey]: doc[schema.primaryKey] }, computeIndexedFields(schema, doc)), computeSynthetics(schema, doc)), computeCompoundIndices(schema, doc)), { '@@@snapshot': doc });
105
- }
106
- export function assignIndexValues(schema, doc) {
107
- Object.assign(doc, computeSynthetics(schema, doc));
108
- Object.assign(doc, computeCompoundIndices(schema, doc));
109
- return doc;
110
- }
111
- export const NULL_INDEX_VALUE = 'null';
112
- export function sanitizeIndexValue(value) {
113
- if (value === null) {
114
- return NULL_INDEX_VALUE;
115
- }
116
- if (typeof value === 'string' || typeof value === 'number') {
117
- return value;
118
- }
119
- if (typeof value === 'boolean' || value === null) {
120
- return `${value}`;
121
- }
122
- if (value === undefined) {
123
- // this shouldn't happen ,but for resiliency...
124
- return 'undefined';
125
- }
126
- if (Array.isArray(value)) {
127
- return value.map(sanitizeIndexValue);
128
- }
129
- throw new Error(`Unsupported index value: ${value}`);
130
- }
1
+ import { isIndexed, } from './index.js';
2
+ // unlikely to be used unicode character
3
+ export const COMPOUND_INDEX_SEPARATOR = '\uFFFFFE';
4
+ // 1 lower than separator
5
+ export const COMPOUND_INDEX_LOWER_BOUND_SEPARATOR = '\u0000';
6
+ // 1 higher than separator
7
+ export const COMPOUND_INDEX_UPPER_BOUND_SEPARATOR = '\uFFFFFF';
8
+ export function createCompoundIndexValue(...fields) {
9
+ const value = expandArrayIndex(fields);
10
+ if (value.length === 1) {
11
+ return value[0];
12
+ }
13
+ return value;
14
+ }
15
+ export function createUpperBoundIndexValue(...fields) {
16
+ return (fields.join(COMPOUND_INDEX_SEPARATOR) +
17
+ `${COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`);
18
+ }
19
+ export function createLowerBoundIndexValue(...fields) {
20
+ return (fields.join(COMPOUND_INDEX_SEPARATOR) +
21
+ `${COMPOUND_INDEX_SEPARATOR}${COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`);
22
+ }
23
+ /**
24
+ * Whenever an array value is included in a compound index, we have to expand
25
+ * the value to include all permutations of values in the array.
26
+ * For example if we had an index id + tags on a document
27
+ *
28
+ * {
29
+ * id: '1',
30
+ * tags: ['a', 'b']
31
+ * }
32
+ *
33
+ * we want to create an index:
34
+ *
35
+ * id_tags: ['1#a', '1#b']
36
+ *
37
+ * If multiple arrays are indexed we have to exponentially expand...
38
+ *
39
+ * id_tags_tags2: ['1#a#a', '1#a#b', '1#b#a', '1#b#b']
40
+ *
41
+ * To generalize this we construct a 2-level array of strings.
42
+ * Iterating over indexed values, we expand each item into N items if
43
+ * the current value is an array.
44
+ *
45
+ * Then we combine all nested arrays into compound index values.
46
+ * This will produce an array of 1 element if none of the indexed values
47
+ * were arrays. The caller should unwrap that.
48
+ *
49
+ * This function also deduplicates values.
50
+ */
51
+ function expandArrayIndex(fields) {
52
+ let value = [[]];
53
+ for (const field of fields) {
54
+ if (Array.isArray(field)) {
55
+ const newValue = [];
56
+ for (const previousValue of value) {
57
+ for (const fieldValue of field) {
58
+ newValue.push(previousValue.concat(fieldValue));
59
+ }
60
+ }
61
+ value = newValue;
62
+ }
63
+ else {
64
+ for (const item of value) {
65
+ item.push(`${field}`);
66
+ }
67
+ }
68
+ }
69
+ return Array.from(new Set(value.map((item) => {
70
+ return item.join(COMPOUND_INDEX_SEPARATOR);
71
+ })));
72
+ }
73
+ export function isDirectSynthetic(index) {
74
+ return !!index.field;
75
+ }
76
+ export function computeSynthetics(schema, obj) {
77
+ const result = {};
78
+ for (const [name, property] of Object.entries(schema.indexes || {})) {
79
+ const index = property;
80
+ if (isDirectSynthetic(index)) {
81
+ result[name] = sanitizeIndexValue(obj[index.field]);
82
+ }
83
+ else {
84
+ result[name] = sanitizeIndexValue(index.compute(obj));
85
+ }
86
+ }
87
+ return result;
88
+ }
89
+ export function computeCompoundIndices(schema, doc) {
90
+ return Object.entries(schema.compounds || {}).reduce((acc, [indexKey, index]) => {
91
+ acc[indexKey] = createCompoundIndexValue(...index.of.map((key) => doc[key]));
92
+ return acc;
93
+ }, {});
94
+ }
95
+ function computeIndexedFields(schema, doc) {
96
+ return Object.entries(schema.fields).reduce((acc, [key, field]) => {
97
+ if (isIndexed(field)) {
98
+ acc[key] = sanitizeIndexValue(doc[key]);
99
+ }
100
+ return acc;
101
+ }, {});
102
+ }
103
+ export function getIndexValues(schema, doc) {
104
+ return Object.assign(Object.assign(Object.assign(Object.assign({ [schema.primaryKey]: doc[schema.primaryKey] }, computeIndexedFields(schema, doc)), computeSynthetics(schema, doc)), computeCompoundIndices(schema, doc)), { '@@@snapshot': doc });
105
+ }
106
+ export function assignIndexValues(schema, doc) {
107
+ Object.assign(doc, computeSynthetics(schema, doc));
108
+ Object.assign(doc, computeCompoundIndices(schema, doc));
109
+ return doc;
110
+ }
111
+ export const NULL_INDEX_VALUE = 'null';
112
+ export function sanitizeIndexValue(value) {
113
+ if (value === null) {
114
+ return NULL_INDEX_VALUE;
115
+ }
116
+ if (typeof value === 'string' || typeof value === 'number') {
117
+ return value;
118
+ }
119
+ if (typeof value === 'boolean' || value === null) {
120
+ return `${value}`;
121
+ }
122
+ if (value === undefined) {
123
+ // this shouldn't happen ,but for resiliency...
124
+ return 'undefined';
125
+ }
126
+ if (Array.isArray(value)) {
127
+ return value.map(sanitizeIndexValue);
128
+ }
129
+ throw new Error(`Unsupported index value: ${value}`);
130
+ }
131
131
  //# sourceMappingURL=indexes.js.map
@@ -1 +1 @@
1
- export {};
1
+ export {};
@@ -1,126 +1,126 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { COMPOUND_INDEX_LOWER_BOUND_SEPARATOR, COMPOUND_INDEX_SEPARATOR, COMPOUND_INDEX_UPPER_BOUND_SEPARATOR, createCompoundIndexValue, createLowerBoundIndexValue, createUpperBoundIndexValue, } from './indexes.js';
3
- describe('compound indexes', () => {
4
- it('can be created from two string values', () => {
5
- expect(createCompoundIndexValue('a', 'b')).toEqual(`a${COMPOUND_INDEX_SEPARATOR}b`);
6
- });
7
- it('can be created from two number values', () => {
8
- expect(createCompoundIndexValue(1, 2)).toEqual(`1${COMPOUND_INDEX_SEPARATOR}2`);
9
- });
10
- it('can be created from an array and string value', () => {
11
- expect(createCompoundIndexValue(['a', 'b'], 'c')).toEqual([
12
- `a${COMPOUND_INDEX_SEPARATOR}c`,
13
- `b${COMPOUND_INDEX_SEPARATOR}c`,
14
- ]);
15
- });
16
- it('can be created from a string and array of numbers', () => {
17
- expect(createCompoundIndexValue('a', [1, 2])).toEqual([
18
- `a${COMPOUND_INDEX_SEPARATOR}1`,
19
- `a${COMPOUND_INDEX_SEPARATOR}2`,
20
- ]);
21
- });
22
- it('can be created from an array of strings and an array of booleans', () => {
23
- expect(createCompoundIndexValue(['a', 'b'], [true, false])).toEqual([
24
- `a${COMPOUND_INDEX_SEPARATOR}true`,
25
- `a${COMPOUND_INDEX_SEPARATOR}false`,
26
- `b${COMPOUND_INDEX_SEPARATOR}true`,
27
- `b${COMPOUND_INDEX_SEPARATOR}false`,
28
- ]);
29
- });
30
- it('remove duplicate array compound index values', () => {
31
- expect(createCompoundIndexValue(['a', 'b', 'b'], [true, false, true])).toEqual([
32
- `a${COMPOUND_INDEX_SEPARATOR}true`,
33
- `a${COMPOUND_INDEX_SEPARATOR}false`,
34
- `b${COMPOUND_INDEX_SEPARATOR}true`,
35
- `b${COMPOUND_INDEX_SEPARATOR}false`,
36
- ]);
37
- });
38
- it('creates lower bound value for multi-value indexes', () => {
39
- expect(createLowerBoundIndexValue('a', 'b')).toEqual(`a${COMPOUND_INDEX_SEPARATOR}b${COMPOUND_INDEX_SEPARATOR}${COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`);
40
- });
41
- it('creates upper bound value for multi-value indexes', () => {
42
- expect(createUpperBoundIndexValue('a', 'b')).toEqual(`a${COMPOUND_INDEX_SEPARATOR}b${COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`);
43
- }),
44
- it('will always be grouped by the first item', () => {
45
- expect([
46
- createCompoundIndexValue('aaaaaa', 'b'),
47
- createCompoundIndexValue('aaaaaa', 'c'),
48
- createCompoundIndexValue('aaaaaa', 'd'),
49
- createCompoundIndexValue('aaaaaa', 'e'),
50
- // extra nefarious!
51
- createCompoundIndexValue('aaaaaaa', 'no!'),
52
- createCompoundIndexValue('aaaaaa ', 'no!'),
53
- createCompoundIndexValue(`aaaaaa😎`, 'no!'),
54
- createCompoundIndexValue(`aaaaaa😎😎`, 'no!'),
55
- createCompoundIndexValue('aaa', 'no!'),
56
- createCompoundIndexValue(`aaaaaaa😎no!`, 'no!'),
57
- ].sort()).toEqual([
58
- `aaaaaa ${COMPOUND_INDEX_SEPARATOR}no!`,
59
- `aaaaaaa😎no!${COMPOUND_INDEX_SEPARATOR}no!`,
60
- `aaaaaaa${COMPOUND_INDEX_SEPARATOR}no!`,
61
- `aaaaaa😎😎${COMPOUND_INDEX_SEPARATOR}no!`,
62
- `aaaaaa😎${COMPOUND_INDEX_SEPARATOR}no!`,
63
- // this is the contiguous block of relevant values
64
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}b`,
65
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}c`,
66
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}d`,
67
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}e`,
68
- // here ends the block
69
- `aaa${COMPOUND_INDEX_SEPARATOR}no!`,
70
- ]);
71
- });
72
- it('will be bounded by upper and lower bound values', () => {
73
- const match = 'aaaaaa';
74
- const sorted = [
75
- createCompoundIndexValue(match, 'b'),
76
- createCompoundIndexValue(match, 'c'),
77
- createCompoundIndexValue(match, 'd'),
78
- createCompoundIndexValue(match, 'e'),
79
- // extra nefarious!
80
- createCompoundIndexValue(`${match}a`, 'no!'),
81
- createCompoundIndexValue(`${match} `, 'no!'),
82
- createCompoundIndexValue(`${match}😎`, 'no!'),
83
- createCompoundIndexValue(`${match}😎😎`, 'no!'),
84
- createCompoundIndexValue('aaa', 'no!'),
85
- createCompoundIndexValue(`${match}a😎no!`, 'no!'),
86
- ].sort();
87
- const boundedBlock = sorted.filter((value) => value > createLowerBoundIndexValue(match) &&
88
- value < createUpperBoundIndexValue(match));
89
- expect(boundedBlock).toEqual([
90
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}b`,
91
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}c`,
92
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}d`,
93
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}e`,
94
- ]);
95
- });
96
- it('will be bounded by upper and lower bounds when matching multiple sections', () => {
97
- const match1 = 'aaaaaa';
98
- const match2 = 'bbbbbb';
99
- const sorted = [
100
- createCompoundIndexValue(match1, match2, 'b'),
101
- createCompoundIndexValue(match1, match2, 'c'),
102
- createCompoundIndexValue(match1, match2, 'd'),
103
- createCompoundIndexValue(match1, match2, 'e'),
104
- // extra nefarious!
105
- createCompoundIndexValue(`${match1}a`, match2, 'no!'),
106
- createCompoundIndexValue(`${match1} `, match2, 'no!'),
107
- createCompoundIndexValue(`${match1}😎`, match2, 'no!'),
108
- createCompoundIndexValue(`${match1}😎😎`, match2, 'no!'),
109
- createCompoundIndexValue('aaa', match2, 'no!'),
110
- createCompoundIndexValue(`${match1}a😎no!`, match2, 'no!'),
111
- createCompoundIndexValue(match1, `${match2}a😎no!`, 'no!'),
112
- createCompoundIndexValue(match1, `${match2} `, 'no!'),
113
- createCompoundIndexValue(match1, ' ', 'no!'),
114
- createCompoundIndexValue(match1, '\u00001', 'no!'),
115
- createCompoundIndexValue(match1, '\uE007f', 'no!'),
116
- ].sort();
117
- expect(sorted.filter((value) => value > createLowerBoundIndexValue(match1, match2) &&
118
- value < createUpperBoundIndexValue(match1, match2))).toEqual([
119
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}b`,
120
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}c`,
121
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}d`,
122
- `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}e`,
123
- ]);
124
- });
125
- });
1
+ import { describe, expect, it } from 'vitest';
2
+ import { COMPOUND_INDEX_LOWER_BOUND_SEPARATOR, COMPOUND_INDEX_SEPARATOR, COMPOUND_INDEX_UPPER_BOUND_SEPARATOR, createCompoundIndexValue, createLowerBoundIndexValue, createUpperBoundIndexValue, } from './indexes.js';
3
+ describe('compound indexes', () => {
4
+ it('can be created from two string values', () => {
5
+ expect(createCompoundIndexValue('a', 'b')).toEqual(`a${COMPOUND_INDEX_SEPARATOR}b`);
6
+ });
7
+ it('can be created from two number values', () => {
8
+ expect(createCompoundIndexValue(1, 2)).toEqual(`1${COMPOUND_INDEX_SEPARATOR}2`);
9
+ });
10
+ it('can be created from an array and string value', () => {
11
+ expect(createCompoundIndexValue(['a', 'b'], 'c')).toEqual([
12
+ `a${COMPOUND_INDEX_SEPARATOR}c`,
13
+ `b${COMPOUND_INDEX_SEPARATOR}c`,
14
+ ]);
15
+ });
16
+ it('can be created from a string and array of numbers', () => {
17
+ expect(createCompoundIndexValue('a', [1, 2])).toEqual([
18
+ `a${COMPOUND_INDEX_SEPARATOR}1`,
19
+ `a${COMPOUND_INDEX_SEPARATOR}2`,
20
+ ]);
21
+ });
22
+ it('can be created from an array of strings and an array of booleans', () => {
23
+ expect(createCompoundIndexValue(['a', 'b'], [true, false])).toEqual([
24
+ `a${COMPOUND_INDEX_SEPARATOR}true`,
25
+ `a${COMPOUND_INDEX_SEPARATOR}false`,
26
+ `b${COMPOUND_INDEX_SEPARATOR}true`,
27
+ `b${COMPOUND_INDEX_SEPARATOR}false`,
28
+ ]);
29
+ });
30
+ it('remove duplicate array compound index values', () => {
31
+ expect(createCompoundIndexValue(['a', 'b', 'b'], [true, false, true])).toEqual([
32
+ `a${COMPOUND_INDEX_SEPARATOR}true`,
33
+ `a${COMPOUND_INDEX_SEPARATOR}false`,
34
+ `b${COMPOUND_INDEX_SEPARATOR}true`,
35
+ `b${COMPOUND_INDEX_SEPARATOR}false`,
36
+ ]);
37
+ });
38
+ it('creates lower bound value for multi-value indexes', () => {
39
+ expect(createLowerBoundIndexValue('a', 'b')).toEqual(`a${COMPOUND_INDEX_SEPARATOR}b${COMPOUND_INDEX_SEPARATOR}${COMPOUND_INDEX_LOWER_BOUND_SEPARATOR}`);
40
+ });
41
+ it('creates upper bound value for multi-value indexes', () => {
42
+ expect(createUpperBoundIndexValue('a', 'b')).toEqual(`a${COMPOUND_INDEX_SEPARATOR}b${COMPOUND_INDEX_UPPER_BOUND_SEPARATOR}`);
43
+ }),
44
+ it('will always be grouped by the first item', () => {
45
+ expect([
46
+ createCompoundIndexValue('aaaaaa', 'b'),
47
+ createCompoundIndexValue('aaaaaa', 'c'),
48
+ createCompoundIndexValue('aaaaaa', 'd'),
49
+ createCompoundIndexValue('aaaaaa', 'e'),
50
+ // extra nefarious!
51
+ createCompoundIndexValue('aaaaaaa', 'no!'),
52
+ createCompoundIndexValue('aaaaaa ', 'no!'),
53
+ createCompoundIndexValue(`aaaaaa😎`, 'no!'),
54
+ createCompoundIndexValue(`aaaaaa😎😎`, 'no!'),
55
+ createCompoundIndexValue('aaa', 'no!'),
56
+ createCompoundIndexValue(`aaaaaaa😎no!`, 'no!'),
57
+ ].sort()).toEqual([
58
+ `aaaaaa ${COMPOUND_INDEX_SEPARATOR}no!`,
59
+ `aaaaaaa😎no!${COMPOUND_INDEX_SEPARATOR}no!`,
60
+ `aaaaaaa${COMPOUND_INDEX_SEPARATOR}no!`,
61
+ `aaaaaa😎😎${COMPOUND_INDEX_SEPARATOR}no!`,
62
+ `aaaaaa😎${COMPOUND_INDEX_SEPARATOR}no!`,
63
+ // this is the contiguous block of relevant values
64
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}b`,
65
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}c`,
66
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}d`,
67
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}e`,
68
+ // here ends the block
69
+ `aaa${COMPOUND_INDEX_SEPARATOR}no!`,
70
+ ]);
71
+ });
72
+ it('will be bounded by upper and lower bound values', () => {
73
+ const match = 'aaaaaa';
74
+ const sorted = [
75
+ createCompoundIndexValue(match, 'b'),
76
+ createCompoundIndexValue(match, 'c'),
77
+ createCompoundIndexValue(match, 'd'),
78
+ createCompoundIndexValue(match, 'e'),
79
+ // extra nefarious!
80
+ createCompoundIndexValue(`${match}a`, 'no!'),
81
+ createCompoundIndexValue(`${match} `, 'no!'),
82
+ createCompoundIndexValue(`${match}😎`, 'no!'),
83
+ createCompoundIndexValue(`${match}😎😎`, 'no!'),
84
+ createCompoundIndexValue('aaa', 'no!'),
85
+ createCompoundIndexValue(`${match}a😎no!`, 'no!'),
86
+ ].sort();
87
+ const boundedBlock = sorted.filter((value) => value > createLowerBoundIndexValue(match) &&
88
+ value < createUpperBoundIndexValue(match));
89
+ expect(boundedBlock).toEqual([
90
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}b`,
91
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}c`,
92
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}d`,
93
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}e`,
94
+ ]);
95
+ });
96
+ it('will be bounded by upper and lower bounds when matching multiple sections', () => {
97
+ const match1 = 'aaaaaa';
98
+ const match2 = 'bbbbbb';
99
+ const sorted = [
100
+ createCompoundIndexValue(match1, match2, 'b'),
101
+ createCompoundIndexValue(match1, match2, 'c'),
102
+ createCompoundIndexValue(match1, match2, 'd'),
103
+ createCompoundIndexValue(match1, match2, 'e'),
104
+ // extra nefarious!
105
+ createCompoundIndexValue(`${match1}a`, match2, 'no!'),
106
+ createCompoundIndexValue(`${match1} `, match2, 'no!'),
107
+ createCompoundIndexValue(`${match1}😎`, match2, 'no!'),
108
+ createCompoundIndexValue(`${match1}😎😎`, match2, 'no!'),
109
+ createCompoundIndexValue('aaa', match2, 'no!'),
110
+ createCompoundIndexValue(`${match1}a😎no!`, match2, 'no!'),
111
+ createCompoundIndexValue(match1, `${match2}a😎no!`, 'no!'),
112
+ createCompoundIndexValue(match1, `${match2} `, 'no!'),
113
+ createCompoundIndexValue(match1, ' ', 'no!'),
114
+ createCompoundIndexValue(match1, '\u00001', 'no!'),
115
+ createCompoundIndexValue(match1, '\uE007f', 'no!'),
116
+ ].sort();
117
+ expect(sorted.filter((value) => value > createLowerBoundIndexValue(match1, match2) &&
118
+ value < createUpperBoundIndexValue(match1, match2))).toEqual([
119
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}b`,
120
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}c`,
121
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}d`,
122
+ `aaaaaa${COMPOUND_INDEX_SEPARATOR}bbbbbb${COMPOUND_INDEX_SEPARATOR}e`,
123
+ ]);
124
+ });
125
+ });
126
126
  //# sourceMappingURL=indexes.test.js.map