UNPKG

@barchart/common-js

Version:
105 lines (103 loc) 3.79 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var string_exports = {}; __export(string_exports, { camelCase: () => camelCase, format: () => format, mask: () => mask, padLeft: () => padLeft, startCase: () => startCase, truncate: () => truncate }); module.exports = __toCommonJS(string_exports); var assert = __toESM(require("./assert.js")); var is = __toESM(require("./is.js")); const regex = {}; regex.camel = {}; regex.camel.violations = /\b[A-Z]/g; function startCase(s) { return s.split(" ").reduce((phrase, word) => { if (word.length !== 0) { phrase.push(word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()); } return phrase; }, []).join(" "); } function camelCase(s) { assert.argumentIsRequired(s, "s", String); return s.replace(regex.camel.violations, (m) => m.toLocaleLowerCase()); } function truncate(s, length) { if (is.string(s) && s.length > length) { return s.substring(0, length) + " ..."; } else { return s; } } function padLeft(s, length, character) { assert.argumentIsRequired(s, "s", String); assert.argumentIsRequired(length, "length", Number); assert.argumentIsRequired(character, "character", String); if (character.length !== 1) { throw new Error('The "character" argument must be one character in length.'); } return character.repeat(length - s.length) + s; } function mask(s, mask2, show, length) { assert.argumentIsRequired(s, "s", String); assert.argumentIsRequired(mask2, "mask", String); assert.argumentIsRequired(show, "show", Number); assert.argumentIsOptional(length, "length", Number); if (is.number(length) && !(length > 0)) { return ""; } const countShown = Math.min(s.length, Math.max(show, 0)); const countMasked = Math.max(s.length, Math.max(length || 0), 0) - countShown; let masked = `${mask2.slice(-1).repeat(countMasked)}${countShown > 0 ? s.slice(~countShown + 1) : ""}`; if (is.number(length) && !(length < 0) && length < s.length) { masked = masked.slice(~length + 1); } return masked; } function format(s, ...data) { assert.argumentIsRequired(s, "s", String); return s.replace(/{(\d+)}/g, (match, i) => { let replacement; if (i < data.length) { const item = data[i]; if (!is.undef(item) && !is.nil(item)) { replacement = item.toString(); } else { replacement = match; } } else { replacement = match; } return replacement; }); }