Browse Source

Initial commit

master
Sven Slootweg 1 year ago
commit
104ca824cb
  1. 3
      .eslintrc
  2. 1
      .gitignore
  3. 20
      example.js
  4. 54
      index.js
  5. 29
      package.json
  6. 1495
      yarn.lock

3
.eslintrc

@ -0,0 +1,3 @@
{
"extends": "@joepie91/eslint-config"
}

1
.gitignore

@ -0,0 +1 @@
node_modules

20
example.js

@ -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" ]
});

54
index.js

@ -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;
})
]);
};

29
package.json

@ -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"
}
}

1495
yarn.lock

File diff suppressed because it is too large
Loading…
Cancel
Save