Skip to content

Commit 5c85909

Browse files
authored
allow features to have numeric ids (#769)
1 parent 2ad7603 commit 5c85909

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

src/lib/string_set.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
11
function StringSet(items) {
22
this._items = {};
3+
this._nums = {};
34
this._length = items ? items.length : 0;
45
if (!items) return;
56
for (let i = 0, l = items.length; i < l; i++) {
7+
this.add(items[i]);
68
if (items[i] === undefined) continue;
7-
this._items[items[i]] = i;
9+
if (typeof items[i] === 'string') this._items[items[i]] = i;
10+
else this._nums[items[i]] = i;
11+
812
}
913
}
1014

1115
StringSet.prototype.add = function(x) {
12-
this._length = this._items[x] ? this._length : this._length + 1;
13-
this._items[x] = this._items[x] ? this._items[x] : this._length;
16+
if (this.has(x)) return this;
17+
this._length++;
18+
if (typeof x === 'string') this._items[x] = this._length;
19+
else this._nums[x] = this._length;
1420
return this;
1521
};
1622

1723
StringSet.prototype.delete = function(x) {
18-
this._length = this._items[x] ? this._length - 1 : this._length;
24+
if (this.has(x) === false) return this;
25+
this._length--;
1926
delete this._items[x];
27+
delete this._nums[x];
2028
return this;
2129
};
2230

2331
StringSet.prototype.has = function(x) {
24-
return this._items[x] !== undefined;
32+
if (typeof x !== 'string' && typeof x !== 'number') return false;
33+
return this._items[x] !== undefined || this._nums[x] !== undefined;
2534
};
2635

2736
StringSet.prototype.values = function() {
28-
const orderedKeys = Object.keys(this._items).sort((a, b) => this._items[a] - this._items[b]);
29-
return orderedKeys;
37+
const values = [];
38+
Object.keys(this._items).forEach(k => {
39+
values.push({ k: k, v: this._items[k] });
40+
});
41+
Object.keys(this._nums).forEach(k => {
42+
values.push({ k: JSON.parse(k), v: this._nums[k] });
43+
});
44+
45+
return values.sort((a, b) => a.v - b.v).map(a => a.k);
3046
};
3147

3248
StringSet.prototype.clear = function() {
3349
this._length = 0;
3450
this._items = {};
51+
this._nums = {};
3552
return this;
3653
};
3754

test/string_set.test.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ test('StringSet constructor and API', t => {
1313
t.equal(typeof StringSet.prototype.clear, 'function', 'exposes set.clear');
1414
t.equal(Object.keys(StringSet.prototype).filter(k => k[0] !== '_').length, 5, 'no unexpected methods');
1515

16-
const populatedSet = new StringSet(['a', 'b']);
17-
t.deepEqual(populatedSet.values(), ['a', 'b'], 'populated by constructor arg');
16+
const populatedSet = new StringSet(['a', 4, 'b']);
17+
t.deepEqual(populatedSet.values(), ['a', 4, 'b'], 'populated by constructor arg');
1818

1919
t.end();
2020
});
@@ -28,29 +28,35 @@ test('StringSet#add', t => {
2828
t.deepEqual(set.values(), ['a', 'b']);
2929
set.add('a');
3030
t.deepEqual(set.values(), ['a', 'b']);
31+
set.add(3);
32+
t.deepEqual(set.values(), ['a', 'b', 3]);
3133
t.end();
3234
});
3335

3436
test('StringSet#delete', t => {
35-
const subject = ['a', 'b'];
37+
const subject = ['a', 'b', 2];
3638
const set = new StringSet(subject);
3739
set.delete('a');
38-
t.deepEqual(set.values(), ['b']);
40+
t.deepEqual(set.values(), ['b', 2]);
3941
set.delete('a');
40-
t.deepEqual(set.values(), ['b']);
42+
t.deepEqual(set.values(), ['b', 2]);
4143
set.delete();
42-
t.deepEqual(set.values(), ['b']);
44+
t.deepEqual(set.values(), ['b', 2]);
4345
set.delete('b');
46+
t.deepEqual(set.values(), [2]);
47+
set.delete(2);
4448
t.deepEqual(set.values(), []);
45-
t.deepEqual(subject, ['a', 'b'], 'source array not mutated');
49+
t.deepEqual(subject, ['a', 'b', 2], 'source array not mutated');
4650
t.end();
4751
});
4852

4953
test('StringSet#has', t => {
50-
const set = new StringSet(['a', 'b']);
54+
const set = new StringSet(['a', 'b', 2]);
5155
t.equal(set.has('a'), true);
5256
t.equal(set.has('b'), true);
57+
t.equal(set.has(2), true);
5358
t.equal(set.has('c'), false);
59+
t.equal(set.has(4), false);
5460
t.end();
5561
});
5662

0 commit comments

Comments
 (0)