@gershy/util-jsfn-encode 0.0.1 → 0.0.3
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/cmp/cjs/main.d.ts +24 -2
- package/cmp/cjs/main.js +55 -3
- package/cmp/esm/main.d.ts +26 -0
- package/cmp/esm/main.js +36 -0
- package/cmp/esm/sideEffects.d.ts +2 -0
- package/package.json +3 -3
- package/cmp/mjs/main.d.ts +0 -4
- package/cmp/mjs/main.js +0 -2
- /package/cmp/{sideEffects.d.ts → cjs/sideEffects.d.ts} +0 -0
- /package/cmp/{mjs → esm}/package.json +0 -0
package/cmp/cjs/main.d.ts
CHANGED
|
@@ -1,4 +1,26 @@
|
|
|
1
|
-
import '../sideEffects.js';
|
|
2
1
|
import '@gershy/clearing';
|
|
3
|
-
|
|
2
|
+
export type JsImport = {
|
|
3
|
+
varDef: null | string;
|
|
4
|
+
importPath: string;
|
|
5
|
+
};
|
|
6
|
+
export type SovereignFn = (...args: any) => any;
|
|
7
|
+
export type JsfnInst<Cls extends abstract new (...args: Jsfn[]) => any> = {
|
|
8
|
+
toJsfn: () => JsfnInstSer<Cls>;
|
|
9
|
+
};
|
|
10
|
+
export type JsfnInstSer<Cls extends abstract new (...args: Jsfn[]) => any> = {
|
|
11
|
+
hoist: `${string}::${string}`;
|
|
12
|
+
form: Cls;
|
|
13
|
+
args: ConstructorParameters<Cls>;
|
|
14
|
+
};
|
|
15
|
+
export type Jsfn = null | boolean | number | string | SovereignFn | JsfnInst<any> | Jsfn[] | {
|
|
16
|
+
[K: string]: Jsfn;
|
|
17
|
+
};
|
|
18
|
+
export type JsfnEncodeArgs<V extends Jsfn> = {
|
|
19
|
+
val: V;
|
|
20
|
+
baseUrl: string;
|
|
21
|
+
};
|
|
22
|
+
declare const _default: <V extends Jsfn>(args: JsfnEncodeArgs<V>) => {
|
|
23
|
+
code: string;
|
|
24
|
+
jsImports: JsImport[];
|
|
25
|
+
};
|
|
4
26
|
export default _default;
|
package/cmp/cjs/main.js
CHANGED
|
@@ -1,4 +1,56 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var main_exports = {};
|
|
20
|
+
__export(main_exports, {
|
|
21
|
+
default: () => main_default
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(main_exports);
|
|
24
|
+
var import_clearing = require("@gershy/clearing");
|
|
25
|
+
var main_default = (args) => {
|
|
26
|
+
const jsImports = [];
|
|
27
|
+
const serializeObjKey = (key) => /^[$_a-zA-Z][$_a-zA-Z]+$/.test(key) ? key : `'${key.replaceAll(`'`, `\\'`)}'`;
|
|
28
|
+
const serialize = (val) => {
|
|
29
|
+
if (cl.isCls(val, Array)) return "[" + val.map((v) => serialize(v)).join(",") + "]";
|
|
30
|
+
if (cl.isCls(val, Object)) return "{" + val[cl.toArr]((v, k) => `${serializeObjKey(k)}:${serialize(v)}`).join(",") + "}";
|
|
31
|
+
if (cl.inCls(val, Function)) {
|
|
32
|
+
const importReg = /\b(?:const|let|var)[ ]*([^=]+)[=][ ]*[a-zA-Z][a-zA-Z0-9.]*[.]jsfnImport[(]["'`]([^"'`]+)["'`][)][;]?/g;
|
|
33
|
+
return val.toString().replace(importReg, (full, varDef, importPath) => {
|
|
34
|
+
jsImports.push({ varDef, importPath });
|
|
35
|
+
return `/*jsfn:hoisted:${full}*/`;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
if (cl.inCls(val?.toJsfn, Function)) {
|
|
39
|
+
const { args: args2, hoist } = val;
|
|
40
|
+
const [importPath, clsName] = hoist.split("::");
|
|
41
|
+
jsImports.push({ varDef: clsName, importPath });
|
|
42
|
+
return `new ${clsName}(${args2.map((a) => serialize(a)).join(",")})`;
|
|
43
|
+
}
|
|
44
|
+
return JSON.stringify(val);
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
// Note `code` is stringified, but it isn't json - it's js, in string representation
|
|
48
|
+
code: serialize(args.val),
|
|
49
|
+
// `jsImports` isn't populated until this is called!
|
|
50
|
+
// Imports beginning with "." are treated as relative paths
|
|
51
|
+
jsImports: jsImports.map((ji) => ji.importPath[0] !== "." ? ji : {
|
|
52
|
+
...ji,
|
|
53
|
+
importPath: new URL(ji.importPath, args.baseUrl).href
|
|
54
|
+
})
|
|
55
|
+
};
|
|
56
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import '@gershy/clearing';
|
|
2
|
+
export type JsImport = {
|
|
3
|
+
varDef: null | string;
|
|
4
|
+
importPath: string;
|
|
5
|
+
};
|
|
6
|
+
export type SovereignFn = (...args: any) => any;
|
|
7
|
+
export type JsfnInst<Cls extends abstract new (...args: Jsfn[]) => any> = {
|
|
8
|
+
toJsfn: () => JsfnInstSer<Cls>;
|
|
9
|
+
};
|
|
10
|
+
export type JsfnInstSer<Cls extends abstract new (...args: Jsfn[]) => any> = {
|
|
11
|
+
hoist: `${string}::${string}`;
|
|
12
|
+
form: Cls;
|
|
13
|
+
args: ConstructorParameters<Cls>;
|
|
14
|
+
};
|
|
15
|
+
export type Jsfn = null | boolean | number | string | SovereignFn | JsfnInst<any> | Jsfn[] | {
|
|
16
|
+
[K: string]: Jsfn;
|
|
17
|
+
};
|
|
18
|
+
export type JsfnEncodeArgs<V extends Jsfn> = {
|
|
19
|
+
val: V;
|
|
20
|
+
baseUrl: string;
|
|
21
|
+
};
|
|
22
|
+
declare const _default: <V extends Jsfn>(args: JsfnEncodeArgs<V>) => {
|
|
23
|
+
code: string;
|
|
24
|
+
jsImports: JsImport[];
|
|
25
|
+
};
|
|
26
|
+
export default _default;
|
package/cmp/esm/main.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import "@gershy/clearing";
|
|
2
|
+
var main_default = (args) => {
|
|
3
|
+
const jsImports = [];
|
|
4
|
+
const serializeObjKey = (key) => /^[$_a-zA-Z][$_a-zA-Z]+$/.test(key) ? key : `'${key.replaceAll(`'`, `\\'`)}'`;
|
|
5
|
+
const serialize = (val) => {
|
|
6
|
+
if (cl.isCls(val, Array)) return "[" + val.map((v) => serialize(v)).join(",") + "]";
|
|
7
|
+
if (cl.isCls(val, Object)) return "{" + val[cl.toArr]((v, k) => `${serializeObjKey(k)}:${serialize(v)}`).join(",") + "}";
|
|
8
|
+
if (cl.inCls(val, Function)) {
|
|
9
|
+
const importReg = /\b(?:const|let|var)[ ]*([^=]+)[=][ ]*[a-zA-Z][a-zA-Z0-9.]*[.]jsfnImport[(]["'`]([^"'`]+)["'`][)][;]?/g;
|
|
10
|
+
return val.toString().replace(importReg, (full, varDef, importPath) => {
|
|
11
|
+
jsImports.push({ varDef, importPath });
|
|
12
|
+
return `/*jsfn:hoisted:${full}*/`;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
if (cl.inCls(val?.toJsfn, Function)) {
|
|
16
|
+
const { args: args2, hoist } = val;
|
|
17
|
+
const [importPath, clsName] = hoist.split("::");
|
|
18
|
+
jsImports.push({ varDef: clsName, importPath });
|
|
19
|
+
return `new ${clsName}(${args2.map((a) => serialize(a)).join(",")})`;
|
|
20
|
+
}
|
|
21
|
+
return JSON.stringify(val);
|
|
22
|
+
};
|
|
23
|
+
return {
|
|
24
|
+
// Note `code` is stringified, but it isn't json - it's js, in string representation
|
|
25
|
+
code: serialize(args.val),
|
|
26
|
+
// `jsImports` isn't populated until this is called!
|
|
27
|
+
// Imports beginning with "." are treated as relative paths
|
|
28
|
+
jsImports: jsImports.map((ji) => ji.importPath[0] !== "." ? ji : {
|
|
29
|
+
...ji,
|
|
30
|
+
importPath: new URL(ji.importPath, args.baseUrl).href
|
|
31
|
+
})
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export {
|
|
35
|
+
main_default as default
|
|
36
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gershy/util-jsfn-encode",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "TODO",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"TODO"
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"homepage": "https://github.com/gershyNpm/utilJsfnEncode#readme",
|
|
17
17
|
"license": "ISC",
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@gershy/clearing": "^0.0.
|
|
19
|
+
"@gershy/clearing": "^0.0.41"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/node": "^24.10.1",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"types": "./cmp/mjs/main.d.ts",
|
|
33
33
|
"exports": {
|
|
34
34
|
".": {
|
|
35
|
-
"import": "./cmp/
|
|
35
|
+
"import": "./cmp/esm/main.js",
|
|
36
36
|
"require": "./cmp/cjs/main.js"
|
|
37
37
|
}
|
|
38
38
|
},
|
package/cmp/mjs/main.d.ts
DELETED
package/cmp/mjs/main.js
DELETED
|
File without changes
|
|
File without changes
|