GithubHelp home page GithubHelp logo

detect-newline's Introduction

detect-newline

Detect the dominant newline character of a string

Install

npm install detect-newline

Usage

import {detectNewline} from 'detect-newline';

detectNewline('foo\nbar\nbaz\r\n');
//=> '\n'

API

detectNewline(string)

Returns the detected newline or undefined when no newline character is found or \n when no dominant newline is present.

detectNewlineGraceful(unknown)

Returns the detected newline or \n when no newline character is found, no dominant newline is present, or the input is not a string.

Related

detect-newline's People

Contributors

bendingbender avatar blakeembrey avatar kevva avatar maximdevoir avatar ntwb avatar richienb avatar silverwind avatar sindresorhus avatar sonicdoe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

detect-newline's Issues

`.graceful()` method should return `\n` when given `null`

Current Behavior

// This is good/expected
detectNewline(null) // Throws TypeError.

// This is bad/unexpected/not graceful
detectNewline.graceful(null) // Throws TypeError

Expected Behavior

detectNewline.graceful(null) // Return '\n'

Expect graceful to be gracefully handle null input and return \n. Package should not throw/crash the app.

Return `undefined` when no new lines

I found a PR that actually did this too, but it was closed as out of scope. (#3)

I think this is actually a common use-case. If there's no newlines, there shouldn't be an newlines returned and the user can use the default. It's on the same bar as https://github.com/sindresorhus/detect-indent, which returns undefined as the indent when no indent is detected. This would allow someone to fall back to the OS EOL instead of \n when editing an existing file without any new lines.

Return `undefined` on equal counts?

I noticed the module has a bias towards LF, e.g. when a string contains a equal amount of LF and CRLF, it will return LF. The module's description says it returns the "dominant" variant, but when they are equal, none dominate and I think it would be more correct to return undefined.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.