4.0.1 • Published 2 years ago

@leismore/plaintext v4.0.1

Weekly downloads
1
License
AGPL-3.0-only
Repository
github
Last release
2 years ago

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

4.0.1

2 years ago

4.0.0

3 years ago

3.0.0

3 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago