Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

existential-proxy

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

existential-proxy - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

coverage/lcov-report/update.ts.html

11

coverage/coverage-final.json

@@ -1,6 +0,7 @@

{"/Users/jake/code/existential-proxy/src/constants.ts": {"path":"/Users/jake/code/existential-proxy/src/constants.ts","statementMap":{"0":{"start":{"line":1,"column":13},"end":{"line":1,"column":63}},"1":{"start":{"line":2,"column":13},"end":{"line":2,"column":37}}},"fnMap":{},"branchMap":{},"s":{"0":3,"1":3},"f":{},"b":{}}
,"/Users/jake/code/existential-proxy/src/get.ts": {"path":"/Users/jake/code/existential-proxy/src/get.ts","statementMap":{"0":{"start":{"line":18,"column":26},"end":{"line":18,"column":31}},"1":{"start":{"line":20,"column":19},"end":{"line":30,"column":3}},"2":{"start":{"line":22,"column":6},"end":{"line":22,"column":32}},"3":{"start":{"line":24,"column":6},"end":{"line":26,"column":7}},"4":{"start":{"line":25,"column":8},"end":{"line":25,"column":49}},"5":{"start":{"line":28,"column":6},"end":{"line":28,"column":37}},"6":{"start":{"line":32,"column":16},"end":{"line":32,"column":58}},"7":{"start":{"line":34,"column":2},"end":{"line":34,"column":18}},"8":{"start":{"line":36,"column":2},"end":{"line":40,"column":3}},"9":{"start":{"line":37,"column":4},"end":{"line":39,"column":5}},"10":{"start":{"line":38,"column":6},"end":{"line":38,"column":26}},"11":{"start":{"line":42,"column":2},"end":{"line":42,"column":22}},"12":{"start":{"line":13,"column":0},"end":{"line":43,"column":1}}},"fnMap":{"0":{"name":"get","decl":{"start":{"line":13,"column":16},"end":{"line":13,"column":19}},"loc":{"start":{"line":16,"column":18},"end":{"line":43,"column":1}}}},"branchMap":{"0":{"loc":{"start":{"line":24,"column":6},"end":{"line":26,"column":7}},"type":"if","locations":[{"start":{"line":24,"column":6},"end":{"line":26,"column":7}},{"start":{"line":24,"column":6},"end":{"line":26,"column":7}}]},"1":{"loc":{"start":{"line":24,"column":10},"end":{"line":24,"column":22}},"type":"binary-expr","locations":[{"start":{"line":24,"column":10},"end":{"line":24,"column":22}},{"start":{"line":24,"column":26},"end":{"line":24,"column":58}}]},"2":{"loc":{"start":{"line":36,"column":2},"end":{"line":40,"column":3}},"type":"if","locations":[{"start":{"line":36,"column":2},"end":{"line":40,"column":3}},{"start":{"line":36,"column":2},"end":{"line":40,"column":3}}]},"3":{"loc":{"start":{"line":36,"column":6},"end":{"line":36,"column":41}},"type":"binary-expr","locations":[{"start":{"line":36,"column":6},"end":{"line":36,"column":41}},{"start":{"line":36,"column":45},"end":{"line":36,"column":66}}]},"4":{"loc":{"start":{"line":37,"column":4},"end":{"line":39,"column":5}},"type":"if","locations":[{"start":{"line":37,"column":4},"end":{"line":39,"column":5}},{"start":{"line":37,"column":4},"end":{"line":39,"column":5}}]}},"s":{"0":16,"1":16,"2":24,"3":24,"4":8,"5":16,"6":16,"7":16,"8":16,"9":8,"10":5,"11":11,"12":2},"f":{"0":16},"b":{"0":[8,16],"1":[24,9],"2":[8,8],"3":[16,9],"4":[5,3]}}
,"/Users/jake/code/existential-proxy/src/index.ts": {"path":"/Users/jake/code/existential-proxy/src/index.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},"1":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}},"2":{"start":{"line":2,"column":0},"end":{"line":2,"column":27}},"3":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}}},"fnMap":{},"branchMap":{},"s":{"0":2,"1":2,"2":2,"3":2},"f":{},"b":{}}
,"/Users/jake/code/existential-proxy/src/set.ts": {"path":"/Users/jake/code/existential-proxy/src/set.ts","statementMap":{"0":{"start":{"line":2,"column":0},"end":{"line":2,"column":31}},"1":{"start":{"line":9,"column":25},"end":{"line":9,"column":27}},"2":{"start":{"line":11,"column":19},"end":{"line":17,"column":3}},"3":{"start":{"line":13,"column":6},"end":{"line":13,"column":21}},"4":{"start":{"line":15,"column":6},"end":{"line":15,"column":37}},"5":{"start":{"line":19,"column":16},"end":{"line":19,"column":55}},"6":{"start":{"line":21,"column":2},"end":{"line":21,"column":18}},"7":{"start":{"line":23,"column":2},"end":{"line":23,"column":38}},"8":{"start":{"line":4,"column":0},"end":{"line":24,"column":1}}},"fnMap":{"0":{"name":"set","decl":{"start":{"line":4,"column":16},"end":{"line":4,"column":19}},"loc":{"start":{"line":7,"column":13},"end":{"line":24,"column":1}}}},"branchMap":{},"s":{"0":2,"1":14,"2":14,"3":28,"4":28,"5":14,"6":14,"7":14,"8":2},"f":{"0":14},"b":{}}
,"/Users/jake/code/existential-proxy/src/utils.ts": {"path":"/Users/jake/code/existential-proxy/src/utils.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":41}},"1":{"start":{"line":4,"column":2},"end":{"line":6,"column":3}},"2":{"start":{"line":5,"column":4},"end":{"line":5,"column":20}},"3":{"start":{"line":8,"column":34},"end":{"line":8,"column":38}},"4":{"start":{"line":9,"column":14},"end":{"line":9,"column":74}},"5":{"start":{"line":12,"column":2},"end":{"line":16,"column":3}},"6":{"start":{"line":13,"column":4},"end":{"line":13,"column":45}},"7":{"start":{"line":15,"column":4},"end":{"line":15,"column":69}},"8":{"start":{"line":18,"column":2},"end":{"line":18,"column":51}},"9":{"start":{"line":20,"column":2},"end":{"line":20,"column":14}},"10":{"start":{"line":3,"column":0},"end":{"line":21,"column":1}}},"fnMap":{"0":{"name":"setIn","decl":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}},"loc":{"start":{"line":3,"column":76},"end":{"line":21,"column":1}}}},"branchMap":{"0":{"loc":{"start":{"line":4,"column":2},"end":{"line":6,"column":3}},"type":"if","locations":[{"start":{"line":4,"column":2},"end":{"line":6,"column":3}},{"start":{"line":4,"column":2},"end":{"line":6,"column":3}}]},"1":{"loc":{"start":{"line":9,"column":43},"end":{"line":9,"column":63}},"type":"cond-expr","locations":[{"start":{"line":9,"column":43},"end":{"line":9,"column":63}},{"start":{"line":9,"column":66},"end":{"line":9,"column":74}}]},"2":{"loc":{"start":{"line":12,"column":2},"end":{"line":16,"column":3}},"type":"if","locations":[{"start":{"line":12,"column":2},"end":{"line":16,"column":3}},{"start":{"line":12,"column":2},"end":{"line":16,"column":3}}]},"3":{"loc":{"start":{"line":12,"column":6},"end":{"line":12,"column":20}},"type":"binary-expr","locations":[{"start":{"line":12,"column":6},"end":{"line":12,"column":20}},{"start":{"line":12,"column":24},"end":{"line":12,"column":52}}]},"4":{"loc":{"start":{"line":13,"column":37},"end":{"line":13,"column":39}},"type":"cond-expr","locations":[{"start":{"line":13,"column":37},"end":{"line":13,"column":39}},{"start":{"line":13,"column":42},"end":{"line":13,"column":44}}]},"5":{"loc":{"start":{"line":15,"column":34},"end":{"line":15,"column":44}},"type":"cond-expr","locations":[{"start":{"line":15,"column":34},"end":{"line":15,"column":44}},{"start":{"line":15,"column":46},"end":{"line":15,"column":68}}]}},"s":{"0":2,"1":42,"2":14,"3":28,"4":28,"5":28,"6":10,"7":18,"8":28,"9":28,"10":2},"f":{"0":42},"b":{"0":[14,28],"1":[14,14],"2":[10,18],"3":[28,26],"4":[7,3],"5":[5,13]}}
{"/Users/jake/code/existential-proxy/src/constants.ts": {"path":"/Users/jake/code/existential-proxy/src/constants.ts","statementMap":{"0":{"start":{"line":1,"column":13},"end":{"line":1,"column":63}},"1":{"start":{"line":2,"column":13},"end":{"line":2,"column":37}}},"fnMap":{},"branchMap":{},"s":{"0":5,"1":5},"f":{},"b":{}}
,"/Users/jake/code/existential-proxy/src/get.ts": {"path":"/Users/jake/code/existential-proxy/src/get.ts","statementMap":{"0":{"start":{"line":18,"column":26},"end":{"line":18,"column":31}},"1":{"start":{"line":20,"column":19},"end":{"line":30,"column":3}},"2":{"start":{"line":22,"column":6},"end":{"line":22,"column":32}},"3":{"start":{"line":24,"column":6},"end":{"line":26,"column":7}},"4":{"start":{"line":25,"column":8},"end":{"line":25,"column":49}},"5":{"start":{"line":28,"column":6},"end":{"line":28,"column":37}},"6":{"start":{"line":32,"column":16},"end":{"line":32,"column":58}},"7":{"start":{"line":34,"column":2},"end":{"line":34,"column":18}},"8":{"start":{"line":36,"column":2},"end":{"line":40,"column":3}},"9":{"start":{"line":37,"column":4},"end":{"line":39,"column":5}},"10":{"start":{"line":38,"column":6},"end":{"line":38,"column":26}},"11":{"start":{"line":42,"column":2},"end":{"line":42,"column":22}},"12":{"start":{"line":13,"column":0},"end":{"line":43,"column":1}}},"fnMap":{"0":{"name":"get","decl":{"start":{"line":13,"column":16},"end":{"line":13,"column":19}},"loc":{"start":{"line":16,"column":18},"end":{"line":43,"column":1}}}},"branchMap":{"0":{"loc":{"start":{"line":24,"column":6},"end":{"line":26,"column":7}},"type":"if","locations":[{"start":{"line":24,"column":6},"end":{"line":26,"column":7}},{"start":{"line":24,"column":6},"end":{"line":26,"column":7}}]},"1":{"loc":{"start":{"line":24,"column":10},"end":{"line":24,"column":22}},"type":"binary-expr","locations":[{"start":{"line":24,"column":10},"end":{"line":24,"column":22}},{"start":{"line":24,"column":26},"end":{"line":24,"column":58}}]},"2":{"loc":{"start":{"line":36,"column":2},"end":{"line":40,"column":3}},"type":"if","locations":[{"start":{"line":36,"column":2},"end":{"line":40,"column":3}},{"start":{"line":36,"column":2},"end":{"line":40,"column":3}}]},"3":{"loc":{"start":{"line":36,"column":6},"end":{"line":36,"column":41}},"type":"binary-expr","locations":[{"start":{"line":36,"column":6},"end":{"line":36,"column":41}},{"start":{"line":36,"column":45},"end":{"line":36,"column":66}}]},"4":{"loc":{"start":{"line":37,"column":4},"end":{"line":39,"column":5}},"type":"if","locations":[{"start":{"line":37,"column":4},"end":{"line":39,"column":5}},{"start":{"line":37,"column":4},"end":{"line":39,"column":5}}]}},"s":{"0":16,"1":16,"2":24,"3":24,"4":8,"5":16,"6":16,"7":16,"8":16,"9":8,"10":5,"11":11,"12":4},"f":{"0":16},"b":{"0":[8,16],"1":[24,9],"2":[8,8],"3":[16,9],"4":[5,3]}}
,"/Users/jake/code/existential-proxy/src/index.ts": {"path":"/Users/jake/code/existential-proxy/src/index.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},"1":{"start":{"line":1,"column":9},"end":{"line":1,"column":12}},"2":{"start":{"line":2,"column":0},"end":{"line":2,"column":27}},"3":{"start":{"line":2,"column":9},"end":{"line":2,"column":12}},"4":{"start":{"line":3,"column":0},"end":{"line":3,"column":33}},"5":{"start":{"line":3,"column":9},"end":{"line":3,"column":15}}},"fnMap":{},"branchMap":{},"s":{"0":4,"1":4,"2":4,"3":4,"4":4,"5":4},"f":{},"b":{}}
,"/Users/jake/code/existential-proxy/src/set.ts": {"path":"/Users/jake/code/existential-proxy/src/set.ts","statementMap":{"0":{"start":{"line":2,"column":0},"end":{"line":2,"column":31}},"1":{"start":{"line":9,"column":25},"end":{"line":9,"column":27}},"2":{"start":{"line":11,"column":19},"end":{"line":17,"column":3}},"3":{"start":{"line":13,"column":6},"end":{"line":13,"column":21}},"4":{"start":{"line":15,"column":6},"end":{"line":15,"column":37}},"5":{"start":{"line":19,"column":16},"end":{"line":19,"column":55}},"6":{"start":{"line":21,"column":2},"end":{"line":21,"column":18}},"7":{"start":{"line":23,"column":2},"end":{"line":23,"column":38}},"8":{"start":{"line":4,"column":0},"end":{"line":24,"column":1}}},"fnMap":{"0":{"name":"set","decl":{"start":{"line":4,"column":16},"end":{"line":4,"column":19}},"loc":{"start":{"line":7,"column":13},"end":{"line":24,"column":1}}}},"branchMap":{},"s":{"0":4,"1":14,"2":14,"3":28,"4":28,"5":14,"6":14,"7":14,"8":4},"f":{"0":14},"b":{}}
,"/Users/jake/code/existential-proxy/src/update.ts": {"path":"/Users/jake/code/existential-proxy/src/update.ts","statementMap":{"0":{"start":{"line":2,"column":0},"end":{"line":2,"column":31}},"1":{"start":{"line":9,"column":25},"end":{"line":9,"column":27}},"2":{"start":{"line":10,"column":26},"end":{"line":10,"column":31}},"3":{"start":{"line":12,"column":19},"end":{"line":23,"column":3}},"4":{"start":{"line":14,"column":6},"end":{"line":14,"column":31}},"5":{"start":{"line":15,"column":6},"end":{"line":15,"column":32}},"6":{"start":{"line":17,"column":6},"end":{"line":19,"column":7}},"7":{"start":{"line":18,"column":8},"end":{"line":18,"column":49}},"8":{"start":{"line":21,"column":6},"end":{"line":21,"column":37}},"9":{"start":{"line":25,"column":16},"end":{"line":25,"column":58}},"10":{"start":{"line":27,"column":2},"end":{"line":27,"column":18}},"11":{"start":{"line":29,"column":2},"end":{"line":29,"column":51}},"12":{"start":{"line":4,"column":0},"end":{"line":30,"column":1}}},"fnMap":{"0":{"name":"update","decl":{"start":{"line":4,"column":16},"end":{"line":4,"column":22}},"loc":{"start":{"line":7,"column":26},"end":{"line":30,"column":1}}}},"branchMap":{"0":{"loc":{"start":{"line":17,"column":6},"end":{"line":19,"column":7}},"type":"if","locations":[{"start":{"line":17,"column":6},"end":{"line":19,"column":7}},{"start":{"line":17,"column":6},"end":{"line":19,"column":7}}]},"1":{"loc":{"start":{"line":17,"column":10},"end":{"line":17,"column":22}},"type":"binary-expr","locations":[{"start":{"line":17,"column":10},"end":{"line":17,"column":22}},{"start":{"line":17,"column":26},"end":{"line":17,"column":58}}]}},"s":{"0":4,"1":12,"2":12,"3":12,"4":14,"5":14,"6":14,"7":7,"8":7,"9":12,"10":12,"11":12,"12":4},"f":{"0":12},"b":{"0":[7,7],"1":[14,8]}}
,"/Users/jake/code/existential-proxy/src/utils.ts": {"path":"/Users/jake/code/existential-proxy/src/utils.ts","statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":41}},"1":{"start":{"line":4,"column":2},"end":{"line":6,"column":3}},"2":{"start":{"line":5,"column":4},"end":{"line":5,"column":20}},"3":{"start":{"line":8,"column":34},"end":{"line":8,"column":38}},"4":{"start":{"line":9,"column":14},"end":{"line":9,"column":74}},"5":{"start":{"line":12,"column":2},"end":{"line":16,"column":3}},"6":{"start":{"line":13,"column":4},"end":{"line":13,"column":45}},"7":{"start":{"line":15,"column":4},"end":{"line":15,"column":69}},"8":{"start":{"line":18,"column":2},"end":{"line":18,"column":51}},"9":{"start":{"line":20,"column":2},"end":{"line":20,"column":14}},"10":{"start":{"line":3,"column":0},"end":{"line":21,"column":1}}},"fnMap":{"0":{"name":"setIn","decl":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}},"loc":{"start":{"line":3,"column":76},"end":{"line":21,"column":1}}}},"branchMap":{"0":{"loc":{"start":{"line":4,"column":2},"end":{"line":6,"column":3}},"type":"if","locations":[{"start":{"line":4,"column":2},"end":{"line":6,"column":3}},{"start":{"line":4,"column":2},"end":{"line":6,"column":3}}]},"1":{"loc":{"start":{"line":9,"column":43},"end":{"line":9,"column":63}},"type":"cond-expr","locations":[{"start":{"line":9,"column":43},"end":{"line":9,"column":63}},{"start":{"line":9,"column":66},"end":{"line":9,"column":74}}]},"2":{"loc":{"start":{"line":12,"column":2},"end":{"line":16,"column":3}},"type":"if","locations":[{"start":{"line":12,"column":2},"end":{"line":16,"column":3}},{"start":{"line":12,"column":2},"end":{"line":16,"column":3}}]},"3":{"loc":{"start":{"line":12,"column":6},"end":{"line":12,"column":20}},"type":"binary-expr","locations":[{"start":{"line":12,"column":6},"end":{"line":12,"column":20}},{"start":{"line":12,"column":24},"end":{"line":12,"column":52}}]},"4":{"loc":{"start":{"line":13,"column":37},"end":{"line":13,"column":39}},"type":"cond-expr","locations":[{"start":{"line":13,"column":37},"end":{"line":13,"column":39}},{"start":{"line":13,"column":42},"end":{"line":13,"column":44}}]},"5":{"loc":{"start":{"line":15,"column":34},"end":{"line":15,"column":44}},"type":"cond-expr","locations":[{"start":{"line":15,"column":34},"end":{"line":15,"column":44}},{"start":{"line":15,"column":46},"end":{"line":15,"column":68}}]}},"s":{"0":4,"1":68,"2":26,"3":42,"4":42,"5":42,"6":12,"7":30,"8":42,"9":42,"10":4},"f":{"0":68},"b":{"0":[26,42],"1":[21,21],"2":[12,30],"3":[42,40],"4":[8,4],"5":[11,19]}}
}

@@ -54,2 +54,6 @@ "use strict";

exports.set = set_1.set;
var update_1 = require("./update");
exports.update = update_1.update;
"use strict";

@@ -91,2 +95,32 @@

const utils_1 = require("./utils");
function update(input, callback, updater) {
const keys = [];
let currentValue = input;
const handlers = {
get(value, key) {
keys.push(key);
currentValue = value[key];
if (currentValue && typeof currentValue === 'object') {
return new Proxy(currentValue, handlers);
}
return new Proxy({}, handlers);
}
};
const proxy = new Proxy(input, handlers);
callback(proxy);
return utils_1.setIn(input, keys, updater(currentValue));
}
exports.update = update;
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const constants_1 = require("./constants");

@@ -93,0 +127,0 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MAGIC_PROXY_SYMBOL=Symbol("Magic proxy symbol"),exports.MATCHES_INT=/^-?\d+$/;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});function get(a,b,c){let d=a;const e={get(a,b){return d=a[b],d&&"object"==typeof d?new Proxy(d,e):new Proxy({},e)}},f=new Proxy(a,e);return b(f),("undefined"==typeof d||null===d)&&"undefined"!=typeof c?c:d}exports.get=get;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var get_1=require("./get");exports.get=get_1.get;var set_1=require("./set");exports.set=set_1.set;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var get_1=require("./get");exports.get=get_1.get;var set_1=require("./set");exports.set=set_1.set;var update_1=require("./update");exports.update=update_1.update;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const utils_1=require("./utils");function set(a,b,c){const d=[],e={get(a,b){return d.push(b),new Proxy({},e)}},f=new Proxy({},e);return b(f),utils_1.setIn(a,d,c)}exports.set=set;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const constants_1=require("./constants");
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const utils_1=require("./utils");function update(a,b,c){const d=[];let e=a;const f={get(a,b){return d.push(b),e=a[b],e&&"object"==typeof e?new Proxy(e,f):new Proxy({},f)}},g=new Proxy(a,f);return b(g),utils_1.setIn(a,d,c(e))}exports.update=update;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const constants_1=require("./constants");function setIn(a,b,c){if(!b.length)return c;const[d,...e]=b,f=constants_1.MATCHES_INT.test(d)?parseFloat(d):d;let g;return g=null===a||"undefined"==typeof a?"number"==typeof f?[]:{}:Array.isArray(a)?[...a]:Object.assign({},a),g[f]=setIn(g[f],e,c),g}exports.setIn=setIn;
//# sourceMappingURL=index.min.js.map
export { get } from './get';
export { set } from './set';
export { update } from './update';
export * from './types';

@@ -7,2 +7,4 @@ "use strict";

exports.set = set_1.set;
var update_1 = require("./update");
exports.update = update_1.update;
//# sourceMappingURL=index.js.map

@@ -33,1 +33,11 @@ // tslint:disable:no-console

console.log(numKeyResult);
const updated = ep.update(abc, proxy => proxy.a.b.c, value => {
if (typeof value === 'string') {
return 'already existed';
}
return 'was not defined';
});
console.log(updated);
{
"name": "existential-proxy",
"version": "0.0.4",
"version": "0.0.5",
"description": "Type safe existential property access using ES6 proxies",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -9,2 +9,4 @@ # existential-proxy

This library gives you the ability to `get`, `set`, or `update` (with an update function) values nested within an object who's keys may be nullable (`undefined` or `null`), without mutating the original input, or having to worry about checking for values' existence.
[Optional chaining](https://github.com/tc39/proposal-optional-chaining) in JavaScript / TypeScript is not yet finalized, and so we need a way to safely access nested values that may or may not exist.

@@ -16,12 +18,10 @@

Unlike destructuring with default values existential-proxy allows access through values that may be `undefined` or `null` without specifying default values for each level, and allows specifying defaults even what the value may be `null`.
Unlike destructuring with default values existential-proxy allows access through values that may be `undefined` or `null` without specifying default values for each level, and allows specifying defaults even when the value may be `null`.
Additionally, unlike (some) alternatives that allow access to nested properties this library also allows setting values inside a nested object in an immutable way (returning a copy of the input with updated values as necessary).
Additionally, unlike (some) alternatives that allow access to nested properties this library also allows setting or updating values inside a nested object in an immutable way (returning a copy of the input with updated values as necessary).
### Future plans
Although this library currently only offers the ability to `get` values from a nested object, and `set` values inside a nested object, I intend to add the ability to `update` (with a function) nested values in an immutable way. This is something that optional chaining would not necessarily provide.
I intend to add a function to create a selector, much like [reselect](https://github.com/reduxjs/reselect), to allow transforming values only when changed, but using proxies removing the need to null check nested values.
Additionally, I may add a function to create a selector, much like [reselect](https://github.com/reduxjs/reselect), to allow transforming values only when changed, but without the need to null check nested values.
## Installation

@@ -91,6 +91,3 @@

Some important things to note:
1. The return type will always match the input type - if keys are nullable, they will still be nullable even if set by this function
2. Keys that when cast to a number are a valid integer (including negative values) will produce arrays if the parent object is undefined or null. This is because there is no way to detect if trying to access values from an array or object if the target is undefined or null (all keys; `.a`, `[0]`, are only available as strings when using a proxy).

@@ -101,10 +98,48 @@ ```typescript

// Will return a copy of the `abc` object with a new `a` value
ep.set(abc, (proxy) => proxy.a, { b: { c: 'hello' } } }); // { a: { b: { c: 'hello' } } }: ABC
ep.set(abc, (proxy) => proxy.a, { b: { c: 'hello' } }); // { a: { b: { c: 'hello' } } }: ABC
// Will return a copy of the `abc` object with a new `b` value
ep.set(abc, (proxy) => proxy.a.b, { c: 'hello' } }); // { a: { b: { c: 'hello' } } }: ABC
ep.set(abc, (proxy) => proxy.a.b, { c: 'hello' }); // { a: { b: { c: 'hello' } } }: ABC
```
This library's `set` function may not give you the output you'd expect if you are using integers as keys in objects.
### Update
The `update` function takes 3 arguments:
1. The object from which you wish to retrieve a value
2. A callback that will be passed a proxy to this object
3. An updater function that will be passed the existing value at the returned proxy key, and returns a new value
```typescript
// Will return the returned value (essentially replacing the input object)
ep.update(abc, (proxy) => proxy, () => ({ a: { b: { c: 'hello' } } })); // { a: { b: { c: 'hello' } } }: ABC
// Will return a copy of the `abc` object with a new `a` value if it is not defined
ep.update(abc, (proxy) => proxy.a, (a) => {
if (!a) {
return { b: { c: 'hello' } };
}
return a;
}); // { a: { b: { c: 'hello' } } }: ABC
// Will return a copy of the `abc` object with a new `b` value if it is not defined
ep.update(abc, (proxy) => proxy.a.b, (b) => {
if (!b) {
return { c: 'hello' };
}
return b;
}); // { a: { b: { c: 'hello' } } }: ABC
```
## Important notes
When using `set` and `update` you should note that:
1. The return type will always match the input type - if keys are nullable, they will still be nullable even if set by one of these functions
2. Keys that when cast to a number are a valid integer (including negative values) will produce arrays if the parent object is `undefined` or `null`. This is because there is no way to detect if trying to access values from an array or object if the target is not already an array or object (all keys; `.a`, `[0]`, are only available as strings when using a proxy).
## Example of potentially unintended array creation
This library's `set` and `update` functions may not give you the output you'd expect if you are using integers as keys in objects.
```typescript
interface NumKey {

@@ -111,0 +146,0 @@ a?: {

@@ -21,3 +21,3 @@ import { WithProxy } from './types';

const handlers = {
get<S>(value: S, key: keyof S): object {
get(value: any, key: string): object {
currentValue = value[key];

@@ -24,0 +24,0 @@

export { get } from './get';
export { set } from './set';
export { update } from './update';
export * from './types';

@@ -5,3 +5,6 @@ import * as CONSTANTS from '../src/constants';

it('should export some constants', () => {
expect(Object.keys(CONSTANTS)).toEqual(['MAGIC_PROXY_SYMBOL', 'MATCHES_INT']);
expect(Object.keys(CONSTANTS)).toEqual([
'MAGIC_PROXY_SYMBOL',
'MATCHES_INT',
]);
expect(typeof CONSTANTS.MAGIC_PROXY_SYMBOL).toBe('symbol');

@@ -8,0 +11,0 @@ expect(CONSTANTS.MATCHES_INT instanceof RegExp).toBe(true);

@@ -11,3 +11,3 @@ import * as ep from '../src';

a: {
b: 'b';
b: string;
};

@@ -14,0 +14,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc