Initial commit
commit
104ca824cb
@ -0,0 +1 @@
|
||||
node_modules
|
@ -0,0 +1,20 @@
|
||||
"use strict";
|
||||
|
||||
const Promise = require("bluebird");
|
||||
const pipe = require("@promistream/pipe");
|
||||
const fromIterable = require("@promistream/from-iterable");
|
||||
const splitLines = require("./");
|
||||
const collect = require("@promistream/collect");
|
||||
|
||||
Promise.try(() => {
|
||||
return pipe([
|
||||
fromIterable([
|
||||
"line 1\nline 2\nline 3\nli",
|
||||
"ne 4\nline 5"
|
||||
]),
|
||||
splitLines(),
|
||||
collect()
|
||||
]).read();
|
||||
}).then((result) => {
|
||||
console.log(result); // [ "line 1", "line 2", "line 3", "line 4", "line 5" ]
|
||||
});
|
@ -0,0 +1,54 @@
|
||||
"use strict";
|
||||
|
||||
// FIXME: Clearly document that this only splits lines in *strings*, not eg. Buffers (and make a separate split-lines-buffer package for that, as it requires a very different implementation)
|
||||
|
||||
const lastItem = require("last-item");
|
||||
|
||||
const pipe = require("@promistream/pipe");
|
||||
const map = require("@promistream/map");
|
||||
const buffer = require("@promistream/buffer");
|
||||
const lastWill = require("@promistream/last-will");
|
||||
|
||||
const { validateOptions, validateValue } = require("@validatem/core");
|
||||
const defaultTo = require("@validatem/default-to");
|
||||
const isBoolean = require("@validatem/is-boolean");
|
||||
const isString = require("@validatem/is-string");
|
||||
|
||||
module.exports = function splitLines(_options) {
|
||||
let options = validateOptions(arguments, [
|
||||
defaultTo({}), {
|
||||
stripCarriageReturn: [ defaultTo(true), isBoolean ]
|
||||
}
|
||||
]);
|
||||
|
||||
let stringBuffer = "";
|
||||
|
||||
return pipe([
|
||||
map((value) => {
|
||||
validateValue(value, [ isString ]); // FIXME: Turn this into a clearer error
|
||||
|
||||
stringBuffer += value;
|
||||
|
||||
let lines = stringBuffer.split("\n");
|
||||
|
||||
if (lines.length > 1) {
|
||||
let completedLines = lines.slice(0, -1);
|
||||
stringBuffer = lastItem(lines);
|
||||
|
||||
if (options.stripCarriageReturn) {
|
||||
return completedLines.map((line) => {
|
||||
return line.replace(/\r+$/g, "");
|
||||
});
|
||||
} else {
|
||||
return completedLines;
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}),
|
||||
buffer(),
|
||||
lastWill(() => {
|
||||
return stringBuffer;
|
||||
})
|
||||
]);
|
||||
};
|
@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "@ppstreams/split-lines",
|
||||
"version": "0.1.0",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"ppstreams"
|
||||
],
|
||||
"repository": "http://git.cryto.net/joepie91/split-lines.git",
|
||||
"author": "Sven Slootweg <admin@cryto.net>",
|
||||
"license": "WTFPL OR CC0-1.0",
|
||||
"devDependencies": {
|
||||
"@joepie91/eslint-config": "^1.1.0",
|
||||
"@promistream/collect": "^0.1.1",
|
||||
"@promistream/from-iterable": "^0.1.0",
|
||||
"bluebird": "^3.7.2",
|
||||
"eslint": "^6.8.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@promistream/buffer": "^0.1.0",
|
||||
"@promistream/last-will": "^0.1.0",
|
||||
"@promistream/map": "^0.1.0",
|
||||
"@promistream/pipe": "^0.1.1",
|
||||
"@validatem/core": "^0.3.13",
|
||||
"@validatem/default-to": "^0.1.0",
|
||||
"@validatem/is-boolean": "^0.1.1",
|
||||
"@validatem/is-string": "^1.0.0",
|
||||
"last-item": "^1.0.0"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue