Git Product home page Git Product logo

firstline's Introduction

Firstline

Build status Test coverage Dependencies Npm version License

Introduction

Firstline is a npm async module for NodeJS, that reads and returns the first line of any file. It uses native JS promises and streams (requires Node >= v6.4.0). It is well tested and built for high performance.

It is particularly suited when you need to programmatically access the first line of a large amount of files, while handling errors if they occur.

Install

npm install firstline

Usage

firstline(filePath, [opts])

  • filePath (String): the full path to the file you want to read.
  • opts (Object, optional):
    • encoding (String), set the file encoding (must be supported by Node.js).
    • lineEnding (String), the character used for line ending (defaults to \n).

Incrementally reads data from filePath until it reaches the end of the first line.

Returns a promise, eventually fulfilled with a string.

Examples

// Imagine the file content is:
// abc
// def
// ghi
//

firstline('./my-file.txt');
// -> Returns a promise that will be fulfilled with 'abc'.

firstline('./my-file.txt', { lineEnding: '\r' });
// -> Same as above, but using '\r' as line ending.

MIT License

firstline's People

Contributors

pensierinmusica avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

firstline's Issues

firstline(...) returns promise object not the firstline string

I am new to js programming. I am interested in extracting the first line of a file.
I use the following lines of code to achieve that

const firstline = require("firstline");
console.log("First line of the file: " + firstline("home/assets/sample.txt");

however, it returns First line of the file: [object Promise] as output.

content of file "home/assets/sample.txt" is as follows
abc
def
ghi

Therefore, I expect "abc" as output. what I am missing?

Getting an error trying to use firstling!

`Bryans-MacBook-Pro:PRSN bryantorres$ node readtxt.js

assert.js:90
throw new assert.AssertionError({
^
AssertionError: missing path
at Module.require (module.js:351:3)
at require (internal/module.js:12:17)
at Object. (/Users/bryantorres/Desktop/PRSN/readtxt.js:1:79)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3`

Any help on how to fix this?

Slice in resolve

Do you need to slice the string in resolve? I find that it trims the last character when you test it out on a CSV with no data only the headers. I am using this module to validate csv headers btw.

example

Hi, I am a beginner with node. Is it possible to get an example of this can be used?

Type definitions

I would like this module to have built-in type definitions.

If you're interested in that, I'm happy to make a pull request to add them. If not, I'll make a pull request to DefinitelyTyped.

UTF-8 BOM handling

This is not working with a file having a UTF-8 BOM
Given:
screen shot 2017-08-14 at 3 10 03 pm

I expected it to return "review_id,review_date,comment"
But It returned everything as the first line.

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.