Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 2, 2018
1 parent 95d3a0e commit 01408d1
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 201 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
116 changes: 58 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
'use strict';
'use strict'

var events = require('events');
var once = require('once');
var events = require('events')
var once = require('once')

module.exports = stream;
module.exports = stream

function stream(processor) {
var chunks = [];
var emitter = new events.EventEmitter();
var ended = false;
var chunks = []
var emitter = new events.EventEmitter()
var ended = false

emitter.processor = processor;
emitter.readable = true;
emitter.writable = true;
emitter.write = write;
emitter.end = end;
emitter.pipe = pipe;
emitter.processor = processor
emitter.readable = true
emitter.writable = true
emitter.write = write
emitter.end = end
emitter.pipe = pipe

return emitter;
return emitter

/* Write a chunk into memory. */
function write(chunk, encoding, callback) {
if (typeof encoding === 'function') {
callback = encoding;
encoding = null;
callback = encoding
encoding = null
}

if (ended) {
throw new Error('Did not expect `write` after `end`');
throw new Error('Did not expect `write` after `end`')
}

chunks.push((chunk || '').toString(encoding || 'utf8'));
chunks.push((chunk || '').toString(encoding || 'utf8'))

if (callback) {
callback();
callback()
}

/* Signal succesful write. */
return true;
return true
}

/* End the writing. Passes all arguments to a final
Expand All @@ -47,37 +47,37 @@ function stream(processor) {
* succesful. If messages are triggered during the
* process, those are triggerd as `warning`s. */
function end() {
write.apply(null, arguments);
write.apply(null, arguments)

ended = true;
ended = true

processor.process(chunks.join(''), done);
processor.process(chunks.join(''), done)

return true;
return true

function done(err, file) {
var messages = file ? file.messages : [];
var length = messages.length;
var index = -1;
var messages = file ? file.messages : []
var length = messages.length
var index = -1

chunks = null;
chunks = null

/* Trigger messages as warnings, except for fatal error. */
while (++index < length) {
/* istanbul ignore else - shouldn’t happen. */
if (messages[index] !== err) {
emitter.emit('warning', messages[index]);
emitter.emit('warning', messages[index])
}
}

if (err) {
/* Don’t enter an infinite error throwing loop. */
global.setTimeout(function () {
emitter.emit('error', err);
}, 4);
global.setTimeout(function() {
emitter.emit('error', err)
}, 4)
} else {
emitter.emit('data', file.contents);
emitter.emit('end');
emitter.emit('data', file.contents)
emitter.emit('end')
}
}
}
Expand All @@ -89,63 +89,63 @@ function stream(processor) {
*
* See https://github.com/nodejs/node/blob/master/lib/stream.js#L26. */
function pipe(dest, options) {
var settings = options || {};
var onend = once(onended);
var settings = options || {}
var onend = once(onended)

emitter.on('data', ondata);
emitter.on('error', onerror);
emitter.on('end', cleanup);
emitter.on('close', cleanup);
emitter.on('data', ondata)
emitter.on('error', onerror)
emitter.on('end', cleanup)
emitter.on('close', cleanup)

/* If the 'end' option is not supplied, dest.end() will be
* called when the 'end' or 'close' events are received.
* Only dest.end() once. */
if (!dest._isStdio && settings.end !== false) {
emitter.on('end', onend);
emitter.on('end', onend)
}

dest.on('error', onerror);
dest.on('close', cleanup);
dest.on('error', onerror)
dest.on('close', cleanup)

dest.emit('pipe', emitter);
dest.emit('pipe', emitter)

return dest;
return dest

/* End destination. */
function onended() {
if (dest.end) {
dest.end();
dest.end()
}
}

/* Handle data. */
function ondata(chunk) {
if (dest.writable) {
dest.write(chunk);
dest.write(chunk)
}
}

/* Clean listeners. */
function cleanup() {
emitter.removeListener('data', ondata);
emitter.removeListener('end', onend);
emitter.removeListener('error', onerror);
emitter.removeListener('end', cleanup);
emitter.removeListener('close', cleanup);

dest.removeListener('error', onerror);
dest.removeListener('close', cleanup);
emitter.removeListener('data', ondata)
emitter.removeListener('end', onend)
emitter.removeListener('error', onerror)
emitter.removeListener('end', cleanup)
emitter.removeListener('close', cleanup)

dest.removeListener('error', onerror)
dest.removeListener('close', cleanup)
}

/* Close dangling pipes and handle unheard errors. */
function onerror(err) {
var handlers = emitter._events.error;
var handlers = emitter._events.error

cleanup();
cleanup()

/* Cannot use `listenerCount` in node <= 0.12. */
if (!handlers || handlers.length === 0 || handlers === onerror) {
throw err; /* Unhandled stream error in pipe. */
throw err /* Unhandled stream error in pipe. */
}
}
}
Expand Down
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,41 @@
"devDependencies": {
"is-function": "^1.0.1",
"nyc": "^11.0.0",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.6.3",
"unified": "^6.0.0",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . -qfo",
"build": "npm run build-md",
"lint": "xo",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"guard-for-in": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
"prefer-arrow-callback": "off",
"unicorn/number-literal-case": "off"
}
},
"remarkConfig": {
Expand Down
10 changes: 4 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ npm install unified-stream
The below example pipes stdin, into an HTML formatter, to stdout.

```js
var stream = require('unified-stream');
var rehype = require('rehype');
var format = require('rehype-format');
var stream = require('unified-stream')
var rehype = require('rehype')
var format = require('rehype-format')

process.stdin
.pipe(stream(rehype().use(format)))
.pipe(process.stdout);
process.stdin.pipe(stream(rehype().use(format))).pipe(process.stdout)
```

## API
Expand Down
Loading

0 comments on commit 01408d1

Please sign in to comment.