UNPKG

@aricma/itemids

Version:

Be faster in creating and updating react state, with the ItemIds object.

182 lines (158 loc) 6.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.set = set; exports.add = add; exports.remove = remove; exports.toggle = toggle; exports.toggleAll = toggleAll; exports.has = has; exports.isEqualTo = isEqualTo; var ERRORS = _interopRequireWildcard(require("../errors")); var staticMethods = _interopRequireWildcard(require("../staticMethods")); 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; } /** * @typedef {( string | number )} ItemId */ /** * @typedef {Array.<ItemId>} ItemIdList */ /** * @callback Reducer * @param {ItemIdList} boundArray * @return {ItemIdList} - the new values for th boundArray */ /** * @param {( ItemIdList | Reducer )} action * @return {ItemIdList} */ function set(action = []) { const isNotBoundToAnArray = !Array.isArray(this); if (isNotBoundToAnArray) throw Error(ERRORS.methods.set.isNotBoundToAnArray); const isItemIdList = staticMethods.isItemIdList(action); const isFunction = typeof action === "function"; const gotNeitherItemIdListNorFunction = !isItemIdList && !isFunction; if (gotNeitherItemIdListNorFunction) throw Error(ERRORS.methods.set.gotNeitherItemIdListNorFunction); let values = action; if (isFunction) values = action(this); values = staticMethods.unify(values); this.splice(0); values.forEach(itemId => this.push(itemId)); return this; } /** * @param {( ItemId | ItemIdList )} value * @return {ItemIdList} */ function add(value = []) { const isNotBoundToAnArray = !Array.isArray(this); if (isNotBoundToAnArray) throw Error(ERRORS.methods.add.isNotBoundToAnArray); const isItemId = staticMethods.isItemId(value); const isItemIdList = staticMethods.isItemIdList(value); const gotNeitherItemIdNorItemIdList = !isItemId && !isItemIdList; if (gotNeitherItemIdNorItemIdList) throw Error(ERRORS.methods.add.gotNeitherItemIdNorItemIdList); let values = value; if (isItemId) values = [value]; values.forEach(itemId => { const isNotInBoundArray = !this.includes(itemId); if (isNotInBoundArray) this.push(itemId); }); return this; } /** * @param {( ItemId | ItemIdList )} value * @return {ItemIdList} */ function remove(value = []) { const isNotBoundToAnArray = !Array.isArray(this); if (isNotBoundToAnArray) throw Error(ERRORS.methods.remove.isNotBoundToAnArray); const isItemId = staticMethods.isItemId(value); const isItemIdList = staticMethods.isItemIdList(value); const gotNeitherItemIdNorItemIdList = !isItemId && !isItemIdList; if (gotNeitherItemIdNorItemIdList) throw Error(ERRORS.methods.remove.gotNeitherItemIdNorItemIdList); let values = value; if (isItemId) values = [value]; values.forEach(itemId => { const index = this.indexOf(itemId); const isRemovable = index > -1; if (isRemovable) this.splice(index, 1); }); return this; } /** * @param {( ItemId | ItemIdList )} value * @return {ItemIdList} */ function toggle(value = []) { const isNotBoundToAnArray = !Array.isArray(this); if (isNotBoundToAnArray) throw Error(ERRORS.methods.toggle.isNotBoundToAnArray); const isItemId = staticMethods.isItemId(value); const isItemIdList = staticMethods.isItemIdList(value); const gotNeitherItemIdNorItemIdList = !isItemId && !isItemIdList; if (gotNeitherItemIdNorItemIdList) throw Error(ERRORS.methods.toggle.gotNeitherItemIdNorItemIdList); let values = value; if (isItemId) values = [value]; values.forEach(itemId => { const isPartOfBoundArray = this.includes(itemId); if (isPartOfBoundArray) { const index = this.indexOf(itemId); this.splice(index, 1); } else { this.push(itemId); } }); return this; } /** * @param {ItemIdList} values * @return {ItemIdList} */ function toggleAll(values = []) { const isNotBoundToAnArray = !Array.isArray(this); if (isNotBoundToAnArray) throw Error(ERRORS.methods.toggleAll.isNotBoundToAnArray); const gotNoItemIdList = !staticMethods.isItemIdList(values); if (gotNoItemIdList) throw Error(ERRORS.methods.toggleAll.gotNoItemIdList); values = staticMethods.unify(values); const givenAndBoundArrayHaveNotTheSameLength = this.length !== values.length; const notAllGivenIdsAreInBoundArray = !values.reduce((hasIds, id) => hasIds && this.includes(id), true); const needsOverwrite = givenAndBoundArrayHaveNotTheSameLength || notAllGivenIdsAreInBoundArray; this.splice(0); if (needsOverwrite) values.forEach(itemId => this.push(itemId)); return this; } /** * @param {( ItemId | ItemIdList )} value * @return {boolean} */ function has(value = []) { const isNotBoundToAnArray = !Array.isArray(this); if (isNotBoundToAnArray) throw Error(ERRORS.methods.has.isNotBoundToAnArray); const isItemId = staticMethods.isItemId(value); const isItemIdList = staticMethods.isItemIdList(value); const gotNeitherItemIdNorItemIdList = !isItemId && !isItemIdList; if (gotNeitherItemIdNorItemIdList) throw Error(ERRORS.methods.has.gotNeitherItemIdNorItemIdList); let result = false; let values = value; if (isItemId) values = [value]; const givenWasNoEmptyArray = values.length > 0; if (givenWasNoEmptyArray) result = values.reduce((hasIds, itemId) => hasIds && this.includes(itemId), true); return result; } /** * @param {ItemIdList} values * @return {boolean} */ function isEqualTo(values) { const isNotBoundToAnArray = !Array.isArray(this); if (isNotBoundToAnArray) throw Error(ERRORS.methods.isEqualTo.isNotBoundToAnArray); const gotNoItemIdList = !staticMethods.isItemIdList(values); if (gotNoItemIdList) throw Error(ERRORS.methods.isEqualTo.gotNoItemIdList); let result = false; const boundAndGivenArrayHavenTheSameLength = this.length === values.length; if (boundAndGivenArrayHavenTheSameLength) { result = values.reduce((hasIds, itemId) => hasIds && this.includes(itemId), true); } return result; }