@koine/utils 2.0.0-alpha.1 → 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/areEqual.d.ts +1 -1
- package/areEqual.js +1 -1
- package/areEqual.mjs +1 -1
- package/capitalize.d.ts +1 -1
- package/createPalette.d.ts +12 -0
- package/createPalette.js +26 -0
- package/createPalette.mjs +22 -0
- package/index.d.ts +1 -0
- package/index.js +5 -3
- package/index.mjs +1 -0
- package/package.json +3 -3
- package/render.d.ts +52 -0
- package/render.js +169 -0
- package/render.mjs +165 -0
- package/split.d.ts +3 -0
- package/split.js +8 -0
- package/split.mjs +4 -0
- package/types.d.ts +24 -0
package/areEqual.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ type Comparable = ComparablePrimitive | object | Record<string, ComparablePrimit
|
|
|
7
7
|
*
|
|
8
8
|
* It support nested `object`s and `array`s only.
|
|
9
9
|
*
|
|
10
|
-
* NB: `undefined` and `null` values do not count in the comparison as
|
|
10
|
+
* NB: `undefined` and `null` values do not count in the comparison as they are
|
|
11
11
|
* usually meant to be ignored in JSON requestBody payloads.
|
|
12
12
|
*
|
|
13
13
|
* According to very rudimentary tests this function takes on average between
|
package/areEqual.js
CHANGED
|
@@ -42,7 +42,7 @@ function areEqualObjects(a, b) {
|
|
|
42
42
|
*
|
|
43
43
|
* It support nested `object`s and `array`s only.
|
|
44
44
|
*
|
|
45
|
-
* NB: `undefined` and `null` values do not count in the comparison as
|
|
45
|
+
* NB: `undefined` and `null` values do not count in the comparison as they are
|
|
46
46
|
* usually meant to be ignored in JSON requestBody payloads.
|
|
47
47
|
*
|
|
48
48
|
* According to very rudimentary tests this function takes on average between
|
package/areEqual.mjs
CHANGED
|
@@ -39,7 +39,7 @@ function areEqualObjects(a, b) {
|
|
|
39
39
|
*
|
|
40
40
|
* It support nested `object`s and `array`s only.
|
|
41
41
|
*
|
|
42
|
-
* NB: `undefined` and `null` values do not count in the comparison as
|
|
42
|
+
* NB: `undefined` and `null` values do not count in the comparison as they are
|
|
43
43
|
* usually meant to be ignored in JSON requestBody payloads.
|
|
44
44
|
*
|
|
45
45
|
* According to very rudimentary tests this function takes on average between
|
package/capitalize.d.ts
CHANGED
|
@@ -4,5 +4,5 @@
|
|
|
4
4
|
* @category text
|
|
5
5
|
* @see https://stackoverflow.com/a/11409944/1938970
|
|
6
6
|
*/
|
|
7
|
-
export declare function capitalize<T extends string>(string
|
|
7
|
+
export declare function capitalize<T extends string>(string?: null | T): Capitalize<T>;
|
|
8
8
|
export default capitalize;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create palette, this is primarily thought to improve the reuse of a palette
|
|
3
|
+
* definition between TailwindCSS and straight ES imports
|
|
4
|
+
*
|
|
5
|
+
* @category styling|tailwind|colours
|
|
6
|
+
*
|
|
7
|
+
* @returns An array with: 1) A flat palette map 2) A TailwindCSS ready palette
|
|
8
|
+
* object 3) A flat array of colors (no special sorting, same order as the `shades`
|
|
9
|
+
* given as argument)
|
|
10
|
+
*/
|
|
11
|
+
export declare const createPalette: <TName extends string, TShades extends readonly (readonly [number, string])[], TShade extends number = TShades[number][0]>(name: TName, shades: TShades) => readonly [Record<TShade, string>, Record<`${TName}-${TShades[number][0]}`, string>, unknown[]];
|
|
12
|
+
export default createPalette;
|
package/createPalette.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPalette = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Create palette, this is primarily thought to improve the reuse of a palette
|
|
6
|
+
* definition between TailwindCSS and straight ES imports
|
|
7
|
+
*
|
|
8
|
+
* @category styling|tailwind|colours
|
|
9
|
+
*
|
|
10
|
+
* @returns An array with: 1) A flat palette map 2) A TailwindCSS ready palette
|
|
11
|
+
* object 3) A flat array of colors (no special sorting, same order as the `shades`
|
|
12
|
+
* given as argument)
|
|
13
|
+
*/
|
|
14
|
+
var createPalette = function (name, shades) {
|
|
15
|
+
var map = shades.reduce(function (map, def) {
|
|
16
|
+
map[def[0]] = def[1];
|
|
17
|
+
return map;
|
|
18
|
+
}, {});
|
|
19
|
+
var tailwindPalette = shades.reduce(function (map, def) {
|
|
20
|
+
map["".concat(name, "-").concat(def[0])] = def[1];
|
|
21
|
+
return map;
|
|
22
|
+
}, {});
|
|
23
|
+
return [map, tailwindPalette, Object.values(map)];
|
|
24
|
+
};
|
|
25
|
+
exports.createPalette = createPalette;
|
|
26
|
+
exports.default = exports.createPalette;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Create palette, this is primarily thought to improve the reuse of a palette
|
|
3
|
+
* definition between TailwindCSS and straight ES imports
|
|
4
|
+
*
|
|
5
|
+
* @category styling|tailwind|colours
|
|
6
|
+
*
|
|
7
|
+
* @returns An array with: 1) A flat palette map 2) A TailwindCSS ready palette
|
|
8
|
+
* object 3) A flat array of colors (no special sorting, same order as the `shades`
|
|
9
|
+
* given as argument)
|
|
10
|
+
*/
|
|
11
|
+
export var createPalette = function (name, shades) {
|
|
12
|
+
var map = shades.reduce(function (map, def) {
|
|
13
|
+
map[def[0]] = def[1];
|
|
14
|
+
return map;
|
|
15
|
+
}, {});
|
|
16
|
+
var tailwindPalette = shades.reduce(function (map, def) {
|
|
17
|
+
map["".concat(name, "-").concat(def[0])] = def[1];
|
|
18
|
+
return map;
|
|
19
|
+
}, {});
|
|
20
|
+
return [map, tailwindPalette, Object.values(map)];
|
|
21
|
+
};
|
|
22
|
+
export default createPalette;
|
package/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export { clamp } from "./clamp";
|
|
|
13
13
|
export { clsx } from "./clsx";
|
|
14
14
|
export { convertRange } from "./convertRange";
|
|
15
15
|
export { type CookieAttributesClient, type CookieAttributesServer, } from "./cookie";
|
|
16
|
+
export { createPalette } from "./createPalette";
|
|
16
17
|
export { debounce } from "./debounce";
|
|
17
18
|
export { debounceRaf } from "./debounceRaf";
|
|
18
19
|
export { debouncePromise, type DebounceOptions, type DebouncedFunction, } from "./debouncePromise";
|
package/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.
|
|
5
|
-
exports.wait = exports.uuidNumeric = exports.uuid = exports.uppercase = exports.updateUrlQueryParams = exports.updateLinkParams = exports.uid = exports.tryUntil = exports.truncate = exports.transformToUrlPathname = exports.toRgba = exports.toNumber = exports.titleCase = exports.throttle = exports.swapMap = exports.slugify = exports.shuffle = exports.setCookie = exports.serializeCookie = exports.roundTo = exports.removeUrlQueryParams = exports.removeTralingSlash = exports.removeIndexesFromArray = exports.removeDuplicatesComparing = exports.removeDuplicatesByKey = exports.removeCookie = exports.removeAccents = exports.readCookie = exports.randomKey = void 0;
|
|
3
|
+
exports.isEmptyObject = exports.isEmptyArray = exports.isDate = exports.isBrowserNow = exports.isBrowser = exports.isBoolean = exports.isBlob = exports.isArray = exports.isAnyObject = exports.imgEmptyPixel = exports.getUrlQueryParams = exports.getUrlPathnameParts = exports.getUrlHashPathname = exports.getUrlHashParams = exports.getType = exports.getParamAsString = exports.getParamAsInt = exports.getParamAmong = exports.getNonce = exports.getMediaQueryWidthTailwindScreens = exports.getMediaQueryWidthResolvers = exports.getKeys = exports.getEmptyArray = exports.gbToBytes = exports.forin = exports.findDuplicatedIndexes = exports.errorToString = exports.ensureInt = exports.encode = exports.Emitter = exports.Defer = exports.decode = exports.debouncePromise = exports.debounceRaf = exports.debounce = exports.createPalette = exports.convertRange = exports.clsx = exports.clamp = exports.chunkBySize = exports.chunkByChunks = exports.changeUrlPath = exports.capitalize = exports.buildUrlQueryString = exports.arrayToLookup = exports.arraySum = exports.arrayOfAll = exports.areEqual = exports.addOrReplaceAtIdx = exports.accentsSets = void 0;
|
|
4
|
+
exports.quaranteneProps = exports.parseURL = exports.parseCookie = exports.objectOmit = exports.objectPick = exports.normaliseUrl = exports.normaliseUrlPathname = exports.noop = exports.moveSortableArrayItemByKey = exports.mergeUrlQueryParams = exports.mergeObjects = exports.mbToBytes = exports.matchSorter = exports.mapListBy = exports.lowercase = exports.kbToBytes = exports.isWeakSet = exports.isWeakMap = exports.isUndefined = exports.isType = exports.isSymbol = exports.isString = exports.isSet = exports.isServerNow = exports.isServer = exports.isRegExp = exports.isPromise = exports.isPrimitive = exports.isPositiveNumber = exports.isPlainObject = exports.isOneOf = exports.isObject = exports.isObjectLike = exports.isNumber = exports.isNull = exports.isNullOrUndefined = exports.isNegativeNumber = exports.isNaNValue = exports.isMap = exports.isInt = exports.isFunction = exports.isFullString = exports.isFullObject = exports.isFullArray = exports.isFormData = exports.isFloat = exports.isFile = exports.isExternalUrl = exports.isError = exports.isEmptyString = void 0;
|
|
5
|
+
exports.wait = exports.uuidNumeric = exports.uuid = exports.uppercase = exports.updateUrlQueryParams = exports.updateLinkParams = exports.uid = exports.tryUntil = exports.truncate = exports.transformToUrlPathname = exports.toRgba = exports.toNumber = exports.titleCase = exports.throttle = exports.swapMap = exports.slugify = exports.shuffle = exports.setCookie = exports.serializeCookie = exports.roundTo = exports.removeUrlQueryParams = exports.removeTralingSlash = exports.removeIndexesFromArray = exports.removeDuplicatesComparing = exports.removeDuplicatesByKey = exports.removeCookie = exports.removeAccents = exports.readCookie = exports.randomKey = exports.randomInt = void 0;
|
|
6
6
|
var accentSets_1 = require("./accentSets");
|
|
7
7
|
Object.defineProperty(exports, "accentsSets", { enumerable: true, get: function () { return accentSets_1.accentsSets; } });
|
|
8
8
|
var addOrReplaceAtIdx_1 = require("./addOrReplaceAtIdx");
|
|
@@ -31,6 +31,8 @@ var clsx_1 = require("./clsx");
|
|
|
31
31
|
Object.defineProperty(exports, "clsx", { enumerable: true, get: function () { return clsx_1.clsx; } });
|
|
32
32
|
var convertRange_1 = require("./convertRange");
|
|
33
33
|
Object.defineProperty(exports, "convertRange", { enumerable: true, get: function () { return convertRange_1.convertRange; } });
|
|
34
|
+
var createPalette_1 = require("./createPalette");
|
|
35
|
+
Object.defineProperty(exports, "createPalette", { enumerable: true, get: function () { return createPalette_1.createPalette; } });
|
|
34
36
|
var debounce_1 = require("./debounce");
|
|
35
37
|
Object.defineProperty(exports, "debounce", { enumerable: true, get: function () { return debounce_1.debounce; } });
|
|
36
38
|
var debounceRaf_1 = require("./debounceRaf");
|
package/index.mjs
CHANGED
|
@@ -12,6 +12,7 @@ export { chunkBySize } from "./chunkBySize";
|
|
|
12
12
|
export { clamp } from "./clamp";
|
|
13
13
|
export { clsx } from "./clsx";
|
|
14
14
|
export { convertRange } from "./convertRange";
|
|
15
|
+
export { createPalette } from "./createPalette";
|
|
15
16
|
export { debounce } from "./debounce";
|
|
16
17
|
export { debounceRaf } from "./debounceRaf";
|
|
17
18
|
export { debouncePromise, } from "./debouncePromise";
|
package/package.json
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"clsx": "1.2.1",
|
|
6
6
|
"ts-debounce": "^4.0.0",
|
|
7
|
-
"type-fest": "^3.
|
|
7
|
+
"type-fest": "^3.11.1"
|
|
8
8
|
},
|
|
9
9
|
"peerDependencies": {
|
|
10
|
-
"tslib": "
|
|
10
|
+
"tslib": "2.5.3"
|
|
11
11
|
},
|
|
12
12
|
"main": "./index.js",
|
|
13
13
|
"types": "./index.d.ts",
|
|
14
|
-
"version": "2.0.0-alpha.
|
|
14
|
+
"version": "2.0.0-alpha.2",
|
|
15
15
|
"module": "./index.mjs"
|
|
16
16
|
}
|
package/render.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
type DefinitionExtended = {
|
|
2
|
+
__exp?: {
|
|
3
|
+
[key: string]: number | boolean | string;
|
|
4
|
+
};
|
|
5
|
+
arg: string;
|
|
6
|
+
text: string;
|
|
7
|
+
};
|
|
8
|
+
type Definitions = {
|
|
9
|
+
[key: string]: DefinitionExtended;
|
|
10
|
+
} & DefinitionExtended;
|
|
11
|
+
/**
|
|
12
|
+
* Returned render function
|
|
13
|
+
*/
|
|
14
|
+
type RenderFunction = (data: object) => string;
|
|
15
|
+
/**
|
|
16
|
+
* Render template (adapted from doT.js)
|
|
17
|
+
*
|
|
18
|
+
* The data made available to the template is always on the `data` key, e.g.:
|
|
19
|
+
* `renderer({ myVal: "xx" })`
|
|
20
|
+
* ... will be accessible on
|
|
21
|
+
* `<%= data.myVal %>`
|
|
22
|
+
*
|
|
23
|
+
* The default delimiters are customised to work without conflicts with Blade and Twig:
|
|
24
|
+
* ```
|
|
25
|
+
* <% %> for evaluation
|
|
26
|
+
* <%= %> for interpolation
|
|
27
|
+
* <%? %> for conditionals
|
|
28
|
+
* <%~ %> for array iteration
|
|
29
|
+
* <%# %> for compile-time evaluation/includes and partials
|
|
30
|
+
* <%## #%> for compile-time defines
|
|
31
|
+
*
|
|
32
|
+
* Unsupported:
|
|
33
|
+
* <%! %> for interpolation with encoding
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
*
|
|
38
|
+
* ```js
|
|
39
|
+
* import { render } from "...";
|
|
40
|
+
*
|
|
41
|
+
* const data = { name: "XYZ" };
|
|
42
|
+
* const tpl = `Hello <%= data.name %>`;
|
|
43
|
+
* const renderer = render(tpl);
|
|
44
|
+
*
|
|
45
|
+
* console.log(renderer(data)); // outputs 'Hello XYZ'
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @borrows [olado/doT by Laura Doktorova](https://github.com/olado/doT)
|
|
49
|
+
* @see https://olado.github.io/doT/index.html
|
|
50
|
+
*/
|
|
51
|
+
export declare const render: (tmpl: string, def?: Definitions) => RenderFunction;
|
|
52
|
+
export default render;
|
package/render.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.render = void 0;
|
|
4
|
+
var varname = "data";
|
|
5
|
+
var evaluate = /<%([\s\S]+?(\}?)+)%>/g;
|
|
6
|
+
var interpolate = /<%=([\s\S]+?)%>/g;
|
|
7
|
+
var conditional = /<%\?(\?)?\s*([\s\S]*?)\s*%>/g;
|
|
8
|
+
var iterate = /<%~\s*(?:%>|([\s\S]+?)\s*:\s*([\w$]+)\s*(?::\s*([\w$]+))?\s*%>)/g;
|
|
9
|
+
// const encode = /<%!([\s\S]+?)%>/g;
|
|
10
|
+
var use = /<%#([\s\S]+?)%>/g;
|
|
11
|
+
var useParams = /(^|[^\w$])def(?:\.|\[['"])([\w$.]+)(?:['"]\])?\s*:\s*([\w$.]+|"[^"]+"|'[^']+'|\{[^}]+\})/g;
|
|
12
|
+
var define = /<%##\s*([\w.$]+)\s*(:|=)([\s\S]+?)#%>/g;
|
|
13
|
+
var defineParams = /^\s*([\w$]+):([\s\S]+)/;
|
|
14
|
+
var start = "'+(";
|
|
15
|
+
var end = ")+'";
|
|
16
|
+
// const startencode = "'+encodeHTML(";
|
|
17
|
+
var skip = /$^/;
|
|
18
|
+
var resolveDefs = function (block, def) {
|
|
19
|
+
return (typeof block === "string" ? block : block.toString())
|
|
20
|
+
.replace(define || skip, function (_, code, assign, value) {
|
|
21
|
+
if (code.indexOf("def.") === 0) {
|
|
22
|
+
code = code.substring(4);
|
|
23
|
+
}
|
|
24
|
+
if (!(code in def)) {
|
|
25
|
+
if (assign === ":") {
|
|
26
|
+
value.replace(defineParams,
|
|
27
|
+
// @ts-expect-error nevermind
|
|
28
|
+
function (_, param, v) {
|
|
29
|
+
def[code] = { arg: param, text: v };
|
|
30
|
+
});
|
|
31
|
+
// @ts-expect-error nevermind
|
|
32
|
+
if (!(code in def))
|
|
33
|
+
def[code] = value;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
new Function("def", "def['" + code + "']=" + value)(def);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return "";
|
|
40
|
+
})
|
|
41
|
+
.replace(use || skip, function (_, code) {
|
|
42
|
+
code = code.replace(useParams, function (_, s, d, param) {
|
|
43
|
+
if (def[d] && def[d].arg && param) {
|
|
44
|
+
var rw = (d + ":" + param).replace(/'|\\/g, "_");
|
|
45
|
+
def.__exp = def.__exp || {};
|
|
46
|
+
def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
|
|
47
|
+
return s + "def.__exp['" + rw + "']";
|
|
48
|
+
}
|
|
49
|
+
return s;
|
|
50
|
+
});
|
|
51
|
+
var v = new Function("def", "return " + code)(def);
|
|
52
|
+
return v ? resolveDefs(v, def) : v;
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
var unescape = function (code) {
|
|
56
|
+
return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Render template (adapted from doT.js)
|
|
60
|
+
*
|
|
61
|
+
* The data made available to the template is always on the `data` key, e.g.:
|
|
62
|
+
* `renderer({ myVal: "xx" })`
|
|
63
|
+
* ... will be accessible on
|
|
64
|
+
* `<%= data.myVal %>`
|
|
65
|
+
*
|
|
66
|
+
* The default delimiters are customised to work without conflicts with Blade and Twig:
|
|
67
|
+
* ```
|
|
68
|
+
* <% %> for evaluation
|
|
69
|
+
* <%= %> for interpolation
|
|
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, "");
|
|
156
|
+
//.replace(/(\s|;|\}|^|\{)X\+=''\+/g,'$1X+=');
|
|
157
|
+
try {
|
|
158
|
+
return new Function(varname, str);
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
if (process.env["NODE_ENV"] !== "production") {
|
|
162
|
+
console.log("Could not create a template function: " + str);
|
|
163
|
+
throw e;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return function () { return ""; };
|
|
167
|
+
};
|
|
168
|
+
exports.render = render;
|
|
169
|
+
exports.default = exports.render;
|
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
package/split.mjs
ADDED
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>;
|