UNPKG

@nodeject/ui-components

Version:

UI library for non-trivial components

91 lines (90 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringToColour = exports.getBasicProps = exports.getClassAndIdNames = exports.getClassName = exports.getIdName = exports.formatWork = exports.formatCurrency = exports.flattenTree = exports.getNodeParentKey = void 0; var _ = require("lodash"); var getNodeParentKey = function (key, tree) { var parentKey; for (var i = 0; i < tree.length; i++) { var node = tree[i]; if (node.children) { if (node.children.some(function (item) { return item.value === key; })) { parentKey = node.value; } else if (exports.getNodeParentKey(key, node.children)) { parentKey = exports.getNodeParentKey(key, node.children); } } } return parentKey; }; exports.getNodeParentKey = getNodeParentKey; var flattenTree = function (entries) { return !entries ? [] : _.flatMapDeep(entries, function (e) { return [ e, e.children ? exports.flattenTree(e.children) : [] ]; }); }; exports.flattenTree = flattenTree; var formatCurrency = function (currencyValue, currencySymbol, decimals) { if (decimals === void 0) { decimals = 0; } if (!currencyValue) { currencyValue = 0; } return (currencySymbol + ("" + currencyValue .toFixed(decimals) .replace(/\B(?=(\d{3})+(?!\d))/g, ','))); }; exports.formatCurrency = formatCurrency; var formatWork = function (workValue, workUnit) { if (!workValue) { workValue = 0; } return (workValue.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') + " " + workUnit); }; exports.formatWork = formatWork; var getIdName = function (cssClassName, id) { return cssClassName + "_" + id; }; exports.getIdName = getIdName; var getClassName = function (cssClassName) { return cssClassName; }; exports.getClassName = getClassName; var getClassAndIdNames = function (cssClassName, id) { return { className: exports.getClassName(cssClassName), idName: exports.getIdName(cssClassName, id) }; }; exports.getClassAndIdNames = getClassAndIdNames; var getBasicProps = function (styleName, id) { var classAndIdNames = exports.getClassAndIdNames(styleName, id); return { key: classAndIdNames.idName, className: classAndIdNames.className, id: classAndIdNames.idName }; }; exports.getBasicProps = getBasicProps; var stringToColour = function (str) { if (str) { var hash = 0; for (var i = 0; i < str.length; i++) { hash = str.charCodeAt(i) + ((hash << 5) - hash); } var colour = '#'; for (var i = 0; i < 3; i++) { var value = (hash >> (i * 8)) & 0xff; colour += ('00' + value.toString(16)).substr(-2); } return colour; } else { return '#ffffff'; } }; exports.stringToColour = stringToColour;