Skip to content

Commit 77add28

Browse files
committed
Support buffer representation on node 6
1 parent 1490bc5 commit 77add28

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ notifications:
77

88
node_js:
99
- "0.10"
10+
- "0.12"
11+
- "4"
12+
- "stable"
1013

1114
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

javascript-stringify.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
*
8686
* @return {String}
8787
*/
88-
var getGlobalVariable = function (value, indent, stringify) {
88+
var toGlobalVariable = function (value, indent, stringify) {
8989
return 'Function(' + stringify('return this;') + ')()';
9090
};
9191

@@ -99,7 +99,12 @@
9999
// Map array values to their stringified values with correct indentation.
100100
var values = array.map(function (value) {
101101
var str = stringify(value);
102-
return indent + (str && str.split('\n').join('\n' + indent));
102+
103+
if (value === undefined) {
104+
return String(str)
105+
}
106+
107+
return indent + str.split('\n').join('\n' + indent);
103108
}).join(indent ? ',\n' : ',');
104109

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

141146
return '{' + values + '}';
142147
},
143-
'[object Date]': function (date, indent, stringify) {
148+
'[object Date]': function (date) {
144149
return 'new Date(' + date.getTime() + ')';
145150
},
146-
'[object String]': function (string, indent, stringify) {
151+
'[object String]': function (string) {
147152
return 'new String(' + stringify(string.toString()) + ')';
148153
},
149-
'[object Number]': function (number, indent, stringify) {
154+
'[object Number]': function (number) {
150155
return 'new Number(' + number + ')';
151156
},
152-
'[object Boolean]': function (boolean, indent, stringify) {
157+
'[object Boolean]': function (boolean) {
153158
return 'new Boolean(' + boolean + ')';
154159
},
160+
'[object Uint8Array]': function (array, indent) {
161+
if (typeof Buffer === 'function' && Buffer.isBuffer(array)) {
162+
return 'new Buffer(' + stringify(array.toString()) + ')';
163+
}
164+
165+
if (indent) {
166+
var str = '';
167+
168+
for (var i = 0; i < array.length; i++) {
169+
str += indent + array[i] + ',\n'
170+
}
171+
172+
return 'new Uint8Array([\n' + str + '\n])'
173+
}
174+
175+
return 'new Uint8Array([' + array.join(indent ? ',\n' : ',') + '])'
176+
},
155177
'[object RegExp]': String,
156178
'[object Function]': String,
157-
'[object global]': getGlobalVariable,
158-
'[object Window]': getGlobalVariable
179+
'[object global]': toGlobalVariable,
180+
'[object Window]': toGlobalVariable
159181
};
160182

161183
/**

test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ var stringify = require('./');
33

44
describe('javascript-stringify', function () {
55
describe('types', function () {
6-
var test = function (type, string) {
6+
var test = function (input, result, indent) {
77
return function () {
8-
expect(stringify(type)).to.equal(string);
8+
expect(stringify(input, null, indent)).to.equal(result);
99
};
1010
};
1111

@@ -48,6 +48,8 @@ describe('javascript-stringify', function () {
4848

4949
describe('arrays', function () {
5050
it('should stringify as array shorthand', test([1, 2, 3], '[1,2,3]'));
51+
52+
it('should indent elements', test([{ x: 10 }], '[\n\t{\n\t\tx: 10\n\t}\n]', '\t'))
5153
});
5254

5355
describe('objects', function () {

0 commit comments

Comments
 (0)