@thisisagile/easy
Version:
Straightforward library for building domain-driven microservice architectures
157 lines (155 loc) • 4.68 kB
JavaScript
import {
entries
} from "./chunk-H2762RTS.mjs";
import {
toList
} from "./chunk-A7C3XND3.mjs";
import {
asString,
capitalize,
replaceAll
} from "./chunk-BDA5LB4S.mjs";
import {
ofGet
} from "./chunk-SJGQU3OG.mjs";
import {
toName
} from "./chunk-ZPNFXK7Y.mjs";
import {
isEmpty,
isNotEmpty
} from "./chunk-DEJ7A5PY.mjs";
// src/types/Template.ts
var Template = class {
constructor(template2, subject = {}, options = {}) {
this.template = template2;
this.subject = subject;
this.options = options;
}
toString = () => {
return entries(this.options).reduce((t, [k]) => this.option(t, k), this.object()).replace(" ", " ");
};
props = (tmpl, key, result = toList()) => {
const i1 = tmpl.indexOf(`{${key}`);
if (i1 < 0) {
return result;
}
const i2 = tmpl.indexOf("}", i1);
return this.props(tmpl.slice(i2 + 1), key, result.add(tmpl.substring(i1 + 1, i2)));
};
object = () => {
return this.props(this.template, "this").reduce((t, p) => t.replace(`{${p}}`, textValue(this.subject, p.replace("this.", ""))), this.template);
};
option = (tmpl, prop) => {
return this.props(tmpl, prop).reduce((t, p) => t.replace(`{${p}}`, textValue(this.options, p)), tmpl);
};
};
function template(tmpl, subject, options = {}) {
return new Template(asString(tmpl), subject, {
type: toName(subject),
subject: text(JSON.stringify(subject)),
...options
});
}
var ToText = class {
constructor(subject) {
this.subject = subject;
}
get cap() {
return this.map((s) => capitalize(s?.toLowerCase()));
}
get capFirst() {
return this.map((s) => capitalize(s));
}
get title() {
return this.map(
(s) => s.split(" ").map((w) => text(w).cap).join(" ")
);
}
get pascal() {
return this.title.replace(" ", "");
}
get lower() {
return this.map((s) => s.toLowerCase());
}
get upper() {
return this.map((s) => s.toUpperCase());
}
get camel() {
return this.title.trim.map((s) => `${s.charAt(0).toLowerCase()}${s.slice(1)}`);
}
get kebab() {
return this.lower.replace(" ", "-");
}
get strictKebab() {
return this.map((s) => s.replace(/[^a-z\d]+/gi, " ").trim()).kebab;
}
get slug() {
return this.map(
(s) => s.replace(/ß/g, "ss").normalize("NFKD").replace(/[\u0300-\u036F]/g, "").toLowerCase().replace(/[^a-z\d]+/g, "-").replace(/(^-)|(-$)/g, "")
);
}
get snake() {
return this.upper.replace(" ", "_");
}
get plural() {
return this.ifLike("") ?? this.add("s");
}
get space() {
return this.map((s) => s.replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/[_-]/g, " "));
}
get sentence() {
return this.isEmpty ? this : this.map((s) => `${s.charAt(0).toUpperCase()}${s.slice(1)}.`);
}
get initials() {
return this.map(
(s) => s.split(" ").map((w) => w[0]).join("")
);
}
get trim() {
return this.map((s) => s.replace(/[- ,_#]/g, ""));
}
get isEmpty() {
return isEmpty(this.toString());
}
parse = (subject, options = {}) => text(template(this.subject, subject, { type: toName(subject), ...options }));
is = (...others) => others.some((o) => this.toString() === text(o).toString());
equals = this.is;
isLike = (...others) => others.some((o) => this.trim.lower.is(text(o).trim.lower));
ifLike = (...others) => this.isLike(...others) ? this : void 0;
endsWith = (end) => this.subject.endsWith(asString(end));
startsWith = (end) => this.subject.startsWith(asString(end));
first = (n) => this.map((s) => s.substring(0, n));
last = (n) => this.map((s) => s.substring(s.length - n));
map = (func) => text(ofGet(func, this.subject));
replace = (search, replace) => this.map((s) => replaceAll(s, search, replace));
add = (add, separator = "") => this.map((s) => isNotEmpty(add) ? `${s}${separator}${text(add)}` : s);
with = (separator, ...other) => this.map(
(s) => toList(s).add(...other.map((u) => text(u).toString())).filter((s2) => isNotEmpty(s2)).join(separator)
);
split = (separator = " ") => toList(this.subject.split(separator));
toString() {
return this.subject;
}
toJSON() {
return this.subject;
}
};
function text(subject, alt = "") {
const sub = subject ? asString(subject) : alt;
return new ToText(sub !== "[object Object]" ? sub : "");
}
function textValue(subject, prop) {
const p = prop.split(".");
const root = subject?.[p[0]];
const initial = typeof root === "object" && root !== null ? root : text(root);
return p.splice(1).reduce((t, s) => t?.[s], initial)?.toString() ?? "";
}
export {
Template,
template,
ToText,
text,
textValue
};
//# sourceMappingURL=chunk-MCCIBDEH.mjs.map