@gershy/util-jsfn-encode 0.0.1 → 0.0.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/cmp/cjs/main.d.ts CHANGED
@@ -1,4 +1,27 @@
1
1
  import '../sideEffects.js';
2
2
  import '@gershy/clearing';
3
- declare const _default: null;
3
+ export type JsImport = {
4
+ varDef: null | string;
5
+ importPath: string;
6
+ };
7
+ export type SovereignFn = (...args: any) => any;
8
+ export type JsfnInst<Cls extends abstract new (...args: Jsfn[]) => any> = {
9
+ toJsfn: () => JsfnInstSer<Cls>;
10
+ };
11
+ export type JsfnInstSer<Cls extends abstract new (...args: Jsfn[]) => any> = {
12
+ hoist: `${string}::${string}`;
13
+ form: Cls;
14
+ args: ConstructorParameters<Cls>;
15
+ };
16
+ export type Jsfn = null | boolean | number | string | SovereignFn | JsfnInst<any> | Jsfn[] | {
17
+ [K: string]: Jsfn;
18
+ };
19
+ export type JsfnEncodeArgs<V extends Jsfn> = {
20
+ val: V;
21
+ baseUrl: string;
22
+ };
23
+ declare const _default: <V extends Jsfn>(args: JsfnEncodeArgs<V>) => {
24
+ code: string;
25
+ jsImports: JsImport[];
26
+ };
4
27
  export default _default;
package/cmp/cjs/main.js CHANGED
@@ -1,4 +1,46 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  require("@gershy/clearing");
4
- exports.default = null;
4
+ exports.default = (args) => {
5
+ // const importReg = niceRegex(String[cl.baseline](`
6
+ // | ^[ ]* (?: )?
7
+ // | const[ ] [=][ ]* [.]jsfnImport[(]['#] ['#][)] [ ]+as[ ]
8
+ // | ([^=]+) [a-zA-Z][a-zA-Z0-9.]* ([^'#]+)
9
+ // `).replaceAll('#', '`'));
10
+ // Captures lines like:
11
+ // const util = ctx.jsfnImport('<repo>/src/boot/util');
12
+ // const util = ctx.jsfnImport('<repo>/src/boot/util') as typeof import('../src/boot/util');
13
+ // const { util1, util2 } = ctx.jsfnImport('<repo>/src/boot/util') as typeof import('../src/boot/util');
14
+ // const { util1, util2 } = c.jsfnImport('<repo>/src/boot/util') as typeof import('../src/boot/util');
15
+ const jsImports = []; // Note "js imports" are a "reference to code, including a url, relative filepath, or name of an npm module"
16
+ const serializeObjKey = (key) => /^[$_a-zA-Z][$_a-zA-Z]+$/.test(key) ? key : `'${key.replaceAll(`'`, `\\'`)}'`;
17
+ const serialize = (val) => {
18
+ if (cl.isCls(val, Array))
19
+ return '[' + val.map((v) => serialize(v)).join(',') + ']';
20
+ if (cl.isCls(val, Object))
21
+ return '{' + val[cl.toArr]((v, k) => `${serializeObjKey(k)}:${serialize(v)}`).join(',') + '}';
22
+ if (cl.inCls(val, Function)) {
23
+ const importReg = /\b(?:const|let|var)[ ]*([^=]+)[=][ ]*[a-zA-Z][a-zA-Z0-9.]*[.]jsfnImport[(]["'`]([^"'`]+)["'`][)][;]?/g;
24
+ return val.toString().replace(importReg, (full, varDef, importPath) => {
25
+ jsImports.push({ varDef, importPath });
26
+ return `/*jsfn:hoisted:${full}*/`;
27
+ });
28
+ }
29
+ if (cl.inCls(val?.toJsfn, Function)) {
30
+ const { args, form, hoist } = val;
31
+ const [importPath, clsName] = hoist.split('::');
32
+ jsImports.push({ varDef: clsName, importPath });
33
+ return `new ${clsName}(${args.map(a => serialize(a)).join(',')})`;
34
+ }
35
+ return JSON.stringify(val);
36
+ };
37
+ return {
38
+ // Note `code` is stringified, but it isn't json - it's js, in string representation
39
+ code: serialize(args.val), // `jsImports` isn't populated until this is called!
40
+ // Imports beginning with "." are treated as relative paths
41
+ jsImports: jsImports.map(ji => ji.importPath[0] !== '.' ? ji : {
42
+ ...ji,
43
+ importPath: new URL(ji.importPath, args.baseUrl).href
44
+ })
45
+ };
46
+ };
package/cmp/mjs/main.d.ts CHANGED
@@ -1,4 +1,27 @@
1
1
  import '../sideEffects.js';
2
2
  import '@gershy/clearing';
3
- declare const _default: null;
3
+ export type JsImport = {
4
+ varDef: null | string;
5
+ importPath: string;
6
+ };
7
+ export type SovereignFn = (...args: any) => any;
8
+ export type JsfnInst<Cls extends abstract new (...args: Jsfn[]) => any> = {
9
+ toJsfn: () => JsfnInstSer<Cls>;
10
+ };
11
+ export type JsfnInstSer<Cls extends abstract new (...args: Jsfn[]) => any> = {
12
+ hoist: `${string}::${string}`;
13
+ form: Cls;
14
+ args: ConstructorParameters<Cls>;
15
+ };
16
+ export type Jsfn = null | boolean | number | string | SovereignFn | JsfnInst<any> | Jsfn[] | {
17
+ [K: string]: Jsfn;
18
+ };
19
+ export type JsfnEncodeArgs<V extends Jsfn> = {
20
+ val: V;
21
+ baseUrl: string;
22
+ };
23
+ declare const _default: <V extends Jsfn>(args: JsfnEncodeArgs<V>) => {
24
+ code: string;
25
+ jsImports: JsImport[];
26
+ };
4
27
  export default _default;
package/cmp/mjs/main.js CHANGED
@@ -1,2 +1,44 @@
1
1
  import '@gershy/clearing';
2
- export default null;
2
+ export default (args) => {
3
+ // const importReg = niceRegex(String[cl.baseline](`
4
+ // | ^[ ]* (?: )?
5
+ // | const[ ] [=][ ]* [.]jsfnImport[(]['#] ['#][)] [ ]+as[ ]
6
+ // | ([^=]+) [a-zA-Z][a-zA-Z0-9.]* ([^'#]+)
7
+ // `).replaceAll('#', '`'));
8
+ // Captures lines like:
9
+ // const util = ctx.jsfnImport('<repo>/src/boot/util');
10
+ // const util = ctx.jsfnImport('<repo>/src/boot/util') as typeof import('../src/boot/util');
11
+ // const { util1, util2 } = ctx.jsfnImport('<repo>/src/boot/util') as typeof import('../src/boot/util');
12
+ // const { util1, util2 } = c.jsfnImport('<repo>/src/boot/util') as typeof import('../src/boot/util');
13
+ const jsImports = []; // Note "js imports" are a "reference to code, including a url, relative filepath, or name of an npm module"
14
+ const serializeObjKey = (key) => /^[$_a-zA-Z][$_a-zA-Z]+$/.test(key) ? key : `'${key.replaceAll(`'`, `\\'`)}'`;
15
+ const serialize = (val) => {
16
+ if (cl.isCls(val, Array))
17
+ return '[' + val.map((v) => serialize(v)).join(',') + ']';
18
+ if (cl.isCls(val, Object))
19
+ return '{' + val[cl.toArr]((v, k) => `${serializeObjKey(k)}:${serialize(v)}`).join(',') + '}';
20
+ if (cl.inCls(val, Function)) {
21
+ const importReg = /\b(?:const|let|var)[ ]*([^=]+)[=][ ]*[a-zA-Z][a-zA-Z0-9.]*[.]jsfnImport[(]["'`]([^"'`]+)["'`][)][;]?/g;
22
+ return val.toString().replace(importReg, (full, varDef, importPath) => {
23
+ jsImports.push({ varDef, importPath });
24
+ return `/*jsfn:hoisted:${full}*/`;
25
+ });
26
+ }
27
+ if (cl.inCls(val?.toJsfn, Function)) {
28
+ const { args, form, hoist } = val;
29
+ const [importPath, clsName] = hoist.split('::');
30
+ jsImports.push({ varDef: clsName, importPath });
31
+ return `new ${clsName}(${args.map(a => serialize(a)).join(',')})`;
32
+ }
33
+ return JSON.stringify(val);
34
+ };
35
+ return {
36
+ // Note `code` is stringified, but it isn't json - it's js, in string representation
37
+ code: serialize(args.val), // `jsImports` isn't populated until this is called!
38
+ // Imports beginning with "." are treated as relative paths
39
+ jsImports: jsImports.map(ji => ji.importPath[0] !== '.' ? ji : {
40
+ ...ji,
41
+ importPath: new URL(ji.importPath, args.baseUrl).href
42
+ })
43
+ };
44
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gershy/util-jsfn-encode",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "TODO",
5
5
  "keywords": [
6
6
  "TODO"