@tempots/std
Version:
Std library for TypeScript. Natural complement to the Tempo libraries.
201 lines (200 loc) • 8.47 kB
JavaScript
import { MissingImplementationError as h } from "./error.js";
import { mapRegExp as l } from "./regexp.js";
const Z = (t, n) => {
const e = t.indexOf(n);
return e < 0 ? "" : t.substring(e + n.length);
}, D = (t, n) => {
const e = t.lastIndexOf(n);
return e < 0 ? "" : t.substring(e + n.length);
}, P = (t, n) => {
const e = t.indexOf(n);
return e < 0 ? "" : t.substring(0, e);
}, R = (t, n) => {
const e = t.lastIndexOf(n);
return e < 0 ? "" : t.substring(0, e);
}, g = (t) => t.substring(0, 1).toUpperCase() + t.substring(1), a = (t) => t.toUpperCase(), F = (t, n = !1) => n ? l(g(t), _, a) : l(g(t), z, a), G = (t) => t.replace($, `
`), Q = (t, n) => t == null && n == null ? 0 : t == null ? -1 : n == null ? 1 : S(t.toLowerCase(), n.toLowerCase()), K = (t, n) => t.substring(t.length - n.length).toLowerCase() === n.toLowerCase(), q = (t, n) => t.substring(0, n.length).toLowerCase() === n.toLowerCase(), J = (t, n) => A(
t.toLowerCase(),
n.map((e) => e.toLowerCase())
), V = (t, n) => y(
t.toLowerCase(),
n.map((e) => e.toLowerCase())
), X = (t) => t.trim().replace(d, " "), S = (t, n) => t < n ? -1 : t > n ? 1 : 0, p = (t, n) => t.toLowerCase().includes(n.toLowerCase()), Y = (t, n) => t.split(n).length - 1, v = (t, n) => n.some((e) => p(t, e)), tt = (t, n) => n.some((e) => t.includes(e)), nt = (t, n) => n.every((e) => p(t, e)), et = (t, n) => n.every((e) => t.includes(e)), rt = (t) => t.replace(/_/g, "-"), st = (t, n) => {
if (t === n) return -1;
const e = Math.min(t.length, n.length);
for (let r = 0; r < e; r++)
if (t.substring(r, r + 1) !== n.substring(r, r + 1)) return r;
return e;
}, b = (t, n = 20, e = "…") => {
const r = t.length, s = e.length;
return r > n ? n < s ? e.slice(s - n, n) : t.slice(0, n - s) + e : t;
}, it = (t, n = 20, e = "…") => {
const r = t.length, s = e.length;
if (r > n) {
if (n <= s)
return b(t, n, e);
const c = Math.ceil((n - s) / 2), u = Math.floor((n - s) / 2);
return t.slice(0, c) + e + t.slice(r - u);
} else return t;
}, A = (t, n) => n.some((e) => t.endsWith(e)), ot = (t, n) => Array.from(t).filter(n).join(""), ct = (t, n) => x(t).filter(n).map((r) => String.fromCharCode(r)).join(""), ut = (t, n = 2166136261) => {
let e = n;
for (let r = 0, s = t.length; r < s; r++)
e ^= t.charCodeAt(r), e += (e << 1) + (e << 4) + (e << 7) + (e << 8) + (e << 24);
return e >>> 0;
}, lt = (t) => t != null && t.length > 0, gt = (t) => E(t).split("_").join(" "), at = (t) => t.length > 0 && !N.test(t), ft = (t) => M.test(t), ht = (t) => !T.test(t), pt = (t) => t.toLowerCase() === t, dt = (t) => t.toUpperCase() === t, Ct = (t, n) => t != null && t !== "" ? t : n, St = (t) => U.test(t), bt = (t) => t == null || t === "", At = (t) => t.substring(0, 1).toLowerCase() + t.substring(1), L = (t, n = 1) => {
const e = Math.floor((t.length - n + 1) * Math.random());
return t.substring(e, e + n);
}, w = (t, n) => Array.from({ length: n }, () => L(t)).join(""), Lt = (t) => w(j, t), wt = (t, n) => Array.from(n).map(t), mt = (t, n) => t.split(n).join(""), It = (t, n) => t.endsWith(n) ? t.substring(0, t.length - n.length) : t, yt = (t, n, e) => t.substring(0, n) + t.substring(n + e), xt = (t, n) => t.startsWith(n) ? t.substring(n.length) : t, Ot = (t, n) => {
const e = t.indexOf(n);
return e < 0 ? t : t.substring(0, e) + t.substring(e + n.length);
}, Wt = (t) => {
const n = Array.from(t);
return n.reverse(), n.join("");
}, m = (t, n = "'") => n === "'" ? t.includes("'") ? t.includes('"') ? "'" + t.split("'").join("\\'") + "'" : '"' + t + '"' : "'" + t + "'" : t.includes('"') ? t.includes("'") ? '"' + t.split('"').join('\\"') + '"' : "'" + t + "'" : '"' + t + '"', I = (t, n = "'") => n + t.split(n).join("\\" + n) + n, Et = (t, n = "'") => t.indexOf(`
`) >= 0 ? I(t, "`") : m(t, n), Bt = (t, n) => {
const e = t.indexOf(n);
return e < 0 ? [t] : [t.substring(0, e), t.substring(e + n.length)];
}, y = (t, n) => n.some((e) => t.startsWith(e)), jt = (t, n, e = n) => `${n}${t}${e}`, x = (t) => Array.from({ length: t.length }, (n, e) => t.charCodeAt(e)), zt = (t, n) => {
const e = [];
for (let r = 0; r < t.length; r += n)
e.push(t.substring(r, r + n));
return e;
}, Tt = (t) => t.split(C), Nt = (t, n) => W(O(t, n), n), O = (t, n) => {
const e = new Set(n);
let r = 0;
for (let s = 0; s < t.length && e.has(t.charAt(s)); s++)
r++;
return t.substring(r);
}, W = (t, n) => {
const e = new Set(n), r = t.length;
let s = r, c;
for (let u = 0; u < r && (c = r - u - 1, e.has(t.charAt(c))); u++)
s = c;
return t.substring(0, s);
}, E = (t) => (t = t.replace(/::/g, "/"), t = t.replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2"), t = t.replace(/([a-z\d])([A-Z])/g, "$1_$2"), t = t.replace(/-/g, "_"), t.toLowerCase()), _t = (t) => t.substring(0, 1).toUpperCase() + t.substring(1), Mt = (t, n = 78, e = "", r = `
`) => t.split(C).map(
(s) => B(s.replace(d, " ").trim(), n, e, r)
).join(r), f = (t, n) => {
if (n < 0 || n >= t.length) return !1;
const e = t.charCodeAt(n);
return e === 9 || e === 10 || e === 11 || e === 12 || e === 13 || e === 32;
}, Ut = (t) => {
if (typeof Buffer < "u")
return Buffer.from(t).toString("base64");
if (typeof btoa < "u")
return btoa(t);
throw new h(
"No implementation found for base64 encoding"
);
}, $t = (t) => {
if (typeof Buffer < "u")
return Buffer.from(t, "base64").toString("utf8");
if (typeof atob < "u")
return atob(t);
throw new h(
"No implementation found for base64 decoding"
);
}, B = (t, n, e, r) => {
const s = [], c = t.length, u = e.length;
let o = 0;
for (n -= u; ; ) {
if (o + n >= c - u) {
s.push(t.substring(o));
break;
}
let i = 0;
for (; !f(t, o + n - i) && i < n; ) i++;
if (i === n) {
for (i = 0; !f(t, o + n + i) && o + n + i < c; ) i++;
s.push(t.substring(o, o + n + i)), o += n + i + 1;
} else
s.push(t.substring(o, o + n - i)), o += n - i + 1;
}
return e + s.join(r + e);
}, kt = (t, n, e) => {
const r = e - t.length;
return r > 0 ? n.repeat(r) + t : t;
}, Ht = (t, n, e) => {
const r = e - t.length;
return r > 0 ? t + n.repeat(r) : t;
}, Zt = (t, n) => {
const e = t.lastIndexOf(n);
return e >= 0 ? [t.substring(0, e), t.substring(e + n.length)] : [t];
}, Dt = (t, n) => {
const e = t.indexOf(n);
return e >= 0 ? [t.substring(0, e), t.substring(e + n.length)] : [t];
}, j = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", z = /[^a-zA-Z]([a-z])/g, T = /[^\t\n\r ]/, N = /[^a-zA-Z]/, _ = /[ \t\r\n][a-z]/g, M = /^[a-z0-9]+$/i, U = /^[0-9]+$/, d = /[ \t\r\n]+/g, C = /\r\n|\n\r|\n|\r/g, $ = /\r\n|\n\r|\r/g;
export {
G as canonicalizeNewlines,
g as capitalize,
F as capitalizeWords,
zt as chunkString,
X as collapseText,
Q as compareCaseInsensitive,
S as compareStrings,
et as containsAllText,
nt as containsAllTextCaseInsensitive,
tt as containsAnyText,
v as containsAnyTextCaseInsensitive,
Y as countStringOccurrences,
rt as dasherize,
$t as decodeBase64,
Ot as deleteFirstFromString,
It as deleteStringAfter,
xt as deleteStringBefore,
mt as deleteSubstring,
b as ellipsis,
it as ellipsisMiddle,
Ut as encodeBase64,
ct as filterCharcodes,
ot as filterChars,
gt as humanize,
Ct as ifEmptyString,
at as isAlpha,
ft as isAlphaNum,
ht as isBreakingWhitespace,
St as isDigitsOnly,
bt as isEmptyString,
pt as isLowerCase,
f as isSpaceAt,
dt as isUpperCase,
Et as jsQuote,
At as lowerCaseFirst,
kt as lpad,
wt as mapChars,
I as quote,
w as randomStringSequence,
Lt as randomStringSequenceBase64,
L as randomSubString,
Wt as reverseString,
Ht as rpad,
m as smartQuote,
Dt as splitStringOnFirst,
Zt as splitStringOnLast,
Bt as splitStringOnce,
A as stringEndsWithAny,
lt as stringHasContent,
ut as stringHashCode,
y as stringStartsWithAny,
x as stringToCharcodes,
st as stringsDifferAtIndex,
Z as substringAfter,
D as substringAfterLast,
P as substringBefore,
R as substringBeforeLast,
jt as surroundString,
p as textContainsCaseInsensitive,
J as textEndsWithAnyCaseInsensitive,
K as textEndsWithCaseInsensitive,
V as textStartsWithAnyCaseInsensitive,
q as textStartsWithCaseInsensitive,
Tt as textToLines,
Nt as trimChars,
O as trimCharsLeft,
W as trimCharsRight,
yt as trimStringSlice,
E as underscore,
_t as upperCaseFirst,
Mt as wrapColumns,
B as wrapLine
};