New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

convert-newline

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

convert-newline - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

2

data/utf8/cr.txt

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc