@broadcom/jclcheck-for-zowe-cli
Version:
JCLCheck Plug-in for Zowe CLI
110 lines • 5.22 kB
JavaScript
;
/**
* Copyright (c) 2023. Broadcom. All rights reserved.
*
* This software and all information contained therein is confidential and
* proprietary and shall not be duplicated, used, disclosed or disseminated
* in any way except as authorized by the applicable license agreement,
* without the express written permission of BROADCOM. All authorized reproductions
* must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT
* PERMITTED BY APPLICABLE LAW, BROADCOM PROVIDES THIS SOFTWARE WITHOUT
* WARRANTY OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN
* NO EVENT WILL BROADCOM BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY
* LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS INTERRUPTION,
* GOODWILL, OR LOST DATA, EVEN IF CA IS EXPRESSLY ADVISED OF SUCH LOSS OR
* DAMAGE.
*
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
**/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
const lodash = require('lodash');
class Utils {
/**
* Traverse an input object (deep traversal) and for each property that
* is of type "string", invoke "resolve" to attempt resolution of any
* ${<var>} style syntax.
* @param object The object to traverse.
* @param substitutions The substitution values (keyword value).
* @param propertyPrefix Prefix of the property to substitute in the text
* (e.g. ${extracted.<propname>}).
*/
static traverse(object, substitutions, propertyPrefix = "") {
Object.keys(object).forEach((value) => {
if (Array.isArray(object[value])) {
for (let x = 0; x < object[value].length; x++) {
if (typeof object[value][x] === "object") {
Utils.traverse(object[value][x], substitutions, propertyPrefix);
}
else {
Utils.resolve(object[value], x, substitutions, propertyPrefix);
}
}
return;
}
if (object[value] !== null && typeof object[value] === "object") {
Utils.traverse(object[value], substitutions, propertyPrefix);
return;
}
if (typeof object[value] === "string") {
Utils.resolve(object, value, substitutions, propertyPrefix);
}
});
}
/**
* Given an object and its property, attempt to find instances of
* ${<propname>}. If instances of ${<propname>} are located, attempt to
* resolve <propname> based on the keyword/value properties of the
* "substitutions".
* @param object The object that contains "property".
* @param property The property of the "object"
* @param substitutions The keyword/value substitutions to replace
* ${<propname>}
* @param propertyPrefix Prefix of the property to substitute in the text
* (e.g. ${extracted.<propname>})
*/
static resolve(object, property, substitutions, propertyPrefix = "") {
// If the property is a string we can attempt resolution, otherwise
// we can skip this property
if (typeof object[property] === "string") {
// Given the string, iterate with regex to find all matches of the
// substitution syntax "${var}"
const str = object[property];
// Assign the property to itself to later manipulation
object[property] = str;
// Regex is matching on ${<varname>}
const reg = /\${[^}]+}}?/g;
let result;
while ((result = reg.exec(str)) !== null) {
// Get the property name from the match and get the value for
// property from the substitution values.
let propName = result[0].replace(`$\{${propertyPrefix}`, "").replace(/}$/, "");
const hasDoubleBraces = propName.startsWith("{") && propName.endsWith("}");
if (hasDoubleBraces) {
propName = propName.slice(1, -1);
}
const value = lodash.get(substitutions, propName);
// If the value is non-null/undefined substitute the value
// either into the string OR directly to the property itself.
if (value != null) {
if (object[property] === result[0] && !hasDoubleBraces) {
object[property] = value;
}
else if (hasDoubleBraces) {
object[property] = object[property].replace(`$\{{${propertyPrefix}${propName}}}`, value);
}
else {
object[property] = object[property].replace(`$\{${propertyPrefix}${propName}}`, value);
}
}
}
}
}
}
exports.Utils = Utils;
//# sourceMappingURL=Utils.js.map