@leismore/plaintext
TypeScript icon, indicating that this package has built-in type declarations

4.0.1 • Public • Published

PlainText

A NodeJS package: PlainText class.

Donation

Buy me a coffee via PayPal Donation

Motivation

Provide a general purpose NodeJS class for handling plain text. It provides:

  1. Line-breaks unification
  2. Combining multiple-line-text into a single-line-text.

Installation

npm install @leismore/plaintext

Test

npm test

Example

import {PlainText} from '@leismore/plaintext';
let text = `First line \r\n Second Line \n More lines...`;
text = new PlainText(text, '\r\n');
console.log(String(text));

Class Structure

class PlainText
{
  public text:PlainText_Text = null;             // Array: one line per element
  public lb:PlainText_LB     = PlainText_canoLB; // Line-break

  /**
   * @param      {string}        text
   * @param      {PlainText_LB}  [lb=PlainText_canoLB] - Line-break
   * @exception  {Error}         - not_string
   */
  public constructor(text:string, lb:PlainText_LB=PlainText_canoLB)

  public toString():string

  /**
   * Unify line-breaks
   * @param      {string}        text
   * @param      {PlainText_LB}  [lb=PlainText_canoLB] - Line-break
   * @returns    {string | null} - Empty string will be converted to null
   * @exception  {Error}         - not_string
   */
  public static unifyLB(text:string, lb:PlainText_LB=PlainText_canoLB): string|null

  /**
   * Remove line breaks
   * @param      {string} text
   * @param      {string} [rep=PlainText_SP2] - Replacement for line-breaks
   * @returns    {string | null}              - Empty string converted to null
   * @exception  {Error}                      - not_string | invalid_replacement
   */
  public static removeLB(text:string, rep:string=PlainText_SP2): string|null
}

type PlainText_Text = ( string[] | null );
type PlainText_LB   = ( '\r' | '\n' | '\r\n' | '\n\r' );

const PlainText_canoLB  = '\r\n';
const PlainText_xmlLB   = '\n';
const PlainText_SP2     = '\x20\x20';

License

GNU Affero General Public License v3.0

Authors

Package Sidebar

Install

npm i @leismore/plaintext

Weekly Downloads

0

Version

4.0.1

License

AGPL-3.0-only

Unpacked Size

43.5 kB

Total Files

13

Last publish

Collaborators

  • kyle.chine.leismore