Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display long arrays in a multiline representation. #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The options available are :
- `-l`: to enable or disable levels (default value: false)
- `-s`: to specify the number of tabulation spaces (default value: 2)
- `-r`: to specify valid JSON as output (default value: true)
- `-m`: to specify the max inline representetion length of an array (default value: 80)

examples :

Expand Down Expand Up @@ -165,6 +166,12 @@ If you need JSON which pases linting:
jsome.params.lintable = true;
```

If you need to change the max length of an inline representetion of an array

```javascript
jsome.params.maxInlineLength = 30;
```

When you have a very long json to display, don't make your code blocking... you can enable the asynchronous mode.

```javascript
Expand All @@ -182,6 +189,7 @@ The default value of `params` is:
'colored' : true
, 'async' : false
, 'lintable': false
, 'maxInlineLength': 80
}
```

Expand Down
6 changes: 6 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ var argv = require('yargs')
, default : false
, describe : 'output valid json'
, type : 'boolean'
}).option('m', {
alias : 'maxInlineLength'
, default : 80
, describe : 'the max inline representetion length of an array'
, type : 'number'
})
.example('jsome -cl /some/dir/file.json', 'print out the content of file.json in color displaying indentation levels')
.example('jsome -c false -l /some/dir/file.json', 'print out the content of file.json without color but with indentation levels')
Expand All @@ -34,6 +39,7 @@ var argv = require('yargs')

jsome.params.colored = argv.c;
jsome.params.lintable = argv.r;
jsome.params.maxInlineLength = argv.m;
jsome.level.show = argv.l;
jsome.level.spaces = argv.s;

Expand Down
13 changes: 11 additions & 2 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,16 @@ module.exports = (function () {
colored.push(result.join(colorifySpec(', ', 'punc') + '\n' ));
colored.push(colorifySpec(']', 'brack', level));

} else {
} else if (JSON.stringify(json).length > jsomeRef.params.maxInlineLength) { // long array

colored.push(colorifySpec('[', 'brack', isChild ? 0 : level));
level++
for (var key in json) {
colored.push(this.gen(json[key], level) + (json.length-1>key ? colorifySpec(',', 'punc') : ''));
}
colored.push(colorifySpec(']', 'brack', --level));

} else { // small array

var coloredArray = colorifySpec('[', 'brack', isChild ? 0 : level);
for (var key in json) {
Expand All @@ -163,4 +172,4 @@ module.exports = (function () {
}
}

})();
})();
1 change: 1 addition & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var colors = {
'colored' : true
, 'async' : false
, 'lintable': false
, 'maxInlineLength': 80
}


Expand Down
28 changes: 27 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ var assert = require('assert'),

describe('Jsome run with', function () {

beforeEach(function () {
jsome.params.colored = true
jsome.params.maxInlineLength = 80
});

describe('spec non-compliant coloured output', function () {
var expected = y('{') +
'\n ' + g('string') + y(': ') + y('"') + m('value') + y('"') +
Expand Down Expand Up @@ -56,4 +61,25 @@ describe('Jsome run with', function () {
})
})

})
describe('decide the better array presentation', function () {

test('should not line broke a small inline array', function () {
jsome.params.colored = false
assert.equal(
jsome.getColoredString([1234567890, 'ABCDEFGHIJ']),
'[1234567890, "ABCDEFGHIJ"]'
)
})

test('should broke long inline in a multiline representation', function () {
jsome.params.colored = false
jsome.params.maxInlineLength = 20
assert.equal(
jsome.getColoredString([1234567890, 'ABCDEFGHIJ']),
'[\n 1234567890,\n "ABCDEFGHIJ"\n]'
)
})

})

})