Skip to content

Commit

Permalink
Add build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jul 23, 2020
1 parent 8f59a39 commit 823b9d6
Show file tree
Hide file tree
Showing 23 changed files with 947 additions and 741 deletions.
17 changes: 13 additions & 4 deletions build.deno.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env node

const path = require('path');
const { mkdirSync, readdirSync, lstatSync, readFileSync, writeFileSync } = require('fs');
const path = require("path");
const {
mkdirSync,
readdirSync,
lstatSync,
readFileSync,
writeFileSync,
} = require("fs");

function processDir(src, dest) {
mkdirSync(dest, { recursive: true });

readdirSync(src)
.forEach(name => {
.forEach((name) => {
const currentPath = path.join(src, name);
const destPath = path.join(dest, name);
const currentStats = lstatSync(currentPath);
Expand All @@ -18,7 +24,10 @@ function processDir(src, dest) {

writeFileSync(
destPath,
readFileSync(currentPath).toString().replace(/from '(\.[.\\/-\w]+)'/gm, "from '$1.ts'"),
readFileSync(currentPath).toString().replace(
/from '(\.[.\\/-\w]+)'/gm,
"from '$1.ts'",
),
);
});
}
Expand Down
21 changes: 15 additions & 6 deletions build.mjs.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#!/usr/bin/env node

const path = require('path');
const { readdirSync, lstatSync, readFileSync, writeFileSync, unlinkSync } = require('fs');
const path = require("path");
const {
readdirSync,
lstatSync,
readFileSync,
writeFileSync,
unlinkSync,
} = require("fs");

function processDir(src) {
readdirSync(src)
.filter(name => !/.d.ts$/.test(name))
.forEach(name => {
.filter((name) => !/.d.ts$/.test(name))
.forEach((name) => {
const currentPath = path.join(src, name);
const currentStats = lstatSync(currentPath);
if (currentStats.isDirectory()) {
Expand All @@ -15,8 +21,11 @@ function processDir(src) {
}

writeFileSync(
currentPath.replace(/\.js$/, '.mjs'),
readFileSync(currentPath).toString().replace(/from '(\.[.\\/-\w]+)'/gm, "from '$1.mjs'"),
currentPath.replace(/\.js$/, ".mjs"),
readFileSync(currentPath).toString().replace(
/from '(\.[.\\/-\w]+)'/gm,
"from '$1.mjs'",
),
);
unlinkSync(currentPath);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"build:mjs": "tsc --module esnext --outDir ./dist/mjs && node build.mjs.js ./dist/mjs",
"build": "npm run build:deno && npm run build:umd && npm run build:cjs && npm run build:mjs",
"dev": "rollup -c -w",
"test": "tap test/*.ts"
"test": "TS_NODE_SKIP_PROJECT=true tap test/*.ts"
},
"license": "MIT",
"tags": [
Expand Down
83 changes: 44 additions & 39 deletions perfomance/index.deno.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
import { readFileStrSync } from "https://deno.land/std/fs/mod.ts";
import JSONParse2 from '../dist/deno/jsonparse.ts';
import JSONParse2 from "../dist/deno/jsonparse.ts";

function repeat(str: string, number: number): string {
return Array(number).fill(str).join('');
return Array(number).fill(str).join("");
}

console.log('====');
console.log('True');
console.log('====');
benchmark(repeat('true', 1000));
console.log("====");
console.log("True");
console.log("====");
benchmark(repeat("true", 1000));

console.log('=====');
console.log('False');
console.log('=====');
benchmark(repeat('false', 1000));
console.log("=====");
console.log("False");
console.log("=====");
benchmark(repeat("false", 1000));

console.log('======');
console.log('String');
console.log('======');
console.log("======");
console.log("String");
console.log("======");
benchmark(repeat('"This is a not-very-long text string."', 1000));

console.log('==============');
console.log('Complex object');
console.log('==============');
benchmark(readFileStrSync('../samplejson/basic.json'));

console.log('==============================');
console.log('Complex object with no numbers');
console.log('==============================');
benchmark(readFileStrSync('../samplejson/basic-no-numbers.json'));

console.log('=======================');
console.log('Object with many spaces');
console.log('=======================');
const spaces = Array(1000).fill(' ').join('');
benchmark(repeat(`${spaces}{${spaces}"test"${spaces}:${spaces}"asdfasdf"${spaces}}`, 1000));

console.log('===========');
console.log('Long string');
console.log('===========');
benchmark(`"${Array(100000).fill('a').join('')}"`);

console.log('===========');
console.log('Long number');
console.log('===========');
benchmark(`${Array(100000).fill('9').join('')}`);
console.log("==============");
console.log("Complex object");
console.log("==============");
benchmark(readFileStrSync("../samplejson/basic.json"));

console.log("==============================");
console.log("Complex object with no numbers");
console.log("==============================");
benchmark(readFileStrSync("../samplejson/basic-no-numbers.json"));

console.log("=======================");
console.log("Object with many spaces");
console.log("=======================");
const spaces = Array(1000).fill(" ").join("");
benchmark(
repeat(
`${spaces}{${spaces}"test"${spaces}:${spaces}"asdfasdf"${spaces}}`,
1000,
),
);

console.log("===========");
console.log("Long string");
console.log("===========");
benchmark(`"${Array(100000).fill("a").join("")}"`);

console.log("===========");
console.log("Long number");
console.log("===========");
benchmark(`${Array(100000).fill("9").join("")}`);

function benchmark(jsonStr: string): void {
const jsonparse2 = new JSONParse2();
Expand All @@ -53,4 +58,4 @@ function benchmark(jsonStr: string): void {
jsonparse2.write(jsonStr);
const end = performance.now();
console.log(`Time: ${end - start} ms.`);
}
}
87 changes: 46 additions & 41 deletions perfomance/index.node.mjs
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
import { performance } from 'perf_hooks';
import { readFileSync } from 'fs';
import JSONParse2 from '../dist/mjs/jsonparse.mjs';
import { performance } from "perf_hooks";
import { readFileSync } from "fs";
import JSONParse2 from "../dist/mjs/jsonparse.mjs";

function repeat(str, number) {
return Array(number).fill(str).join('');
return Array(number).fill(str).join("");
}

console.log('====');
console.log('True');
console.log('====');
benchmark(repeat('true', 1000));
console.log("====");
console.log("True");
console.log("====");
benchmark(repeat("true", 1000));

console.log('=====');
console.log('False');
console.log('=====');
benchmark(repeat('false', 1000));
console.log("=====");
console.log("False");
console.log("=====");
benchmark(repeat("false", 1000));

console.log('======');
console.log('String');
console.log('======');
console.log("======");
console.log("String");
console.log("======");
benchmark(repeat('"This is a not-very-long text string."', 1000));

console.log('==============');
console.log('Complex object');
console.log('==============');
benchmark(readFileSync('../samplejson/basic.json').toString());

console.log('==============================');
console.log('Complex object with no numbers');
console.log('==============================');
benchmark(readFileSync('../samplejson/basic-no-numbers.json').toString());

console.log('=======================');
console.log('Object with many spaces');
console.log('=======================');
const spaces = Array(1000).fill(' ').join('');
benchmark(repeat(`${spaces}{${spaces}"test"${spaces}:${spaces}"asdfasdf"${spaces}}`, 1000));

console.log('===========');
console.log('Long string');
console.log('===========');
benchmark(`"${Array(100000).fill('a').join('')}"`);

console.log('===========');
console.log('Long number');
console.log('===========');
benchmark(`${Array(100000).fill('9').join('')}`);
console.log("==============");
console.log("Complex object");
console.log("==============");
benchmark(readFileSync("../samplejson/basic.json").toString());

console.log("==============================");
console.log("Complex object with no numbers");
console.log("==============================");
benchmark(readFileSync("../samplejson/basic-no-numbers.json").toString());

console.log("=======================");
console.log("Object with many spaces");
console.log("=======================");
const spaces = Array(1000).fill(" ").join("");
benchmark(
repeat(
`${spaces}{${spaces}"test"${spaces}:${spaces}"asdfasdf"${spaces}}`,
1000,
),
);

console.log("===========");
console.log("Long string");
console.log("===========");
benchmark(`"${Array(100000).fill("a").join("")}"`);

console.log("===========");
console.log("Long number");
console.log("===========");
benchmark(`${Array(100000).fill("9").join("")}`);

function benchmark(jsonStr) {
const jsonparse2 = new JSONParse2();
Expand All @@ -54,4 +59,4 @@ function benchmark(jsonStr) {
jsonparse2.write(jsonStr);
const end = performance.now();
console.log(`Time: ${end - start} ms.`);
}
}
18 changes: 9 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';
import typescript from "rollup-plugin-typescript2";
import pkg from "./package.json";

export default [
{
input: 'src/index.ts',
input: "src/index.ts",
output: {
file: pkg.browser,
format: 'umd',
name: 'jsonparse'
format: "umd",
name: "jsonparse",
},
plugins: [
typescript({
tsconfigOverride: {
compilerOptions: {
target: "es5",
}
}
})
]
},
},
}),
],
},
];
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { default as Tokenizer } from './tokenizer';
export { default as Parser } from './parser';
export { default as JsonParser } from './jsonparse';
export * as utf8 from './utils/utf-8';
export { TokenType } from './utils/constants';
export { default as Tokenizer } from "./tokenizer";
export { default as Parser } from "./parser";
export { default as JsonParser } from "./jsonparse";
export * as utf8 from "./utils/utf-8";
export { TokenType } from "./utils/constants";
19 changes: 13 additions & 6 deletions src/jsonparse.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import Tokenizer, { TokenizerOptions } from './tokenizer';
import Parser, { StackElement } from './parser';
import Tokenizer, { TokenizerOptions } from "./tokenizer";
import Parser, { StackElement } from "./parser";

export default class JSONParser {
private tokenizer: Tokenizer;
private parser: Parser;

constructor (opts: TokenizerOptions = {}) {
constructor(opts: TokenizerOptions = {}) {
this.tokenizer = new Tokenizer(opts);
this.parser = new Parser();
this.tokenizer.onToken = this.parser.write.bind(this.parser);
}

public write(buffer: any) { // TODO tighten types
this.tokenizer.write(buffer);
public write(input: Iterable<number> | string) {
this.tokenizer.write(input);
}

public set onToken(cb: (token: number, value: any, offset: number) => void) {
this.tokenizer.onToken = cb;
}

public set onValue(cb: (value: any, key: string | number | undefined, parent: any, stack: StackElement[]) => void) {
public set onValue(
cb: (
value: any,
key: string | number | undefined,
parent: any,
stack: StackElement[],
) => void,
) {
this.parser.onValue = cb;
}
}
Loading

0 comments on commit 823b9d6

Please sign in to comment.