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 diff --git a/cli/deno.ts b/cli/deno.ts new file mode 100644 index 0000000..14ca1ed --- /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)); + +Deno.stdout.write(new TextEncoder().encode(JSON.stringify(parse(stdin)))) diff --git a/cli/node.js b/cli/node.js new file mode 100644 index 0000000..3e0b58b --- /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'); + +process.stdout.write(JSON.stringify(parse(data))) diff --git a/package.json b/package.json index d456b40..04e807c 100644 --- a/package.json +++ b/package.json @@ -10,9 +10,11 @@ ], "author": "Moritz Schmidt ", "files": [ + "cli/*.*", "dist/*.js", "dist/*.d.ts", - "dist/*.map.js" + "dist/*.map.js", + "src/class-parser.ts" ], "main": "dist/class-parser", "dependencies": {}, @@ -32,9 +34,10 @@ }, "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" + "build": "tsc", + "build:release": "npm run build:web && npm run build:amd" }, "jest": { "verbose": true, diff --git a/src/class-parser.ts b/src/class-parser.ts index b57e6b8..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] || ''; @@ -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!'); }