Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ completely ignores

# prerequisites

NodeJS
NodeJS or Deno

### in NodeJS

# similar projects in other languages

Expand Down
5 changes: 5 additions & 0 deletions cli/deno.ts
Original file line number Diff line number Diff line change
@@ -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))))
6 changes: 6 additions & 0 deletions cli/node.js
Original file line number Diff line number Diff line change
@@ -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)))
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
],
"author": "Moritz Schmidt <fusselwurm@gmail.com>",
"files": [
"cli/*.*",
"dist/*.js",
"dist/*.d.ts",
"dist/*.map.js"
"dist/*.map.js",
"src/class-parser.ts"
],
"main": "dist/class-parser",
"dependencies": {},
Expand All @@ -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,
Expand Down
18 changes: 8 additions & 10 deletions src/class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] || '';
Expand All @@ -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[] {
Expand All @@ -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;

Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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);
Expand All @@ -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') ||
Expand Down Expand Up @@ -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!');
}
Expand Down