forked from indexeddbshim/IndexedDBShim
/
DOMStringList.js
156 lines (148 loc) · 4.87 KB
/
DOMStringList.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
let cleanInterface = false;
const testObject = {test: true};
// Test whether Object.defineProperty really works.
if (Object.defineProperty) {
try {
Object.defineProperty(testObject, 'test', { enumerable: false });
if (testObject.test) {
cleanInterface = true;
}
} catch (e) {
// Object.defineProperty does not work as intended.
}
}
/**
* Shim the DOMStringList object.
*
*/
const DOMStringList = function () {
throw new TypeError('Illegal constructor');
};
DOMStringList.prototype = {
constructor: DOMStringList,
// Interface.
contains: function (str) {
if (!arguments.length) {
throw new TypeError('DOMStringList.contains must be supplied a value');
}
return this._items.includes(str);
},
item: function (key) {
if (!arguments.length) {
throw new TypeError('DOMStringList.item must be supplied a value');
}
if (key < 0 || key >= this.length || !Number.isInteger(key)) {
return null;
}
return this._items[key];
},
// Helpers. Should only be used internally.
clone: function () {
const stringList = DOMStringList.__createInstance();
stringList._items = this._items.slice();
stringList._length = this.length;
stringList.addIndexes();
return stringList;
},
addIndexes: function () {
for (let i = 0; i < this._items.length; i++) {
this[i] = this._items[i];
}
},
sortList: function () {
// http://w3c.github.io/IndexedDB/#sorted-list
// https://tc39.github.io/ecma262/#sec-abstract-relational-comparison
this._items.sort();
this.addIndexes();
return this._items;
},
forEach: function (cb, thisArg) {
this._items.forEach(cb, thisArg);
},
map: function (cb, thisArg) {
return this._items.map(cb, thisArg);
},
indexOf: function (str) {
return this._items.indexOf(str);
},
push: function (item) {
this._items.push(item);
this._length++;
this.sortList();
},
splice: function (...args /* index, howmany, item1, ..., itemX */) {
this._items.splice(...args);
this._length = this._items.length;
for (const i in this) {
if (i === String(parseInt(i, 10))) {
delete this[i];
}
}
this.sortList();
},
[Symbol.toStringTag]: 'DOMStringListPrototype',
// At least because `DOMStringList`, as a [list](https://infra.spec.whatwg.org/#list)
// can be converted to a sequence per https://infra.spec.whatwg.org/#list-iterate
// and particularly as some methods, e.g., `IDBDatabase.transaction`
// expect such sequence<DOMString> (or DOMString), we need an iterator (some of
// the Mocha tests rely on these)
[Symbol.iterator]: function * () {
let i = 0;
while (i < this._items.length) {
yield this._items[i++];
}
}
};
Object.defineProperty(DOMStringList, Symbol.hasInstance, {
value: function (obj) {
return ({}.toString.call(obj) === 'DOMStringListPrototype');
}
});
const DOMStringListAlias = DOMStringList;
Object.defineProperty(DOMStringList, '__createInstance', {
value: function () {
const DOMStringList = function DOMStringList () {
this.toString = function () {
return '[object DOMStringList]';
};
// Internal functions on the prototype have been made non-enumerable below.
Object.defineProperty(this, 'length', {
enumerable: true,
get: function () {
return this._length;
}
});
this._items = [];
this._length = 0;
};
DOMStringList.prototype = DOMStringListAlias.prototype;
return new DOMStringList();
}
});
if (cleanInterface) {
Object.defineProperty(DOMStringList, 'prototype', {
writable: false
});
const nonenumerableReadonly = ['addIndexes', 'sortList', 'forEach', 'map', 'indexOf', 'push', 'splice', 'constructor', '__createInstance'];
nonenumerableReadonly.forEach((nonenumerableReadonly) => {
Object.defineProperty(DOMStringList.prototype, nonenumerableReadonly, {
enumerable: false
});
});
// Illegal invocations
Object.defineProperty(DOMStringList.prototype, 'length', {
configurable: true,
enumerable: true,
get: function () {
throw new TypeError('Illegal invocation');
}
});
const nonenumerableWritable = ['_items', '_length'];
nonenumerableWritable.forEach((nonenumerableWritable) => {
Object.defineProperty(DOMStringList.prototype, nonenumerableWritable, {
enumerable: false,
writable: true
});
});
}
export default DOMStringList;