Comparing version 0.9.1 to 0.10.0
13
eol.d.ts
@@ -27,2 +27,9 @@ declare module eol { | ||
/** | ||
* Dubnormalize. Used internally. Mixin friendly. | ||
* Create normalizer where linebreaks become your desire. | ||
* @return anonymous function | ||
*/ | ||
export function dub(text: string): (text: string) => string; | ||
/** | ||
* Add linebreak before text | ||
@@ -40,2 +47,8 @@ * @return string with linebreak added before text | ||
/** | ||
* Detect or inspect linebreaks | ||
* @return array of matched linebreaks | ||
*/ | ||
export function match(text: string): Array<string>; | ||
/** | ||
* Split text by newline | ||
@@ -42,0 +55,0 @@ * @return array of lines |
36
eol.js
!function(root, name, make) { | ||
if (typeof module != 'undefined' && module.exports) module.exports = make() | ||
if (typeof module != "undefined" && module.exports) module.exports = make() | ||
else root[name] = make() | ||
}(this, 'eol', function() { | ||
}(this, "eol", function() { | ||
var api = {} | ||
var isWindows = typeof process != 'undefined' && 'win32' === process.platform | ||
var linebreak = isWindows ? '\r\n' : '\n' | ||
var isWindows = typeof process != "undefined" && "win32" === process.platform | ||
var linebreak = isWindows ? "\r\n" : "\n" | ||
var newline = /\r\n|\r|\n/g | ||
@@ -19,12 +19,16 @@ | ||
function converts(to) { | ||
function convert(text) { | ||
function dub(to) { | ||
function change(text) { | ||
return text.replace(newline, to) | ||
} | ||
convert.toString = function() { | ||
change.toString = function() { | ||
return to | ||
} | ||
return convert | ||
return change | ||
} | ||
function match(text) { | ||
return text.match(newline) || [] | ||
} | ||
function split(text) { | ||
@@ -34,10 +38,12 @@ return text.split(newline) | ||
api['lf'] = converts('\n') | ||
api['cr'] = converts('\r') | ||
api['crlf'] = converts('\r\n') | ||
api['auto'] = converts(linebreak) | ||
api['before'] = before | ||
api['after'] = after | ||
api['split'] = split | ||
api["lf"] = dub("\n") | ||
api["cr"] = dub("\r") | ||
api["crlf"] = dub("\r\n") | ||
api["auto"] = dub(linebreak) | ||
api["before"] = before | ||
api["after"] = after | ||
api["match"] = match | ||
api["split"] = split | ||
api["dub"] = dub | ||
return api | ||
}); |
{ | ||
"name": "eol", | ||
"description": "Newline character converter", | ||
"version": "0.9.1", | ||
"homepage": "https://github.com/ryanve/eol", | ||
"version": "0.10.0", | ||
"homepage": "https://ryanve.github.io/eol", | ||
"funding": "https://github.com/sponsors/ryanve", | ||
"license": "MIT", | ||
@@ -12,3 +13,3 @@ "author": "Ryan Van Etten", | ||
"scripts": { | ||
"lint": "eslint . --ext .js", | ||
"lint": "npx eslint", | ||
"preversion": "npm test", | ||
@@ -46,4 +47,4 @@ "pretest": "npm run lint", | ||
"aok": "^1.9.0", | ||
"eslint": "^3.15.0" | ||
"eslint": "^9.9.1" | ||
} | ||
} |
156
README.md
@@ -1,55 +0,70 @@ | ||
# eol | ||
[Newline](http://en.wikipedia.org/wiki/Newline) character converter for JavaScript. Available [on npm](https://www.npmjs.com/package/eol). | ||
# [eol](https://ryanve.github.io/eol) | ||
``` | ||
npm install eol --save | ||
``` | ||
[Newline](http://en.wikipedia.org/wiki/Newline) character converter node module for [JavaScript](eol.js) or [TypeScript](eol.d.ts) | ||
### `require` or `import` | ||
### [npm.im/eol](https://npmjs.com/package/eol) | ||
```js | ||
const eol = require('eol') | ||
``` | ||
* `npm install eol` | ||
* <code><b>let</b> eol = <b>require</b>("eol")</code> | ||
* <code><b>import</b> eol <b>from</b> "eol"</code> | ||
```js | ||
import eol from 'eol' | ||
``` | ||
## [API](https://ryanve.github.io/eol#methods) | ||
## API | ||
### eol.auto(<var>text</var>) | ||
### `eol.auto(text)` | ||
- Normalize line endings in <var>text</var> for the current operating system | ||
- <b>@return</b> string with line endings normalized to `\r\n` or `\n` | ||
* Normalize line endings in <var>text</var> to match the current operating system | ||
* Returns string with line endings normalized to `\r\n` or `\n` | ||
### `eol.crlf(text)` | ||
- Normalize line endings in <var>text</var> to <b>CRLF</b> (Windows, DOS) | ||
- <b>@return</b> string with line endings normalized to `\r\n` | ||
### eol.crlf(<var>text</var>) | ||
### `eol.lf(text)` | ||
- Normalize line endings in <var>text</var> to <b>LF</b> (Unix, OS X) | ||
- <b>@return</b> string with line endings normalized to `\n` | ||
* Normalize line endings in <var>text</var> to <b>CRLF</b> (Windows, DOS) | ||
* Returns string with line endings normalized to `\r\n` | ||
### `eol.cr(text)` | ||
- Normalize line endings in <var>text</var> to <b>CR</b> (Mac OS) | ||
- <b>@return</b> string with line endings normalized to `\r` | ||
### eol.lf(<var>text</var>) | ||
### `eol.before(text)` | ||
- Add linebreak before <var>text</var> | ||
- <b>@return</b> string with linebreak added before text | ||
* Normalize line endings in <var>text</var> to <b>LF</b> (Unix, OS X) | ||
* Returns string with line endings normalized to `\n` | ||
### `eol.after(text)` | ||
- Add linebreak after <var>text</var> | ||
- <b>@return</b> string with linebreak added after text | ||
### eol.cr(<var>text</var>) | ||
### `eol.split(text)` | ||
- Split <var>text</var> by newline | ||
- <b>@return</b> array of lines | ||
* Normalize line endings in <var>text</var> to <b>CR</b> (Mac OS) | ||
* Returns string with line endings normalized to `\r` | ||
### eol.dub(<var>text</var>) | ||
* Generate normalizer where linebreaks become <var>text</var> | ||
* Used [internally](eol.js) to generate the [normalizers](#api) above | ||
* Returns composed pure function with [creative potential](#dubbing) | ||
### eol.before(<var>text</var>) | ||
* Add linebreak before <var>text</var> | ||
* Returns string with linebreak added before text | ||
* Uses `eol.auto` linebreak | ||
* `eol.lf(eol.before(text))` ⋮ | ||
### eol.after(<var>text</var>) | ||
* Add linebreak after <var>text</var> | ||
* Returns string with linebreak added after text | ||
* Uses `eol.auto` linebreak | ||
* `eol.lf(eol.after(text))` ⋮ | ||
### eol.match(<var>text</var>) | ||
* Detect or inspect linebreaks in <var>text</var> | ||
* Returns array of matched linebreaks | ||
### eol.split(<var>text</var>) | ||
* Split <var>text</var> by newline | ||
* Returns array of lines | ||
### Joining | ||
Coercing `eol.auto`|`eol.crlf`|`eol.lf`|`eol.cr` to string yields the appropriate character. This is useful for joining. | ||
Coercing [normalizers](#api) to string yields the appropriate character...useful glue for joining | ||
```js | ||
String(eol.lf) // "\n" | ||
eol.split(text).join(eol.auto) // same as eol.auto(text) | ||
eol.split(text).join(eol.auto) // === eol.auto(text) | ||
eol.split(text).filter(line => line).join(eol.auto) // text joined after removing empty lines | ||
@@ -59,3 +74,68 @@ eol.split(text).slice(-3).join(eol.auto) // last 3 lines joined | ||
## License | ||
MIT | ||
### Matching | ||
Detect or inspect via match | ||
```js | ||
eol.match(" ") // [] | ||
eol.match("world\nwide\nweb") // ["\n","\n"] | ||
``` | ||
### Dubbing | ||
Generate alternate [normalizers](#api) | ||
``` | ||
let extra = eol.dub("\n\n\n") | ||
extra("...") | ||
``` | ||
``` | ||
let huh = eol.dub("???") | ||
huh("...") | ||
``` | ||
## modularitY | ||
### [edit-file](https://github.com/ryanve/edit-file) | ||
```js | ||
let eol = require("eol") | ||
let edit = require("edit-file") | ||
edit("sample.txt", eol.lf) | ||
``` | ||
### [map-file](https://github.com/ryanve/map-file) | ||
```js | ||
let eol = require("eol") | ||
let map = require("map-file") | ||
map({ | ||
from: "from.txt", | ||
to: "to.txt", | ||
map: eol.lf | ||
}) | ||
``` | ||
### [ssv](https://ryanve.github.io/ssv) | ||
```js | ||
let ssv = require("ssv") | ||
let eol = require("eol") | ||
let deep = eol.split("spaced.txt").map(ssv.split) | ||
``` | ||
### Yours | ||
Have an `eol` sample to share? | ||
[Then please do](../../issues/new) :test_tube: :test_tube: :test_tube: :test_tube: | ||
## [opensource](package.json) | ||
[<b>MIT</b> License](LICENSE.md) | ||
[**∞/0**](#eol) |
79
test.js
@@ -10,39 +10,44 @@ !function(root) { | ||
var common = typeof module != 'undefined' && !!module.exports | ||
var aok = common ? require('aok') : root.aok | ||
var eol = common ? require('./') : root.eol | ||
var isWindows = typeof process != 'undefined' && 'win32' === process.platform | ||
var meths = ['lf', 'cr', 'crlf', 'auto'] | ||
var chars = ['\n', '\r', '\r\n', isWindows ? '\r\n' : '\n'] | ||
var sample = ' ' + chars.join() + 'text' + chars.join() | ||
var common = typeof module != "undefined" && !!module.exports | ||
var aok = common ? require("aok") : root.aok | ||
var eol = common ? require("./") : root.eol | ||
var isWindows = typeof process != "undefined" && "win32" === process.platform | ||
var meths = ["lf", "cr", "crlf", "auto"] | ||
var chars = ["\n", "\r", "\r\n", isWindows ? "\r\n" : "\n"] | ||
var sample = " " + chars.join() + "text" + chars.join() | ||
aok.prototype.fail = function() { | ||
throw new Error('FAILED TEST: ' + this.id) | ||
throw new Error("FAILED TEST: " + this.id) | ||
} | ||
aok('contains sees contained text', contains('ab', 'a') === true) | ||
aok('sample contains newlines', contains(sample, '\n') && contains(sample, '\r')) | ||
aok('returns other strings as is', eol.auto('random') === 'random') | ||
aok('returns empty strings as is', eol.auto('') === '') | ||
aok('whitespace intact', eol.lf(' \t ') === ' \t ') | ||
aok('lf repeat newlines intact', eol.lf('\n\n\r\r') === '\n\n\n\n') | ||
aok('cr repeat newlines intact', eol.cr('\n\n\r\r') === '\r\r\r\r') | ||
aok('crlf repeat newlines intact', eol.crlf('\r\n\r\n') === '\r\n\r\n') | ||
aok('lf function coerces to string', String(eol.lf) === '\n') | ||
aok('crlf function coerces to string', String(eol.crlf) === '\r\n') | ||
aok('cr function coerces to string', String(eol.cr) === '\r') | ||
aok('auto function coerces to string', String(eol.auto) === isWindows ? '\r\n' : '\n') | ||
aok('split return type', eol.split('0\n1\n2') instanceof Array) | ||
aok('split lf', eol.split('0\n1\n2').join('') === '012') | ||
aok('split cr', eol.split('0\r1\r2').join('') === '012') | ||
aok('split crlf', eol.split('0\r\n1\r\n2').join('') === '012') | ||
aok('split mixed', eol.split('0\r\n1\n2\r3\r\n4').join('') === '01234') | ||
aok('split join', eol.split('0\n1\n\n2\n').join(eol.auto) === eol.auto('0\n1\n\n2\n')) | ||
aok('split filter join', eol.split('A\n\nB').filter(identity).join(eol.lf) === 'A\nB') | ||
aok('split slice join', eol.split('A\nB\nC\nD').slice(-2).join(eol.lf) === 'C\nD') | ||
aok("dub method", typeof eol.dub == "function") | ||
aok("dub return", typeof eol.dub() == "function") | ||
aok("contains sees contained text", contains("ab", "a") === true) | ||
aok("sample contains newlines", contains(sample, "\n") && contains(sample, "\r")) | ||
aok("returns other strings as is", eol.auto("random") === "random") | ||
aok("returns empty strings as is", eol.auto("") === "") | ||
aok("whitespace intact", eol.lf(" \t ") === " \t ") | ||
aok("lf repeat newlines intact", eol.lf("\n\n\r\r") === "\n\n\n\n") | ||
aok("cr repeat newlines intact", eol.cr("\n\n\r\r") === "\r\r\r\r") | ||
aok("crlf repeat newlines intact", eol.crlf("\r\n\r\n") === "\r\n\r\n") | ||
aok("lf function coerces to string", String(eol.lf) === "\n") | ||
aok("crlf function coerces to string", String(eol.crlf) === "\r\n") | ||
aok("cr function coerces to string", String(eol.cr) === "\r") | ||
aok("auto function coerces to string", String(eol.auto) === isWindows ? "\r\n" : "\n") | ||
aok("match none", !eol.match(" ").length) | ||
aok("match some", eol.match("\n").length) | ||
aok("match www", eol.match("world\nwide\nweb").length > 1) | ||
aok("split return type", eol.split("0\n1\n2") instanceof Array) | ||
aok("split lf", eol.split("0\n1\n2").join("") === "012") | ||
aok("split cr", eol.split("0\r1\r2").join("") === "012") | ||
aok("split crlf", eol.split("0\r\n1\r\n2").join("") === "012") | ||
aok("split mixed", eol.split("0\r\n1\n2\r3\r\n4").join("") === "01234") | ||
aok("split join", eol.split("0\n1\n\n2\n").join(eol.auto) === eol.auto("0\n1\n\n2\n")) | ||
aok("split filter join", eol.split("A\n\nB").filter(identity).join(eol.lf) === "A\nB") | ||
aok("split slice join", eol.split("A\nB\nC\nD").slice(-2).join(eol.lf) === "C\nD") | ||
aok.pass(meths, function(method, i) { | ||
var normalized = eol[method](sample) | ||
aok(method + ' retains', contains(normalized, chars[i])) | ||
aok(method + ' normalizes', !aok.fail(chars, function(c) { | ||
aok(method + " retains", contains(normalized, chars[i])) | ||
aok(method + " normalizes", !aok.fail(chars, function(c) { | ||
return contains(chars[i], c) === contains(normalized, c) | ||
@@ -54,13 +59,13 @@ })) | ||
aok('auto is aware', eol[isWindows ? 'crlf' : 'lf'](sample) === eol.auto(sample)) | ||
aok('auto matches only 1 and self', aok.pass(meths, function(method) { | ||
aok("auto is aware", eol[isWindows ? "crlf" : "lf"](sample) === eol.auto(sample)) | ||
aok("auto matches only 1 and self", aok.pass(meths, function(method) { | ||
return eol.auto(sample) === eol[method](sample); | ||
}) === 2) | ||
aok('before', eol.lf(eol.before('text')) === '\ntext') | ||
aok('before2', eol.lf(eol.before('\ntext\n')) === '\n\ntext\n') | ||
aok('after', eol.lf(eol.after('text')) === 'text\n') | ||
aok('after2', eol.lf(eol.after('\ntext\n')) === '\ntext\n\n') | ||
aok("before", eol.lf(eol.before("text")) === "\ntext") | ||
aok("before2", eol.lf(eol.before("\ntext\n")) === "\n\ntext\n") | ||
aok("after", eol.lf(eol.after("text")) === "text\n") | ||
aok("after2", eol.lf(eol.after("\ntext\n")) === "\ntext\n\n") | ||
aok.log('All tests passed =)') | ||
aok.log("All tests passed =)") | ||
}(this); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17650
227
141
11