@koine/utils 1.2.3 → 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/render.mjs ADDED
@@ -0,0 +1,165 @@
1
+ var varname = "data";
2
+ var evaluate = /<%([\s\S]+?(\}?)+)%>/g;
3
+ var interpolate = /<%=([\s\S]+?)%>/g;
4
+ var conditional = /<%\?(\?)?\s*([\s\S]*?)\s*%>/g;
5
+ var iterate = /<%~\s*(?:%>|([\s\S]+?)\s*:\s*([\w$]+)\s*(?::\s*([\w$]+))?\s*%>)/g;
6
+ // const encode = /<%!([\s\S]+?)%>/g;
7
+ var use = /<%#([\s\S]+?)%>/g;
8
+ var useParams = /(^|[^\w$])def(?:\.|\[['"])([\w$.]+)(?:['"]\])?\s*:\s*([\w$.]+|"[^"]+"|'[^']+'|\{[^}]+\})/g;
9
+ var define = /<%##\s*([\w.$]+)\s*(:|=)([\s\S]+?)#%>/g;
10
+ var defineParams = /^\s*([\w$]+):([\s\S]+)/;
11
+ var start = "'+(";
12
+ var end = ")+'";
13
+ // const startencode = "'+encodeHTML(";
14
+ var skip = /$^/;
15
+ var resolveDefs = function (block, def) {
16
+ return (typeof block === "string" ? block : block.toString())
17
+ .replace(define || skip, function (_, code, assign, value) {
18
+ if (code.indexOf("def.") === 0) {
19
+ code = code.substring(4);
20
+ }
21
+ if (!(code in def)) {
22
+ if (assign === ":") {
23
+ value.replace(defineParams,
24
+ // @ts-expect-error nevermind
25
+ function (_, param, v) {
26
+ def[code] = { arg: param, text: v };
27
+ });
28
+ // @ts-expect-error nevermind
29
+ if (!(code in def))
30
+ def[code] = value;
31
+ }
32
+ else {
33
+ new Function("def", "def['" + code + "']=" + value)(def);
34
+ }
35
+ }
36
+ return "";
37
+ })
38
+ .replace(use || skip, function (_, code) {
39
+ code = code.replace(useParams, function (_, s, d, param) {
40
+ if (def[d] && def[d].arg && param) {
41
+ var rw = (d + ":" + param).replace(/'|\\/g, "_");
42
+ def.__exp = def.__exp || {};
43
+ def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
44
+ return s + "def.__exp['" + rw + "']";
45
+ }
46
+ return s;
47
+ });
48
+ var v = new Function("def", "return " + code)(def);
49
+ return v ? resolveDefs(v, def) : v;
50
+ });
51
+ };
52
+ var unescape = function (code) {
53
+ return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
54
+ };
55
+ /**
56
+ * Render template (adapted from doT.js)
57
+ *
58
+ * The data made available to the template is always on the `data` key, e.g.:
59
+ * `renderer({ myVal: "xx" })`
60
+ * ... will be accessible on
61
+ * `<%= data.myVal %>`
62
+ *
63
+ * The default delimiters are customised to work without conflicts with Blade and Twig:
64
+ * ```
65
+ * <% %> for evaluation
66
+ * <%= %> for interpolation
67
+ * <%? %> for conditionals
68
+ * <%~ %> for array iteration
69
+ * <%# %> for compile-time evaluation/includes and partials
70
+ * <%## #%> for compile-time defines
71
+ *
72
+ * Unsupported:
73
+ * <%! %> for interpolation with encoding
74
+ * ```
75
+ *
76
+ * @example
77
+ *
78
+ * ```js
79
+ * import { render } from "...";
80
+ *
81
+ * const data = { name: "XYZ" };
82
+ * const tpl = `Hello <%= data.name %>`;
83
+ * const renderer = render(tpl);
84
+ *
85
+ * console.log(renderer(data)); // outputs 'Hello XYZ'
86
+ * ```
87
+ *
88
+ * @borrows [olado/doT by Laura Doktorova](https://github.com/olado/doT)
89
+ * @see https://olado.github.io/doT/index.html
90
+ */
91
+ export var render = function (tmpl, def) {
92
+ var sid = 0;
93
+ var indv;
94
+ var str = use || define ? resolveDefs(tmpl, def || {}) : tmpl;
95
+ str = ("var X='" +
96
+ str
97
+ .replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g, " ")
98
+ .replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g, "")
99
+ .replace(/'|\\/g, "\\$&")
100
+ .replace(interpolate || skip, function (_, code) { return start + unescape(code) + end; })
101
+ // .replace(
102
+ // encode || skip,
103
+ // (_, code) => cse.startencode + unescape(code) + cse.end
104
+ // )
105
+ .replace(conditional || skip, function (_, elseCase, code) {
106
+ return elseCase
107
+ ? code
108
+ ? "';}else if(" + unescape(code) + "){X+='"
109
+ : "';}else{X+='"
110
+ : code
111
+ ? "';if(" + unescape(code) + "){X+='"
112
+ : "';}X+='";
113
+ })
114
+ .replace(iterate || skip, function (_, arr, vName, iName) {
115
+ if (!arr)
116
+ return "';} } X+='";
117
+ sid++;
118
+ indv = iName || "i" + sid;
119
+ arr = unescape(arr);
120
+ return ("';var arr" +
121
+ sid +
122
+ "=" +
123
+ arr +
124
+ ";if(arr" +
125
+ sid +
126
+ "){var " +
127
+ vName +
128
+ "," +
129
+ indv +
130
+ "=-1,l" +
131
+ sid +
132
+ "=arr" +
133
+ sid +
134
+ ".length-1;while(" +
135
+ indv +
136
+ "<l" +
137
+ sid +
138
+ "){" +
139
+ vName +
140
+ "=arr" +
141
+ sid +
142
+ "[" +
143
+ indv +
144
+ "+=1];X+='");
145
+ })
146
+ .replace(evaluate || skip, function (_, code) { return "';" + unescape(code) + "X+='"; }) +
147
+ "';return X;")
148
+ .replace(/\n/g, "\\n")
149
+ .replace(/\t/g, "\\t")
150
+ .replace(/\r/g, "\\r")
151
+ .replace(/(\s|;|\}|^|\{)X\+='';/g, "$1")
152
+ .replace(/\+''/g, "");
153
+ //.replace(/(\s|;|\}|^|\{)X\+=''\+/g,'$1X+=');
154
+ try {
155
+ return new Function(varname, str);
156
+ }
157
+ catch (e) {
158
+ if (process.env["NODE_ENV"] !== "production") {
159
+ console.log("Could not create a template function: " + str);
160
+ throw e;
161
+ }
162
+ }
163
+ return function () { return ""; };
164
+ };
165
+ export default render;
package/split.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export type Split<S extends string, D extends string> = string extends S ? string[] : S extends "" ? [] : S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : [S];
2
+ export declare function split<T extends string, D extends string>(str: T, delimiter: D): Split<T, D>;
3
+ export default split;
package/split.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.split = void 0;
4
+ function split(str, delimiter) {
5
+ return str.split(delimiter);
6
+ }
7
+ exports.split = split;
8
+ exports.default = split;
package/split.mjs ADDED
@@ -0,0 +1,4 @@
1
+ export function split(str, delimiter) {
2
+ return str.split(delimiter);
3
+ }
4
+ export default split;
package/types.d.ts CHANGED
@@ -1,4 +1,28 @@
1
+ import type { Replace } from "type-fest";
1
2
  /**
2
3
  * Whatever that in javascript returns `false` when checked in an `if` condition
3
4
  */
4
5
  export type AnythingFalsy = null | undefined | 0 | "";
6
+ /**
7
+ * Pick the keys of an object `T` that starts with `S`. It produces a mapped type
8
+ * with a subset of `T` whose keys start with `S`.
9
+ */
10
+ export type PickStartsWith<T extends object, S extends string> = {
11
+ [K in keyof T as K extends `${S}${string}` ? K : never]: T[K];
12
+ };
13
+ /**
14
+ * Returns a union of all the keys of an object `T` which starts with `S`.
15
+ */
16
+ export type KeysStartsWith<T extends object, S extends string> = keyof PickStartsWith<T, S>;
17
+ /**
18
+ * Pick the keys of an object `T` that starts with `S`. It produces a mapped type
19
+ * with a subset of `T` whose keys start with `S` *and have `S`* removed.
20
+ */
21
+ export type PickStartsWithTails<T extends object, S extends string> = {
22
+ [K in keyof T as K extends `${S}${string}` ? Replace<K, S, ""> : never]: T[K];
23
+ };
24
+ /**
25
+ * Returns a union of all the keys of an object `T` which starts with `S`.
26
+ * The strings in the produced union *have `S` removed*.
27
+ */
28
+ export type KeysTailsStartsWith<T extends object, S extends string> = keyof PickStartsWithTails<T, S>;