bc-payments-sdk
Version:
BetterCommerce's Payments NodeJS SDK is a complete solution for storefront clients that integrate payments. `bc-payments-sdk` is a single point interface for storefront clients for interacting with payment gateways.
77 lines (76 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.groupMatch = exports.tryParseJson = exports.stringToBoolean = exports.matchStrings = void 0;
/**
* Compares two strings for equality.
*
* @param input1 - The first string to compare.
* @param input2 - The second string to compare.
* @param ignoreCase - Optional boolean to indicate if the comparison should ignore case differences. Defaults to false.
* @returns A boolean indicating whether the two strings are equal. Returns false if either string is null or undefined.
*/
const matchStrings = (input1, input2, ignoreCase = false) => {
if (input1 && input2) {
if (ignoreCase) {
return input1.toLowerCase() === input2.toLowerCase();
}
return input1 === input2;
}
return false;
};
exports.matchStrings = matchStrings;
/**
* Parses boolean from string.
* @param stringValue
* @returns
*/
const stringToBoolean = (stringValue) => {
if (stringValue) {
switch (stringValue.toLowerCase()) {
case "true":
case "1":
case "on":
case "yes":
return true;
default:
return false;
}
}
return false;
};
exports.stringToBoolean = stringToBoolean;
/**
* Attempts to parse a JSON string and returns the resulting object.
*
* @param json - The JSON string to parse.
* @returns The parsed object if successful, otherwise null if the input is invalid or parsing fails.
*/
const tryParseJson = (json) => {
if (json) {
let parsed = {};
try {
// If the string contains escaped quotes, unescape them first
if (typeof json === 'string' && json.includes('\\"')) {
json = json.replace(/\\"/g, '"');
}
parsed = JSON.parse(json);
return parsed;
}
catch (e) {
}
}
return null;
};
exports.tryParseJson = tryParseJson;
/**
* Executes a regular expression on a string and returns the match results.
*
* @param input - The string to search for matches.
* @param regExp - The regular expression to execute.
* @returns The match results as an array, or null if no match was found.
*/
const groupMatch = (input, regExp) => {
const matches = regExp.exec(input);
return matches;
};
exports.groupMatch = groupMatch;