UNPKG

bc-node-sdk

Version:

BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.

109 lines (108 loc) 5.35 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../domain/constants"); const app_util_1 = __importDefault(require("./app-util")); /** * Class {@link UIUtil} encapsulates the utility methods related to User Interface. */ class UIUtil { /** * Returns a class string consisting of the given class names. Ignores falsy values. * Example: classNames('foo', 0, 'bar', null, 'baz') => 'foo bar baz' * * @function * @param {...any} classes The class names to be combined. * @returns {string} The combined class string. */ static classNames(...classes) { return classes.filter(Boolean).join(' '); } /** * Generates a flat array from a tree structured data. * * @param {any[]} treeData - The tree structured data to be flattened. * @param {boolean} [isChild=false] - Whether the current element is a child or not. * @param {string} [suffix=''] - The suffix to be used for the parent id. * @returns {any[]} - The flattened array. */ static generateFlatArrayFromTree(treeData, isChild = false, suffix = constants_1.Defaults.String.Value) { return treeData === null || treeData === void 0 ? void 0 : treeData.reduce((flattenedArray, currentElem) => { var _a; const parentId = isChild ? `${suffix}--` : constants_1.Defaults.String.Value; const flattenedArr = Object.assign(Object.assign({}, currentElem), { parentId }); const { items } = flattenedArr, rest = __rest(flattenedArr, ["items"]); const flattenedArrExcludingItemsProp = Object.assign({}, rest); // Remove the "items" property flattenedArray.push(flattenedArrExcludingItemsProp); if ((currentElem === null || currentElem === void 0 ? void 0 : currentElem.items) && ((_a = currentElem === null || currentElem === void 0 ? void 0 : currentElem.items) === null || _a === void 0 ? void 0 : _a.length) > 0) { flattenedArray.push(...this.generateFlatArrayFromTree(currentElem === null || currentElem === void 0 ? void 0 : currentElem.items, true, parentId)); } return flattenedArray; }, []); } /** * Converts a file to a base64 string. If onlyBinary is true, the method will return only the binary part of the base64 string. * @param file the file to convert * @param onlyBinary if true, the method will return only the binary part of the base64 string * @returns a promise resolved with the base64 string */ static convertToBase64(file, onlyBinary = false) { return new Promise((resolve) => { const fileReader = new FileReader(); fileReader.readAsDataURL(file); fileReader.onload = () => { var _a; let result = fileReader === null || fileReader === void 0 ? void 0 : fileReader.result; if (onlyBinary) { result = (_a = result === null || result === void 0 ? void 0 : result.split(',')) === null || _a === void 0 ? void 0 : _a[1]; } resolve(result); }; fileReader.onerror = (error) => { app_util_1.default.logError(error); }; }); } /** * Converts a base64 string to a blob. * @param {string} base64File The base64 string to convert. * @returns {Blob} The blob from the base64 string. */ static convertBase64ToBlob(base64File, options) { try { const byteCharacters = atob(base64File); const byteNumbers = new Uint8Array(byteCharacters === null || byteCharacters === void 0 ? void 0 : byteCharacters.length); for (let i = 0; i < (byteCharacters === null || byteCharacters === void 0 ? void 0 : byteCharacters.length); i++) { byteNumbers[i] = byteCharacters === null || byteCharacters === void 0 ? void 0 : byteCharacters.charCodeAt(i); } return new Blob([byteNumbers], Object.assign({}, options)); } catch (error) { return new Blob(constants_1.Defaults.Array.Value); } } static async copyToClipboard(text) { var _a, _b; try { await ((_b = (_a = window === null || window === void 0 ? void 0 : window.navigator) === null || _a === void 0 ? void 0 : _a.clipboard) === null || _b === void 0 ? void 0 : _b.writeText(text)); return true; } catch (error) { return false; } } } exports.default = UIUtil;