Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig http://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
# Set default charset (override if necessary)
charset = utf-8
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
14
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm versions higher than 6 (which is what comes with node 14) change the version of package-lock.json from 1 to 2.

Version 2 is backwards compatible with package-lock.json version 1, but I didn't want to make that change in this PR.

41 changes: 20 additions & 21 deletions lib/prettyjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ var conflictChars = /[^\w\s\n\r\v\t\.,]/i;
exports.version = require('../package.json').version;

// Helper function to detect if an object should be printed or ignored
var isPrintable = function(input, options) {
var isPrintable = function (input, options) {
return input !== undefined || options.renderUndefined;
};

// Helper function to detect if an object can be directly serializable
var isSerializable = function(input, onlyPrimitives, options) {
var isSerializable = function (input, onlyPrimitives, options) {
if (
typeof input === 'boolean' ||
typeof input === 'number' ||
Expand All @@ -38,7 +38,7 @@ var isSerializable = function(input, onlyPrimitives, options) {
return false;
};

var addColorToData = function(input, options) {
var addColorToData = function (input, options) {
if (options.noColor) {
return input;
}
Expand Down Expand Up @@ -77,26 +77,24 @@ var addColorToData = function(input, options) {
return sInput;
};

var colorMultilineString = function(options, line) {
if (options.multilineStringColor === null || options.noColor) {
return line;
} else {
return colors[options.multilineStringColor](line);
}
var colorMultilineString = function (options, line) {
if (options.multilineStringColor === null || options.noColor) {
return line;
} else {
return colors[options.multilineStringColor](line);
}
};

var indentLines = function(string, spaces, options){
var indentLines = function (string, spaces, options) {
var lines = string.split('\n');
lines = lines.map(function(line){
lines = lines.map(function (line) {
return Utils.indent(spaces) + colorMultilineString(options, line);
});
return lines.join('\n');
};

var renderToArray = function(data, options, indentation) {

if (typeof data === 'string' && data.match(conflictChars) &&
options.escape) {
var renderToArray = function (data, options, indentation) {
if (typeof data === 'string' && data.match(conflictChars) && options.escape) {
data = JSON.stringify(data);
}

Expand Down Expand Up @@ -125,7 +123,7 @@ var renderToArray = function(data, options, indentation) {

var outputArray = [];

data.forEach(function(element) {
data.forEach(function (element) {
if (!isPrintable(element, options)) {
return;
}
Expand All @@ -143,13 +141,15 @@ var renderToArray = function(data, options, indentation) {
line += renderToArray(element, options, 0)[0];
outputArray.push(line);

// If the element is an array or object, render it in next line
// If the element is an array or object, render it in next line
} else {
outputArray.push(line);
outputArray.push.apply(
outputArray,
renderToArray(
element, options, indentation + options.defaultIndentation
element,
options,
indentation + options.defaultIndentation
)
);
}
Expand All @@ -175,13 +175,13 @@ var renderToArray = function(data, options, indentation) {
var key;
var output = [];

Object.getOwnPropertyNames(data).forEach(function(i) {
Object.getOwnPropertyNames(data).forEach(function (i) {
if (!isPrintable(data[i], options)) {
return;
}

// Prepend the index at the beginning of the line
key = (i + ': ');
key = i + ': ';
if (!options.noColor) {
key = colors[options.keysColor](key);
}
Expand Down Expand Up @@ -266,7 +266,6 @@ exports.render = function render(data, options, indentation) {
// defaultIndentation: 2 // Indentation on nested objects
// }
exports.renderString = function renderString(data, options, indentation) {

var output = '';
var parsedData;
// If the input is not a string or if it's empty, just return an empty string
Expand Down
16 changes: 16 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
overrides: [
{
// prettier will strip newlines out of package.json files unless you tell it to use the json parser
files: ['package.json', '**/package.json'],
options: { parser: 'json' }
}
],
printWidth: 80,
semi: true,
singleQuote: true,
trailingComma: 'none',
tabWidth: 2,
useTabs: false,
xmlWhitespaceSensitivity: 'ignore'
};