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

es-string-algorithm

v1.1.0

Published

port from C++STL std::basic_string

Downloads

1

Readme

es-string-algorithm

CircleCI Greenkeeper badge

NPM

Introduction

C++ STL provide find_first_of / find_first_not_of / find_last_of / find_last_not_of / find / rfind member function.

However, JavaScript String class does not provide such method. So, this package provide these functions.

Reference

Every function manipulate a string as if that is UTF-32 encoded.

To describe simply, we use two function like below:

  • at(s: string, n: number) => string: Returns nth charactor.

findFirstOf

export declare const findFirstOf: (target: string, key: string, pos?: number, n?: number | undefined) => number;

Determines the lowest position xpos, if possible, such that both of the following conditions hold:

  1. pos <= xpos and xpos < std.size(target)
  2. k.includes(at(target, xpos)) (When n is undefined (omitted), k is equal to key. Otherwise, k is equal to std.substr(key, 0, n))

Parameters

  • target: search target string
  • key: string identifying characters to search for
  • pos = 0: position at which to begin searching
  • n(opt): length of character string identifying characters to search for

Return value

xpos if the function can determine such a value for xpos. Otherwise, returns -1.

Example

const std = require('es-string-algorithm');
const s = 'Hello, world. Welcome to C++ world.';
const str = 'world';
console.log(std.findFirstOf(s, str, 14));// => 16
console.log(std.findFirstOf(s, ',.+', 14));// => 26
console.log(std.findFirstOf('arikitari na sekai', 'a', 1));// => 6
console.log(std.findFirstOf('🍣🍺', '🍺'));// => 1

findLastof

export declare const findLastof: (target: string, key: string, pos?: number, n?: number | undefined) => number;

Determines the highest position xpos, if possible, such that both of the following conditions hold:

  1. pos <= xpos and xpos < std.size(target)
  2. k.includes(at(target, xpos)) (When n is undefined (omitted), k is equal to key. Otherwise, k is equal to std.substr(key, 0, n))

Parameters

  • target: search target string
  • key: string identifying characters to search for
  • pos = -1: position at which the search is to finish. -1 is equal to the length of search target string
  • n(opt): length of character string identifying characters to search for

Return value

xpos if the function can determine such a value for xpos. Otherwise, returns -1.

Example

const std = require('es-string-algorithm');
const s = 'Hello, world. Welcome to C++ world.';
const str = 'world';
console.log(std.findLastof(s, str, 25));// => 23
console.log(std.findLastof(s, ',.+', 5));// => 5
console.log(std.findLastof('arikitari na sekai', 'a', 1));// => 0
console.log(std.findLastof('🍣🍺', '🍺'));// => 1

findFirstNotOf

export declare const findFirstNotOf: (target: string, key: string, pos?: number, n?: number | undefined) => number;

Determines the lowest position xpos, if possible, such that both of the following conditions hold:

  1. pos <= xpos and xpos < std.size(target)
  2. !k.includes(at(target, xpos)) (When n is undefined (omitted), k is equal to key. Otherwise, k is equal to std.substr(key, 0, n))

Parameters

  • target: search target string
  • key: string identifying characters to search for
  • pos = 0: position at which to begin searching
  • n(opt): length of character string identifying characters to search for

Return value

xpos if the function can determine such a value for xpos. Otherwise, returns -1.

Example

const std = require('es-string-algorithm');
const s = 'Hello, world. Welcome to C++ world.';
const str = 'world';
console.log(std.findFirstNotOf(s, str, 2));// => 5
console.log(std.findFirstNotOf(s, 'worlde,. ', 1));// => 14
console.log(std.findFirstNotOf('arikitari na sekai datta', 't', 21));// => 23
console.log(std.findFirstNotOf('🍣🍺', '🍣'));// => 1

findLastNotof

export declare const findLastNotof: (target: string, key: string, pos?: number, n?: number | undefined) => number;

Determines the highest position xpos, if possible, such that both of the following conditions hold:

  1. pos <= xpos and xpos < std.size(target)
  2. k.includes(at(target, xpos)) (When n is undefined (omitted), k is equal to key. Otherwise, k is equal to std.substr(key, 0, n))

Parameters

  • target: search target string
  • key: string identifying characters to search for
  • pos = -1: position at which the search is to finish. -1 is equal to the length of search target string
  • n(opt): length of character string identifying characters to search for

Return value

xpos if the function can determine such a value for xpos. Otherwise, returns -1.

Example

const std = require('es-string-algorithm');
const s = 'Hello, world. Welcome to C++ world.';
const str = 'world';
console.log(std.findLastNotof(s, str, 11));// => 6
console.log(std.findLastNotof(s, 'Welcome to C++ world.', 1));// => 5
console.log(std.findLastNotof('arikitari na sekai', 'a', 0));// => -1
console.log(std.findLastNotof('🍣🍺', '🍺'));// => 0

find

export declare const find: (target: string, key: string, pos = 0, n?: number) => number;

Determines the lowest position xpos, if possible, such that both of the following conditions hold:

  1. xpos <= pos and xpos + n <= std.size(target)
  2. k.includes(at(target, xpos)) (When n is undefined (omitted), k is equal to key. Otherwise, k is equal to std.substr(key, 0, n))

Parameters

  • target: search target string
  • key: string identifying characters to search for
  • pos = 0: position at which to begin searching
  • n(opt): length of character string identifying characters to search for

Return value

xpos if the function can determine such a value for xpos. Otherwise, returns -1.

Example

const std = require('es-string-algorithm');
const s = 'Hello, world. Welcome to C++ world.';
const str = 'world';
console.log(std.find(s, findWord));// => 7
console.log(std.find(s, findWord, 12));// => 29
console.log(std.find(s, findWord, 33));// => -1
console.log(std.find('🍣🍺📧💾', '🍺📧'));// => 1

rfind

export declare const rfind: (target: string, key: string, pos = -1, n?: number) => number;

Determines the highest position xpos, if possible, such that both of the following conditions hold:

  1. xpos <= pos and xpos + n <= std.size(target)
  2. k.includes(at(target, xpos)) (When n is undefined (omitted), k is equal to key. Otherwise, k is equal to std.substr(key, 0, n))

Parameters

  • target: search target string
  • key: string identifying characters to search for
  • pos = -1: position at which to begin searching
  • n(opt): length of character string identifying characters to search for

Return value

xpos if the function can determine such a value for xpos. Otherwise, returns -1.

Example

const std = require('es-string-algorithm');
const s = 'Hello, world. Welcome to C++ world.';
const str = 'world';
console.log(std.rfind(s, findWord, 29));// => 29
console.log(std.rfind(s, findWord, 28));// => 7
console.log(std.rfind(s, 'W', 29));// => 14
console.log(std.rfind('🍣🍺📧💾', '🍺📧'));// => 1

substr

export declare const substr: (s: string, pos?: number, n?: number | undefined) => string;

Create part of the s

Parameters

  • s: string
  • pos = 0: copy start position
  • n(opt): copy length

Return value

part of the s in range of [pos...rlast] (rlast is the smaller of pos + n and std.size(s))

Exception

Throws RangeError when pos or n is negative or pos > std.size(s)

size

export declare const size: (s: string) => number;

A count of the number of codepoint currently in the string.

Parameters

  • s: string

Complexity

O(n)