|
| 1 | +import { WMLConstructorDecorator } from "./models"; |
| 2 | + |
| 3 | + |
| 4 | +// TODO find a library that is framework and agnosticfriendly |
| 5 | +@WMLConstructorDecorator |
| 6 | +export class WMLStringObject { |
| 7 | + constructor(props = {}) { |
| 8 | + Object.assign(this, props); |
| 9 | + } |
| 10 | + |
| 11 | + orig = "myString"; |
| 12 | + entitySuffix = ""; |
| 13 | + |
| 14 | + get prefix() { |
| 15 | + return this.orig.split(this.entitySuffix)[0]; |
| 16 | + } |
| 17 | + |
| 18 | + camelCase = (stripSuffix = true, suffix = "") => { |
| 19 | + const str = stripSuffix ? this.prefix : this.orig; |
| 20 | + return str |
| 21 | + .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")) |
| 22 | + .replace(/^(.)/, (_, c) => c.toLowerCase()) + suffix; |
| 23 | + }; |
| 24 | + |
| 25 | + classify = (stripSuffix = true, suffix = "") => { |
| 26 | + const str = stripSuffix ? this.prefix : this.orig; |
| 27 | + return str |
| 28 | + .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")) |
| 29 | + .replace(/^(.)/, (_, c) => c.toUpperCase()) + suffix; |
| 30 | + }; |
| 31 | + |
| 32 | + capitalize = (stripSuffix = true, suffix = "") => { |
| 33 | + const str = stripSuffix ? this.prefix : this.orig; |
| 34 | + return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase() + suffix; |
| 35 | + }; |
| 36 | + |
| 37 | + dasherize = (stripSuffix = true, suffix = "") => { |
| 38 | + const str = stripSuffix ? this.prefix : this.orig; |
| 39 | + return str.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/[_\s]+/g, "-") + suffix; |
| 40 | + }; |
| 41 | + |
| 42 | + lowerCase = (stripSuffix = true, suffix = "") => { |
| 43 | + return (stripSuffix ? this.prefix : this.orig).toLowerCase() + suffix; |
| 44 | + }; |
| 45 | + |
| 46 | + pascalCase = (stripSuffix = true, suffix = "") => { |
| 47 | + const str = stripSuffix ? this.prefix : this.orig; |
| 48 | + return str |
| 49 | + .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")) |
| 50 | + .replace(/^(.)/, (_, c) => c.toUpperCase()) + suffix; |
| 51 | + }; |
| 52 | + |
| 53 | + upperCase = (stripSuffix = true, suffix = "") => { |
| 54 | + return (stripSuffix ? this.prefix : this.orig).toUpperCase() + suffix; |
| 55 | + }; |
| 56 | + |
| 57 | + titleCase = (stripSuffix = true, suffix = "") => { |
| 58 | + const str = stripSuffix ? this.prefix : this.orig; |
| 59 | + return str |
| 60 | + .replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : "")) |
| 61 | + .replace(/^(.)/, (_, c) => c.toUpperCase()) + suffix; |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
0 commit comments