Skip to content

Commit edcea74

Browse files
committed
[Robustness] use string.prototype.trim, array.prototype.foreach, array.prototype.push
1 parent 0d4aa82 commit edcea74

2 files changed

Lines changed: 25 additions & 19 deletions

File tree

index.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ var through2 = require('through2');
44
var duplexer = require('duplexer');
55
var parser = require('tap-parser');
66
var sprintf = require('sprintf-js').sprintf;
7+
var forEach = require('array.prototype.foreach');
8+
var push = require('array.prototype.push');
9+
var trim = require('string.prototype.trim');
710

811
module.exports = function (opts) {
912
var tap = parser();
1013
var out = through2();
1114

12-
function trim(s) {
15+
function trimWidth(s) {
1316
if (opts && opts.width && s.length > opts.width - 2) {
1417
return s.slice(0, opts.width - 5) + '...';
1518
}
1619
return s;
1720
}
1821

1922
function updateName(y, str, c) {
20-
return '\x1b[' + y + 'A\x1b[1G\x1b[1m\x1b[' + c + 'm' + trim(str) + '\x1b[0m\x1b[' + y + 'B\x1b[1G';
23+
return '\x1b[' + y + 'A\x1b[1G\x1b[1m\x1b[' + c + 'm' + trimWidth(str) + '\x1b[0m\x1b[' + y + 'B\x1b[1G';
2124
}
2225

2326
var test;
@@ -31,10 +34,10 @@ module.exports = function (opts) {
3134
&& test.assertions.length === 0
3235
&& (/^(tests|pass)\s+\d+$/).test(test.name)
3336
) {
34-
out.push('\r' + trim(test.name));
37+
push(out, '\r' + trimWidth(test.name));
3538
} else if (test && test.ok) {
3639
var s = updateName(test.offset + 1, '✓ ' + test.name, 32);
37-
out.push('\r' + s);
40+
push(out, '\r' + s);
3841
}
3942

4043
test = {
@@ -43,18 +46,18 @@ module.exports = function (opts) {
4346
offset: 0,
4447
ok: true
4548
};
46-
out.push('\r' + trim('# ' + comment) + '\x1b[K\n');
49+
push(out, '\r' + trimWidth('# ' + comment) + '\x1b[K\n');
4750
});
4851

4952
tap.on('assert', function (res) {
5053
var ok = res.ok ? 'ok' : 'not ok';
5154
var c = res.ok ? 32 : 31;
5255
if (!test) {
5356
// mocha produces TAP results this way, whatever
54-
var s = trim(res.name.trim());
55-
out.push(sprintf(
57+
var s = trimWidth(trim(res.name));
58+
push(out, sprintf(
5659
'\x1b[1m\x1b[' + c + 'm%s\x1b[0m\n',
57-
trim((res.ok ? '✓' : '⨯') + ' ' + s)
60+
trimWidth((res.ok ? '✓' : '⨯') + ' ' + s)
5861
));
5962
return;
6063
}
@@ -70,15 +73,15 @@ module.exports = function (opts) {
7073
}
7174
test.ok = false;
7275
}
73-
out.push(str);
74-
test.assertions.push(res);
76+
push(out, str);
77+
push(test.assertions, res);
7578
});
7679

7780
tap.on('extra', function (extra) {
7881
if (!test || test.assertions.length === 0) { return; }
7982
var last = test.assertions[test.assertions.length - 1];
8083
if (!last.ok) {
81-
out.push(extra.split('\n').map(function (line) {
84+
push(out, extra.split('\n').map(function (line) {
8285
return ' ' + line;
8386
}).join('\n') + '\n');
8487
}
@@ -88,27 +91,27 @@ module.exports = function (opts) {
8891

8992
tap.on('results', function (res) {
9093
if (test && (/^fail\s+\d+$/).test(test.name)) {
91-
out.push(updateName(test.offset + 1, '⨯ ' + test.name, 31));
94+
push(out, updateName(test.offset + 1, '⨯ ' + test.name, 31));
9295
} else if (test && test.ok) {
93-
out.push(updateName(test.offset + 1, '✓ ' + test.name, 32));
96+
push(out, updateName(test.offset + 1, '✓ ' + test.name, 32));
9497
}
9598

96-
res.errors.forEach(function (err, ix) {
97-
out.push(sprintf(
99+
forEach(res.errors, function (err, ix) {
100+
push(out, sprintf(
98101
'not ok \x1b[1m\x1b[31m%d\x1b[0m %s\n',
99102
ix + 1 + res.asserts.length,
100103
err.message
101104
));
102105
});
103106

104107
if (!res.ok && !(/^fail\s+\d+$/).test(test && test.name)) {
105-
out.push(sprintf(
108+
push(out, sprintf(
106109
'\r\x1b[1m\x1b[31m⨯ fail %s\x1b[0m\x1b[K\n',
107110
(res.errors.length + res.fail.length) || ''
108111
));
109112
}
110113

111-
out.push(null);
114+
push(out, null);
112115

113116
dup.emit('results', res);
114117
if (!res.ok) { dup.emit('fail'); }

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
"faucet": "bin/cmd.js"
88
},
99
"scripts": {
10-
"lint": "eslint --ext=js,mjs .",
11-
"pretest": "npm run lint"
10+
"lint": "eslint --ext=js,mjs .",
11+
"pretest": "npm run lint"
1212
},
1313
"dependencies": {
14+
"array.prototype.foreach": "^1.0.2",
15+
"array.prototype.push": "^1.0.2",
1416
"defined": "^0.0.0",
1517
"duplexer": "^0.1.2",
1618
"minimist": "^1.2.6",
1719
"npm-which": "^1.0.2",
1820
"sprintf-js": "^1.1.2",
21+
"string.prototype.trim": "^1.2.6",
1922
"tap-parser": "^0.4.3",
2023
"tape": "^5.6.1",
2124
"through2": "^0.2.3"

0 commit comments

Comments
 (0)