forked from indexeddbshim/IndexedDBShim
/
IDBKeyRange.js
160 lines (149 loc) · 6.24 KB
/
IDBKeyRange.js
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
import {createDOMException} from './DOMException';
import * as Key from './Key';
import * as util from './util';
const readonlyProperties = ['lower', 'upper', 'lowerOpen', 'upperOpen'];
/**
* The IndexedDB KeyRange object
* http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html#dfn-key-range
* @param {Object} lower
* @param {Object} upper
* @param {Object} lowerOpen
* @param {Object} upperOpen
*/
function IDBKeyRange () {
throw new TypeError('Illegal constructor');
}
const IDBKeyRangeAlias = IDBKeyRange;
IDBKeyRange.__createInstance = function (lower, upper, lowerOpen, upperOpen) {
function IDBKeyRange () {
this[Symbol.toStringTag] = 'IDBKeyRange';
if (lower === undefined && upper === undefined) {
throw createDOMException('DataError', 'Both arguments to the key range method cannot be undefined');
}
let lowerConverted, upperConverted;
if (lower !== undefined) {
lowerConverted = Key.roundTrip(lower); // Todo: does this make the "conversions" redundant
Key.convertValueToKeyRethrowingAndIfInvalid(lower);
}
if (upper !== undefined) {
upperConverted = Key.roundTrip(upper); // Todo: does this make the "conversions" redundant
Key.convertValueToKeyRethrowingAndIfInvalid(upper);
}
if (lower !== undefined && upper !== undefined && lower !== upper) {
if (Key.encode(lower) > Key.encode(upper)) {
throw createDOMException('DataError', '`lower` must not be greater than `upper` argument in `bound()` call.');
}
}
this.__lower = lowerConverted;
this.__upper = upperConverted;
this.__lowerOpen = !!lowerOpen;
this.__upperOpen = !!upperOpen;
}
IDBKeyRange.prototype = IDBKeyRangeAlias.prototype;
return new IDBKeyRange();
};
IDBKeyRange.prototype.includes = function (key) {
// We can't do a regular instanceof check as it will create a loop given our hasInstance implementation
if (!util.isObj(this) || typeof this.__lowerOpen !== 'boolean') {
throw new TypeError('Illegal invocation');
}
if (!arguments.length) {
throw new TypeError('IDBKeyRange.includes requires a key argument');
}
Key.convertValueToKeyRethrowingAndIfInvalid(key);
return Key.isKeyInRange(key, this);
};
IDBKeyRange.only = function (value) {
if (!arguments.length) {
throw new TypeError('IDBKeyRange.only requires a value argument');
}
return IDBKeyRange.__createInstance(value, value, false, false);
};
IDBKeyRange.lowerBound = function (value /*, open */) {
if (!arguments.length) {
throw new TypeError('IDBKeyRange.lowerBound requires a value argument');
}
return IDBKeyRange.__createInstance(value, undefined, arguments[1], true);
};
IDBKeyRange.upperBound = function (value /*, open */) {
if (!arguments.length) {
throw new TypeError('IDBKeyRange.upperBound requires a value argument');
}
return IDBKeyRange.__createInstance(undefined, value, true, arguments[1]);
};
IDBKeyRange.bound = function (lower, upper /* , lowerOpen, upperOpen */) {
if (arguments.length <= 1) {
throw new TypeError('IDBKeyRange.bound requires lower and upper arguments');
}
return IDBKeyRange.__createInstance(lower, upper, arguments[2], arguments[3]);
};
IDBKeyRange.prototype[Symbol.toStringTag] = 'IDBKeyRangePrototype';
readonlyProperties.forEach((prop) => {
Object.defineProperty(IDBKeyRange.prototype, '__' + prop, {
enumerable: false,
configurable: false,
writable: true
});
Object.defineProperty(IDBKeyRange.prototype, prop, {
enumerable: true,
configurable: true,
get: function () {
// We can't do a regular instanceof check as it will create a loop given our hasInstance implementation
if (!util.isObj(this) || typeof this.__lowerOpen !== 'boolean') {
throw new TypeError('Illegal invocation');
}
return this['__' + prop];
}
});
});
Object.defineProperty(IDBKeyRange, Symbol.hasInstance, {
value: obj => util.isObj(obj) && 'upper' in obj && typeof obj.lowerOpen === 'boolean'
});
Object.defineProperty(IDBKeyRange, 'prototype', {
writable: false
});
function setSQLForKeyRange (range, quotedKeyColumnName, sql, sqlValues, addAnd, checkCached) {
if (range && (range.lower !== undefined || range.upper !== undefined)) {
if (addAnd) sql.push('AND');
let encodedLowerKey, encodedUpperKey;
const hasLower = range.lower !== undefined;
const hasUpper = range.upper !== undefined;
if (hasLower) {
encodedLowerKey = checkCached ? range.__lowerCached : Key.encode(range.lower);
}
if (hasUpper) {
encodedUpperKey = checkCached ? range.__upperCached : Key.encode(range.upper);
}
if (hasLower) {
sqlValues.push(util.escapeSQLiteStatement(encodedLowerKey));
if (hasUpper && encodedLowerKey === encodedUpperKey && !range.lowerOpen && !range.upperOpen) {
sql.push(quotedKeyColumnName, '=', '?');
return;
}
sql.push(quotedKeyColumnName, (range.lowerOpen ? '>' : '>='), '?');
}
(hasLower && hasUpper) && sql.push('AND');
if (hasUpper) {
sql.push(quotedKeyColumnName, (range.upperOpen ? '<' : '<='), '?');
sqlValues.push(util.escapeSQLiteStatement(encodedUpperKey));
}
}
}
function convertValueToKeyRange (value, nullDisallowed) {
if (util.instanceOf(value, IDBKeyRange)) {
// We still need to validate IDBKeyRange-like objects (the above check is based on loose duck-typing)
if (!value.toString() !== '[object IDBKeyRange]') {
return IDBKeyRange.__createInstance(value.lower, value.upper, value.lowerOpen, value.upperOpen);
}
return value;
}
if (value == null) {
if (nullDisallowed) {
throw createDOMException('DataError', 'No key or range was specified');
}
return undefined; // Represents unbounded
}
Key.convertValueToKeyRethrowingAndIfInvalid(value);
return IDBKeyRange.only(value);
}
export {setSQLForKeyRange, IDBKeyRange, convertValueToKeyRange, IDBKeyRange as default};