UNPKG

ilib-common

Version:

Common utility functions for ilib. iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

68 lines (67 loc) 5.45 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports["default"]=void 0;function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}function _classCallCheck(a,n){if(!(a instanceof n))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,r){for(var t=0;t<r.length;t++){var o=r[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,_toPropertyKey(o.key),o)}}function _createClass(e,r,t){return r&&_defineProperties(e.prototype,r),t&&_defineProperties(e,t),Object.defineProperty(e,"prototype",{writable:!1}),e}function _toPropertyKey(t){var i=_toPrimitive(t,"string");return"symbol"==_typeof(i)?i:i+""}function _toPrimitive(t,r){if("object"!=_typeof(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var i=e.call(t,r||"default");if("object"!=_typeof(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}/* * ISet.js - ilib Set class definition for platforms older than ES6 * (basically a polyfill for Set) * * Copyright © 2015, 2021-2022 JEDLSoft * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * limitations under the License. *//** * @class A polyfill for Set in older browsers. */var ISet=/*#__PURE__*/function(){/** * Create a new set with elements in the given array. The type of * the set is gleaned from the type of the first element in the * elements array, or the first element added to the set. The type * may be "string" or "number", and all elements will be returned * as elements of that type. * * @param {Array.<string|number>=} elements initial elements to add to the set * @constructor */function ISet(elements){_classCallCheck(this,ISet);this.elements={};if(elements&&elements.length){for(var i=0;i<elements.length;i++){this.elements[elements[i]]=true}this.type=_typeof(elements[0])}}/** * @private */return _createClass(ISet,[{key:"_addOne",value:function _addOne(element){if(this.isEmpty()){this.type=_typeof(element)}if(!this.elements[element]){this.elements[element]=true;return true}return false}/** * Adds the specified element or array of elements to this set if it is or they are not * already present. * * @param {*|Array.<*>} element element or array of elements to add * @return {boolean} true if this set did not already contain the specified element[s] */},{key:"add",value:function add(element){var ret=false;if(_typeof(element)==="object"){for(var i=0;i<element.length;i++){ret=this._addOne(element[i])||ret}}else{ret=this._addOne(element)}return ret}/** * Removes all of the elements from this set. */},{key:"clear",value:function clear(){this.elements={}}/** * Returns true if this set contains the specified element. * @param {*} element the element to test * @return {boolean} */},{key:"contains",value:function contains(element){return this.elements[element]||false}/** * Returns true if this set contains no elements. * @return {boolean} */},{key:"isEmpty",value:function isEmpty(){return Object.keys(this.elements).length===0}/** * Removes the specified element from this set if it is present. * @param {*} element the element to remove * @return {boolean} true if the set contained the specified element */},{key:"remove",value:function remove(element){if(this.elements[element]){delete this.elements[element];return true}return false}/** * Return the set as a javascript array. * @return {Array.<*>} the set represented as a javascript array */},{key:"asArray",value:function asArray(){var keys=Object.keys(this.elements);// keys is an array of strings. Convert to numbers if necessary if(this.type==="number"){var tmp=[];for(var i=0;i<keys.length;i++){tmp.push(Number(keys[i]).valueOf())}keys=tmp}return keys}/** * Represents the current set as json. * @return {string} the current set represented as json */},{key:"toJson",value:function toJson(){return JSON.stringify(this.asArray())}/** * Convert to a javascript representation of this object. * In this case, it is a normal JS array. * @return {*} the JS representation of this object */},{key:"toJS",value:function toJS(){return this.asArray()}/** * Convert from a js representation to an internal one. * @return {ISet|undefined} the current object, or undefined if the conversion did not work */},{key:"fromJS",value:function fromJS(obj){return this.add(obj)?this:undefined}}])}();ISet.prototype.has=ISet.prototype.contains;// for compatibility with ES6 var _default=exports["default"]=ISet;module.exports=exports.default; //# sourceMappingURL=ISet.js.map