Skip to content

Commit

Permalink
Support buffer representation on node 6
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jul 27, 2016
1 parent 1490bc5 commit 77add28
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -7,5 +7,8 @@ notifications:

node_js:
- "0.10"
- "0.12"
- "4"
- "stable"

after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
38 changes: 30 additions & 8 deletions javascript-stringify.js
Expand Up @@ -85,7 +85,7 @@
*
* @return {String}
*/
var getGlobalVariable = function (value, indent, stringify) {
var toGlobalVariable = function (value, indent, stringify) {
return 'Function(' + stringify('return this;') + ')()';
};

Expand All @@ -99,7 +99,12 @@
// Map array values to their stringified values with correct indentation.
var values = array.map(function (value) {
var str = stringify(value);
return indent + (str && str.split('\n').join('\n' + indent));

if (value === undefined) {

This comment has been minimized.

Copy link
@zalmoxisus

zalmoxisus Aug 5, 2016

Contributor

It should be if (str === undefined) {

This comment has been minimized.

Copy link
@blakeembrey

blakeembrey Aug 6, 2016

Author Owner

Thanks, sorry. Would you like commit access to maintain this? I'm kind of burnt out on open source lately.

This comment has been minimized.

Copy link
@zalmoxisus

zalmoxisus Aug 6, 2016

Contributor

It would be great, though I don't think we need more changes in future. I also started to use this lib for redux-devtools-test-generator, so the data are now there in js format not json.

return String(str)
}

return indent + str.split('\n').join('\n' + indent);
}).join(indent ? ',\n' : ',');

// Wrap the array in newlines if we have indentation set.
Expand Down Expand Up @@ -140,22 +145,39 @@

return '{' + values + '}';
},
'[object Date]': function (date, indent, stringify) {
'[object Date]': function (date) {
return 'new Date(' + date.getTime() + ')';
},
'[object String]': function (string, indent, stringify) {
'[object String]': function (string) {
return 'new String(' + stringify(string.toString()) + ')';
},
'[object Number]': function (number, indent, stringify) {
'[object Number]': function (number) {
return 'new Number(' + number + ')';
},
'[object Boolean]': function (boolean, indent, stringify) {
'[object Boolean]': function (boolean) {
return 'new Boolean(' + boolean + ')';
},
'[object Uint8Array]': function (array, indent) {
if (typeof Buffer === 'function' && Buffer.isBuffer(array)) {
return 'new Buffer(' + stringify(array.toString()) + ')';
}

if (indent) {
var str = '';

for (var i = 0; i < array.length; i++) {
str += indent + array[i] + ',\n'
}

return 'new Uint8Array([\n' + str + '\n])'
}

return 'new Uint8Array([' + array.join(indent ? ',\n' : ',') + '])'
},
'[object RegExp]': String,
'[object Function]': String,
'[object global]': getGlobalVariable,
'[object Window]': getGlobalVariable
'[object global]': toGlobalVariable,
'[object Window]': toGlobalVariable
};

/**
Expand Down
6 changes: 4 additions & 2 deletions test.js
Expand Up @@ -3,9 +3,9 @@ var stringify = require('./');

describe('javascript-stringify', function () {
describe('types', function () {
var test = function (type, string) {
var test = function (input, result, indent) {
return function () {
expect(stringify(type)).to.equal(string);
expect(stringify(input, null, indent)).to.equal(result);
};
};

Expand Down Expand Up @@ -48,6 +48,8 @@ describe('javascript-stringify', function () {

describe('arrays', function () {
it('should stringify as array shorthand', test([1, 2, 3], '[1,2,3]'));

it('should indent elements', test([{ x: 10 }], '[\n\t{\n\t\tx: 10\n\t}\n]', '\t'))
});

describe('objects', function () {
Expand Down

0 comments on commit 77add28

Please sign in to comment.