Commit 10b82b95 authored by Chris's avatar Chris

Cleaning up the code

parent b2b9cba3
Pipeline #191 failed with stages
in 5 seconds
...@@ -21,23 +21,38 @@ function BasicSet() { ...@@ -21,23 +21,38 @@ function BasicSet() {
this.add.apply(this, arguments); this.add.apply(this, arguments);
} }
// Usage: /**
// add(key) * Parses input in the form of single values, arrays of values, and any
// add(key1, key2, key3, ...) * number/combination of those. Arrays of values must be 1 dimensional.
// add([key1, key2, key3, ...]) *
// add(key1, [key2, key3, key4], ...) * Returns an array of all the values of the input, without duplicates.
BasicSet.prototype.add = function() { */
BasicSet.prototype._mixedInputToArray = function() {
var arr = {};
var key; var key;
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
key = arguments[i]; key = arguments[i];
if (Array.isArray(key)) { if (Array.isArray(key)) {
for (var j = 0; j < key.length; j++) { for (var j = 0; j < key.length; j++) {
this._data[key[j]] = key[j]; arr[key[j]] = key[j];
} }
} else { } else {
this._data[key] = key; arr[key] = key;
} }
} }
return Object.keys(arr).map(function (key) { return arr[key]; });
};
// Usage:
// add(key)
// add(key1, key2, key3, ...)
// add([key1, key2, key3, ...])
// add(key1, [key2, key3, key4], ...)
BasicSet.prototype.add = function() {
var input = this._mixedInputToArray.apply(this, arguments);
for (var i = 0; i < input.length; i++) {
this._data[input[i]] = input[i];
}
return this; return this;
}; };
...@@ -47,65 +62,25 @@ BasicSet.prototype.add = function() { ...@@ -47,65 +62,25 @@ BasicSet.prototype.add = function() {
// remove([key1, key2, key3, ...]) // remove([key1, key2, key3, ...])
// remove(key1, [key2, key3, key4], ...) // remove(key1, [key2, key3, key4], ...)
BasicSet.prototype.remove = function() { BasicSet.prototype.remove = function() {
var item; var input = this._mixedInputToArray.apply(this, arguments);
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < input.length; i++) {
item = arguments[i]; delete this._data[input[i]];
if (Array.isArray(item)) {
for (var j = 0; j < item.length; j++) {
delete this._data[item[j]];
}
} else {
delete this._data[item];
}
} }
return this; return this;
}; };
BasicSet.prototype.has = function(key) { BasicSet.prototype.has = function(key) {
// return Object.prototype.hasOwnProperty.call(this._data, key);
return this._data[key] !== undefined; return this._data[key] !== undefined;
}; };
BasicSet.prototype.hasSome = function() { BasicSet.prototype.hasSome = function() {
var key; var input = this._mixedInputToArray.apply(this, arguments);
for (var i = 0; i < arguments.length; i++) { return this.getIntersection(input).length > 0;
key = arguments[i];
if (Array.isArray(key)) {
for (var j = 0; j < key.length; j++) {
//if (Object.prototype.hasOwnProperty.call(this._data, key[j])) {
if (this._data[key[j]] !== undefined) {
return true;
}
}
} else {
//if (Object.prototype.hasOwnProperty.call(this._data, key)) {
if (this._data[key] !== undefined) {
return true;
}
}
}
return false;
}; };
BasicSet.prototype.hasAll = function() { BasicSet.prototype.hasAll = function() {
var key; var input = this._mixedInputToArray.apply(this, arguments);
for (var i = 0; i < arguments.length; i++) { return input.length === this.getIntersection(input).length;
key = arguments[i];
if (Array.isArray(key)) {
for (var j = 0; j < key.length; j++) {
//if (!Object.prototype.hasOwnProperty.call(this._data, key[j])) {
if (this._data[key[j]] === undefined) {
return false;
}
}
} else {
//if (!Object.prototype.hasOwnProperty.call(this._data, key)) {
if (this._data[key] === undefined) {
return false;
}
}
}
return true;
}; };
BasicSet.prototype.getAll = function() { BasicSet.prototype.getAll = function() {
...@@ -162,9 +137,5 @@ BasicSet.prototype.clear = function() { ...@@ -162,9 +137,5 @@ BasicSet.prototype.clear = function() {
}; };
BasicSet.prototype.toString = function() { BasicSet.prototype.toString = function() {
var results = []; return this.getAll().join(',');
for (var key in this._data) {
results.push(this._data[key]);
}
return results.join(',');
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment