UNPKG

@khatastroffik/react-text-renderer-components

Version:

a zero-dependencies component library providing (pure) text rendering for common and custom data/field types.

89 lines (82 loc) 3.9 kB
var $d05063a33f9bc032$exports = require("./AbstractRenderer.51d1084a.js"); function $parcel$export(e, n, v, s) { Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true}); } $parcel$export(module.exports, "TextTransformations", () => $119c2bd86b00d3e4$export$fcbd92318e8591d4); $parcel$export(module.exports, "TextRenderer", () => $119c2bd86b00d3e4$export$75ebcd2c8ed5b901); const $119c2bd86b00d3e4$export$fcbd92318e8591d4 = [ "no-op", "lower-case", "upper-case", "snake-case", "pascal-case", "camel-case", "kebab-case", "to-base64", "from-base64", "camel-to-kebab", "pascal-to-kebab" ]; const $119c2bd86b00d3e4$var$INVALID_CHARACTER_ERROR = "InvalidCharacterError"; const $119c2bd86b00d3e4$var$INVALID_UTF16_ERROR_MSG = "The input value is not well formed: it is not a valid UTF-16 string, since it contains lone surrogates."; const $119c2bd86b00d3e4$var$INVALID_BASE64_ERROR_MSG = "The input value is not a valid BASE64 string and cannot be decoded."; const $119c2bd86b00d3e4$var$TransformationRegex = /([a-zA-Z0-9]+)(?=)/g; const $119c2bd86b00d3e4$var$Base64ValidityRegex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/; class $119c2bd86b00d3e4$export$75ebcd2c8ed5b901 extends (0, $d05063a33f9bc032$exports.AbstractRenderer) { getFormatedText() { const sourceText = this.value ?? ""; switch(this.props.transform ?? "noop"){ case 'lower-case': return sourceText.toLowerCase(); case 'upper-case': return sourceText.toUpperCase(); case 'noop': return sourceText; case 'snake-case': return this.snakeCase(sourceText); case 'pascal-case': return this.pascalCase(sourceText); case 'camel-case': return this.camelCase(sourceText); case 'kebab-case': return this.kebabCase(sourceText); case 'to-base64': return this.toBase64(sourceText); case 'from-base64': return this.fromBase64(sourceText); case 'camel-to-kebab': case 'pascal-to-kebab': return this.boundedTextToKebabCase(sourceText); default: return sourceText; } } snakeCase(text) { return text.match($119c2bd86b00d3e4$var$TransformationRegex).join("_"); } camelCase(text) { return text.match($119c2bd86b00d3e4$var$TransformationRegex).map((match, index)=>(index > 0 ? match[0].toUpperCase() : match[0].toLowerCase()) + match.slice(1).toLowerCase()).join(""); } pascalCase(text) { return text.match($119c2bd86b00d3e4$var$TransformationRegex).map((match)=>match[0].toUpperCase() + match.slice(1).toLowerCase()).join(""); } kebabCase(text) { return text.match($119c2bd86b00d3e4$var$TransformationRegex).join("-").toLowerCase(); } boundedTextToKebabCase(text) { return text.replace(/(?<!^)[A-Z]/g, (match)=>"-" + match).toLowerCase(); } toBase64(text) { if (!text.isWellFormed()) throw new DOMException($119c2bd86b00d3e4$var$INVALID_UTF16_ERROR_MSG, $119c2bd86b00d3e4$var$INVALID_CHARACTER_ERROR); const bytes = new TextEncoder().encode(text); const binString = Array.from(bytes, (byte)=>String.fromCodePoint(byte)).join(""); return btoa(binString); } fromBase64(base64) { if (!$119c2bd86b00d3e4$var$Base64ValidityRegex.test(base64)) throw new DOMException($119c2bd86b00d3e4$var$INVALID_BASE64_ERROR_MSG, $119c2bd86b00d3e4$var$INVALID_CHARACTER_ERROR); const binString = atob(base64); const bytes = Uint8Array.from(binString, (m)=>m.codePointAt(0)); return new TextDecoder().decode(bytes); } } //# sourceMappingURL=TextRenderer.4be38bb5.js.map