fake-indexeddb 4.0.2 → 5.0.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/README.md +0 -6
- package/build/cjs/FDBCursor.js +39 -159
- package/build/cjs/FDBCursorWithValue.js +1 -9
- package/build/cjs/FDBDatabase.js +9 -72
- package/build/cjs/FDBFactory.js +23 -66
- package/build/cjs/FDBIndex.js +17 -73
- package/build/cjs/FDBKeyRange.js +3 -28
- package/build/cjs/FDBObjectStore.js +25 -145
- package/build/cjs/FDBOpenDBRequest.js +1 -8
- package/build/cjs/FDBRequest.js +1 -15
- package/build/cjs/FDBTransaction.js +26 -64
- package/build/cjs/FDBVersionChangeEvent.js +1 -8
- package/build/cjs/fakeIndexedDB.js +1 -5
- package/build/cjs/index.js +1 -16
- package/build/cjs/lib/Database.js +1 -11
- package/build/cjs/lib/FakeDOMStringList.js +5 -13
- package/build/cjs/lib/FakeEvent.js +3 -10
- package/build/cjs/lib/FakeEventTarget.js +15 -32
- package/build/cjs/lib/Index.js +14 -46
- package/build/cjs/lib/KeyGenerator.js +3 -12
- package/build/cjs/lib/ObjectStore.js +24 -63
- package/build/cjs/lib/RecordStore.js +4 -42
- package/build/cjs/lib/binarySearch.js +5 -24
- package/build/cjs/lib/canInjectKey.js +2 -14
- package/build/cjs/lib/cmp.js +3 -29
- package/build/cjs/lib/enforceRange.js +2 -6
- package/build/cjs/lib/errors.js +0 -32
- package/build/cjs/lib/extractKey.js +2 -17
- package/build/cjs/lib/scheduling.js +4 -7
- package/build/cjs/lib/validateKeyPath.js +4 -16
- package/build/cjs/lib/valueToKey.js +2 -15
- package/build/cjs/lib/valueToKeyRange.js +1 -11
- package/build/esm/FDBCursor.js +33 -144
- package/build/esm/FDBCursorWithValue.js +0 -5
- package/build/esm/FDBDatabase.js +8 -62
- package/build/esm/FDBFactory.js +22 -54
- package/build/esm/FDBIndex.js +16 -61
- package/build/esm/FDBKeyRange.js +4 -22
- package/build/esm/FDBObjectStore.js +23 -126
- package/build/esm/FDBOpenDBRequest.js +0 -4
- package/build/esm/FDBRequest.js +0 -10
- package/build/esm/FDBTransaction.js +25 -54
- package/build/esm/FDBVersionChangeEvent.js +0 -4
- package/build/esm/lib/Database.js +2 -8
- package/build/esm/lib/FakeDOMStringList.js +4 -9
- package/build/esm/lib/FakeEvent.js +2 -7
- package/build/esm/lib/FakeEventTarget.js +14 -29
- package/build/esm/lib/Index.js +14 -37
- package/build/esm/lib/KeyGenerator.js +2 -8
- package/build/esm/lib/ObjectStore.js +19 -51
- package/build/esm/lib/RecordStore.js +3 -36
- package/build/esm/lib/binarySearch.js +5 -18
- package/build/esm/lib/canInjectKey.js +1 -11
- package/build/esm/lib/cmp.js +2 -24
- package/build/esm/lib/enforceRange.js +1 -3
- package/build/esm/lib/errors.js +0 -11
- package/build/esm/lib/extractKey.js +3 -13
- package/build/esm/lib/scheduling.js +3 -3
- package/build/esm/lib/validateKeyPath.js +3 -13
- package/build/esm/lib/valueToKey.js +1 -12
- package/build/esm/lib/valueToKeyRange.js +2 -5
- package/package.json +30 -28
- package/build/cjs/lib/structuredClone.js +0 -30
- package/build/esm/lib/structuredClone.js +0 -18
|
@@ -1,28 +1,22 @@
|
|
|
1
1
|
import { ConstraintError } from "./errors.js";
|
|
2
2
|
const MAX_KEY = 9007199254740992;
|
|
3
|
-
|
|
4
3
|
class KeyGenerator {
|
|
5
4
|
// This is kind of wrong. Should start at 1 and increment only after record is saved
|
|
6
5
|
num = 0;
|
|
7
|
-
|
|
8
6
|
next() {
|
|
9
7
|
if (this.num >= MAX_KEY) {
|
|
10
8
|
throw new ConstraintError();
|
|
11
9
|
}
|
|
12
|
-
|
|
13
10
|
this.num += 1;
|
|
14
11
|
return this.num;
|
|
15
|
-
}
|
|
16
|
-
|
|
12
|
+
}
|
|
17
13
|
|
|
14
|
+
// https://w3c.github.io/IndexedDB/#possibly-update-the-key-generator
|
|
18
15
|
setIfLarger(num) {
|
|
19
16
|
const value = Math.floor(Math.min(num, MAX_KEY)) - 1;
|
|
20
|
-
|
|
21
17
|
if (value >= this.num) {
|
|
22
18
|
this.num = value + 1;
|
|
23
19
|
}
|
|
24
20
|
}
|
|
25
|
-
|
|
26
21
|
}
|
|
27
|
-
|
|
28
22
|
export default KeyGenerator;
|
|
@@ -2,14 +2,11 @@ import { ConstraintError, DataError } from "./errors.js";
|
|
|
2
2
|
import extractKey from "./extractKey.js";
|
|
3
3
|
import KeyGenerator from "./KeyGenerator.js";
|
|
4
4
|
import RecordStore from "./RecordStore.js";
|
|
5
|
-
import structuredClone from "./structuredClone.js";
|
|
6
|
-
|
|
7
5
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-object-store
|
|
8
6
|
class ObjectStore {
|
|
9
7
|
deleted = false;
|
|
10
8
|
records = new RecordStore();
|
|
11
9
|
rawIndexes = new Map();
|
|
12
|
-
|
|
13
10
|
constructor(rawDatabase, name, keyPath, autoIncrement) {
|
|
14
11
|
this.rawDatabase = rawDatabase;
|
|
15
12
|
this.keyGenerator = autoIncrement === true ? new KeyGenerator() : null;
|
|
@@ -17,68 +14,58 @@ class ObjectStore {
|
|
|
17
14
|
this.name = name;
|
|
18
15
|
this.keyPath = keyPath;
|
|
19
16
|
this.autoIncrement = autoIncrement;
|
|
20
|
-
}
|
|
21
|
-
|
|
17
|
+
}
|
|
22
18
|
|
|
19
|
+
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-retrieving-a-value-from-an-object-store
|
|
23
20
|
getKey(key) {
|
|
24
21
|
const record = this.records.get(key);
|
|
25
22
|
return record !== undefined ? structuredClone(record.key) : undefined;
|
|
26
|
-
}
|
|
27
|
-
|
|
23
|
+
}
|
|
28
24
|
|
|
25
|
+
// http://w3c.github.io/IndexedDB/#retrieve-multiple-keys-from-an-object-store
|
|
29
26
|
getAllKeys(range, count) {
|
|
30
27
|
if (count === undefined || count === 0) {
|
|
31
28
|
count = Infinity;
|
|
32
29
|
}
|
|
33
|
-
|
|
34
30
|
const records = [];
|
|
35
|
-
|
|
36
31
|
for (const record of this.records.values(range)) {
|
|
37
32
|
records.push(structuredClone(record.key));
|
|
38
|
-
|
|
39
33
|
if (records.length >= count) {
|
|
40
34
|
break;
|
|
41
35
|
}
|
|
42
36
|
}
|
|
43
|
-
|
|
44
37
|
return records;
|
|
45
|
-
}
|
|
46
|
-
|
|
38
|
+
}
|
|
47
39
|
|
|
40
|
+
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-retrieving-a-value-from-an-object-store
|
|
48
41
|
getValue(key) {
|
|
49
42
|
const record = this.records.get(key);
|
|
50
43
|
return record !== undefined ? structuredClone(record.value) : undefined;
|
|
51
|
-
}
|
|
52
|
-
|
|
44
|
+
}
|
|
53
45
|
|
|
46
|
+
// http://w3c.github.io/IndexedDB/#retrieve-multiple-values-from-an-object-store
|
|
54
47
|
getAllValues(range, count) {
|
|
55
48
|
if (count === undefined || count === 0) {
|
|
56
49
|
count = Infinity;
|
|
57
50
|
}
|
|
58
|
-
|
|
59
51
|
const records = [];
|
|
60
|
-
|
|
61
52
|
for (const record of this.records.values(range)) {
|
|
62
53
|
records.push(structuredClone(record.value));
|
|
63
|
-
|
|
64
54
|
if (records.length >= count) {
|
|
65
55
|
break;
|
|
66
56
|
}
|
|
67
57
|
}
|
|
68
|
-
|
|
69
58
|
return records;
|
|
70
|
-
}
|
|
71
|
-
|
|
59
|
+
}
|
|
72
60
|
|
|
61
|
+
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-storing-a-record-into-an-object-store
|
|
73
62
|
storeRecord(newRecord, noOverwrite, rollbackLog) {
|
|
74
63
|
if (this.keyPath !== null) {
|
|
75
64
|
const key = extractKey(this.keyPath, newRecord.value);
|
|
76
|
-
|
|
77
65
|
if (key !== undefined) {
|
|
78
66
|
newRecord.key = key;
|
|
79
67
|
}
|
|
80
68
|
}
|
|
81
|
-
|
|
82
69
|
if (this.keyGenerator !== null && newRecord.key === undefined) {
|
|
83
70
|
if (rollbackLog) {
|
|
84
71
|
const keyGeneratorBefore = this.keyGenerator.num;
|
|
@@ -88,78 +75,64 @@ class ObjectStore {
|
|
|
88
75
|
}
|
|
89
76
|
});
|
|
90
77
|
}
|
|
78
|
+
newRecord.key = this.keyGenerator.next();
|
|
91
79
|
|
|
92
|
-
|
|
80
|
+
// Set in value if keyPath defiend but led to no key
|
|
93
81
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-to-assign-a-key-to-a-value-using-a-key-path
|
|
94
|
-
|
|
95
82
|
if (this.keyPath !== null) {
|
|
96
83
|
if (Array.isArray(this.keyPath)) {
|
|
97
84
|
throw new Error("Cannot have an array key path in an object store with a key generator");
|
|
98
85
|
}
|
|
99
|
-
|
|
100
86
|
let remainingKeyPath = this.keyPath;
|
|
101
87
|
let object = newRecord.value;
|
|
102
88
|
let identifier;
|
|
103
89
|
let i = 0; // Just to run the loop at least once
|
|
104
|
-
|
|
105
90
|
while (i >= 0) {
|
|
106
91
|
if (typeof object !== "object") {
|
|
107
92
|
throw new DataError();
|
|
108
93
|
}
|
|
109
|
-
|
|
110
94
|
i = remainingKeyPath.indexOf(".");
|
|
111
|
-
|
|
112
95
|
if (i >= 0) {
|
|
113
96
|
identifier = remainingKeyPath.slice(0, i);
|
|
114
97
|
remainingKeyPath = remainingKeyPath.slice(i + 1);
|
|
115
|
-
|
|
116
|
-
if (!object.hasOwnProperty(identifier)) {
|
|
98
|
+
if (!Object.hasOwn(object, identifier)) {
|
|
117
99
|
object[identifier] = {};
|
|
118
100
|
}
|
|
119
|
-
|
|
120
101
|
object = object[identifier];
|
|
121
102
|
}
|
|
122
103
|
}
|
|
123
|
-
|
|
124
104
|
identifier = remainingKeyPath;
|
|
125
105
|
object[identifier] = newRecord.key;
|
|
126
106
|
}
|
|
127
107
|
} else if (this.keyGenerator !== null && typeof newRecord.key === "number") {
|
|
128
108
|
this.keyGenerator.setIfLarger(newRecord.key);
|
|
129
109
|
}
|
|
130
|
-
|
|
131
110
|
const existingRecord = this.records.get(newRecord.key);
|
|
132
|
-
|
|
133
111
|
if (existingRecord) {
|
|
134
112
|
if (noOverwrite) {
|
|
135
113
|
throw new ConstraintError();
|
|
136
114
|
}
|
|
137
|
-
|
|
138
115
|
this.deleteRecord(newRecord.key, rollbackLog);
|
|
139
116
|
}
|
|
140
|
-
|
|
141
117
|
this.records.add(newRecord);
|
|
142
|
-
|
|
143
118
|
if (rollbackLog) {
|
|
144
119
|
rollbackLog.push(() => {
|
|
145
120
|
this.deleteRecord(newRecord.key);
|
|
146
121
|
});
|
|
147
|
-
}
|
|
148
|
-
|
|
122
|
+
}
|
|
149
123
|
|
|
124
|
+
// Update indexes
|
|
150
125
|
for (const rawIndex of this.rawIndexes.values()) {
|
|
151
126
|
if (rawIndex.initialized) {
|
|
152
127
|
rawIndex.storeRecord(newRecord);
|
|
153
128
|
}
|
|
154
129
|
}
|
|
155
|
-
|
|
156
130
|
return newRecord.key;
|
|
157
|
-
}
|
|
158
|
-
|
|
131
|
+
}
|
|
159
132
|
|
|
133
|
+
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-deleting-records-from-an-object-store
|
|
160
134
|
deleteRecord(key, rollbackLog) {
|
|
161
135
|
const deletedRecords = this.records.delete(key);
|
|
162
|
-
|
|
163
136
|
if (rollbackLog) {
|
|
164
137
|
for (const record of deletedRecords) {
|
|
165
138
|
rollbackLog.push(() => {
|
|
@@ -167,16 +140,14 @@ class ObjectStore {
|
|
|
167
140
|
});
|
|
168
141
|
}
|
|
169
142
|
}
|
|
170
|
-
|
|
171
143
|
for (const rawIndex of this.rawIndexes.values()) {
|
|
172
144
|
rawIndex.records.deleteByValue(key);
|
|
173
145
|
}
|
|
174
|
-
}
|
|
175
|
-
|
|
146
|
+
}
|
|
176
147
|
|
|
148
|
+
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#dfn-steps-for-clearing-an-object-store
|
|
177
149
|
clear(rollbackLog) {
|
|
178
150
|
const deletedRecords = this.records.clear();
|
|
179
|
-
|
|
180
151
|
if (rollbackLog) {
|
|
181
152
|
for (const record of deletedRecords) {
|
|
182
153
|
rollbackLog.push(() => {
|
|
@@ -184,12 +155,9 @@ class ObjectStore {
|
|
|
184
155
|
});
|
|
185
156
|
}
|
|
186
157
|
}
|
|
187
|
-
|
|
188
158
|
for (const rawIndex of this.rawIndexes.values()) {
|
|
189
159
|
rawIndex.records.clear();
|
|
190
160
|
}
|
|
191
161
|
}
|
|
192
|
-
|
|
193
162
|
}
|
|
194
|
-
|
|
195
163
|
export default ObjectStore;
|
|
@@ -1,27 +1,21 @@
|
|
|
1
1
|
import FDBKeyRange from "../FDBKeyRange.js";
|
|
2
2
|
import { getByKey, getByKeyRange, getIndexByKey, getIndexByKeyGTE, getIndexByKeyRange } from "./binarySearch.js";
|
|
3
3
|
import cmp from "./cmp.js";
|
|
4
|
-
|
|
5
4
|
class RecordStore {
|
|
6
5
|
records = [];
|
|
7
|
-
|
|
8
6
|
get(key) {
|
|
9
7
|
if (key instanceof FDBKeyRange) {
|
|
10
8
|
return getByKeyRange(this.records, key);
|
|
11
9
|
}
|
|
12
|
-
|
|
13
10
|
return getByKey(this.records, key);
|
|
14
11
|
}
|
|
15
|
-
|
|
16
12
|
add(newRecord) {
|
|
17
13
|
// Find where to put it so it's sorted by key
|
|
18
14
|
let i;
|
|
19
|
-
|
|
20
15
|
if (this.records.length === 0) {
|
|
21
16
|
i = 0;
|
|
22
17
|
} else {
|
|
23
18
|
i = getIndexByKeyGTE(this.records, newRecord.key);
|
|
24
|
-
|
|
25
19
|
if (i === -1) {
|
|
26
20
|
// If no matching key, add to end
|
|
27
21
|
i = this.records.length;
|
|
@@ -32,7 +26,6 @@ class RecordStore {
|
|
|
32
26
|
// Record value >= newRecord value, so insert here
|
|
33
27
|
break;
|
|
34
28
|
}
|
|
35
|
-
|
|
36
29
|
i += 1; // Look at next record
|
|
37
30
|
}
|
|
38
31
|
}
|
|
@@ -40,95 +33,74 @@ class RecordStore {
|
|
|
40
33
|
|
|
41
34
|
this.records.splice(i, 0, newRecord);
|
|
42
35
|
}
|
|
43
|
-
|
|
44
36
|
delete(key) {
|
|
45
37
|
const deletedRecords = [];
|
|
46
38
|
const isRange = key instanceof FDBKeyRange;
|
|
47
|
-
|
|
48
39
|
while (true) {
|
|
49
40
|
const idx = isRange ? getIndexByKeyRange(this.records, key) : getIndexByKey(this.records, key);
|
|
50
|
-
|
|
51
41
|
if (idx === -1) {
|
|
52
42
|
break;
|
|
53
43
|
}
|
|
54
|
-
|
|
55
44
|
deletedRecords.push(this.records[idx]);
|
|
56
45
|
this.records.splice(idx, 1);
|
|
57
46
|
}
|
|
58
|
-
|
|
59
47
|
return deletedRecords;
|
|
60
48
|
}
|
|
61
|
-
|
|
62
49
|
deleteByValue(key) {
|
|
63
50
|
const range = key instanceof FDBKeyRange ? key : FDBKeyRange.only(key);
|
|
64
51
|
const deletedRecords = [];
|
|
65
52
|
this.records = this.records.filter(record => {
|
|
66
53
|
const shouldDelete = range.includes(record.value);
|
|
67
|
-
|
|
68
54
|
if (shouldDelete) {
|
|
69
55
|
deletedRecords.push(record);
|
|
70
56
|
}
|
|
71
|
-
|
|
72
57
|
return !shouldDelete;
|
|
73
58
|
});
|
|
74
59
|
return deletedRecords;
|
|
75
60
|
}
|
|
76
|
-
|
|
77
61
|
clear() {
|
|
78
62
|
const deletedRecords = this.records.slice();
|
|
79
63
|
this.records = [];
|
|
80
64
|
return deletedRecords;
|
|
81
65
|
}
|
|
82
|
-
|
|
83
66
|
values(range, direction = "next") {
|
|
84
67
|
return {
|
|
85
68
|
[Symbol.iterator]: () => {
|
|
86
69
|
let i;
|
|
87
|
-
|
|
88
70
|
if (direction === "next") {
|
|
89
71
|
i = 0;
|
|
90
|
-
|
|
91
72
|
if (range !== undefined && range.lower !== undefined) {
|
|
92
73
|
while (this.records[i] !== undefined) {
|
|
93
74
|
const cmpResult = cmp(this.records[i].key, range.lower);
|
|
94
|
-
|
|
95
75
|
if (cmpResult === 1 || cmpResult === 0 && !range.lowerOpen) {
|
|
96
76
|
break;
|
|
97
77
|
}
|
|
98
|
-
|
|
99
78
|
i += 1;
|
|
100
79
|
}
|
|
101
80
|
}
|
|
102
81
|
} else {
|
|
103
82
|
i = this.records.length - 1;
|
|
104
|
-
|
|
105
83
|
if (range !== undefined && range.upper !== undefined) {
|
|
106
84
|
while (this.records[i] !== undefined) {
|
|
107
85
|
const cmpResult = cmp(this.records[i].key, range.upper);
|
|
108
|
-
|
|
109
86
|
if (cmpResult === -1 || cmpResult === 0 && !range.upperOpen) {
|
|
110
87
|
break;
|
|
111
88
|
}
|
|
112
|
-
|
|
113
89
|
i -= 1;
|
|
114
90
|
}
|
|
115
91
|
}
|
|
116
92
|
}
|
|
117
|
-
|
|
118
93
|
return {
|
|
119
94
|
next: () => {
|
|
120
95
|
let done;
|
|
121
96
|
let value;
|
|
122
|
-
|
|
123
97
|
if (direction === "next") {
|
|
124
98
|
value = this.records[i];
|
|
125
99
|
done = i >= this.records.length;
|
|
126
100
|
i += 1;
|
|
127
|
-
|
|
128
101
|
if (!done && range !== undefined && range.upper !== undefined) {
|
|
129
102
|
const cmpResult = cmp(value.key, range.upper);
|
|
130
103
|
done = cmpResult === 1 || cmpResult === 0 && range.upperOpen;
|
|
131
|
-
|
|
132
104
|
if (done) {
|
|
133
105
|
value = undefined;
|
|
134
106
|
}
|
|
@@ -137,21 +109,18 @@ class RecordStore {
|
|
|
137
109
|
value = this.records[i];
|
|
138
110
|
done = i < 0;
|
|
139
111
|
i -= 1;
|
|
140
|
-
|
|
141
112
|
if (!done && range !== undefined && range.lower !== undefined) {
|
|
142
113
|
const cmpResult = cmp(value.key, range.lower);
|
|
143
114
|
done = cmpResult === -1 || cmpResult === 0 && range.lowerOpen;
|
|
144
|
-
|
|
145
115
|
if (done) {
|
|
146
116
|
value = undefined;
|
|
147
117
|
}
|
|
148
118
|
}
|
|
149
|
-
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The weird "as IteratorResult<Record>" is needed because of
|
|
150
122
|
// https://github.com/Microsoft/TypeScript/issues/11375 and
|
|
151
123
|
// https://github.com/Microsoft/TypeScript/issues/2983
|
|
152
|
-
// tslint:disable-next-line no-object-literal-type-assertion
|
|
153
|
-
|
|
154
|
-
|
|
155
124
|
return {
|
|
156
125
|
done,
|
|
157
126
|
value
|
|
@@ -161,7 +130,5 @@ class RecordStore {
|
|
|
161
130
|
}
|
|
162
131
|
};
|
|
163
132
|
}
|
|
164
|
-
|
|
165
133
|
}
|
|
166
|
-
|
|
167
134
|
export default RecordStore;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import cmp from "./cmp.js";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* Classic binary search implementation. Returns the index where the key
|
|
5
4
|
* should be inserted, assuming the records list is ordered.
|
|
@@ -8,80 +7,68 @@ function binarySearch(records, key) {
|
|
|
8
7
|
let low = 0;
|
|
9
8
|
let high = records.length;
|
|
10
9
|
let mid;
|
|
11
|
-
|
|
12
10
|
while (low < high) {
|
|
13
|
-
// tslint:disable-next-line:no-bitwise
|
|
14
11
|
mid = low + high >>> 1; // like Math.floor((low + high) / 2) but fast
|
|
15
|
-
|
|
16
12
|
if (cmp(records[mid].key, key) < 0) {
|
|
17
13
|
low = mid + 1;
|
|
18
14
|
} else {
|
|
19
15
|
high = mid;
|
|
20
16
|
}
|
|
21
17
|
}
|
|
22
|
-
|
|
23
18
|
return low;
|
|
24
19
|
}
|
|
20
|
+
|
|
25
21
|
/**
|
|
26
22
|
* Equivalent to `records.findIndex(record => cmp(record.key, key) === 0)`
|
|
27
23
|
*/
|
|
28
|
-
|
|
29
|
-
|
|
30
24
|
export function getIndexByKey(records, key) {
|
|
31
25
|
const idx = binarySearch(records, key);
|
|
32
26
|
const record = records[idx];
|
|
33
|
-
|
|
34
27
|
if (record && cmp(record.key, key) === 0) {
|
|
35
28
|
return idx;
|
|
36
29
|
}
|
|
37
|
-
|
|
38
30
|
return -1;
|
|
39
31
|
}
|
|
32
|
+
|
|
40
33
|
/**
|
|
41
34
|
* Equivalent to `records.find(record => cmp(record.key, key) === 0)`
|
|
42
35
|
*/
|
|
43
|
-
|
|
44
36
|
export function getByKey(records, key) {
|
|
45
37
|
const idx = getIndexByKey(records, key);
|
|
46
38
|
return records[idx];
|
|
47
39
|
}
|
|
40
|
+
|
|
48
41
|
/**
|
|
49
42
|
* Equivalent to `records.findIndex(record => key.includes(record.key))`
|
|
50
43
|
*/
|
|
51
|
-
|
|
52
44
|
export function getIndexByKeyRange(records, keyRange) {
|
|
53
45
|
const lowerIdx = typeof keyRange.lower === "undefined" ? 0 : binarySearch(records, keyRange.lower);
|
|
54
46
|
const upperIdx = typeof keyRange.upper === "undefined" ? records.length - 1 : binarySearch(records, keyRange.upper);
|
|
55
|
-
|
|
56
47
|
for (let i = lowerIdx; i <= upperIdx; i++) {
|
|
57
48
|
const record = records[i];
|
|
58
|
-
|
|
59
49
|
if (record && keyRange.includes(record.key)) {
|
|
60
50
|
return i;
|
|
61
51
|
}
|
|
62
52
|
}
|
|
63
|
-
|
|
64
53
|
return -1;
|
|
65
54
|
}
|
|
55
|
+
|
|
66
56
|
/**
|
|
67
57
|
* Equivalent to `records.find(record => key.includes(record.key))`
|
|
68
58
|
*/
|
|
69
|
-
|
|
70
59
|
export function getByKeyRange(records, keyRange) {
|
|
71
60
|
const idx = getIndexByKeyRange(records, keyRange);
|
|
72
61
|
return records[idx];
|
|
73
62
|
}
|
|
63
|
+
|
|
74
64
|
/**
|
|
75
65
|
* Equivalent to `records.findIndex(record => cmp(record.key, key) >= 0)`
|
|
76
66
|
*/
|
|
77
|
-
|
|
78
67
|
export function getIndexByKeyGTE(records, key) {
|
|
79
68
|
const idx = binarySearch(records, key);
|
|
80
69
|
const record = records[idx];
|
|
81
|
-
|
|
82
70
|
if (record && cmp(record.key, key) >= 0) {
|
|
83
71
|
return idx;
|
|
84
72
|
}
|
|
85
|
-
|
|
86
73
|
return -1;
|
|
87
74
|
}
|
|
@@ -1,33 +1,23 @@
|
|
|
1
1
|
// http://w3c.github.io/IndexedDB/#check-that-a-key-could-be-injected-into-a-value
|
|
2
2
|
const canInjectKey = (keyPath, value) => {
|
|
3
3
|
if (Array.isArray(keyPath)) {
|
|
4
|
-
// tslint:disable-next-line max-line-length
|
|
5
4
|
throw new Error("The key paths used in this section are always strings and never sequences, since it is not possible to create a object store which has a key generator and also has a key path that is a sequence.");
|
|
6
5
|
}
|
|
7
|
-
|
|
8
6
|
const identifiers = keyPath.split(".");
|
|
9
|
-
|
|
10
7
|
if (identifiers.length === 0) {
|
|
11
8
|
throw new Error("Assert: identifiers is not empty");
|
|
12
9
|
}
|
|
13
|
-
|
|
14
10
|
identifiers.pop();
|
|
15
|
-
|
|
16
11
|
for (const identifier of identifiers) {
|
|
17
12
|
if (typeof value !== "object" && !Array.isArray(value)) {
|
|
18
13
|
return false;
|
|
19
14
|
}
|
|
20
|
-
|
|
21
|
-
const hop = value.hasOwnProperty(identifier);
|
|
22
|
-
|
|
15
|
+
const hop = Object.hasOwn(value, identifier);
|
|
23
16
|
if (!hop) {
|
|
24
17
|
return true;
|
|
25
18
|
}
|
|
26
|
-
|
|
27
19
|
value = value[identifier];
|
|
28
20
|
}
|
|
29
|
-
|
|
30
21
|
return typeof value === "object" || Array.isArray(value);
|
|
31
22
|
};
|
|
32
|
-
|
|
33
23
|
export default canInjectKey;
|
package/build/esm/lib/cmp.js
CHANGED
|
@@ -1,88 +1,68 @@
|
|
|
1
1
|
import { DataError } from "./errors.js";
|
|
2
2
|
import valueToKey from "./valueToKey.js";
|
|
3
|
-
|
|
4
3
|
const getType = x => {
|
|
5
4
|
if (typeof x === "number") {
|
|
6
5
|
return "Number";
|
|
7
6
|
}
|
|
8
|
-
|
|
9
7
|
if (x instanceof Date) {
|
|
10
8
|
return "Date";
|
|
11
9
|
}
|
|
12
|
-
|
|
13
10
|
if (Array.isArray(x)) {
|
|
14
11
|
return "Array";
|
|
15
12
|
}
|
|
16
|
-
|
|
17
13
|
if (typeof x === "string") {
|
|
18
14
|
return "String";
|
|
19
15
|
}
|
|
20
|
-
|
|
21
16
|
if (x instanceof ArrayBuffer) {
|
|
22
17
|
return "Binary";
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
throw new DataError();
|
|
26
|
-
};
|
|
27
|
-
|
|
20
|
+
};
|
|
28
21
|
|
|
22
|
+
// https://w3c.github.io/IndexedDB/#compare-two-keys
|
|
29
23
|
const cmp = (first, second) => {
|
|
30
24
|
if (second === undefined) {
|
|
31
25
|
throw new TypeError();
|
|
32
26
|
}
|
|
33
|
-
|
|
34
27
|
first = valueToKey(first);
|
|
35
28
|
second = valueToKey(second);
|
|
36
29
|
const t1 = getType(first);
|
|
37
30
|
const t2 = getType(second);
|
|
38
|
-
|
|
39
31
|
if (t1 !== t2) {
|
|
40
32
|
if (t1 === "Array") {
|
|
41
33
|
return 1;
|
|
42
34
|
}
|
|
43
|
-
|
|
44
35
|
if (t1 === "Binary" && (t2 === "String" || t2 === "Date" || t2 === "Number")) {
|
|
45
36
|
return 1;
|
|
46
37
|
}
|
|
47
|
-
|
|
48
38
|
if (t1 === "String" && (t2 === "Date" || t2 === "Number")) {
|
|
49
39
|
return 1;
|
|
50
40
|
}
|
|
51
|
-
|
|
52
41
|
if (t1 === "Date" && t2 === "Number") {
|
|
53
42
|
return 1;
|
|
54
43
|
}
|
|
55
|
-
|
|
56
44
|
return -1;
|
|
57
45
|
}
|
|
58
|
-
|
|
59
46
|
if (t1 === "Binary") {
|
|
60
47
|
first = new Uint8Array(first);
|
|
61
48
|
second = new Uint8Array(second);
|
|
62
49
|
}
|
|
63
|
-
|
|
64
50
|
if (t1 === "Array" || t1 === "Binary") {
|
|
65
51
|
const length = Math.min(first.length, second.length);
|
|
66
|
-
|
|
67
52
|
for (let i = 0; i < length; i++) {
|
|
68
53
|
const result = cmp(first[i], second[i]);
|
|
69
|
-
|
|
70
54
|
if (result !== 0) {
|
|
71
55
|
return result;
|
|
72
56
|
}
|
|
73
57
|
}
|
|
74
|
-
|
|
75
58
|
if (first.length > second.length) {
|
|
76
59
|
return 1;
|
|
77
60
|
}
|
|
78
|
-
|
|
79
61
|
if (first.length < second.length) {
|
|
80
62
|
return -1;
|
|
81
63
|
}
|
|
82
|
-
|
|
83
64
|
return 0;
|
|
84
65
|
}
|
|
85
|
-
|
|
86
66
|
if (t1 === "Date") {
|
|
87
67
|
if (first.getTime() === second.getTime()) {
|
|
88
68
|
return 0;
|
|
@@ -92,8 +72,6 @@ const cmp = (first, second) => {
|
|
|
92
72
|
return 0;
|
|
93
73
|
}
|
|
94
74
|
}
|
|
95
|
-
|
|
96
75
|
return first > second ? 1 : -1;
|
|
97
76
|
};
|
|
98
|
-
|
|
99
77
|
export default cmp;
|
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
// https://heycam.github.io/webidl/#EnforceRange
|
|
2
|
+
|
|
2
3
|
const enforceRange = (num, type) => {
|
|
3
4
|
const min = 0;
|
|
4
5
|
const max = type === "unsigned long" ? 4294967295 : 9007199254740991;
|
|
5
|
-
|
|
6
6
|
if (isNaN(num) || num < min || num > max) {
|
|
7
7
|
throw new TypeError();
|
|
8
8
|
}
|
|
9
|
-
|
|
10
9
|
if (num >= 0) {
|
|
11
10
|
return Math.floor(num);
|
|
12
11
|
}
|
|
13
12
|
};
|
|
14
|
-
|
|
15
13
|
export default enforceRange;
|