1.0.17 • Published 3 years ago

z-str v1.0.17

Weekly downloads
32
License
ISC
Repository
github
Last release
3 years ago

z-str

A lib to easily manipulate strings

Installation

  npm i z-str

Usage

Javascript

  const ZStr = require('z-str');

  const myString = 'aBcdefg dgf dxf';
  const str = new ZStr(myString, {caseSensitive: false});

  console.log(str.from('a').till('e').toString()); //Bcd
  console.log(str.fromLast('d').tillLast('f').toString()); //x
  console.log(str.startsWith('abc')); //true
  console.log(str.search('b').next());
  console.log(str.batch().from('d').till('f').split().asArray()); //['e', 'g','x']

Typescript

  import ZStr from 'z-str';

  const str = new ZStr('aBcdefg', {caseSensitive: false});

  console.log(str.from('a').till('e').toString()); //Bcd
  console.log(str.fromLast('d').tillLast('f').toString()); //x
  console.log(str.startsWith('abc')); //true
  console.log(str.search('b').next());
  console.log(str.batch().from('d').till('f').split().asArray()); //['e', 'g','x']

Summary

Constructor isEmpty sub equals substr substring containsAny containsAll findFirst findLast from fromLast till tillLast startsWith endsWith

Constructor

  new ZStr(string, options);

Default values of options:

  {
    caseSensitive: true,
    inclusive: false,
    ignoreNotFoundPatterns: false
  }

Is empty

  console.log(new ZStr('').isEmpty()); //true
  console.log(new ZStr('abc').isEmpty()); //false

Sub

Create a new ZStr with other options:

str.sub(options)
  const str = new ZStr('Abc');
  const sub = str.sub({caseSensitive: false});

  console.log(str.equals('abc')); //false
  console.log(sub.equals('abc')); //true

Equals

Compare two strings

str.equals(otherString: string | ZStr)
  const a = new ZStr('a');
  const b = new ZStr('A', {caseSensitive: false});

  console.log(a.equals('a')); //true
  console.log(a.equals('A')); //false

  console.log(b.equals('a')); //true
  console.log(b.equals('A')); //true
  
  console.log(a.equals(b)); //false
  console.log(b.equals(a)); //true

Search

str.search(patterns: string|string[], patternsToIgnore?: string[])

Example 1

  const source = 'Name: Edward; Age:: 15';
  const search = new ZStr(source).search(':');
  const results = search.list();
  console.log(results);

Output

  [
    {
      start: 4,
      end: 5,
      pattern: ':',
      valid: true,
      getFoundPattern: [Function]
    },
    {
      start: 17,
      end: 18,
      pattern: ':',
      valid: true,
      getFoundPattern: [Function]
    },
    {
      start: 18,
      end: 19,
      pattern: ':',
      valid: true,
      getFoundPattern: [Function]
    }
  ]

Example 2

  const source = 'Name: Edward; Age:: 15';
  const search = new ZStr(source).search([':'], ['::']);
  const results = search.list();
  console.log(results);

Output

  [
    {
      start: 4,
      end: 5,
      pattern: ':',
      valid: true,
      getFoundPattern: [Function]
    }
  ]

Example 3

  const source = 'Name: Edward; Age:: 15';
  const search = new ZStr(source).search([':'], ['::']);
  while (search.hasNext()){
    console.log(search.next().start);
  }

Output

  4
  17
  18

Example 4 - com reverseDirection()

  const source = 'Name: Edward; Age:: 15';
  const search = new ZStr(source).search([':'], ['::']);
  search.reverseDirection();
  while (search.hasNext()){
    console.log(search.next().start);
  }

Output

  18
  17
  4

Substr

str.substr(start: number, length?: number)
  const str = new ZStr('abcdef');
  console.log(str.substr(3, 1).toString()); //d

Substring

str.substring(start: number, end?: number)
  const str = new ZStr('abcdef');
  console.log(str.substring(3, 5)).toString(); //de

Contains any

str.containsAny(patterns: string | string[], patternsToIgnore: string[])
  const str = new ZStr('abc');

  coonsole.log(str.containsAny(['e','a'])); //true
  coonsole.log(str.containsAny(['e','f'])); //false

Contains all

str.containsAll(patterns: string | string[], patternsToIgnore: string[])
  const str = new ZStr('abc');

  coonsole.log(str.containsAll(['b','a'])); //true
  coonsole.log(str.containsAll(['a','e'])); //false

Find first

str.findFirst(patterns: string|string[], patternsToIgnore: string[])
  const str = new ZStr('abc:def:ghi');

  coonsole.log(str.findFirst([':'])); 

Output:

  {
    start: 3,
    end: 4,
    pattern: ':',
    valid: true,
    getFoundPattern: [Function]
  }

Don't found

Example:

  const str = new ZStr('abc:def:ghi');

  coonsole.log(str.findFirst(['::'])); 

Output:

  {
    start: 0,
    end: 0,
    pattern: '',
    valid: false,
    getFoundPattern: [Function: getFoundPattern]
  }

Find last

str.findLast(patterns: string|string[], patternsToIgnore: string[])
  const str = new ZStr('abc:def:ghi');

  coonsole.log(str.findLast([':'])); 

Output:

  {
    start: 7,
    end: 8,
    pattern: ':',
    valid: true,
    getFoundPattern: [Function]
  }

From

str.from(patterns: string|string[], patternsToIgnore: string[])

Example 1

  const str = new ZStr('abc->def<-ghi');

  coonsole.log(str.from('->').toString())); //def<-ghi

Example 2

  const str = new ZStr('abc->def<-ghi', {inclusive: true});

  coonsole.log(str.from('->').toString())); //->def<-ghi

From last

str.fromLast(patterns: string|string[], patternsToIgnore: string[])
  const str = new ZStr('abc->->def<-ghi');

  coonsole.log(str.fromLast('->').toString())); //def<-ghi

Till

str.till(patterns: string|string[], patternsToIgnore: string[])

Example 1

  const str = new ZStr('abc->def<-<-ghi');

  coonsole.log(str.till('<-').toString())); //abc->def

Example 2

  const str = new ZStr('abc->def<-<-ghi', {inclusive: true});

  coonsole.log(str.till('<-').toString())); //abc->def<-

Till last

tr.tillLast(patterns: string|string[], patternsToIgnore: string[])
  const str = new ZStr('abc->def<-<-ghi', {inclusive: true});

  coonsole.log(str.tillLast('<-').toString())); //abc->def<-<-

Starts with

tr.startsWith(patterns: string|string[], patternsToIgnore: string[])
  const str = new ZStr('aBcdEf');

  coonsole.log(str.startsWith('aBc')); //true
  coonsole.log(str.startsWith(['eFg','aBc'])); //true
  coonsole.log(str.startsWith('abc')); //false
  coonsole.log(str.sub({caseSensitive: false}).startsWith('abc')); //true

Ends with

str.endsWith(patterns: string|string[], patternsToIgnore: string[])
  const str = new ZStr('aBcdEf');

  coonsole.log(str.endsWith('Ef')); //true
  coonsole.log(str.endsWith(['zzz','dEf'])); //true
  coonsole.log(str.endsWith('def')); //false
  coonsole.log(str.sub({caseSensitive: false}).endsWith('def')); //true
1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago