ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 1.26 kB
JavaScript
var ISet=function(elements){if(this.elements={},elements&&elements.length){for(var i=0;i<elements.length;i++)this.elements[elements[i]]=!0;this.type=typeof elements[0]}};ISet.prototype._addOne=function(element){return this.isEmpty()&&(this.type=typeof element),!this.elements[element]&&(this.elements[element]=!0)},ISet.prototype.add=function(element){var ret=!1;if("object"==typeof element)for(var i=0;i<element.length;i++)ret=this._addOne(element[i])||ret;else ret=this._addOne(element);return ret},ISet.prototype.clear=function(){this.elements={}},ISet.prototype.contains=function(element){return this.elements[element]||!1},ISet.prototype.has=ISet.prototype.contains,ISet.prototype.isEmpty=function(){return 0===Object.keys(this.elements).length},ISet.prototype.remove=function(element){return!!this.elements[element]&&(delete this.elements[element],!0)},ISet.prototype.asArray=function(){var keys=Object.keys(this.elements);if("number"===this.type){for(var tmp=[],i=0;i<keys.length;i++)tmp.push(Number(keys[i]).valueOf());keys=tmp}return keys},ISet.prototype.toJson=function(){return JSON.stringify(this.asArray())},ISet.prototype.toJS=function(){return this.asArray()},ISet.prototype.fromJS=function(obj){return this.add(obj)?this:void 0},module.exports=ISet;