convert-newline
Advanced tools
Comparing version 0.0.2 to 0.0.3
{ | ||
"name": "convert-newline", | ||
"version": "0.0.2", | ||
"description": "Convert newlines to specifc one", | ||
"version": "0.0.3", | ||
"description": "Unify newline characters to either \\n, \\r\\n, or \\r.", | ||
"homepage": "https://github.com/takenspc/convert-newline/", | ||
@@ -9,4 +9,6 @@ "author": "Takeshi Kurosawa <taken.spc@gmail.com>", | ||
"scripts": { | ||
"coverage": "istanbul cover --report html node_modules/mocha/bin/_mocha -- test.js -R spec", | ||
"test": "eslint . && mocha test.js" | ||
"coverage": "istanbul cover node_modules/mocha/bin/_mocha -- test.js -R spec", | ||
"coveralls": "cat ./coverage/lcov.info | coveralls", | ||
"lint": "eslint .", | ||
"test": "mocha test.js" | ||
}, | ||
@@ -18,2 +20,3 @@ "repository": { | ||
"dependencies": { | ||
"coveralls": "^2.11.2", | ||
"iconv-lite": "^0.4.8" | ||
@@ -27,4 +30,4 @@ }, | ||
"engines": { | ||
"iojs" : "2.0.x" | ||
"iojs": "2.0.x" | ||
} | ||
} |
# convert-newline | ||
[![Build Status](https://travis-ci.org/takenspc/convert-newline.svg?branch=master)](https://travis-ci.org/takenspc/convert-newline) | ||
[![Coverage Status](https://coveralls.io/repos/takenspc/convert-newline/badge.svg?branch=master)](https://coveralls.io/r/takenspc/convert-newline?branch=master) | ||
Unify newline characters to either `\n`,`\r\n`, or `\r`. | ||
@@ -4,0 +7,0 @@ |
178
test.js
"use strict"; | ||
var assert = require("assert"); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var util = require("util"); | ||
var iconv = require("iconv-lite"); | ||
@@ -10,2 +12,3 @@ var convertNewline = require("./"); | ||
// | ||
var PACKAGE_NAME = "convert-newline"; | ||
var UTF8 = "utf8"; | ||
@@ -27,5 +30,5 @@ var SHIFT_JIS = "shift_jis"; | ||
return { | ||
cr: fs.readFileSync("data/utf8/cr.txt"), | ||
crlf: fs.readFileSync("data/utf8/crlf.txt"), | ||
lf: fs.readFileSync("data/utf8/lf.txt") | ||
cr: fs.readFileSync(path.join("data", UTF8, "cr.txt")), | ||
crlf: fs.readFileSync(path.join("data", UTF8, "crlf.txt")), | ||
lf: fs.readFileSync(path.join("data", UTF8, "lf.txt")) | ||
}; | ||
@@ -45,5 +48,5 @@ | ||
return { | ||
cr: fs.readFileSync("data/shift_jis/cr.txt"), | ||
crlf: fs.readFileSync("data/shift_jis/crlf.txt"), | ||
lf: fs.readFileSync("data/shift_jis/lf.txt") | ||
cr: fs.readFileSync(path.join("data", SHIFT_JIS, "cr.txt")), | ||
crlf: fs.readFileSync(path.join("data", SHIFT_JIS, "crlf.txt")), | ||
lf: fs.readFileSync(path.join("data", SHIFT_JIS, "lf.txt")) | ||
}; | ||
@@ -70,11 +73,13 @@ } | ||
// | ||
describe("crlf", function() { | ||
it("string", function() { | ||
var data = getStringTestData(); | ||
var keys = Object.keys(data); | ||
keys.forEach(function(key1) { | ||
var converter = convertNewline(key1).string(); | ||
var reference = data[key1]; | ||
keys.forEach(function(key2) { | ||
assert.strictEqual(converter(data[key2]), reference, key2 + " were converted to " + key1); | ||
describe(PACKAGE_NAME, function() { | ||
describe("in string mode", function() { | ||
var testData = getStringTestData(); | ||
var newlinews = Object.keys(testData); | ||
newlinews.forEach(function(toNewline) { | ||
newlinews.forEach(function(fromNewline) { | ||
it(util.format("should convert from %s to %s", fromNewline, toNewline), function() { | ||
var converter = convertNewline(toNewline).string(); | ||
var reference = testData[toNewline]; | ||
assert.strictEqual(converter(testData[fromNewline]), reference); | ||
}); | ||
}); | ||
@@ -84,10 +89,12 @@ }); | ||
it("buffer (simple)", function() { | ||
var data = getUTF8BufferTestData(); | ||
var keys = Object.keys(data); | ||
keys.forEach(function(key1) { | ||
var converter = convertNewline(key1).buffer(); | ||
var reference = data[key1]; | ||
keys.forEach(function(key2) { | ||
assert.ok(reference.equals(converter(data[key2])), key2 + " were converted to " + key1); | ||
describe("in buffer mode (simple)", function() { | ||
var testData = getUTF8BufferTestData(); | ||
var newlines = Object.keys(testData); | ||
newlines.forEach(function(toNewline) { | ||
newlines.forEach(function(fromNewline) { | ||
it(util.format("should convert from %s to %s", fromNewline, toNewline), function() { | ||
var converter = convertNewline(toNewline).buffer(); | ||
var reference = testData[toNewline]; | ||
assert.ok(reference.equals(converter(testData[fromNewline]))); | ||
}); | ||
}); | ||
@@ -97,10 +104,12 @@ }); | ||
describe("buffer (iconv)", function() { | ||
var data = getShiftJISBufferTestData(); | ||
var keys = Object.keys(data); | ||
keys.forEach(function(key1) { | ||
var converter = convertNewline(key1, SHIFT_JIS).buffer(); | ||
var reference = data[key1]; | ||
keys.forEach(function(key2) { | ||
assert.ok(reference.equals(converter(data[key2])), key2 + " were converted to " + key1); | ||
describe("in buffer mode (iconv)", function() { | ||
var testData = getShiftJISBufferTestData(); | ||
var newlines = Object.keys(testData); | ||
newlines.forEach(function(toNewline) { | ||
newlines.forEach(function(fromNewline) { | ||
it(util.format("should convert from %s to %s", fromNewline, toNewline), function() { | ||
var converter = convertNewline(toNewline, SHIFT_JIS).buffer(); | ||
var reference = testData[toNewline]; | ||
assert.ok(reference.equals(converter(testData[fromNewline]))); | ||
}); | ||
}); | ||
@@ -110,13 +119,17 @@ }); | ||
it("load test data (utf8)", function() { | ||
var filedata = getUTF8FileTestData(); | ||
var bufferdata = getUTF8FileTestData(); | ||
var keys = Object.keys(filedata); | ||
keys.forEach(function(key1) { | ||
var reference = bufferdata[key1]; | ||
assert.ok(reference.equals(filedata[key1]), key1 + ".txt is loaded correctly"); | ||
keys.forEach(function(key2) { | ||
if (key1 !== key2) { | ||
assert.ok(!reference.equals(filedata[key2]), key1 + ".txt is not same as " + key2 + ".txt"); | ||
} | ||
describe("test code (utf8)", function() { | ||
var testData = getUTF8FileTestData(); | ||
var referenceData = getUTF8BufferTestData(); | ||
var newlines = Object.keys(testData); | ||
newlines.forEach(function(newline1) { | ||
it("should load test data correctly", function() { | ||
newlines.forEach(function (newline2) { | ||
var target = testData[newline2]; | ||
var reference = referenceData[newline1]; | ||
if (newline1 === newline2) { | ||
assert.ok(reference.equals(target), util.format("%s test data is loaded correctly", newline1)); | ||
} else { | ||
assert.ok(!reference.equals(target), util.format("%s test data is not same as %s correctly", newline1, newline2)); | ||
} | ||
}); | ||
}); | ||
@@ -126,12 +139,17 @@ }); | ||
it("load test data (shift_jis)", function() { | ||
var filedata = getShiftJISFileTestData(); | ||
var bufferdata = getShiftJISBufferTestData(); | ||
var keys = Object.keys(filedata); | ||
keys.forEach(function(key1) { | ||
assert.ok(bufferdata[key1].equals(filedata[key1]), key1 + ".txt is loaded correctly"); | ||
keys.forEach(function(key2) { | ||
if (key1 !== key2) { | ||
assert.ok(!filedata[key1].equals(filedata[key2]), key1 + ".txt is not same as " + key2 + ".txt"); | ||
} | ||
describe("test code (utf8)", function() { | ||
var testData = getShiftJISFileTestData(); | ||
var referenceData = getShiftJISBufferTestData(); | ||
var newlines = Object.keys(testData); | ||
newlines.forEach(function(newline1) { | ||
it("should load test data correctly", function() { | ||
newlines.forEach(function (newline2) { | ||
var target = testData[newline2]; | ||
var reference = referenceData[newline1]; | ||
if (newline1 === newline2) { | ||
assert.ok(reference.equals(target), util.format("%s test data is loaded correctly", newline1)); | ||
} else { | ||
assert.ok(!reference.equals(target), util.format("%s test data is not same as %s correctly", newline1, newline2)); | ||
} | ||
}); | ||
}); | ||
@@ -141,17 +159,20 @@ }); | ||
describe("stream (utf8)", function() { | ||
function testStream(key1, key2, reference, done) { | ||
var originalFilename = "data/utf8/" + key2 + ".txt"; | ||
var targetFilename = originalFilename + "." + key1; | ||
describe("in stream mode (simple)", function() { | ||
function testStream(toNewline, fromNewline, reference, done) { | ||
var fromFilename = path.join("data", UTF8, fromNewline + ".txt"); | ||
var targetFilename = fromFilename + "." + toNewline; | ||
var options = { | ||
encoding: UTF8 | ||
}; | ||
var reader = fs.createReadStream(originalFilename, options); | ||
var reader = fs.createReadStream(fromFilename, options); | ||
var writer = fs.createWriteStream(targetFilename); | ||
var converter = convertNewline(key1).stream(); | ||
var converter = convertNewline(toNewline).stream(); | ||
writer.on("finish", function() { | ||
var target = fs.readFileSync(targetFilename); | ||
assert.ok(reference.equals(target), key2 + " were converted to " + key1); | ||
assert.ok(reference.equals(target)); | ||
done(); | ||
}); | ||
reader | ||
@@ -161,8 +182,9 @@ .pipe(converter) | ||
} | ||
var references = getUTF8BufferTestData(); | ||
var keys = Object.keys(references); | ||
keys.forEach(function (key1) { | ||
keys.forEach(function(key2) { | ||
it("Convert " + key2 + " to " + key1, function(done) { | ||
testStream(key1, key2, references[key1], done); | ||
var newlines = Object.keys(references); | ||
newlines.forEach(function (toNewline) { | ||
newlines.forEach(function(fromNewline) { | ||
it(util.format("should convert from %s to %s", fromNewline, toNewline), function(done) { | ||
testStream(toNewline, fromNewline, references[toNewline], done); | ||
}); | ||
@@ -173,14 +195,17 @@ }); | ||
describe("stream (shift_jis)", function() { | ||
function testStream(key1, key2, reference, done) { | ||
var originalFilename = "data/shift_jis/" + key2 + ".txt"; | ||
var targetFilename = originalFilename + "." + key1; | ||
var reader = fs.createReadStream(originalFilename); | ||
describe("in stream mode (iconv)", function() { | ||
function testStream(toNewline, fromNewline, reference, done) { | ||
var fromFilename = path.join("data", SHIFT_JIS, fromNewline + ".txt"); | ||
var targetFilename = fromFilename + "." + toNewline; | ||
var reader = fs.createReadStream(fromFilename); | ||
var writer = fs.createWriteStream(targetFilename); | ||
var converter = convertNewline(key1).stream(); | ||
var converter = convertNewline(toNewline).stream(); | ||
writer.on("finish", function() { | ||
var target = fs.readFileSync(targetFilename); | ||
assert.ok(reference.equals(target), key2 + " were converted to " + key1); | ||
assert.ok(reference.equals(target)); | ||
done(); | ||
}); | ||
reader | ||
@@ -192,8 +217,9 @@ .pipe(iconv.decodeStream(SHIFT_JIS)) | ||
} | ||
var references = getShiftJISBufferTestData(); | ||
var keys = Object.keys(references); | ||
keys.forEach(function (key1) { | ||
keys.forEach(function(key2) { | ||
it("Convert " + key2 + " to " + key1, function (done) { | ||
testStream(key1, key2, references[key1], done); | ||
var newlines = Object.keys(references); | ||
newlines.forEach(function (toNewline) { | ||
newlines.forEach(function(fromNewline) { | ||
it(util.format("should convert from %s to %s", fromNewline, toNewline), function(done) { | ||
testStream(toNewline, fromNewline, references[toNewline], done); | ||
}); | ||
@@ -200,0 +226,0 @@ }); |
Sorry, the diff of this file is not supported yet
14605
309
78
2
17
+ Addedcoveralls@^2.11.2
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@0.2.01.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.6.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedboom@2.10.1(transitive)
+ Addedcaseless@0.11.0(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addedcoveralls@2.13.3(transitive)
+ Addedcryptiles@2.0.5(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedesprima@2.7.3(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.1.4(transitive)
+ Addedgenerate-function@2.3.1(transitive)
+ Addedgenerate-object-property@1.2.0(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-validator@2.0.6(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhawk@3.1.3(transitive)
+ Addedhoek@2.16.3(transitive)
+ Addedhttp-signature@1.1.1(transitive)
+ Addedis-my-ip-valid@1.0.1(transitive)
+ Addedis-my-json-valid@2.20.6(transitive)
+ Addedis-property@1.0.2(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjs-yaml@3.6.1(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsonpointer@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlcov-parse@0.0.10(transitive)
+ Addedlog-driver@1.2.5(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedminimist@1.2.0(transitive)
+ Addedoauth-sign@0.8.2(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@6.3.3(transitive)
+ Addedrequest@2.79.0(transitive)
+ Addedsntp@1.0.9(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedstringstream@0.0.6(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedtough-cookie@2.3.4(transitive)
+ Addedtunnel-agent@0.4.3(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedxtend@4.0.2(transitive)