kadence-lang 0.2.0
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/LICENSE +21 -0
- package/README.md +208 -0
- package/bin/kadence.js +806 -0
- package/package.json +64 -0
- package/src/compiler.js +2291 -0
- package/src/vite-plugin-kadence.js +39 -0
- package/stdlib/check-helpers.js +13 -0
- package/stdlib/check.js +57 -0
- package/stdlib/check.kade +21 -0
- package/stdlib/color-helpers.js +34 -0
- package/stdlib/color.js +60 -0
- package/stdlib/color.kade +24 -0
- package/stdlib/console.js +57 -0
- package/stdlib/console.kade +21 -0
- package/stdlib/crypto-helpers.js +17 -0
- package/stdlib/crypto.js +46 -0
- package/stdlib/crypto.kade +11 -0
- package/stdlib/datetime.js +68 -0
- package/stdlib/datetime.kade +32 -0
- package/stdlib/encoding.js +55 -0
- package/stdlib/encoding.kade +19 -0
- package/stdlib/env.js +46 -0
- package/stdlib/env.kade +11 -0
- package/stdlib/file.js +94 -0
- package/stdlib/file.kade +57 -0
- package/stdlib/html.js +65 -0
- package/stdlib/html.kade +29 -0
- package/stdlib/json.js +63 -0
- package/stdlib/json.kade +28 -0
- package/stdlib/list.js +109 -0
- package/stdlib/list.kade +75 -0
- package/stdlib/math.js +76 -0
- package/stdlib/math.kade +39 -0
- package/stdlib/network.js +66 -0
- package/stdlib/network.kade +38 -0
- package/stdlib/number.js +53 -0
- package/stdlib/number.kade +17 -0
- package/stdlib/object-helpers.js +44 -0
- package/stdlib/object.js +59 -0
- package/stdlib/object.kade +23 -0
- package/stdlib/path.js +58 -0
- package/stdlib/path.kade +23 -0
- package/stdlib/process-helpers.js +18 -0
- package/stdlib/process.js +62 -0
- package/stdlib/process.kade +29 -0
- package/stdlib/promise-helpers.js +21 -0
- package/stdlib/promise.js +54 -0
- package/stdlib/promise.kade +19 -0
- package/stdlib/random.js +82 -0
- package/stdlib/random.kade +46 -0
- package/stdlib/regex-helpers.js +18 -0
- package/stdlib/regex.js +46 -0
- package/stdlib/regex.kade +11 -0
- package/stdlib/stream.js +58 -0
- package/stdlib/stream.kade +22 -0
- package/stdlib/string-helpers.js +16 -0
- package/stdlib/string.js +101 -0
- package/stdlib/string.kade +66 -0
- package/stdlib/system.js +66 -0
- package/stdlib/system.kade +31 -0
- package/stdlib/test-helpers.js +18 -0
- package/stdlib/test.js +72 -0
- package/stdlib/test.kade +37 -0
- package/stdlib/url.js +50 -0
- package/stdlib/url.kade +14 -0
- package/stdlib/uuid.js +70 -0
- package/stdlib/uuid.kade +35 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
note: Network utilities for Kadence
|
|
2
|
+
|
|
3
|
+
let http = run require "http"
|
|
4
|
+
|
|
5
|
+
export function serve port handler
|
|
6
|
+
let server = http.createServer((req, res) => {
|
|
7
|
+
let response = {
|
|
8
|
+
send: (body) => {
|
|
9
|
+
res.writeHead(200, { "Content-Type": "text/plain" })
|
|
10
|
+
res.end(body)
|
|
11
|
+
}
|
|
12
|
+
json: (body) => {
|
|
13
|
+
res.writeHead(200, { "Content-Type": "application/json" })
|
|
14
|
+
res.end(JSON.stringify(body))
|
|
15
|
+
}
|
|
16
|
+
html: (body) => {
|
|
17
|
+
res.writeHead(200, { "Content-Type": "text/html" })
|
|
18
|
+
res.end(body)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
set response.status to (code) => {
|
|
23
|
+
res.statusCode = code
|
|
24
|
+
return response
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
run handler req response
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
server.listen(port, () => {
|
|
31
|
+
say "Server running at http://localhost:{port}/"
|
|
32
|
+
})
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
export function fetchJson url
|
|
36
|
+
let result = await get from url
|
|
37
|
+
return result
|
|
38
|
+
end
|
package/stdlib/number.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
|
|
3
|
+
function __kadence_echo(val) {
|
|
4
|
+
const s = String(val);
|
|
5
|
+
const low = s.toLowerCase();
|
|
6
|
+
if (typeof process !== 'undefined' && process.stdout && process.stdout.isTTY) {
|
|
7
|
+
if (low.includes('error')) console.log("\x1b[31m" + s + "\x1b[0m");
|
|
8
|
+
else if (low.includes('warning')) console.log("\x1b[33m" + s + "\x1b[0m");
|
|
9
|
+
else if (low.includes('success')) console.log("\x1b[32m" + s + "\x1b[0m");
|
|
10
|
+
else console.log(s);
|
|
11
|
+
} else {
|
|
12
|
+
console.log(s);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function __kadence_min(val) {
|
|
16
|
+
if (Array.isArray(val)) return Math.min(...val);
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
function __kadence_max(val) {
|
|
20
|
+
if (Array.isArray(val)) return Math.max(...val);
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
function __kadence_add(parent, child) {
|
|
24
|
+
if (Array.isArray(parent)) {
|
|
25
|
+
parent.push(child);
|
|
26
|
+
return parent;
|
|
27
|
+
}
|
|
28
|
+
if (typeof parent === 'object' && parent !== null && typeof parent.appendChild === 'function') {
|
|
29
|
+
parent.appendChild(child);
|
|
30
|
+
return parent;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Runtime Error: Cannot add item to " + typeof parent);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseNumber (str) {
|
|
36
|
+
return Number(str);
|
|
37
|
+
}
|
|
38
|
+
if (typeof exports !== 'undefined') exports.parseNumber = parseNumber;
|
|
39
|
+
function formatNumber (num, decimals) {
|
|
40
|
+
return num.toFixed(decimals);
|
|
41
|
+
}
|
|
42
|
+
if (typeof exports !== 'undefined') exports.formatNumber = formatNumber;
|
|
43
|
+
function isNaNVal (val) {
|
|
44
|
+
return Number.isNaN(val);
|
|
45
|
+
}
|
|
46
|
+
if (typeof exports !== 'undefined') exports.isNaNVal = isNaNVal;
|
|
47
|
+
function isFiniteVal (val) {
|
|
48
|
+
return Number.isFinite(val);
|
|
49
|
+
}
|
|
50
|
+
if (typeof exports !== 'undefined') exports.isFiniteVal = isFiniteVal;
|
|
51
|
+
(async () => {
|
|
52
|
+
|
|
53
|
+
})().catch(err => { if (err) console.error("\x1b[31mRuntime Error:\x1b[0m", err.stack || err.message); });
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
note: Number utilities for Kadence
|
|
2
|
+
|
|
3
|
+
export function parseNumber str
|
|
4
|
+
return convert str to number
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
export function formatNumber num decimals
|
|
8
|
+
return run num.toFixed decimals
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
export function isNaNVal val
|
|
12
|
+
return run Number.isNaN val
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
export function isFiniteVal val
|
|
16
|
+
return run Number.isFinite val
|
|
17
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function pick(obj, keys) {
|
|
4
|
+
const r = {};
|
|
5
|
+
for (const k of keys) {
|
|
6
|
+
if (Object.prototype.hasOwnProperty.call(obj, k)) r[k] = obj[k];
|
|
7
|
+
}
|
|
8
|
+
return r;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function omit(obj, keys) {
|
|
12
|
+
const set = new Set(keys);
|
|
13
|
+
const r = {};
|
|
14
|
+
for (const k of Object.keys(obj)) {
|
|
15
|
+
if (!set.has(k)) r[k] = obj[k];
|
|
16
|
+
}
|
|
17
|
+
return r;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function deepClone(obj) {
|
|
21
|
+
return JSON.parse(JSON.stringify(obj));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function get(obj, path) {
|
|
25
|
+
if (obj == null) return undefined;
|
|
26
|
+
const parts = typeof path === "string" ? path.split(".") : path;
|
|
27
|
+
let cur = obj;
|
|
28
|
+
for (const p of parts) {
|
|
29
|
+
cur = cur == null ? undefined : cur[p];
|
|
30
|
+
}
|
|
31
|
+
return cur;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function defaults(obj, defs) {
|
|
35
|
+
const r = { ...defs };
|
|
36
|
+
for (const k of Object.keys(obj || {})) {
|
|
37
|
+
if (r[k] === undefined) r[k] = obj[k];
|
|
38
|
+
}
|
|
39
|
+
return r;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
43
|
+
module.exports = { pick, omit, deepClone, get, defaults };
|
|
44
|
+
}
|
package/stdlib/object.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
|
|
3
|
+
function __kadence_echo(val) {
|
|
4
|
+
const s = String(val);
|
|
5
|
+
const low = s.toLowerCase();
|
|
6
|
+
if (typeof process !== 'undefined' && process.stdout && process.stdout.isTTY) {
|
|
7
|
+
if (low.includes('error')) console.log("\x1b[31m" + s + "\x1b[0m");
|
|
8
|
+
else if (low.includes('warning')) console.log("\x1b[33m" + s + "\x1b[0m");
|
|
9
|
+
else if (low.includes('success')) console.log("\x1b[32m" + s + "\x1b[0m");
|
|
10
|
+
else console.log(s);
|
|
11
|
+
} else {
|
|
12
|
+
console.log(s);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function __kadence_min(val) {
|
|
16
|
+
if (Array.isArray(val)) return Math.min(...val);
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
function __kadence_max(val) {
|
|
20
|
+
if (Array.isArray(val)) return Math.max(...val);
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
function __kadence_add(parent, child) {
|
|
24
|
+
if (Array.isArray(parent)) {
|
|
25
|
+
parent.push(child);
|
|
26
|
+
return parent;
|
|
27
|
+
}
|
|
28
|
+
if (typeof parent === 'object' && parent !== null && typeof parent.appendChild === 'function') {
|
|
29
|
+
parent.appendChild(child);
|
|
30
|
+
return parent;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Runtime Error: Cannot add item to " + typeof parent);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function hasKey (obj, key) {
|
|
36
|
+
let ks = Object.keys(obj);
|
|
37
|
+
for (let k of ks) {
|
|
38
|
+
if (k === key ) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (typeof exports !== 'undefined') exports.hasKey = hasKey;
|
|
45
|
+
function getKeys (obj) {
|
|
46
|
+
return Object.keys(obj);
|
|
47
|
+
}
|
|
48
|
+
if (typeof exports !== 'undefined') exports.getKeys = getKeys;
|
|
49
|
+
function getValues (obj) {
|
|
50
|
+
return Object.values(obj);
|
|
51
|
+
}
|
|
52
|
+
if (typeof exports !== 'undefined') exports.getValues = getValues;
|
|
53
|
+
function mergeObjects (a, b) {
|
|
54
|
+
return Object.assign({}, a, b);
|
|
55
|
+
}
|
|
56
|
+
if (typeof exports !== 'undefined') exports.mergeObjects = mergeObjects;
|
|
57
|
+
(async () => {
|
|
58
|
+
|
|
59
|
+
})().catch(err => { if (err) console.error("\x1b[31mRuntime Error:\x1b[0m", err.stack || err.message); });
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
note: Object utilities for Kadence
|
|
2
|
+
|
|
3
|
+
export function hasKey obj key
|
|
4
|
+
let ks = keys of obj
|
|
5
|
+
for each k in ks
|
|
6
|
+
if k equals key
|
|
7
|
+
return true
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
return false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
export function getKeys obj
|
|
14
|
+
return keys of obj
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
export function getValues obj
|
|
18
|
+
return values of obj
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
export function mergeObjects a b
|
|
22
|
+
return merge a with b
|
|
23
|
+
end
|
package/stdlib/path.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
|
|
3
|
+
function __kadence_echo(val) {
|
|
4
|
+
const s = String(val);
|
|
5
|
+
const low = s.toLowerCase();
|
|
6
|
+
if (typeof process !== 'undefined' && process.stdout && process.stdout.isTTY) {
|
|
7
|
+
if (low.includes('error')) console.log("\x1b[31m" + s + "\x1b[0m");
|
|
8
|
+
else if (low.includes('warning')) console.log("\x1b[33m" + s + "\x1b[0m");
|
|
9
|
+
else if (low.includes('success')) console.log("\x1b[32m" + s + "\x1b[0m");
|
|
10
|
+
else console.log(s);
|
|
11
|
+
} else {
|
|
12
|
+
console.log(s);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function __kadence_min(val) {
|
|
16
|
+
if (Array.isArray(val)) return Math.min(...val);
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
function __kadence_max(val) {
|
|
20
|
+
if (Array.isArray(val)) return Math.max(...val);
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
function __kadence_add(parent, child) {
|
|
24
|
+
if (Array.isArray(parent)) {
|
|
25
|
+
parent.push(child);
|
|
26
|
+
return parent;
|
|
27
|
+
}
|
|
28
|
+
if (typeof parent === 'object' && parent !== null && typeof parent.appendChild === 'function') {
|
|
29
|
+
parent.appendChild(child);
|
|
30
|
+
return parent;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Runtime Error: Cannot add item to " + typeof parent);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let pathMod = require(`path`);
|
|
36
|
+
function joinPaths (a, b) {
|
|
37
|
+
return pathMod.join(a, b);
|
|
38
|
+
}
|
|
39
|
+
if (typeof exports !== 'undefined') exports.joinPaths = joinPaths;
|
|
40
|
+
function resolve (path) {
|
|
41
|
+
return pathMod.resolve(path);
|
|
42
|
+
}
|
|
43
|
+
if (typeof exports !== 'undefined') exports.resolve = resolve;
|
|
44
|
+
function dirname (path) {
|
|
45
|
+
return pathMod.dirname(path);
|
|
46
|
+
}
|
|
47
|
+
if (typeof exports !== 'undefined') exports.dirname = dirname;
|
|
48
|
+
function basename (path) {
|
|
49
|
+
return pathMod.basename(path);
|
|
50
|
+
}
|
|
51
|
+
if (typeof exports !== 'undefined') exports.basename = basename;
|
|
52
|
+
function extension (path) {
|
|
53
|
+
return pathMod.extname(path);
|
|
54
|
+
}
|
|
55
|
+
if (typeof exports !== 'undefined') exports.extension = extension;
|
|
56
|
+
(async () => {
|
|
57
|
+
|
|
58
|
+
})().catch(err => { if (err) console.error("\x1b[31mRuntime Error:\x1b[0m", err.stack || err.message); });
|
package/stdlib/path.kade
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
note: Path utilities for Kadence
|
|
2
|
+
|
|
3
|
+
let pathMod = run require "path"
|
|
4
|
+
|
|
5
|
+
export function joinPaths a b
|
|
6
|
+
return run pathMod.join a b
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
export function resolve path
|
|
10
|
+
return run pathMod.resolve path
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
export function dirname path
|
|
14
|
+
return run pathMod.dirname path
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
export function basename path
|
|
18
|
+
return run pathMod.basename path
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
export function extension path
|
|
22
|
+
return run pathMod.extname path
|
|
23
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const readline = typeof require !== "undefined" ? require("readline") : null;
|
|
4
|
+
|
|
5
|
+
function readLine(_ignored) {
|
|
6
|
+
if (!readline) return Promise.reject(new Error("readLine requires Node.js"));
|
|
7
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
rl.question("", (answer) => {
|
|
10
|
+
rl.close();
|
|
11
|
+
resolve(answer);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
17
|
+
module.exports = { readLine };
|
|
18
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
|
|
3
|
+
function __kadence_echo(val) {
|
|
4
|
+
const s = String(val);
|
|
5
|
+
const low = s.toLowerCase();
|
|
6
|
+
if (typeof process !== 'undefined' && process.stdout && process.stdout.isTTY) {
|
|
7
|
+
if (low.includes('error')) console.log("\x1b[31m" + s + "\x1b[0m");
|
|
8
|
+
else if (low.includes('warning')) console.log("\x1b[33m" + s + "\x1b[0m");
|
|
9
|
+
else if (low.includes('success')) console.log("\x1b[32m" + s + "\x1b[0m");
|
|
10
|
+
else console.log(s);
|
|
11
|
+
} else {
|
|
12
|
+
console.log(s);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function __kadence_min(val) {
|
|
16
|
+
if (Array.isArray(val)) return Math.min(...val);
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
function __kadence_max(val) {
|
|
20
|
+
if (Array.isArray(val)) return Math.max(...val);
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
function __kadence_add(parent, child) {
|
|
24
|
+
if (Array.isArray(parent)) {
|
|
25
|
+
parent.push(child);
|
|
26
|
+
return parent;
|
|
27
|
+
}
|
|
28
|
+
if (typeof parent === 'object' && parent !== null && typeof parent.appendChild === 'function') {
|
|
29
|
+
parent.appendChild(child);
|
|
30
|
+
return parent;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Runtime Error: Cannot add item to " + typeof parent);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let processMod = require(`process`);
|
|
36
|
+
function exit (code) {
|
|
37
|
+
if (code === undefined ) {
|
|
38
|
+
processMod.exit(0);
|
|
39
|
+
} else {
|
|
40
|
+
processMod.exit(code);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (typeof exports !== 'undefined') exports.exit = exit;
|
|
44
|
+
function args () {
|
|
45
|
+
let allArgs = processMod.argv;
|
|
46
|
+
let properArgs = [];
|
|
47
|
+
let idx = 2;
|
|
48
|
+
let len = allArgs.length;
|
|
49
|
+
while (idx < len ) {
|
|
50
|
+
__kadence_add(properArgs, allArgs[idx]);
|
|
51
|
+
idx++;
|
|
52
|
+
}
|
|
53
|
+
return properArgs;
|
|
54
|
+
}
|
|
55
|
+
if (typeof exports !== 'undefined') exports.args = args;
|
|
56
|
+
function cwd () {
|
|
57
|
+
return processMod.cwd();
|
|
58
|
+
}
|
|
59
|
+
if (typeof exports !== 'undefined') exports.cwd = cwd;
|
|
60
|
+
(async () => {
|
|
61
|
+
|
|
62
|
+
})().catch(err => { if (err) console.error("\x1b[31mRuntime Error:\x1b[0m", err.stack || err.message); });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
note: Process control utilities
|
|
2
|
+
|
|
3
|
+
let processMod = run require "process"
|
|
4
|
+
|
|
5
|
+
export function exit code
|
|
6
|
+
if code equals undefined
|
|
7
|
+
run processMod.exit 0
|
|
8
|
+
else
|
|
9
|
+
run processMod.exit code
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
export function args
|
|
14
|
+
let allArgs = processMod.argv
|
|
15
|
+
let properArgs = list
|
|
16
|
+
let idx = 2
|
|
17
|
+
let len = size of allArgs
|
|
18
|
+
|
|
19
|
+
while idx less than len
|
|
20
|
+
add allArgs[idx] to properArgs
|
|
21
|
+
increment idx
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
return properArgs
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
export function cwd
|
|
28
|
+
return run processMod.cwd
|
|
29
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function delay(ms) {
|
|
4
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function retry(fn, times) {
|
|
8
|
+
let lastErr;
|
|
9
|
+
const run = () =>
|
|
10
|
+
Promise.resolve(fn()).catch((err) => {
|
|
11
|
+
lastErr = err;
|
|
12
|
+
if (times <= 1) throw err;
|
|
13
|
+
times -= 1;
|
|
14
|
+
return run();
|
|
15
|
+
});
|
|
16
|
+
return run();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
20
|
+
module.exports = { delay, retry };
|
|
21
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
|
|
3
|
+
function __kadence_echo(val) {
|
|
4
|
+
const s = String(val);
|
|
5
|
+
const low = s.toLowerCase();
|
|
6
|
+
if (typeof process !== 'undefined' && process.stdout && process.stdout.isTTY) {
|
|
7
|
+
if (low.includes('error')) console.log("\x1b[31m" + s + "\x1b[0m");
|
|
8
|
+
else if (low.includes('warning')) console.log("\x1b[33m" + s + "\x1b[0m");
|
|
9
|
+
else if (low.includes('success')) console.log("\x1b[32m" + s + "\x1b[0m");
|
|
10
|
+
else console.log(s);
|
|
11
|
+
} else {
|
|
12
|
+
console.log(s);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function __kadence_min(val) {
|
|
16
|
+
if (Array.isArray(val)) return Math.min(...val);
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
function __kadence_max(val) {
|
|
20
|
+
if (Array.isArray(val)) return Math.max(...val);
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
function __kadence_add(parent, child) {
|
|
24
|
+
if (Array.isArray(parent)) {
|
|
25
|
+
parent.push(child);
|
|
26
|
+
return parent;
|
|
27
|
+
}
|
|
28
|
+
if (typeof parent === 'object' && parent !== null && typeof parent.appendChild === 'function') {
|
|
29
|
+
parent.appendChild(child);
|
|
30
|
+
return parent;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Runtime Error: Cannot add item to " + typeof parent);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let mod = require(`./promise-helpers.js`);
|
|
36
|
+
function promiseAll (promises) {
|
|
37
|
+
return Promise[`all`](promises);
|
|
38
|
+
}
|
|
39
|
+
if (typeof exports !== 'undefined') exports.promiseAll = promiseAll;
|
|
40
|
+
function promiseRace (promises) {
|
|
41
|
+
return Promise[`race`](promises);
|
|
42
|
+
}
|
|
43
|
+
if (typeof exports !== 'undefined') exports.promiseRace = promiseRace;
|
|
44
|
+
function delay (ms) {
|
|
45
|
+
return mod.delay(ms);
|
|
46
|
+
}
|
|
47
|
+
if (typeof exports !== 'undefined') exports.delay = delay;
|
|
48
|
+
function retry (fn, maxRetries) {
|
|
49
|
+
return mod.retry(fn, maxRetries);
|
|
50
|
+
}
|
|
51
|
+
if (typeof exports !== 'undefined') exports.retry = retry;
|
|
52
|
+
(async () => {
|
|
53
|
+
|
|
54
|
+
})().catch(err => { if (err) console.error("\x1b[31mRuntime Error:\x1b[0m", err.stack || err.message); });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
note: Promise utilities for Kadence (uses promise-helpers.js)
|
|
2
|
+
|
|
3
|
+
let mod = run require "./promise-helpers.js"
|
|
4
|
+
|
|
5
|
+
export function promiseAll promises
|
|
6
|
+
return run Promise["all"] promises
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
export function promiseRace promises
|
|
10
|
+
return run Promise["race"] promises
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
export function delay ms
|
|
14
|
+
return run mod.delay ms
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
export function retry fn maxRetries
|
|
18
|
+
return run mod.retry fn maxRetries
|
|
19
|
+
end
|
package/stdlib/random.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
|
|
3
|
+
function __kadence_echo(val) {
|
|
4
|
+
const s = String(val);
|
|
5
|
+
const low = s.toLowerCase();
|
|
6
|
+
if (typeof process !== 'undefined' && process.stdout && process.stdout.isTTY) {
|
|
7
|
+
if (low.includes('error')) console.log("\x1b[31m" + s + "\x1b[0m");
|
|
8
|
+
else if (low.includes('warning')) console.log("\x1b[33m" + s + "\x1b[0m");
|
|
9
|
+
else if (low.includes('success')) console.log("\x1b[32m" + s + "\x1b[0m");
|
|
10
|
+
else console.log(s);
|
|
11
|
+
} else {
|
|
12
|
+
console.log(s);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function __kadence_min(val) {
|
|
16
|
+
if (Array.isArray(val)) return Math.min(...val);
|
|
17
|
+
return val;
|
|
18
|
+
}
|
|
19
|
+
function __kadence_max(val) {
|
|
20
|
+
if (Array.isArray(val)) return Math.max(...val);
|
|
21
|
+
return val;
|
|
22
|
+
}
|
|
23
|
+
function __kadence_add(parent, child) {
|
|
24
|
+
if (Array.isArray(parent)) {
|
|
25
|
+
parent.push(child);
|
|
26
|
+
return parent;
|
|
27
|
+
}
|
|
28
|
+
if (typeof parent === 'object' && parent !== null && typeof parent.appendChild === 'function') {
|
|
29
|
+
parent.appendChild(child);
|
|
30
|
+
return parent;
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Runtime Error: Cannot add item to " + typeof parent);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function float (min, max) {
|
|
36
|
+
return min + (Math.random() * (max - min ) ) ;
|
|
37
|
+
}
|
|
38
|
+
if (typeof exports !== 'undefined') exports.float = float;
|
|
39
|
+
function integer (min, max) {
|
|
40
|
+
let rangeVal = (max - min ) + 1 ;
|
|
41
|
+
let factor = Math.random();
|
|
42
|
+
return Math.floor((min + (factor * rangeVal ) ));
|
|
43
|
+
}
|
|
44
|
+
if (typeof exports !== 'undefined') exports.integer = integer;
|
|
45
|
+
function choice (items) {
|
|
46
|
+
let len = items.length;
|
|
47
|
+
if (len === 0 ) {
|
|
48
|
+
return ``;
|
|
49
|
+
}
|
|
50
|
+
let idx = integer(0, (len - 1 ));
|
|
51
|
+
return items[idx];
|
|
52
|
+
}
|
|
53
|
+
if (typeof exports !== 'undefined') exports.choice = choice;
|
|
54
|
+
function shuffle (items) {
|
|
55
|
+
let res = [];
|
|
56
|
+
let temp = [];
|
|
57
|
+
for (let x of items) {
|
|
58
|
+
__kadence_add(temp, x);
|
|
59
|
+
}
|
|
60
|
+
let n = temp.length;
|
|
61
|
+
for (let __i = 0; __i < n; __i++) {
|
|
62
|
+
let idx = integer(0, ((temp.length) - 1 ));
|
|
63
|
+
let picked = temp[idx];
|
|
64
|
+
__kadence_add(res, picked);
|
|
65
|
+
temp.splice(idx, 1);
|
|
66
|
+
}
|
|
67
|
+
return res;
|
|
68
|
+
}
|
|
69
|
+
if (typeof exports !== 'undefined') exports.shuffle = shuffle;
|
|
70
|
+
function uuid () {
|
|
71
|
+
let chars = `0123456789abcdef`;
|
|
72
|
+
let result = `#`;
|
|
73
|
+
for (let __i = 0; __i < 6; __i++) {
|
|
74
|
+
let idx = integer(0, 15);
|
|
75
|
+
let result = result + (chars[idx]) ;
|
|
76
|
+
}
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
if (typeof exports !== 'undefined') exports.uuid = uuid;
|
|
80
|
+
(async () => {
|
|
81
|
+
|
|
82
|
+
})().catch(err => { if (err) console.error("\x1b[31mRuntime Error:\x1b[0m", err.stack || err.message); });
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
note: Random utilities for Kadence
|
|
2
|
+
|
|
3
|
+
export function float min max
|
|
4
|
+
return min plus (random times (max minus min))
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
export function integer min max
|
|
8
|
+
let rangeVal = (max minus min) plus 1
|
|
9
|
+
let factor = random
|
|
10
|
+
return floor (min plus (factor times rangeVal))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
export function choice items
|
|
14
|
+
let len = size of items
|
|
15
|
+
if len equals 0
|
|
16
|
+
return ""
|
|
17
|
+
end
|
|
18
|
+
let idx = run integer 0 (len minus 1)
|
|
19
|
+
return items[idx]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
export function shuffle items
|
|
23
|
+
let res = list
|
|
24
|
+
let temp = list
|
|
25
|
+
for each x in items
|
|
26
|
+
add x to temp
|
|
27
|
+
end
|
|
28
|
+
let n = size of temp
|
|
29
|
+
repeat n times
|
|
30
|
+
let idx = run integer 0 ((size of temp) minus 1)
|
|
31
|
+
let picked = temp[idx]
|
|
32
|
+
add picked to res
|
|
33
|
+
remove item idx from temp
|
|
34
|
+
end
|
|
35
|
+
return res
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
export function uuid
|
|
39
|
+
let chars = "0123456789abcdef"
|
|
40
|
+
let result = "#"
|
|
41
|
+
repeat 6 times
|
|
42
|
+
let idx = run integer 0 15
|
|
43
|
+
let result = result plus (chars[idx])
|
|
44
|
+
end
|
|
45
|
+
return result
|
|
46
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function test(pattern, str) {
|
|
4
|
+
return new RegExp(pattern).test(str);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function replaceAll(pattern, str, replacement) {
|
|
8
|
+
return str.replace(new RegExp(pattern, "g"), replacement);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function validateUuid(uuid) {
|
|
12
|
+
const pattern = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
13
|
+
return pattern.test(uuid);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (typeof module !== "undefined" && module.exports) {
|
|
17
|
+
module.exports = { test, replaceAll, validateUuid };
|
|
18
|
+
}
|