convert-newline
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -1,1 +0,1 @@ | ||
AAA BBB CCC | ||
aaa bbb ccc |
@@ -1,3 +0,3 @@ | ||
AAA | ||
BBB | ||
CCC | ||
aaa | ||
bbb | ||
ccc |
@@ -1,3 +0,3 @@ | ||
AAA | ||
BBB | ||
CCC | ||
aaa | ||
bbb | ||
ccc |
@@ -62,3 +62,3 @@ "use strict"; | ||
if (typeof chunk !== "string") { | ||
return done(new Error(PACKAGE_NAME + " needs string as its input.")); | ||
return done(new Error(util.format("%s needs string as its input.", PACKAGE_NAME))); | ||
} | ||
@@ -65,0 +65,0 @@ try { |
{ | ||
"name": "convert-newline", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Unify newline characters to either \\n, \\r\\n, or \\r.", | ||
@@ -19,6 +19,6 @@ "homepage": "https://github.com/takenspc/convert-newline/", | ||
"dependencies": { | ||
"coveralls": "^2.11.2", | ||
"iconv-lite": "^0.4.8" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.2", | ||
"eslint": "^0.20.0", | ||
@@ -25,0 +25,0 @@ "istanbul": "^0.3.13", |
@@ -12,55 +12,64 @@ # convert-newline | ||
var convertNewline = require("convert-newline"); | ||
var converter = convertNewline("lf").string(); | ||
converter("AAA\rBBB\r\nCCC\n"); // -> "AAA\nBBB\nCCC\n" | ||
```js | ||
var convertNewline = require("convert-newline"); | ||
var converter = convertNewline("lf").string(); | ||
converter("aaa\rbbb\r\nccc\n"); // -> "aaa\nbbb\nccc\n" | ||
``` | ||
### Buffer | ||
var convertNewline = require("convert-newline"); | ||
var buffer = new Buffer("AAA\rBBB\r\nCCC\n"); | ||
var converter = convertNewline("lf").buffer(); | ||
var newBuffer = converter(buffer); | ||
newBuffer.toString(); // -> "AAA\nBBB\nCCC\n" | ||
```js | ||
var convertNewline = require("convert-newline"); | ||
var buffer = new Buffer("aaa\rbbb\r\nccc\n"); | ||
var converter = convertNewline("lf").buffer(); | ||
var newBuffer = converter(buffer); | ||
newBuffer.toString(); // -> "aaa\nbbb\nccc\n" | ||
``` | ||
Converting non-utf8 encoded buffers is supported. | ||
var iconv = require("iconv-lite"); | ||
var convertNewline = require("convert-newline"); | ||
var buffer = iconv.encode(new Buffer("あ\rい\r\nう\n"), "shift_jis"); | ||
var converter = convertNewline("lf", "shift_jis").buffer(); | ||
var newBuffer = converter(buffer); | ||
iconv.decode(newBuffer, "shift_jis"); // -> "あ\nい\nう\n" | ||
```js | ||
var iconv = require("iconv-lite"); | ||
var convertNewline = require("convert-newline"); | ||
var buffer = iconv.encode(new Buffer("あ\rい\r\nう\n"), "shift_jis"); | ||
var converter = convertNewline("lf", "shift_jis").buffer(); | ||
var newBuffer = converter(buffer); | ||
iconv.decode(newBuffer, "shift_jis"); // -> "あ\nい\nう\n" | ||
``` | ||
### Stream | ||
var fs = require("fs"); | ||
var convertNewline = require("convert-newline"); | ||
var reader = fs.createReadStream("foo.txt", { encoding: "utf8" }); | ||
var writer = fs.createWriteStream("bar.txt"); | ||
var converter = convertNewline("lf").stream(); | ||
reader | ||
.pipe(converter) | ||
.pipe(writer); | ||
```js | ||
var fs = require("fs"); | ||
var convertNewline = require("convert-newline"); | ||
var reader = fs.createReadStream("foo.txt", { encoding: "utf8" }); | ||
var writer = fs.createWriteStream("bar.txt"); | ||
var converter = convertNewline("lf").stream(); | ||
reader | ||
.pipe(converter) | ||
.pipe(writer); | ||
``` | ||
To convert non-utf8 encoded streams, you can use [iconv-lite](https://github.com/ashtuchkin/iconv-lite/). | ||
var fs = require("fs"); | ||
var iconv = require("iconv-lite"); | ||
var convertNewline = require("convert-newline"); | ||
var reader = fs.createReadStream("foo.txt"); | ||
var writer = fs.createWriteStream("bar.txt"); | ||
var converter = convertNewline("lf").stream(); | ||
reader | ||
.pipe(iconv.decodeStream("shift_jis")) | ||
.pipe(converter) | ||
.pipe(iconv.encodeStream("shift_jis")) | ||
.pipe(writer); | ||
```js | ||
var fs = require("fs"); | ||
var iconv = require("iconv-lite"); | ||
var convertNewline = require("convert-newline"); | ||
var reader = fs.createReadStream("foo.txt"); | ||
var writer = fs.createWriteStream("bar.txt"); | ||
var converter = convertNewline("lf").stream(); | ||
reader | ||
.pipe(iconv.decodeStream("shift_jis")) | ||
.pipe(converter) | ||
.pipe(iconv.encodeStream("shift_jis")) | ||
.pipe(writer); | ||
``` | ||
@@ -67,0 +76,0 @@ ## API |
67
test.js
@@ -5,2 +5,3 @@ "use strict"; | ||
var path = require("path"); | ||
var stream = require("stream"); | ||
var util = require("util"); | ||
@@ -22,5 +23,5 @@ var iconv = require("iconv-lite"); | ||
return { | ||
cr: "AAA\rBBB\rCCC\r", | ||
crlf: "AAA\r\nBBB\r\nCCC\r\n", | ||
lf: "AAA\nBBB\nCCC\n" | ||
cr: "aaa\rbbb\rccc\r", | ||
crlf: "aaa\r\nbbb\r\nccc\r\n", | ||
lf: "aaa\nbbb\nccc\n" | ||
}; | ||
@@ -40,5 +41,5 @@ } | ||
return { | ||
cr: new Buffer("AAA\rBBB\rCCC\r"), | ||
crlf: new Buffer("AAA\r\nBBB\r\nCCC\r\n"), | ||
lf: new Buffer("AAA\nBBB\nCCC\n") | ||
cr: new Buffer("aaa\rbbb\rccc\r"), | ||
crlf: new Buffer("aaa\r\nbbb\r\nccc\r\n"), | ||
lf: new Buffer("aaa\nbbb\nccc\n") | ||
}; | ||
@@ -74,2 +75,17 @@ } | ||
describe(PACKAGE_NAME, function() { | ||
it("should throw for invalid option", function() { | ||
assert.throws(function() { | ||
convertNewline("\n"); | ||
}, /Unsupported `newline`/); | ||
}); | ||
it("should use \"lf\" as default option", function() { | ||
var testData = getStringTestData(); | ||
var converter = convertNewline().string(); | ||
var newlinews = Object.keys(testData); | ||
newlinews.forEach(function(fromNewline) { | ||
assert.strictEqual(converter(testData[fromNewline]), testData.lf); | ||
}); | ||
}); | ||
describe("in string mode", function() { | ||
@@ -187,2 +203,41 @@ var testData = getStringTestData(); | ||
}); | ||
it("should throw an error for non string stream", function(done) { | ||
var newline = "lf"; | ||
var fromFilename = path.join("data", UTF8, newline + ".txt"); | ||
var reader = fs.createReadStream(fromFilename); | ||
var converter = convertNewline(newline).stream(); | ||
converter.on("error", function (err) { | ||
assert.ok(err instanceof Error); | ||
assert.strictEqual(err.message, util.format("%s needs string as its input.", PACKAGE_NAME)); | ||
done(); | ||
}); | ||
reader | ||
.pipe(converter); | ||
}); | ||
it("should treat a sequence of \\r and \\n as a newline", function(done) { | ||
var newline = "lf"; | ||
var converter = convertNewline(newline).stream(); | ||
var converted = []; | ||
converter.on("data", function (chunk) { | ||
converted.push(chunk); | ||
}); | ||
converter.on("end", function () { | ||
assert.deepStrictEqual(converted, ["aaa", "\n\nbbb", "\nccc", "\n"]); | ||
done(); | ||
}); | ||
var reader = new stream.Readable({ | ||
encoding: UTF8 | ||
}); | ||
["aaa\r", "\rbbb\r", "\nccc\r"].forEach(function(item) { | ||
reader.push(item, UTF8); | ||
}); | ||
reader.push(null); | ||
reader | ||
.pipe(converter); | ||
}); | ||
}); | ||
@@ -189,0 +244,0 @@ |
16211
1
357
87
4
- Removedcoveralls@^2.11.2
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedargparse@1.0.10(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@0.2.01.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.6.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedboom@2.10.1(transitive)
- Removedcaseless@0.11.0(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removedcoveralls@2.13.3(transitive)
- Removedcryptiles@2.0.5(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesprima@2.7.3(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.1.4(transitive)
- Removedgenerate-function@2.3.1(transitive)
- Removedgenerate-object-property@1.2.0(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-validator@2.0.6(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhawk@3.1.3(transitive)
- Removedhoek@2.16.3(transitive)
- Removedhttp-signature@1.1.1(transitive)
- Removedis-my-ip-valid@1.0.1(transitive)
- Removedis-my-json-valid@2.20.6(transitive)
- Removedis-property@1.0.2(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjs-yaml@3.6.1(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsonpointer@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedlcov-parse@0.0.10(transitive)
- Removedlog-driver@1.2.5(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedminimist@1.2.0(transitive)
- Removedoauth-sign@0.8.2(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedpunycode@1.4.1(transitive)
- Removedqs@6.3.3(transitive)
- Removedrequest@2.79.0(transitive)
- Removedsntp@1.0.9(transitive)
- Removedsprintf-js@1.0.3(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedstringstream@0.0.6(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedtough-cookie@2.3.4(transitive)
- Removedtunnel-agent@0.4.3(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
- Removedxtend@4.0.2(transitive)