npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

linez

v4.1.4

Published

Parses lines from text, preserving line numbers, offsets and line endings.

Downloads

62,532

Readme

linez

Parses lines from text, preserving line numbers, offsets and line endings.

Build Status Dependency Status npm version codecov

npm

Getting Started

Installation

$ npm install linez

Importing

TypeScript

import * as linez from 'linez';

Babel/ES6+

import linez from 'linez';

JavaScript

var linez = require('linez');

Introduction

By default, linez uses /\r?\n/g as the regular expression to detect newline character sequences and split lines. This regular expression is tuned for performance and only covers the most common newline types (i.e., \n and \r\n). If you have need for more newline character sequences, you can configure linez with the configure method.

linez.configure({
  newlines: ['\n', '\r\n', '\r', '\u000B']
});

Setting this property will automatically create a piped regular expression for you and use it in any future linez() calls. You can make up your own newlines if you want. Linez doesn't care one way or the other.

linez.configure({
  newlines: ['foo', 'bar']
});

This would be converted into /(foo|bar)/g. Newlines are just strings. They can be anything. There are, however, some known newline character sequences. Should you need them, refer to the following table:

| String | Unicode | Name | | -------- |:-------------- |:--------------------------- | | \n | U+000A | Line feed | | \r\n | U+000D, U+000A | Carriage Return + Line Feed | | \r | U+000D | Carriage Return | | \u000B | U+000B | Vertical Tab | | \u000C | U+000C | Form Feed | | \u0085 | U+0085 | Next Line | | \u2028 | U+2028 | Line Separator | | \u2029 | U+2029 | Paragraph Separator |

Byte Order Marks

Also referred to as BOM signatures, these are the bytes at the beginning of a file that indicating the encoding in which the file is written. Currently, linez only reads BOMs to detect the encoding and does not take into account the contents of the file.

Supported BOMs

  • utf-8-bom
  • utf-16le
  • utf-16be

Unsupported BOMs

  • utf-32le
  • utf-32be

If linez detects an unsupported BOM, an error will be thrown, indicating that decoding the detected charset is not supported.

Default decoding

By default, the document will attempt to be decoded as utf8. This is the default behavior of the Node API's conversion from buffers into strings.

API

configure(options: IOptions)

Configures linez to use the supplied options. Currently, only the newlines property is available, where you can specify any number of newline character sequences.

linez.configure({
  newlines: ['\n', '\r\n', '\r', '\u000B']
});

resetConfiguration()

Resets the configuration to the default settings, using /\r?\n/g as the newlines regular expression.

Document

constructor(public lines: Line[]);

Calling the toString() method converts the document's lines into a string, discarding information about line numbers and offsets.

Line

interface Line {
  offset: number;
  number: number;
  text: string;
  ending: string;
}

Options

interface Options {
  newlines?: string[];
}

linez(file: string|Buffer): Document

Parses text into a Document.

The specs show some great usage examples.

var lines = linez('foo\nbar\nbaz').lines;
lines[1].offset; // 4
lines[1].number; // 2
lines[1].text; // bar
lines[1].ending; // \n

Note: You can also pass-in a Buffer.

License

Released under the MIT license.