Skip to content

Commit

Permalink
Merge 44b3d21 into 0e6996d
Browse files Browse the repository at this point in the history
  • Loading branch information
exdis committed Jul 25, 2019
2 parents 0e6996d + 44b3d21 commit 8a8e7e4
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exclude": ["utils"]
}
Empty file modified bin/jora
100644 → 100755
Empty file.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const cli = require('clap');
const jora = require('jora/dist/jora');
const colorize = require('./utils/colorize');

function readFromStream(stream, processBuffer) {
const buffer = [];
Expand All @@ -15,6 +16,7 @@ function readFromStream(stream, processBuffer) {
function processOptions(options, args) {
const query = options.query || args[0];
const pretty = options.pretty || false;
const color = options.color;
let inputFile = options.input;
let outputFile = options.output;

Expand All @@ -35,6 +37,7 @@ function processOptions(options, args) {
return {
query,
pretty,
color,
inputFile,
outputFile
};
Expand Down Expand Up @@ -88,7 +91,8 @@ function processStream(options) {
if (options.outputFile) {
fs.writeFileSync(options.outputFile, serializedResult, 'utf-8');
} else {
console.log(serializedResult);
const result = options.color ? colorize(serializedResult) : serializedResult;
console.log(result);
}
});
}
Expand All @@ -101,6 +105,7 @@ var command = cli.create('jora', '[query]')
.option('-p, --pretty [indent]', 'Pretty print with optionally specified indentation (4 spaces by default)', value =>
value === undefined ? 4 : Number(value) || false
, false)
.option('--no-color', 'Suppress color output')
.action(function(args) {
var options = processOptions(this.values, args);

Expand Down
84 changes: 62 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"dependencies": {
"chalk": "^2.4.2",
"clap": "^1.0.9",
"jora": "1.0.0-alpha.10"
},
"devDependencies": {
"ansi-regex": "^4.1.0",
"coveralls": "^3.0.4",
"eslint": "^6.0.1",
"mocha": "^5.2.0",
Expand Down
15 changes: 15 additions & 0 deletions test/fixture.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"string": "st\"ri\u1234ng",
"number": 1234,
"emptyArray": [],
"emptyObject": {},
"null": null,
"false": false,
"true": true,
"complexJSON": {
"f\"oo\u01234"
:-0.1234, "bar": 0e-3,
"ba/": -0.1234e12,
"quux": -1.123
}
}
23 changes: 23 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const {
LEFT_BRACE,
RIGHT_BRACE,
LEFT_BRACKET,
RIGHT_BRACKET,
STRING,
NUMBER,
NULL,
FALSE,
TRUE
} = require('../utils/constants').TOKENS;
const { TOKEN_COLORS } = require('../utils/constants');
const ansiRegex = require('ansi-regex');

module.exports = {
STRING: TOKEN_COLORS[STRING](' ').match(ansiRegex()),
NUMBER: TOKEN_COLORS[NUMBER](' ').match(ansiRegex()),
EMPTY_ARRAY: [...TOKEN_COLORS[LEFT_BRACKET](' ').match(ansiRegex()), ...TOKEN_COLORS[RIGHT_BRACKET](' ').match(ansiRegex())],
EMPTY_OBJECT: [...TOKEN_COLORS[LEFT_BRACE](' ').match(ansiRegex()), ...TOKEN_COLORS[RIGHT_BRACE](' ').match(ansiRegex())],
NULL: TOKEN_COLORS[NULL](' ').match(ansiRegex()),
FALSE: TOKEN_COLORS[FALSE](' ').match(ansiRegex()),
TRUE: TOKEN_COLORS[TRUE](' ').match(ansiRegex())
};
62 changes: 61 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,28 @@ const child = require('child_process');
const cmd = 'node';
const pkgJson = path.join(__dirname, '../package.json');
const pkgJsonData = require(pkgJson);
const fs = require('fs');
const colorize = require('../utils/colorize');
const fixture = require('../test/fixture.json');
const ansiRegex = require('ansi-regex');
const {
STRING,
NUMBER,
EMPTY_ARRAY,
EMPTY_OBJECT,
NULL,
FALSE,
TRUE
} = require('./helpers');

function match(rx) {
return actual => rx.test(actual);
}

function matchANSI(fixture) {
return data => assert.deepEqual(data.match(ansiRegex()), fixture);
}

function run() {
var args = [path.join(__dirname, '../bin/jora')].concat(Array.prototype.slice.call(arguments));
var proc = child.spawn(cmd, args, { stdio: 'pipe' });
Expand Down Expand Up @@ -74,7 +91,7 @@ it('should output version', () =>
);

it('should read content from stdin if no file specified', () =>
run('version')
run('version', '--no-color')
.input(JSON.stringify(pkgJsonData))
.output(JSON.stringify(pkgJsonData.version))
);
Expand Down Expand Up @@ -120,3 +137,46 @@ describe('errors', function() {
)
);
});

describe.skip('colorizer', function() {
it('Should colorize string', () =>
run('string')
.input(JSON.stringify(fixture))
.output(matchANSI(STRING))
);
it('Should colorize number', () =>
run('number')
.input(JSON.stringify(fixture))
.output(matchANSI(NUMBER))
);
it('Should colorize empty array', () =>
run('emptyArray')
.input(JSON.stringify(fixture))
.output(matchANSI(EMPTY_ARRAY))
);
it('Should colorize empty object', () =>
run('emptyArray')
.input(JSON.stringify(fixture))
.output(matchANSI(EMPTY_OBJECT))
);
it('Should colorize empty object', () =>
run('null')
.input(JSON.stringify(fixture))
.output(matchANSI(NULL))
);
it('Should colorize empty object', () =>
run('false')
.input(JSON.stringify(fixture))
.output(matchANSI(FALSE))
);
it('Should colorize empty object', () =>
run('true')
.input(JSON.stringify(fixture))
.output(matchANSI(TRUE))
);
it('Should colorize raw complex JSON', () =>
colorize(
fs.readFileSync(path.resolve(__dirname, './fixture.json')).toString()
).match(ansiRegex())
);
});
56 changes: 56 additions & 0 deletions utils/colorize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const {
STRING,
STRING_KEY,
WHITESPACE,
COLON,
RIGHT_BRACE,
RIGHT_BRACKET
} = require('./constants').TOKENS;
const { TOKEN_COLORS } = require('./constants');
const tokenize = require('./tokenize');

const markKeys = (tokens) => {
for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];

if (token.type === STRING) {
for (let j = i + 1; j < tokens.length; j++) {
if (tokens[j].type === WHITESPACE) {
continue;
}
if (tokens[j].type === COLON) {
token.type = STRING_KEY;
break;
}
if (
tokens[j].type === STRING ||
tokens[j].type === RIGHT_BRACE ||
tokens[j].type === RIGHT_BRACKET
) {
break;
}
}
}
}

return tokens;
};

module.exports = (input) => {
let result = '';

const tokens = tokenize(input);
const markedTokens = markKeys(tokens);

for (let i = 0; i < markedTokens.length; i++) {
let token = markedTokens[i];

if (TOKEN_COLORS[token.type]) {
result += TOKEN_COLORS[token.type](token.value);
} else {
result += token.value;
}
}

return result;
};
Loading

0 comments on commit 8a8e7e4

Please sign in to comment.