PNW-TechPros / js-natural-lenses

A JavaScript-native lens (and ancillary optics) system loosely based on Kmett-style lenses.
MIT License
3 stars 0 forks source link

Support a tagged template constructor for JSONPath syntax #2

Closed rtweeks closed 2 years ago

rtweeks commented 3 years ago

To some library consumers, it would be convenient if natural-lenses supported construction of a lens from JSONPath syntax.

Example:

// Instead of:
const lens = require('natural-lenses');
const lensOrig = lens('x', 'y', 0, 'z', 'odd key');

// Tagged template JSONPath construction could look like:
const { A } = require('natural-lenses');
const lensTT = A`$.x.y[0].z['odd key']`;

The syntax for constructing the lens looks like the JavaScript that would be used for getting or setting (if on the left hand side of =) the target slot if it could be assumed all intermediate containers existed and were JSON types.

rtweeks commented 3 years ago

Ideally, JavaScript values could be interpolated within square brackets for more complicated values, including customized steps:

const customStep = new lens.Step(...);
const lensOrig = lens(customStep);
const lensTT = A`$[${customStep}]`;

This would facilitate use of the tagged template construction with ES6 Map containers using non-string keys. This also suggests it might be good to expose an A on the lens.Factory to fully facilitate use of Map containers.