UNPKG

json-prism

Version:

Immutable Prisms, i.e. Taking a Multifoci Json Path to Immutability, or A sentient item has some ability to communicate, either by sharing its emotions, broadcasting its thoughts telepathically, or speaking aloud.

89 lines (72 loc) 3.12 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var jp = require('jsonpath/jsonpath.min'); var _require = require('./trace'), show = _require.show, aLine = _require.aLine; // from node_modules/jsonpath/lib/index.js:192 var normalize = function normalize(path) { if (path === undefined) throw new Error("Invalid argument: path, cannot be ".concat(path)); path = typeof path === 'number' ? "".concat(path) : path; // FIX FOR: the code accepts [1, 'a'], [1] but barfs on 1 as standalone numeric subscript for root at index === 0 if (typeof path === 'string') { return jp.parse(path); } else if (Array.isArray(path) && ['string', 'number'].includes((0, _typeof2["default"])(path[0]))) { // FIX FOR: the code accepts '0.a.b' but barfs on [0, 'a', 'b'] var _path = [{ expression: { type: 'root', value: '$' } }]; path.forEach(function (component, index) { if (index === 0) { if (component === '$') return; if (typeof component === 'number') component = "".concat(component); // FIX FOR: the code accepts '0.a.b' but barfs on [0, 'a', 'b'] } if (typeof component === 'string' && component.match(/'^[A-Z_a-z]+[0-9A-Z_a-z]*$/)) { // dict.identifier _path.push({ operation: 'member', scope: 'child', expression: { value: component, type: 'identifier' } }); } else { var type = typeof component === 'number' ? 'numeric_literal' : 'string_literal'; _path.push({ operation: 'subscript', scope: 'child', expression: { value: component, type: type } }); } }); return _path; } else if (Array.isArray(path) && (0, _typeof2["default"])(path[0]) === 'object') { return path; } throw new Error("couldn't understand path: ".concat(path)); }; var stringify = function stringify(variousPathFormats) { var ast = normalize(variousPathFormats); // show(`ast:`, ast); var parts = ast.map(function (_ref) { var value = _ref.expression.value; return value; }); // @TODO: union and descendant expressions are nested, may be recursive-reduce is best fit // show({parts}); // show(typeof parts[0]); var partsStartsWithNonNumeric = [typeof parts[0] === 'number' ? "".concat(parts[0]) : parts[0]].concat((0, _toConsumableArray2["default"])(parts.slice(1))); // FIX FOR: the code accepts [1, 'a'], [1] but barfs on 1 as standalone numeric subscript for root at index === 0 // show(`>>># before stringify`, partsStartsWithNonNumeric); // show(`partsStartsWithNonNumeric:`, partsStartsWithNonNumeric); var stringified = jp.stringify(partsStartsWithNonNumeric); // show(stringified); return stringified; }; module.exports = { normalize: normalize, stringify: stringify };