From 8dc49c736c247bc9840801b24c0290ae1af48028 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Tue, 20 Jul 2021 13:43:22 +0200 Subject: [PATCH 1/7] be nice to Deno parser --- src/class-parser.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/class-parser.ts b/src/class-parser.ts index b57e6b8..37f00cf 100644 --- a/src/class-parser.ts +++ b/src/class-parser.ts @@ -19,7 +19,7 @@ export interface Options { } } -export const parse = function (raw: string, options?: Options): any { +export const parse = function (raw: string, options: Options = {}): any { let currentPosition: number = 0; let current = function (): string { return raw[currentPosition] || ''; @@ -33,7 +33,7 @@ export const parse = function (raw: string, options?: Options): any { return string; }; let indexOfOrMaxInt = function (str: string, fromPos: number) { - const pos = this.indexOf(str, fromPos); + const pos = raw.indexOf(str, fromPos); return pos === -1 ? Infinity : pos; }; let parseArray = function (): any[] { @@ -55,7 +55,7 @@ export const parse = function (raw: string, options?: Options): any { next(); return result; }; - let parseProperty = function (context): any { + let parseProperty = function (context: { [index:string]: any }): any { let name = parsePropertyName(), value; @@ -172,7 +172,7 @@ export const parse = function (raw: string, options?: Options): any { return raw.substr(currentPosition, 6).indexOf('" \\n "') === 0; }, forwardToNextQuote = function(): void { - currentPosition = indexOfOrMaxInt.call(raw, chars.QUOTE, currentPosition + 1); + currentPosition = indexOfOrMaxInt(chars.QUOTE, currentPosition + 1); }, parseString = function(): any { let result = ''; @@ -230,9 +230,9 @@ export const parse = function (raw: string, options?: Options): any { }, parseMathExpression = function() { const posOfExpressionEnd = Math.min( - indexOfOrMaxInt.call(raw, chars.SEMICOLON, currentPosition), - indexOfOrMaxInt.call(raw, chars.CURLY_CLOSE, currentPosition), - indexOfOrMaxInt.call(raw, chars.COMMA, currentPosition) + indexOfOrMaxInt(chars.SEMICOLON, currentPosition), + indexOfOrMaxInt(chars.CURLY_CLOSE, currentPosition), + indexOfOrMaxInt(chars.COMMA, currentPosition) ); const expression = raw.substr(currentPosition, posOfExpressionEnd - currentPosition); assert(posOfExpressionEnd !== Infinity); @@ -254,7 +254,7 @@ export const parse = function (raw: string, options?: Options): any { } return result; }, - isValidVarnameChar = function(char): boolean { + isValidVarnameChar = function(char: string): boolean { return (char >= '0' && char <= '9') || (char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z') || @@ -285,8 +285,6 @@ export const parse = function (raw: string, options?: Options): any { return result; }; - options = options || {}; - if (typeof raw !== 'string') { throw new TypeError('expecting string!'); } From 98c71abe78e900ce320a18f30aaf0e03cf1aa651 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Tue, 20 Jul 2021 14:31:12 +0200 Subject: [PATCH 2/7] add CLI for NodeJS & Deno --- cli/deno.ts | 5 +++++ cli/node.js | 6 ++++++ package.json | 3 ++- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 cli/deno.ts create mode 100644 cli/node.js diff --git a/cli/deno.ts b/cli/deno.ts new file mode 100644 index 0000000..99cf401 --- /dev/null +++ b/cli/deno.ts @@ -0,0 +1,5 @@ +import { parse } from './src/class-parser.ts' + +const stdin = new TextDecoder().decode(await Deno.readAll(Deno.stdin)); + +console.log(parse(stdin)) diff --git a/cli/node.js b/cli/node.js new file mode 100644 index 0000000..73b4ba9 --- /dev/null +++ b/cli/node.js @@ -0,0 +1,6 @@ +const fs = require('fs'); +const parse = require('../dist/class-parser.js').parse; + +const data = fs.readFileSync(process.stdin.fd, 'utf-8'); + +console.log(parse(data)) diff --git a/package.json b/package.json index d456b40..415bb6f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "files": [ "dist/*.js", "dist/*.d.ts", - "dist/*.map.js" + "dist/*.map.js", + "cli/*.* ], "main": "dist/class-parser", "dependencies": {}, From e64ceed4e003f0601056dfdd17a51b0fff4820f5 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Tue, 20 Jul 2021 14:47:05 +0200 Subject: [PATCH 3/7] fix. add class-parser source to dist --- cli/deno.ts | 4 ++-- cli/node.js | 2 +- package.json | 2 +- src/class-parser.ts | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/deno.ts b/cli/deno.ts index 99cf401..14ca1ed 100644 --- a/cli/deno.ts +++ b/cli/deno.ts @@ -1,5 +1,5 @@ -import { parse } from './src/class-parser.ts' +import { parse } from "../src/class-parser.ts" const stdin = new TextDecoder().decode(await Deno.readAll(Deno.stdin)); -console.log(parse(stdin)) +Deno.stdout.write(new TextEncoder().encode(JSON.stringify(parse(stdin)))) diff --git a/cli/node.js b/cli/node.js index 73b4ba9..3e0b58b 100644 --- a/cli/node.js +++ b/cli/node.js @@ -3,4 +3,4 @@ const parse = require('../dist/class-parser.js').parse; const data = fs.readFileSync(process.stdin.fd, 'utf-8'); -console.log(parse(data)) +process.stdout.write(JSON.stringify(parse(data))) diff --git a/package.json b/package.json index 415bb6f..185dc7d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "dist/*.js", "dist/*.d.ts", "dist/*.map.js", - "cli/*.* + "src/class-parser.ts" ], "main": "dist/class-parser", "dependencies": {}, diff --git a/src/class-parser.ts b/src/class-parser.ts index 37f00cf..548baf3 100644 --- a/src/class-parser.ts +++ b/src/class-parser.ts @@ -19,7 +19,7 @@ export interface Options { } } -export const parse = function (raw: string, options: Options = {}): any { +export const parse = function (raw: string, options: Options = {}): object { let currentPosition: number = 0; let current = function (): string { return raw[currentPosition] || ''; From 3a2266a28d822f33d5bc561c882600ec1bba9408 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Tue, 20 Jul 2021 14:50:01 +0200 Subject: [PATCH 4/7] mention Deno in readme --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6c50d04..69f625a 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,9 @@ completely ignores # prerequisites -NodeJS +NodeJS or Deno + +### in NodeJS # similar projects in other languages From 864365e5f90b1d066343f28db86520f60686fda9 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Wed, 21 Jul 2021 12:13:09 +0200 Subject: [PATCH 5/7] add cli files to package --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 185dc7d..355482f 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ ], "author": "Moritz Schmidt ", "files": [ + "cli/*.*", "dist/*.js", "dist/*.d.ts", "dist/*.map.js", From 040d15ee884318acf2705bd7f79a5bd0d0b92975 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Wed, 21 Jul 2021 12:13:25 +0200 Subject: [PATCH 6/7] fix amd resolution --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 355482f..9fdef15 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ }, "scripts": { "test": "tsc && jest --config jestconfig.json", - "build:amd": "tsc --module amd --outFile dist/class-parser.amd.js", + "build:amd": "tsc --module amd --moduleResolution node --outFile dist/class-parser.amd.js", "build:web": "tsc && webpack -p && rm -r docs/dist", "build": "tsc" }, From a729690ba4cbf73fd954bcceeacd5d475fcb9d92 Mon Sep 17 00:00:00 2001 From: Moritz Schmidt Date: Wed, 21 Jul 2021 12:16:01 +0200 Subject: [PATCH 7/7] add build:release --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 9fdef15..04e807c 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "test": "tsc && jest --config jestconfig.json", "build:amd": "tsc --module amd --moduleResolution node --outFile dist/class-parser.amd.js", "build:web": "tsc && webpack -p && rm -r docs/dist", - "build": "tsc" + "build": "tsc", + "build:release": "npm run build:web && npm run build:amd" }, "jest": { "verbose": true,