UNPKG

adaptive-expressions

Version:
94 lines 3.25 kB
"use strict"; /** * @module adaptive-expressions */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JPath = void 0; const jsPath = __importStar(require("jspath")); const expressionEvaluator_1 = require("../expressionEvaluator"); const expressionType_1 = require("../expressionType"); const functionUtils_1 = require("../functionUtils"); const returnType_1 = require("../returnType"); /** * Check JSON or a JSON string for nodes or values that match a path expression, and return the matching nodes. */ class JPath extends expressionEvaluator_1.ExpressionEvaluator { /** * Initializes a new instance of the [JPath](xref:adaptive-expressions.JPath) class. */ constructor() { super(expressionType_1.ExpressionType.JPath, JPath.evaluator(), returnType_1.ReturnType.Object, JPath.validator); } /** * @private */ static evaluator() { return functionUtils_1.FunctionUtils.applyWithError((args) => JPath.evalJPath(args[0], args[1].toString())); } /** * @private */ static evalJPath(jsonEntity, path) { let error; let evaled; let json; if (typeof jsonEntity === 'string') { try { json = JSON.parse(jsonEntity); } catch (_a) { error = `${jsonEntity} is not a valid json string`; } } else if (typeof jsonEntity === 'object') { json = jsonEntity; } else { error = 'the first parameter should be either an object or a string'; } if (!error) { try { evaled = jsPath.apply(path, json); } catch (e) { error = `${path} is not a valid path + ${e}`; } } return { value: evaled, error }; } /** * @private */ static validator(expr) { functionUtils_1.FunctionUtils.validateOrder(expr, undefined, returnType_1.ReturnType.Object, returnType_1.ReturnType.String); } } exports.JPath = JPath; //# sourceMappingURL=jPath.js.map