@aricma/itemids
Version:
Be faster in creating and updating react state, with the ItemIds object.
48 lines (36 loc) • 2.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ItemIds = ItemIds;
var staticMethods = _interopRequireWildcard(require("./staticMethods"));
var methods = _interopRequireWildcard(require("./methods"));
var _utils = require("./utils");
var ERRORS = _interopRequireWildcard(require("./errors"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
/**
* the ItemIds object
*/
/**
* @typedef {( number | string )} ItemId
*/
/**
* @typedef {Array.<ItemId>} ItemIdList
*/
/**
* @param {ItemIdList} ids
* @return {Array}
*/
function ItemIds(ids = []) {
const givenWasNoArray = !Array.isArray(ids);
if (givenWasNoArray) throw ERRORS.ItemIds.givenWasNoArray;
ids = staticMethods.unify(ids);
return Object.assign(ids, // properties
(0, _utils._with)(ids, "name", "ItemIds"), // methods
(0, _utils._can)(ids, "set", methods.set), (0, _utils._can)(ids, "add", methods.add), (0, _utils._can)(ids, "remove", methods.remove), (0, _utils._can)(ids, "toggle", methods.toggle), (0, _utils._can)(ids, "toggleAll", methods.toggleAll), (0, _utils._can)(ids, "has", methods.has), (0, _utils._can)(ids, "isEqualTo", methods.isEqualTo));
}
ItemIds.isItemIds = staticMethods.isItemIds;
ItemIds.isItemId = staticMethods.isItemId;
ItemIds.isItemIdList = staticMethods.isItemIdList;
ItemIds.unify = staticMethods.unify;