-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (45 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
var through = require('through2')
, pumpify = require('pumpify')
, split = require('split2');
//
// Expose the transform.
//
module.exports = condenseify;
/**
* Browserify transform to condense multiple blank lines
* into a single blank line.
*
* @param {String} file File name
* @param {Object} [options] Options object
* @returns {Stream} Transform stream
* @api public
*/
function condenseify(file, options) {
if (/\.json$/.test(file)) return through();
var appendNewLine = true
, regex = /^[ \t]+$/
, isBlank = false
, eol ='\n';
options = options || {};
function transform(line, encoding, next) {
if (!line.length) {
isBlank = true;
return next();
}
line = line.toString();
if (!options['keep-non-empty'] && regex.test(line)) return next();
if (isBlank) {
isBlank = false;
this.push(eol);
}
appendNewLine = false;
this.push(line + eol);
next();
}
function flush(done) {
if (appendNewLine) this.push(eol);
done();
}
return pumpify(split(), through(transform, flush));
}