UNPKG

@aappddeevv/dynamics-client-ui

Version:

## What is it? A library to help you create great dynamics applications.

54 lines 1.69 kB
"use strict"; /** * Enormously useful when managing state when its a list and * you need easy changes from callbacks. */ Object.defineProperty(exports, "__esModule", { value: true }); const ramda_1 = require("ramda"); /** Return a mutated map. */ function changeMap(m, changeType, item, getId) { switch (changeType) { case "add": m[getId(item)] = item; case "remove": delete m[getId(item)]; case "change": m[getId(item)] = item; } return m; } exports.changeMap = changeMap; /** * Add, remove or change list entries. If change indicated and * the element is not found, it is added to the end. Uses * strict equals by default. */ function changeListBase(input, changeType, change, getId, equals = (t, t2) => t === t2) { let result = []; const id = getId(change); switch (changeType) { case "add": result = [...input, change]; break; case "remove": result = input.filter(t => !equals(getId(t), id)); break; case "change": result = [...input]; const idx = ramda_1.findIndex(t => equals(getId(t), id), result); // if found, change it, otherwise add it if (idx > -1) result[idx] = change; else result.push(change); break; } return result; } exports.changeListBase = changeListBase; /** Change a last assuming it has an "id" property. */ function changeList(input, changeType, change) { return changeListBase(input, changeType, change, t => t.id); } exports.changeList = changeList; //# sourceMappingURL=changeList.js.map