bc-node-sdk
Version:
BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.
177 lines (176 loc) • 6.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// Package Imports
const format = require('string-format');
// Other Imports
const constants_1 = require("../domain/constants");
/**
* Class {@link ParseUtil} encapsulates utility methods for parsing data.
*/
class ParseUtil {
/**
* Parses boolean from string.
* @param stringValue
* @returns
*/
static stringToBoolean(stringValue) {
if (typeof stringValue === 'string') {
switch (stringValue.toLowerCase()) {
case 'true':
case '1':
case 'on':
case 'yes':
return true;
default:
return false;
}
}
return stringValue || false;
}
/**
* Parses number from string.
* @param stringValue
* @returns
*/
static stringToNumber(stringValue) {
if (stringValue) {
try {
return parseInt(stringValue);
}
catch (e) {
return 0;
}
}
return 0;
}
/**
* Checks if two strings are equal.
* @param input1 First string to compare
* @param input2 Second string to compare
* @param ignoreCase If true, ignores case while comparing
* @param replaceInput If true, input strings are replaced with the replacement string before comparison
* @param replacementRegExp RegExp which is replaced with the replacement string
* @param replacement String with which the replacementRegExp is replaced
* @returns True if strings are equal, otherwise false
*/
static matchStrings(input1, input2, ignoreCase = false, replaceInput = false, replacementRegExp = /(\s)/g, replacement = constants_1.Defaults.String.Value) {
if (input1 && input2) {
if (ignoreCase) {
return replaceInput
? input1.replaceAll(replacementRegExp, replacement).toLowerCase() ===
input2.replaceAll(replacementRegExp, replacement).toLowerCase()
: input1.toLowerCase() === input2.toLowerCase();
}
return replaceInput
? input1.replaceAll(replacementRegExp, replacement) ===
input2.replaceAll(replacementRegExp, replacement)
: input1 === input2;
}
return false;
}
/**
* Converts a JSON object into a JSON string.
* @param json The JSON object to be converted
* @returns The JSON string representation of the JSON object
*/
static jsonStringify(json) {
return JSON.stringify(json);
}
/**
* Attempts to parse a JSON string into a JSON object.
* @param input The JSON string to be parsed
* @returns The parsed JSON object if successful, otherwise null
*/
static stringToObject(input) {
var _a;
input = (_a = (input === null || input === void 0 ? void 0 : input.replaceAll(`"`, `'`))) === null || _a === void 0 ? void 0 : _a.replaceAll(`'`, `"`);
try {
return JSON.parse(input);
}
catch (error) {
return constants_1.Defaults.Object.Value;
}
}
/**
* Attempts to parse a JSON string into a JSON object.
* @param json The JSON string to be parsed
* @returns The parsed JSON object if successful, otherwise null
*/
static tryParseJson(json) {
if (json) {
let parsed = {};
try {
parsed = JSON.parse(json);
return parsed;
}
catch (e) {
}
}
return null;
}
/**
* Format a given string using the format function from the string-format package.
* @param input The string to be formatted
* @param data An object containing the key-value pairs used to replace placeholders in the string
* @returns The formatted string if the input string is not null or undefined, otherwise an empty string
*/
static stringFormat(input, data) {
if (input) {
return format(input, Object.assign({}, data));
}
return constants_1.Defaults.String.Value;
}
/**
* Determines if a given string is valid JSON.
* @param input the string to be validated
* @returns true if the string is valid JSON, false otherwise
*/
static isValidJSON(input) {
try {
JSON.parse(input);
return true;
}
catch (e) {
return false;
}
}
/**
* Formats a given byte value into a human-readable format.
* @param bytes the value in bytes to be formatted
* @param decimals the number of decimal places to round to
* @returns the formatted string, e.g. "1.23 KB"
*/
static formatBytes(bytes, decimals = 2) {
if (bytes === 0)
return '0 Bytes';
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
;
/**
* Converts a JSON object into a blob, which can be used
* to create a file download.
* @param {any} json - The JSON object to convert.
* @returns {Blob} The blob containing the stringified JSON.
*/
static jsonToBlob(json) {
const stringified = JSON.stringify(json, null, 2);
const bytes = new TextEncoder().encode(stringified);
const blob = new Blob([bytes], {
type: "application/json;charset=utf-8"
});
return blob;
}
/**
* Checks if a given object is empty.
* @param obj The object to be checked
* @returns true if the object is empty, false otherwise
*/
static isEmptyObject(obj) {
return Object.keys(obj).length === 0;
}
}
exports.default = ParseUtil;