@koine/utils 2.0.0-beta.2 → 2.0.0-beta.4
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/Defer.js +23 -18
- package/Defer.mjs +6 -5
- package/Emitter.js +31 -20
- package/Emitter.mjs +13 -16
- package/README.md +5 -1
- package/accentSets.js +129 -37
- package/accentSets.mjs +113 -29
- package/addOrReplaceAtIdx.js +27 -10
- package/addOrReplaceAtIdx.mjs +9 -6
- package/areEqual.js +35 -41
- package/areEqual.mjs +16 -22
- package/arrayOfAll.js +20 -11
- package/arrayOfAll.mjs +1 -6
- package/arraySum.js +20 -9
- package/arraySum.mjs +1 -4
- package/arrayToLookup.js +21 -9
- package/arrayToLookup.mjs +2 -4
- package/buildUrlQueryString.js +32 -29
- package/buildUrlQueryString.mjs +11 -14
- package/capitalize.js +21 -9
- package/capitalize.mjs +3 -5
- package/changeUrlPath.js +23 -17
- package/changeUrlPath.mjs +2 -3
- package/chunkByChunks.js +35 -24
- package/chunkByChunks.mjs +17 -20
- package/chunkBySize.js +21 -8
- package/chunkBySize.mjs +3 -4
- package/clamp.js +19 -6
- package/clamp.mjs +1 -2
- package/clsx.js +29 -23
- package/clsx.mjs +11 -14
- package/convertRange.js +20 -7
- package/convertRange.mjs +2 -3
- package/cookie.js +13 -5
- package/cookie.mjs +3 -2
- package/createPalette.js +27 -19
- package/createPalette.mjs +9 -6
- package/debounce.js +27 -21
- package/debounce.mjs +9 -17
- package/debouncePromise.js +20 -11
- package/debouncePromise.mjs +1 -2
- package/debounceRaf.js +23 -14
- package/debounceRaf.mjs +5 -10
- package/decode.js +20 -7
- package/decode.mjs +2 -3
- package/encode.js +20 -7
- package/encode.mjs +2 -3
- package/ensureInt.js +20 -9
- package/ensureInt.mjs +1 -4
- package/env.js +1 -1
- package/env.mjs +0 -1
- package/errorToString.js +21 -14
- package/errorToString.mjs +1 -4
- package/findDuplicatedIndexes.js +22 -9
- package/findDuplicatedIndexes.mjs +4 -5
- package/forin.js +20 -7
- package/forin.mjs +2 -3
- package/gbToBytes.js +20 -7
- package/gbToBytes.mjs +1 -2
- package/getEmptyArray.js +25 -16
- package/getEmptyArray.mjs +4 -3
- package/getKeys.js +20 -6
- package/getKeys.mjs +1 -2
- package/getMediaQueryWidthResolvers.js +62 -53
- package/getMediaQueryWidthResolvers.mjs +44 -49
- package/getMediaQueryWidthTailwindScreens.js +36 -18
- package/getMediaQueryWidthTailwindScreens.mjs +18 -14
- package/getNonce.js +20 -10
- package/getNonce.mjs +1 -2
- package/getParamAmong.js +22 -19
- package/getParamAmong.mjs +2 -4
- package/getParamAsInt.js +22 -19
- package/getParamAsInt.mjs +2 -4
- package/getParamAsString.js +21 -16
- package/getParamAsString.mjs +1 -2
- package/getType.js +19 -12
- package/getType.mjs +2 -4
- package/getUrlHashParams.js +20 -13
- package/getUrlHashParams.mjs +2 -4
- package/getUrlHashPathname.js +20 -8
- package/getUrlHashPathname.mjs +1 -3
- package/getUrlPathnameParts.js +23 -23
- package/getUrlPathnameParts.mjs +2 -7
- package/getUrlQueryParams.js +26 -33
- package/getUrlQueryParams.mjs +7 -18
- package/imgEmptyPixel.js +20 -6
- package/imgEmptyPixel.mjs +1 -2
- package/index.js +531 -266
- package/index.mjs +2 -2
- package/invariant.js +24 -12
- package/invariant.mjs +6 -8
- package/isAnyObject.js +21 -12
- package/isAnyObject.mjs +1 -2
- package/isArray.js +21 -12
- package/isArray.mjs +1 -2
- package/isBlob.js +21 -12
- package/isBlob.mjs +1 -2
- package/isBoolean.js +21 -12
- package/isBoolean.mjs +1 -2
- package/isBrowser.js +20 -6
- package/isBrowser.mjs +1 -2
- package/isBrowserNow.js +21 -11
- package/isBrowserNow.mjs +1 -2
- package/isDate.js +21 -12
- package/isDate.mjs +1 -2
- package/isEmptyArray.js +21 -12
- package/isEmptyArray.mjs +1 -2
- package/isEmptyObject.js +21 -12
- package/isEmptyObject.mjs +1 -2
- package/isEmptyString.js +19 -6
- package/isEmptyString.mjs +1 -2
- package/isError.js +21 -12
- package/isError.mjs +1 -2
- package/isExternalUrl.js +24 -18
- package/isExternalUrl.mjs +4 -6
- package/isFile.js +21 -12
- package/isFile.mjs +1 -2
- package/isFloat.js +21 -12
- package/isFloat.mjs +1 -2
- package/isFormData.js +21 -12
- package/isFormData.mjs +1 -2
- package/isFullArray.js +21 -12
- package/isFullArray.mjs +1 -2
- package/isFullObject.js +21 -12
- package/isFullObject.mjs +1 -2
- package/isFullString.js +21 -12
- package/isFullString.mjs +1 -2
- package/isFunction.js +18 -9
- package/isFunction.mjs +1 -2
- package/isInt.js +21 -12
- package/isInt.mjs +1 -2
- package/isMap.js +21 -12
- package/isMap.mjs +1 -2
- package/isNaNValue.js +21 -12
- package/isNaNValue.mjs +1 -2
- package/isNegativeNumber.js +21 -12
- package/isNegativeNumber.mjs +1 -2
- package/isNull.js +21 -12
- package/isNull.mjs +1 -2
- package/isNullOrUndefined.js +23 -13
- package/isNullOrUndefined.mjs +1 -2
- package/isNumber.js +21 -14
- package/isNumber.mjs +1 -2
- package/isObject.js +21 -12
- package/isObject.mjs +1 -2
- package/isObjectLike.js +21 -14
- package/isObjectLike.mjs +1 -2
- package/isOneOf.js +19 -11
- package/isOneOf.mjs +1 -7
- package/isPlainObject.js +22 -15
- package/isPlainObject.mjs +3 -6
- package/isPositiveNumber.js +21 -12
- package/isPositiveNumber.mjs +1 -2
- package/isPrimitive.js +26 -22
- package/isPrimitive.mjs +5 -11
- package/isPromise.js +21 -12
- package/isPromise.mjs +1 -2
- package/isRegExp.js +21 -12
- package/isRegExp.mjs +1 -2
- package/isServer.js +22 -11
- package/isServer.mjs +1 -2
- package/isServerNow.js +21 -11
- package/isServerNow.mjs +1 -2
- package/isSet.js +21 -12
- package/isSet.mjs +1 -2
- package/isString.js +21 -12
- package/isString.mjs +1 -2
- package/isSymbol.js +21 -12
- package/isSymbol.mjs +1 -2
- package/isType.js +22 -17
- package/isType.mjs +3 -5
- package/isUndefined.js +21 -12
- package/isUndefined.mjs +1 -2
- package/isWeakMap.js +21 -12
- package/isWeakMap.mjs +1 -2
- package/isWeakSet.js +21 -12
- package/isWeakSet.mjs +1 -2
- package/kbToBytes.js +20 -7
- package/kbToBytes.mjs +1 -2
- package/location.js +6 -2
- package/location.mjs +3 -1
- package/lowercase.js +20 -9
- package/lowercase.mjs +1 -4
- package/mapListBy.js +21 -10
- package/mapListBy.mjs +2 -5
- package/matchSorter.js +132 -133
- package/matchSorter.mjs +108 -126
- package/mbToBytes.js +20 -7
- package/mbToBytes.mjs +1 -2
- package/mergeObjects.d.ts +1 -1
- package/mergeObjects.js +24 -26
- package/mergeObjects.mjs +12 -15
- package/mergeUrlQueryParams.js +25 -20
- package/mergeUrlQueryParams.mjs +4 -8
- package/moveSortableArrayItemByKey.js +26 -12
- package/moveSortableArrayItemByKey.mjs +8 -8
- package/noop.js +20 -7
- package/noop.mjs +1 -2
- package/normaliseUrl.js +22 -18
- package/normaliseUrl.mjs +1 -3
- package/normaliseUrlPathname.js +22 -17
- package/normaliseUrlPathname.mjs +1 -3
- package/objectOmit.js +21 -9
- package/objectOmit.mjs +3 -5
- package/objectPick.js +22 -9
- package/objectPick.mjs +4 -5
- package/package.json +11 -8
- package/parseCookie.js +29 -26
- package/parseCookie.mjs +11 -15
- package/parseURL.js +23 -10
- package/parseURL.mjs +5 -6
- package/quaranteneProps.js +24 -12
- package/quaranteneProps.mjs +6 -8
- package/randomInt.js +19 -6
- package/randomInt.mjs +1 -2
- package/randomKey.js +21 -8
- package/randomKey.mjs +3 -4
- package/readCookie.js +25 -12
- package/readCookie.mjs +7 -8
- package/removeAccents.js +24 -15
- package/removeAccents.mjs +5 -8
- package/removeCookie.js +26 -17
- package/removeCookie.mjs +6 -5
- package/removeDuplicates.js +1 -1
- package/removeDuplicates.mjs +0 -1
- package/removeDuplicatesByKey.js +24 -12
- package/removeDuplicatesByKey.mjs +5 -7
- package/removeDuplicatesComparing.js +22 -11
- package/removeDuplicatesComparing.mjs +3 -4
- package/removeIndexesFromArray.js +21 -8
- package/removeIndexesFromArray.mjs +3 -4
- package/removeTrailingSlash.js +20 -8
- package/removeTrailingSlash.mjs +1 -3
- package/removeUrlQueryParams.js +27 -20
- package/removeUrlQueryParams.mjs +5 -7
- package/render.js +61 -137
- package/render.mjs +43 -98
- package/roundTo.js +34 -22
- package/roundTo.mjs +15 -17
- package/serializeCookie.js +29 -35
- package/serializeCookie.mjs +9 -15
- package/setCookie.js +37 -31
- package/setCookie.mjs +13 -16
- package/shuffle.js +28 -14
- package/shuffle.mjs +10 -10
- package/slugify.js +33 -25
- package/slugify.mjs +13 -11
- package/split.js +18 -4
- package/swapMap.js +22 -10
- package/swapMap.mjs +3 -5
- package/throttle.d.ts +1 -0
- package/throttle.js +23 -15
- package/throttle.mjs +5 -11
- package/titleCase.js +20 -11
- package/titleCase.mjs +1 -6
- package/toNumber.js +20 -9
- package/toNumber.mjs +1 -2
- package/toRgba.js +22 -10
- package/toRgba.mjs +3 -5
- package/transformToUrlPathname.js +21 -18
- package/transformToUrlPathname.mjs +2 -5
- package/truncate.js +20 -13
- package/truncate.mjs +1 -8
- package/tryUntil.js +26 -15
- package/tryUntil.mjs +7 -10
- package/types.js +4 -1
- package/types.mjs +2 -1
- package/uid.js +21 -13
- package/uid.mjs +3 -5
- package/updateLinkParams.js +21 -14
- package/updateLinkParams.mjs +2 -3
- package/updateUrlQueryParams.js +26 -21
- package/updateUrlQueryParams.mjs +3 -7
- package/uppercase.js +20 -9
- package/uppercase.mjs +1 -4
- package/uuid.js +21 -10
- package/uuid.mjs +2 -5
- package/uuidNumeric.js +20 -7
- package/uuidNumeric.mjs +1 -2
- package/wait.js +20 -9
- package/wait.mjs +1 -4
- package/without.js +2 -3
- package/without.mjs +1 -3
package/render.js
CHANGED
|
@@ -1,169 +1,93 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
render: function() {
|
|
13
|
+
return render;
|
|
14
|
+
},
|
|
15
|
+
default: function() {
|
|
16
|
+
return _default;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const varname = "data";
|
|
20
|
+
const evaluate = /<%([\s\S]+?(\}?)+)%>/g;
|
|
21
|
+
const interpolate = /<%=([\s\S]+?)%>/g;
|
|
22
|
+
const conditional = /<%\?(\?)?\s*([\s\S]*?)\s*%>/g;
|
|
23
|
+
const iterate = /<%~\s*(?:%>|([\s\S]+?)\s*:\s*([\w$]+)\s*(?::\s*([\w$]+))?\s*%>)/g;
|
|
9
24
|
// const encode = /<%!([\s\S]+?)%>/g;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
25
|
+
const use = /<%#([\s\S]+?)%>/g;
|
|
26
|
+
const useParams = /(^|[^\w$])def(?:\.|\[['"])([\w$.]+)(?:['"]\])?\s*:\s*([\w$.]+|"[^"]+"|'[^']+'|\{[^}]+\})/g;
|
|
27
|
+
const define = /<%##\s*([\w.$]+)\s*(:|=)([\s\S]+?)#%>/g;
|
|
28
|
+
const defineParams = /^\s*([\w$]+):([\s\S]+)/;
|
|
29
|
+
const start = "'+(";
|
|
30
|
+
const end = ")+'";
|
|
16
31
|
// const startencode = "'+encodeHTML(";
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return (typeof block === "string" ? block : block.toString())
|
|
20
|
-
.replace(define || skip, function (_, code, assign, value) {
|
|
32
|
+
const skip = /$^/;
|
|
33
|
+
const resolveDefs = (block, def)=>(typeof block === "string" ? block : block.toString()).replace(define || skip, (_, code, assign, value)=>{
|
|
21
34
|
if (code.indexOf("def.") === 0) {
|
|
22
35
|
code = code.substring(4);
|
|
23
36
|
}
|
|
24
37
|
if (!(code in def)) {
|
|
25
38
|
if (assign === ":") {
|
|
26
|
-
value.replace(defineParams,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
39
|
+
value.replace(defineParams, // @ts-expect-error nevermind
|
|
40
|
+
(_, param, v)=>{
|
|
41
|
+
def[code] = {
|
|
42
|
+
arg: param,
|
|
43
|
+
text: v
|
|
44
|
+
};
|
|
30
45
|
});
|
|
31
46
|
// @ts-expect-error nevermind
|
|
32
|
-
if (!(code in def))
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
47
|
+
if (!(code in def)) def[code] = value;
|
|
48
|
+
} else {
|
|
36
49
|
new Function("def", "def['" + code + "']=" + value)(def);
|
|
37
50
|
}
|
|
38
51
|
}
|
|
39
52
|
return "";
|
|
40
|
-
})
|
|
41
|
-
.replace(
|
|
42
|
-
code = code.replace(useParams, function (_, s, d, param) {
|
|
53
|
+
}).replace(use || skip, (_, code)=>{
|
|
54
|
+
code = code.replace(useParams, (_, s, d, param)=>{
|
|
43
55
|
if (def[d] && def[d].arg && param) {
|
|
44
|
-
|
|
56
|
+
const rw = (d + ":" + param).replace(/'|\\/g, "_");
|
|
45
57
|
def.__exp = def.__exp || {};
|
|
46
58
|
def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
|
|
47
59
|
return s + "def.__exp['" + rw + "']";
|
|
48
60
|
}
|
|
49
61
|
return s;
|
|
50
62
|
});
|
|
51
|
-
|
|
63
|
+
const v = new Function("def", "return " + code)(def);
|
|
52
64
|
return v ? resolveDefs(v, def) : v;
|
|
53
65
|
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
*
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
* <%? %> for conditionals
|
|
71
|
-
* <%~ %> for array iteration
|
|
72
|
-
* <%# %> for compile-time evaluation/includes and partials
|
|
73
|
-
* <%## #%> for compile-time defines
|
|
74
|
-
*
|
|
75
|
-
* Unsupported:
|
|
76
|
-
* <%! %> for interpolation with encoding
|
|
77
|
-
* ```
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
*
|
|
81
|
-
* ```js
|
|
82
|
-
* import { render } from "...";
|
|
83
|
-
*
|
|
84
|
-
* const data = { name: "XYZ" };
|
|
85
|
-
* const tpl = `Hello <%= data.name %>`;
|
|
86
|
-
* const renderer = render(tpl);
|
|
87
|
-
*
|
|
88
|
-
* console.log(renderer(data)); // outputs 'Hello XYZ'
|
|
89
|
-
* ```
|
|
90
|
-
*
|
|
91
|
-
* @borrows [olado/doT by Laura Doktorova](https://github.com/olado/doT)
|
|
92
|
-
* @see https://olado.github.io/doT/index.html
|
|
93
|
-
*/
|
|
94
|
-
var render = function (tmpl, def) {
|
|
95
|
-
var sid = 0;
|
|
96
|
-
var indv;
|
|
97
|
-
var str = use || define ? resolveDefs(tmpl, def || {}) : tmpl;
|
|
98
|
-
str = ("var X='" +
|
|
99
|
-
str
|
|
100
|
-
.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g, " ")
|
|
101
|
-
.replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g, "")
|
|
102
|
-
.replace(/'|\\/g, "\\$&")
|
|
103
|
-
.replace(interpolate || skip, function (_, code) { return start + unescape(code) + end; })
|
|
104
|
-
// .replace(
|
|
105
|
-
// encode || skip,
|
|
106
|
-
// (_, code) => cse.startencode + unescape(code) + cse.end
|
|
107
|
-
// )
|
|
108
|
-
.replace(conditional || skip, function (_, elseCase, code) {
|
|
109
|
-
return elseCase
|
|
110
|
-
? code
|
|
111
|
-
? "';}else if(" + unescape(code) + "){X+='"
|
|
112
|
-
: "';}else{X+='"
|
|
113
|
-
: code
|
|
114
|
-
? "';if(" + unescape(code) + "){X+='"
|
|
115
|
-
: "';}X+='";
|
|
116
|
-
})
|
|
117
|
-
.replace(iterate || skip, function (_, arr, vName, iName) {
|
|
118
|
-
if (!arr)
|
|
119
|
-
return "';} } X+='";
|
|
120
|
-
sid++;
|
|
121
|
-
indv = iName || "i" + sid;
|
|
122
|
-
arr = unescape(arr);
|
|
123
|
-
return ("';var arr" +
|
|
124
|
-
sid +
|
|
125
|
-
"=" +
|
|
126
|
-
arr +
|
|
127
|
-
";if(arr" +
|
|
128
|
-
sid +
|
|
129
|
-
"){var " +
|
|
130
|
-
vName +
|
|
131
|
-
"," +
|
|
132
|
-
indv +
|
|
133
|
-
"=-1,l" +
|
|
134
|
-
sid +
|
|
135
|
-
"=arr" +
|
|
136
|
-
sid +
|
|
137
|
-
".length-1;while(" +
|
|
138
|
-
indv +
|
|
139
|
-
"<l" +
|
|
140
|
-
sid +
|
|
141
|
-
"){" +
|
|
142
|
-
vName +
|
|
143
|
-
"=arr" +
|
|
144
|
-
sid +
|
|
145
|
-
"[" +
|
|
146
|
-
indv +
|
|
147
|
-
"+=1];X+='");
|
|
148
|
-
})
|
|
149
|
-
.replace(evaluate || skip, function (_, code) { return "';" + unescape(code) + "X+='"; }) +
|
|
150
|
-
"';return X;")
|
|
151
|
-
.replace(/\n/g, "\\n")
|
|
152
|
-
.replace(/\t/g, "\\t")
|
|
153
|
-
.replace(/\r/g, "\\r")
|
|
154
|
-
.replace(/(\s|;|\}|^|\{)X\+='';/g, "$1")
|
|
155
|
-
.replace(/\+''/g, "");
|
|
66
|
+
const unescape = (code)=>code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
|
|
67
|
+
const render = (tmpl, def)=>{
|
|
68
|
+
let sid = 0;
|
|
69
|
+
let indv;
|
|
70
|
+
let str = use || define ? resolveDefs(tmpl, def || {}) : tmpl;
|
|
71
|
+
str = ("var X='" + str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g, " ").replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g, "").replace(/'|\\/g, "\\$&").replace(interpolate || skip, (_, code)=>start + unescape(code) + end)// .replace(
|
|
72
|
+
// encode || skip,
|
|
73
|
+
// (_, code) => cse.startencode + unescape(code) + cse.end
|
|
74
|
+
// )
|
|
75
|
+
.replace(conditional || skip, (_, elseCase, code)=>elseCase ? code ? "';}else if(" + unescape(code) + "){X+='" : "';}else{X+='" : code ? "';if(" + unescape(code) + "){X+='" : "';}X+='").replace(iterate || skip, (_, arr, vName, iName)=>{
|
|
76
|
+
if (!arr) return "';} } X+='";
|
|
77
|
+
sid++;
|
|
78
|
+
indv = iName || "i" + sid;
|
|
79
|
+
arr = unescape(arr);
|
|
80
|
+
return "';var arr" + sid + "=" + arr + ";if(arr" + sid + "){var " + vName + "," + indv + "=-1,l" + sid + "=arr" + sid + ".length-1;while(" + indv + "<l" + sid + "){" + vName + "=arr" + sid + "[" + indv + "+=1];X+='";
|
|
81
|
+
}).replace(evaluate || skip, (_, code)=>"';" + unescape(code) + "X+='") + "';return X;").replace(/\n/g, "\\n").replace(/\t/g, "\\t").replace(/\r/g, "\\r").replace(/(\s|;|\}|^|\{)X\+='';/g, "$1").replace(/\+''/g, "");
|
|
156
82
|
//.replace(/(\s|;|\}|^|\{)X\+=''\+/g,'$1X+=');
|
|
157
83
|
try {
|
|
158
84
|
return new Function(varname, str);
|
|
159
|
-
}
|
|
160
|
-
catch (e) {
|
|
85
|
+
} catch (e) {
|
|
161
86
|
if (process.env["NODE_ENV"] !== "production") {
|
|
162
87
|
console.log("Could not create a template function: " + str);
|
|
163
88
|
throw e;
|
|
164
89
|
}
|
|
165
90
|
}
|
|
166
|
-
return
|
|
91
|
+
return ()=>"";
|
|
167
92
|
};
|
|
168
|
-
|
|
169
|
-
exports.default = exports.render;
|
|
93
|
+
const _default = render;
|
package/render.mjs
CHANGED
|
@@ -1,57 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const varname = "data";
|
|
2
|
+
const evaluate = /<%([\s\S]+?(\}?)+)%>/g;
|
|
3
|
+
const interpolate = /<%=([\s\S]+?)%>/g;
|
|
4
|
+
const conditional = /<%\?(\?)?\s*([\s\S]*?)\s*%>/g;
|
|
5
|
+
const iterate = /<%~\s*(?:%>|([\s\S]+?)\s*:\s*([\w$]+)\s*(?::\s*([\w$]+))?\s*%>)/g;
|
|
6
6
|
// const encode = /<%!([\s\S]+?)%>/g;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
const use = /<%#([\s\S]+?)%>/g;
|
|
8
|
+
const useParams = /(^|[^\w$])def(?:\.|\[['"])([\w$.]+)(?:['"]\])?\s*:\s*([\w$.]+|"[^"]+"|'[^']+'|\{[^}]+\})/g;
|
|
9
|
+
const define = /<%##\s*([\w.$]+)\s*(:|=)([\s\S]+?)#%>/g;
|
|
10
|
+
const defineParams = /^\s*([\w$]+):([\s\S]+)/;
|
|
11
|
+
const start = "'+(";
|
|
12
|
+
const end = ")+'";
|
|
13
13
|
// const startencode = "'+encodeHTML(";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return (typeof block === "string" ? block : block.toString())
|
|
17
|
-
.replace(define || skip, function (_, code, assign, value) {
|
|
14
|
+
const skip = /$^/;
|
|
15
|
+
const resolveDefs = (block, def)=>(typeof block === "string" ? block : block.toString()).replace(define || skip, (_, code, assign, value)=>{
|
|
18
16
|
if (code.indexOf("def.") === 0) {
|
|
19
17
|
code = code.substring(4);
|
|
20
18
|
}
|
|
21
19
|
if (!(code in def)) {
|
|
22
20
|
if (assign === ":") {
|
|
23
|
-
value.replace(defineParams,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
value.replace(defineParams, // @ts-expect-error nevermind
|
|
22
|
+
(_, param, v)=>{
|
|
23
|
+
def[code] = {
|
|
24
|
+
arg: param,
|
|
25
|
+
text: v
|
|
26
|
+
};
|
|
27
27
|
});
|
|
28
28
|
// @ts-expect-error nevermind
|
|
29
|
-
if (!(code in def))
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
29
|
+
if (!(code in def)) def[code] = value;
|
|
30
|
+
} else {
|
|
33
31
|
new Function("def", "def['" + code + "']=" + value)(def);
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
34
|
return "";
|
|
37
|
-
})
|
|
38
|
-
.replace(
|
|
39
|
-
code = code.replace(useParams, function (_, s, d, param) {
|
|
35
|
+
}).replace(use || skip, (_, code)=>{
|
|
36
|
+
code = code.replace(useParams, (_, s, d, param)=>{
|
|
40
37
|
if (def[d] && def[d].arg && param) {
|
|
41
|
-
|
|
38
|
+
const rw = (d + ":" + param).replace(/'|\\/g, "_");
|
|
42
39
|
def.__exp = def.__exp || {};
|
|
43
40
|
def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
|
|
44
41
|
return s + "def.__exp['" + rw + "']";
|
|
45
42
|
}
|
|
46
43
|
return s;
|
|
47
44
|
});
|
|
48
|
-
|
|
45
|
+
const v = new Function("def", "return " + code)(def);
|
|
49
46
|
return v ? resolveDefs(v, def) : v;
|
|
50
47
|
});
|
|
51
|
-
|
|
52
|
-
var unescape = function (code) {
|
|
53
|
-
return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
|
|
54
|
-
};
|
|
48
|
+
const unescape = (code)=>code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
|
|
55
49
|
/**
|
|
56
50
|
* Render template (adapted from doT.js)
|
|
57
51
|
*
|
|
@@ -87,79 +81,30 @@ var unescape = function (code) {
|
|
|
87
81
|
*
|
|
88
82
|
* @borrows [olado/doT by Laura Doktorova](https://github.com/olado/doT)
|
|
89
83
|
* @see https://olado.github.io/doT/index.html
|
|
90
|
-
*/
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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, "");
|
|
84
|
+
*/ export const render = (tmpl, def)=>{
|
|
85
|
+
let sid = 0;
|
|
86
|
+
let indv;
|
|
87
|
+
let str = use || define ? resolveDefs(tmpl, def || {}) : tmpl;
|
|
88
|
+
str = ("var X='" + str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g, " ").replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g, "").replace(/'|\\/g, "\\$&").replace(interpolate || skip, (_, code)=>start + unescape(code) + end)// .replace(
|
|
89
|
+
// encode || skip,
|
|
90
|
+
// (_, code) => cse.startencode + unescape(code) + cse.end
|
|
91
|
+
// )
|
|
92
|
+
.replace(conditional || skip, (_, elseCase, code)=>elseCase ? code ? "';}else if(" + unescape(code) + "){X+='" : "';}else{X+='" : code ? "';if(" + unescape(code) + "){X+='" : "';}X+='").replace(iterate || skip, (_, arr, vName, iName)=>{
|
|
93
|
+
if (!arr) return "';} } X+='";
|
|
94
|
+
sid++;
|
|
95
|
+
indv = iName || "i" + sid;
|
|
96
|
+
arr = unescape(arr);
|
|
97
|
+
return "';var arr" + sid + "=" + arr + ";if(arr" + sid + "){var " + vName + "," + indv + "=-1,l" + sid + "=arr" + sid + ".length-1;while(" + indv + "<l" + sid + "){" + vName + "=arr" + sid + "[" + indv + "+=1];X+='";
|
|
98
|
+
}).replace(evaluate || skip, (_, code)=>"';" + unescape(code) + "X+='") + "';return X;").replace(/\n/g, "\\n").replace(/\t/g, "\\t").replace(/\r/g, "\\r").replace(/(\s|;|\}|^|\{)X\+='';/g, "$1").replace(/\+''/g, "");
|
|
153
99
|
//.replace(/(\s|;|\}|^|\{)X\+=''\+/g,'$1X+=');
|
|
154
100
|
try {
|
|
155
101
|
return new Function(varname, str);
|
|
156
|
-
}
|
|
157
|
-
catch (e) {
|
|
102
|
+
} catch (e) {
|
|
158
103
|
if (process.env["NODE_ENV"] !== "production") {
|
|
159
104
|
console.log("Could not create a template function: " + str);
|
|
160
105
|
throw e;
|
|
161
106
|
}
|
|
162
107
|
}
|
|
163
|
-
return
|
|
108
|
+
return ()=>"";
|
|
164
109
|
};
|
|
165
110
|
export default render;
|
package/roundTo.js
CHANGED
|
@@ -1,38 +1,50 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.roundTo = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Round to given number of the given number of decimals
|
|
6
3
|
*
|
|
7
4
|
* @category math
|
|
8
5
|
* @see https://stackoverflow.com/a/15762794/1938970
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
*/ "use strict";
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
function _export(target, all) {
|
|
11
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: all[name]
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
_export(exports, {
|
|
17
|
+
roundTo: function() {
|
|
18
|
+
return roundTo;
|
|
19
|
+
},
|
|
20
|
+
default: function() {
|
|
21
|
+
return _default;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
function roundTo(num, decimals = 2) {
|
|
12
25
|
if (isFinite(num) && !isNaN(num)) {
|
|
13
26
|
// method 1
|
|
14
27
|
// return Number(num).toFixed(decimals);
|
|
15
28
|
// method 2: @see https://stackoverflow.com/a/43532829/1938970
|
|
16
|
-
|
|
29
|
+
const multiplicator = Math.pow(10, decimals);
|
|
17
30
|
return Math.round(num * multiplicator) / multiplicator + "";
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
// method 3: @see https://stackoverflow.com/a/15762794/1938970
|
|
32
|
+
// let negative = false;
|
|
33
|
+
// if (num < 0) {
|
|
34
|
+
// negative = true;
|
|
35
|
+
// num = num * -1;
|
|
36
|
+
// }
|
|
37
|
+
// const multiplicator = Math.pow(10, decimals);
|
|
38
|
+
// const outputStr = parseFloat((num * multiplicator).toFixed(11));
|
|
39
|
+
// let outputNum = (Math.round(outputStr) / multiplicator).toFixed(decimals);
|
|
40
|
+
// if (negative) {
|
|
41
|
+
// return (Number(outputNum) * -1).toFixed(decimals);
|
|
42
|
+
// }
|
|
43
|
+
// return outputNum;
|
|
31
44
|
}
|
|
32
45
|
if (process.env["NODE_ENV"] !== "production") {
|
|
33
46
|
console.warn("[@koine/utils] math:roundTo -> given not a finite number as first arg");
|
|
34
47
|
}
|
|
35
48
|
return "";
|
|
36
49
|
}
|
|
37
|
-
|
|
38
|
-
exports.default = roundTo;
|
|
50
|
+
const _default = roundTo;
|
package/roundTo.mjs
CHANGED
|
@@ -3,28 +3,26 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @category math
|
|
5
5
|
* @see https://stackoverflow.com/a/15762794/1938970
|
|
6
|
-
*/
|
|
7
|
-
export function roundTo(num, decimals) {
|
|
8
|
-
if (decimals === void 0) { decimals = 2; }
|
|
6
|
+
*/ export function roundTo(num, decimals = 2) {
|
|
9
7
|
if (isFinite(num) && !isNaN(num)) {
|
|
10
8
|
// method 1
|
|
11
9
|
// return Number(num).toFixed(decimals);
|
|
12
10
|
// method 2: @see https://stackoverflow.com/a/43532829/1938970
|
|
13
|
-
|
|
11
|
+
const multiplicator = Math.pow(10, decimals);
|
|
14
12
|
return Math.round(num * multiplicator) / multiplicator + "";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
13
|
+
// method 3: @see https://stackoverflow.com/a/15762794/1938970
|
|
14
|
+
// let negative = false;
|
|
15
|
+
// if (num < 0) {
|
|
16
|
+
// negative = true;
|
|
17
|
+
// num = num * -1;
|
|
18
|
+
// }
|
|
19
|
+
// const multiplicator = Math.pow(10, decimals);
|
|
20
|
+
// const outputStr = parseFloat((num * multiplicator).toFixed(11));
|
|
21
|
+
// let outputNum = (Math.round(outputStr) / multiplicator).toFixed(decimals);
|
|
22
|
+
// if (negative) {
|
|
23
|
+
// return (Number(outputNum) * -1).toFixed(decimals);
|
|
24
|
+
// }
|
|
25
|
+
// return outputNum;
|
|
28
26
|
}
|
|
29
27
|
if (process.env["NODE_ENV"] !== "production") {
|
|
30
28
|
console.warn("[@koine/utils] math:roundTo -> given not a finite number as first arg");
|
package/serializeCookie.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @file
|
|
4
3
|
*
|
|
@@ -7,39 +6,37 @@
|
|
|
7
6
|
* All cookie related code is inspired and adapted from:
|
|
8
7
|
* - [js-cookie](https://github.com/js-cookie/js-cookie)
|
|
9
8
|
* - [cookie](https://github.com/jshttp/cookie)
|
|
10
|
-
*/
|
|
11
|
-
Object.defineProperty(exports, "__esModule", {
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
*/ "use strict";
|
|
10
|
+
Object.defineProperty(exports, "__esModule", {
|
|
11
|
+
value: true
|
|
12
|
+
});
|
|
13
|
+
function _export(target, all) {
|
|
14
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: all[name]
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
_export(exports, {
|
|
20
|
+
serializeCookie: function() {
|
|
21
|
+
return serializeCookie;
|
|
22
|
+
},
|
|
23
|
+
default: function() {
|
|
24
|
+
return _default;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
const _isNumber = require("./isNumber");
|
|
14
28
|
/**
|
|
15
29
|
* RegExp to match field-content in RFC 7230 sec 3.2
|
|
16
30
|
*
|
|
17
31
|
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
18
32
|
* field-vchar = VCHAR / obs-text
|
|
19
33
|
* obs-text = %x80-FF
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
* Serialize the a name value pair into a cookie string suitable for
|
|
27
|
-
* http headers. An optional attributes object specified cookie parameters.
|
|
28
|
-
*
|
|
29
|
-
* serialize('foo', 'bar', { httpOnly: true })
|
|
30
|
-
* => "foo=bar; httpOnly"
|
|
31
|
-
*
|
|
32
|
-
* @category cookie
|
|
33
|
-
*
|
|
34
|
-
* All cookie related code is inspired and adapted from:
|
|
35
|
-
* - [js-cookie](https://github.com/js-cookie/js-cookie)
|
|
36
|
-
* - [cookie](https://github.com/jshttp/cookie)
|
|
37
|
-
*/
|
|
38
|
-
function serializeCookie(name, val, attributes) {
|
|
39
|
-
if (attributes === void 0) { attributes = {}; }
|
|
40
|
-
var _a = attributes.encode, encode = _a === void 0 ? encodeURIComponent : _a, domain = attributes.domain, path = attributes.path, httpOnly = attributes.httpOnly, secure = attributes.secure, sameSite = attributes.sameSite;
|
|
41
|
-
var maxAge = attributes.maxAge, expires = attributes.expires;
|
|
42
|
-
var value = encode(val);
|
|
34
|
+
*/ // eslint-disable-next-line no-control-regex
|
|
35
|
+
const fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
36
|
+
function serializeCookie(name, val, attributes = {}) {
|
|
37
|
+
const { encode = encodeURIComponent, domain, path, httpOnly, secure, sameSite } = attributes;
|
|
38
|
+
let { maxAge, expires } = attributes;
|
|
39
|
+
const value = encode(val);
|
|
43
40
|
if (process.env["NODE_ENV"] !== "production") {
|
|
44
41
|
if (!fieldContentRegExp.test(name)) {
|
|
45
42
|
throw new TypeError("argument name is invalid");
|
|
@@ -51,7 +48,7 @@ function serializeCookie(name, val, attributes) {
|
|
|
51
48
|
throw new TypeError("argument val is invalid");
|
|
52
49
|
}
|
|
53
50
|
}
|
|
54
|
-
|
|
51
|
+
let str = name + "=" + value;
|
|
55
52
|
if (null != maxAge) {
|
|
56
53
|
maxAge = maxAge - 0;
|
|
57
54
|
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
@@ -76,7 +73,7 @@ function serializeCookie(name, val, attributes) {
|
|
|
76
73
|
str += "; Path=" + path;
|
|
77
74
|
}
|
|
78
75
|
if (expires) {
|
|
79
|
-
if ((0,
|
|
76
|
+
if ((0, _isNumber.isNumber)(expires)) {
|
|
80
77
|
expires = new Date(Date.now() + expires * 864e5);
|
|
81
78
|
}
|
|
82
79
|
str += "; Expires=" + expires.toUTCString();
|
|
@@ -88,7 +85,7 @@ function serializeCookie(name, val, attributes) {
|
|
|
88
85
|
str += "; Secure";
|
|
89
86
|
}
|
|
90
87
|
if (sameSite) {
|
|
91
|
-
switch
|
|
88
|
+
switch(sameSite.toLowerCase()){
|
|
92
89
|
case "lax":
|
|
93
90
|
str += "; SameSite=Lax";
|
|
94
91
|
break;
|
|
@@ -98,11 +95,8 @@ function serializeCookie(name, val, attributes) {
|
|
|
98
95
|
case "none":
|
|
99
96
|
str += "; SameSite=None";
|
|
100
97
|
break;
|
|
101
|
-
// default:
|
|
102
|
-
// throw new TypeError('option sameSite is invalid');
|
|
103
98
|
}
|
|
104
99
|
}
|
|
105
100
|
return str;
|
|
106
101
|
}
|
|
107
|
-
|
|
108
|
-
exports.default = serializeCookie;
|
|
102
|
+
const _default = serializeCookie;
|