Skip to content

Commit f1ba5e6

Browse files
author
Geoffrey Hendrey
committed
Typescript
1 parent 2e341b3 commit f1ba5e6

27 files changed

+273
-308
lines changed

.babelrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"presets": ["@babel/preset-env","@babel/preset-modules"],
3-
"plugins": ["@babel/plugin-proposal-unicode-property-regex"],
4-
"sourceMaps": "inline"
3+
"plugins": ["@babel/plugin-proposal-unicode-property-regex"]
54
}

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ to execute efficiently:
2323
}
2424
```
2525
Unlike an ordinary program, Stated templates can be kept "alive" indefinitely. A change to any of the independent fields
26-
causes change propagation throughout the DAG. Stated includes a node REPL, `stated.js`, for testing Stated json templates, and a JS library for embedding stated
26+
causes change propagation throughout the DAG. Stated includes a node REPL, `stated.ts`, for testing Stated json templates, and a JS library for embedding stated
2727
in applications. A typical REPL session consists of loading a template with the `init` command, viewing the computed
2828
output with the `.out` command and then setting values with the `.set` command and observing the changed output.
2929
```json
@@ -142,7 +142,7 @@ REPL:
142142
> .init -f "example/ex01.json"
143143
```
144144
### Oneshot mode
145-
in oneshot mode, stated.js simply computes the output template and exits. This is useful when you do not intend to
145+
in oneshot mode, stated.ts simply computes the output template and exits. This is useful when you do not intend to
146146
change any if the fields after the initial output render
147147
```bash
148148
falken$ stated example/ex01.json
@@ -240,7 +240,7 @@ verbose: initializing...
240240
debug: tags: {}
241241
verbose: evaluating template...
242242
error: Error evaluating expression at /b
243-
error: The right side of the "+" operator must evaluate to a number {"code":"T2002","position":3,"stack":"Error\n at evaluateNumericExpression (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:4122:25)\n at evaluateBinary (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:3900:30)\n at async evaluate (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:3490:26)\n at async Object.evaluate (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:5558:26)\n at async TemplateProcessor._evaluateExprNode (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.js:637:25)\n at async TemplateProcessor._evaluateExpression (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.js:556:28)\n at async TemplateProcessor.evaluateJsonPointersInOrder (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.js:515:31)\n at async TemplateProcessor.evaluateDependencies (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.js:358:16)\n at async TemplateProcessor.evaluate (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.js:123:9)\n at async TemplateProcessor.initialize (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.js:113:9)","token":"+","value":" is not a string"}
243+
error: The right side of the "+" operator must evaluate to a number {"code":"T2002","position":3,"stack":"Error\n at evaluateNumericExpression (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:4122:25)\n at evaluateBinary (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:3900:30)\n at async evaluate (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:3490:26)\n at async Object.evaluate (/Users/ghendrey/proj/jsonataexperiments/node_modules/jsonata/jsonata.js:5558:26)\n at async TemplateProcessor._evaluateExprNode (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.ts:637:25)\n at async TemplateProcessor._evaluateExpression (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.ts:556:28)\n at async TemplateProcessor.evaluateJsonPointersInOrder (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.ts:515:31)\n at async TemplateProcessor.evaluateDependencies (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.ts:358:16)\n at async TemplateProcessor.evaluate (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.ts:123:9)\n at async TemplateProcessor.initialize (file:///Users/ghendrey/proj/jsonataexperiments/src/TemplateProcessor.ts:113:9)","token":"+","value":" is not a string"}
244244
debug: Expression: a + ' is not a string'
245245
debug: Target: {
246246
"a": 42,

README.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import CliCore from './src/CliCore';
15+
import CliCore from './dist/src/CliCore.js';
1616
import fs from 'fs';
17-
import StatedREPL from './src/StatedREPL.js';
17+
import StatedREPL from './dist/src/StatedREPL.js';
1818

1919
/**
2020
* Regular expression for command extraction from README.md file. This program finds all the markup code blocks

index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77
</head>
88
<body>
99
<h1>Template Processor Test</h1>
10+
<h2>You should serve this file. It will CORS error if you just try to open the file.</h2>
1011
<div id="output"></div>
12+
<!--
13+
serve this HTML file with http-server.
14+
> npm install -g http-server
15+
> http-server
1116
17+
Then point browser at http://127.0.0.1:8080/
18+
-->
1219
<script type="module">
13-
import TemplateProcessor from "/dist/bundle.mjs"
20+
import TemplateProcessor from "http://127.0.0.1:8080/dist/bundle.mjs"
1421

1522
// Wait for the document to load
1623
document.addEventListener('DOMContentLoaded', async () => {

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
"version": "0.0.57",
44
"license": "Apache-2.0",
55
"description": "JSONata embedded in JSON",
6-
"main": "src/TemplateProcessor.js",
7-
"module": "src/TemplateProcessor.js",
6+
"main": "dist/TemplateProcessor.js",
7+
"module": "dist/bundle.mjs",
88
"typings": "src/types/TemplateProcessor.d.ts",
99
"exports": {
1010
".": {
1111
"import": "./dist/bundle.mjs"
1212
}
1313
},
1414
"scripts": {
15+
"clean": "rm -rf dist",
16+
"build": "tsc && webpack",
17+
"webpack": "webpack",
1518
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
16-
"start": "node index.js",
17-
"stated": "./stated.js"
19+
"stated": "node --experimental-vm-modules dist/stated.js"
1820
},
1921
"bin": {
20-
"stated": "./stated.js"
22+
"stated": "stated.js"
2123
},
2224
"repository": {
2325
"type": "git",
@@ -35,6 +37,7 @@
3537
"flatbuffers": "^23.5.26",
3638
"js-yaml": "^4.1.0",
3739
"jsonata": "^2.0.3",
40+
"lodash": "^4.17.21",
3841
"lodash-es": "^4.17.21",
3942
"minimist": "^1.2.8",
4043
"repl": "^0.1.3",
@@ -49,6 +52,7 @@
4952
"babel-loader": "^9.1.3",
5053
"jest": "^29.7.0",
5154
"path-browserify": "^1.0.1",
55+
"typescript": "^5.2.2",
5256
"webpack": "^5.88.2",
5357
"webpack-cli": "^5.1.4"
5458
},
@@ -58,7 +62,7 @@
5862
"package.json",
5963
"README.md",
6064
"README.test.js",
61-
"stated.js",
65+
"stated.ts",
6266
"yarn.lock",
6367
"src/",
6468
"dist/",

src/CliCore.js renamed to src/CliCore.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ import TemplateProcessor from './TemplateProcessor.js';
1717
import yaml from 'js-yaml';
1818
import minimist from 'minimist';
1919
import {parseArgsStringToArgv} from 'string-argv';
20+
import {logLevels} from "./ConsoleLogger.js";
2021

2122

2223
export default class CliCore {
24+
private templateProcessor: TemplateProcessor;
25+
private logLevel: keyof typeof logLevels;
2326
constructor() {
2427
this.templateProcessor = null;
2528
this.logLevel = "info";
@@ -184,7 +187,7 @@ export default class CliCore {
184187
return {"log level":level};
185188
}
186189

187-
note(note){
190+
note(){
188191
return "=============================================================";
189192
}
190193

@@ -196,7 +199,7 @@ export default class CliCore {
196199
return this.templateProcessor.debugger.processCommands(parsed);
197200
}
198201

199-
async errors(replCmdInputStr) {
202+
async errors() {
200203
if (!this.templateProcessor) {
201204
throw new Error('Initialize the template first.');
202205
}

src/ConsoleLogger.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/ConsoleLogger.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
export interface StatedLogger{
2+
debug(...args):void;
3+
error(...args):void;
4+
warn(...args):void;
5+
info(...args):void;
6+
verbose(...args):void;
7+
log(level, ...args): void;
8+
level:string;
9+
}
10+
export const logLevels = {
11+
silent: 0,
12+
error: 1,
13+
warn: 2,
14+
info: 3,
15+
verbose: 4,
16+
debug: 5
17+
};
18+
export default class ConsoleLogger implements StatedLogger{
19+
20+
public level: string;
21+
constructor(initialLevel = 'verbose') {
22+
this.level = initialLevel;
23+
}
24+
25+
debug(...args) {
26+
if (logLevels[this.level] >= logLevels.debug) {
27+
console.debug(...args);
28+
}
29+
}
30+
31+
error(...args) {
32+
if (logLevels[this.level] >= logLevels.error) {
33+
console.error(...args);
34+
}
35+
}
36+
37+
warn(...args) {
38+
if (logLevels[this.level] >= logLevels.warn) {
39+
console.warn(...args);
40+
}
41+
}
42+
43+
info(...args) {
44+
if (logLevels[this.level] >= logLevels.info) {
45+
console.info(...args);
46+
}
47+
}
48+
49+
verbose(...args) {
50+
if (logLevels[this.level] >= logLevels.verbose) {
51+
console.log(...args);
52+
}
53+
}
54+
55+
log(level, ...args) {
56+
this[level](args);
57+
}
58+
}
59+

src/Debugger.js renamed to src/Debugger.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import {default as jp} from './JsonPointer.js';
44
* Debugger class that manages breakpoints.
55
*/
66
export default class Debugger {
7+
private _breakpoints: Map<any, any>;
8+
private _templateMeta: any;
9+
logger: any;
710
constructor(metaInfosByJsonPointer, logger) {
811
/**
912
* Stores the breakpoints set by the user.
@@ -81,7 +84,7 @@ export default class Debugger {
8184
enableBreakpoint(metaInfo, enabled) {
8285
this._breakpoints.get(metaInfo.jsonPointer__).enabled = enabled;
8386
if(enabled===true) {
84-
metaInfo.break__ = handler;
87+
metaInfo.break__ = {}//handler;
8588
}else{
8689
delete metaInfo.break__;
8790
}

0 commit comments

Comments
 (0)