immutable-path
Version:
Immutable `get`, `set`, `has`, `unset` deep path operations libraray for object, array and `Map`.
31 lines • 1.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const has_1 = __importDefault(require("./has"));
/**
* Deletes immutably given key/index from object/array/Map. Original value is not changed.
*
* @ignore
* @param source is the source object/array/Map.
* @param key is the key to delete.
* @returns new object/array/Map.
*/
function _unset(source, key) {
if (!has_1.default(source, key))
return source;
if (source instanceof Map) {
const newSource = new Map(source);
newSource.delete(key);
return newSource;
}
if (Array.isArray(source)) {
const index = Number(key);
return [...source.slice(0, index), ...source.slice(index + 1)];
}
const { [key]: deleted, ...newSource } = source; // eslint-disable-line @typescript-eslint/no-unused-vars
return newSource;
}
exports.default = _unset;
//# sourceMappingURL=unset.js.map