Skip to content

Commit

Permalink
Merge eeaf17d into 4055ec0
Browse files Browse the repository at this point in the history
  • Loading branch information
exdis committed Jul 25, 2019
2 parents 4055ec0 + eeaf17d commit acc1df9
Show file tree
Hide file tree
Showing 7 changed files with 508 additions and 20 deletions.
Empty file modified bin/jora
100644 → 100755
Empty file.
5 changes: 4 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 Down Expand Up @@ -88,7 +89,8 @@ function processStream(options) {
if (options.outputFile) {
fs.writeFileSync(options.outputFile, serializedResult, 'utf-8');
} else {
console.log(serializedResult);
const result = options.noColor ? serializeResult : colorize(serializedResult);
console.log(result);
}
});
}
Expand All @@ -101,6 +103,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
63 changes: 44 additions & 19 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"dependencies": {
"chalk": "^2.4.2",
"clap": "^1.0.9",
"jora": "1.0.0-alpha.10"
},
Expand Down
47 changes: 47 additions & 0 deletions utils/colorize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const {
STRING,
STRING_KEY,
WHITESPACE,
COLON,
RIGHT_BRACE,
RIGHT_BRACKET
} = require('./constants').TOKENS;
const { TOKEN_COLORS } = require('./constants');
const tokenize = require('./tokenize');

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

const tokens = tokenize(input);

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

if (TOKEN_COLORS[token.type]) {
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;
}
}
}
result += TOKEN_COLORS[token.type](token.value);
} else {
result += token.value;
}
}


return result;
};
87 changes: 87 additions & 0 deletions utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const chalk = require('chalk');

const TOKENS = {
LEFT_BRACE: 0, // {
RIGHT_BRACE: 1, // }
LEFT_BRACKET: 2, // [
RIGHT_BRACKET: 3, // ]
COLON: 4, // :
COMMA: 5, // ,
STRING: 6, //
STRING_KEY: 7, //
NUMBER: 8, //
TRUE: 9, // true
FALSE: 10, // false
NULL: 11, // null
WHITESPACE: 12 //
};

const TOKEN_COLORS = {
[TOKENS.LEFT_BRACE]: chalk.bold.white,
[TOKENS.RIGHT_BRACE]: chalk.bold.white,
[TOKENS.LEFT_BRACKET]: chalk.bold.white,
[TOKENS.RIGHT_BRACKET]: chalk.bold.white,
[TOKENS.COLON]: chalk.bold.white,
[TOKENS.COMMA]: chalk.bold.white,
[TOKENS.STRING]: chalk.green,
[TOKENS.STRING_KEY]: chalk.green.yellow,
[TOKENS.NUMBER]: chalk.blue,
[TOKENS.TRUE]: chalk.cyan,
[TOKENS.FALSE]: chalk.cyan,
[TOKENS.NULL]: chalk.red
};

const PUNCTUATOR_TOKENS_MAP = {
'{': TOKENS.LEFT_BRACE,
'}': TOKENS.RIGHT_BRACE,
'[': TOKENS.LEFT_BRACKET,
']': TOKENS.RIGHT_BRACKET,
':': TOKENS.COLON,
',': TOKENS.COMMA
};

const KEYWORD_TOKENS_MAP = {
'true': TOKENS.TRUE,
'false': TOKENS.FALSE,
'null': TOKENS.NULL
};

const STRING_STATES = {
_START_: 0,
START_QUOTE_OR_CHAR: 1,
ESCAPE: 2
};

const ESCAPES = {
'"': 0, // Quotation mask
'\\': 1, // Reverse solidus
'/': 2, // Solidus
'b': 3, // Backspace
'f': 4, // Form feed
'n': 5, // New line
'r': 6, // Carriage return
't': 7, // Horizontal tab
'u': 8 // 4 hexadecimal digits
};

const NUMBER_STATES = {
_START_: 0,
MINUS: 1,
ZERO: 2,
DIGIT: 3,
POINT: 4,
DIGIT_FRACTION: 5,
EXP: 6,
EXP_DIGIT_OR_SIGN: 7
};


module.exports = {
TOKENS,
TOKEN_COLORS,
PUNCTUATOR_TOKENS_MAP,
KEYWORD_TOKENS_MAP,
STRING_STATES,
ESCAPES,
NUMBER_STATES
};

0 comments on commit acc1df9

Please sign in to comment.