@khatastroffik/react-text-renderer-components
Version:
a zero-dependencies component library providing (pure) text rendering for common and custom data/field types.
83 lines (78 loc) • 3.71 kB
JavaScript
import {AbstractRenderer as $akhrn$AbstractRenderer} from "./AbstractRenderer.78c11f00.js";
const $1aabebd496893ba2$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 $1aabebd496893ba2$var$INVALID_CHARACTER_ERROR = "InvalidCharacterError";
const $1aabebd496893ba2$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 $1aabebd496893ba2$var$INVALID_BASE64_ERROR_MSG = "The input value is not a valid BASE64 string and cannot be decoded.";
const $1aabebd496893ba2$var$TransformationRegex = /([a-zA-Z0-9]+)(?=)/g;
const $1aabebd496893ba2$var$Base64ValidityRegex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
class $1aabebd496893ba2$export$75ebcd2c8ed5b901 extends (0, $akhrn$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($1aabebd496893ba2$var$TransformationRegex).join("_");
}
camelCase(text) {
return text.match($1aabebd496893ba2$var$TransformationRegex).map((match, index)=>(index > 0 ? match[0].toUpperCase() : match[0].toLowerCase()) + match.slice(1).toLowerCase()).join("");
}
pascalCase(text) {
return text.match($1aabebd496893ba2$var$TransformationRegex).map((match)=>match[0].toUpperCase() + match.slice(1).toLowerCase()).join("");
}
kebabCase(text) {
return text.match($1aabebd496893ba2$var$TransformationRegex).join("-").toLowerCase();
}
boundedTextToKebabCase(text) {
return text.replace(/(?<!^)[A-Z]/g, (match)=>"-" + match).toLowerCase();
}
toBase64(text) {
if (!text.isWellFormed()) throw new DOMException($1aabebd496893ba2$var$INVALID_UTF16_ERROR_MSG, $1aabebd496893ba2$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 (!$1aabebd496893ba2$var$Base64ValidityRegex.test(base64)) throw new DOMException($1aabebd496893ba2$var$INVALID_BASE64_ERROR_MSG, $1aabebd496893ba2$var$INVALID_CHARACTER_ERROR);
const binString = atob(base64);
const bytes = Uint8Array.from(binString, (m)=>m.codePointAt(0));
return new TextDecoder().decode(bytes);
}
}
export {$1aabebd496893ba2$export$fcbd92318e8591d4 as TextTransformations, $1aabebd496893ba2$export$75ebcd2c8ed5b901 as TextRenderer};
//# sourceMappingURL=TextRenderer.132754ef.js.map