@cloud-copilot/iam-simulate
Version:
Simulate evaluation of AWS IAM policies
56 lines • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseNumber = parseNumber;
exports.checkIfNumeric = checkIfNumeric;
const util_js_1 = require("../../util.js");
/**
* Parse a string to a number, returns undefined if the string is not a number
*
* @param value the string to parse
* @returns the number or undefined
*/
function parseNumber(value) {
let number = undefined;
if (value.includes('.')) {
number = parseFloat(value);
}
else {
number = parseInt(value, 10);
}
if ((0, util_js_1.isNotDefined)(number) || isNaN(number)) {
return undefined;
}
return number;
}
/**
* Test two values to see if they are numbers, if they are, run the check function
*
* @param policyValue
* @param testValue
* @param check
* @returns
*/
function checkIfNumeric(policyValue, testValue, check) {
const policyNumber = parseNumber(policyValue);
const testNumber = parseNumber(testValue);
if ((0, util_js_1.isNotDefined)(policyNumber)) {
return {
value: policyValue,
matches: false,
errors: [`${policyValue} is not a number`]
};
}
if ((0, util_js_1.isNotDefined)(testNumber)) {
return {
value: policyValue,
matches: false,
errors: [`request value '${testValue}' is not a number`]
};
}
const matches = check(policyNumber, testNumber);
return {
value: policyValue,
matches
};
}
//# sourceMappingURL=numeric.js.map