Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

es-string-algorithm

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

es-string-algorithm - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

.circleci/config.yml

47

dist/index.d.ts
/**
* Determines the lowest position `xpos`, if possible, such that both of the following conditions hold:
*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -17,3 +17,3 @@ *

*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -31,3 +31,3 @@ *

*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`!k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -45,3 +45,3 @@ *

*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -56,1 +56,40 @@ *

export declare const findLastNotof: (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 + n <= std.size(target)`;
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)
* @param target search target string
* @param key string identifying characters to search for
* @param pos position at which to begin searching
* @param n length of character string identifying characters to search for
* @returns `xpos` if the function can determine such a value for `xpos`. Otherwise, returns `-1`.
*/
export declare const find: (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. `xpos <= pos` and `xpos + n <= std.size(target)`
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)
* @param target search target string
* @param key string identifying characters to search for
* @param pos position at which to begin searching
* @param n length of character string identifying characters to search for
* @returns `xpos` if the function can determine such a value for `xpos`. Otherwise, returns `-1`.
*/
export declare const rfind: (target: string, key: string, pos?: number, n?: number | undefined) => number;
/**
* Create part of the `s`
* @param s string
* @param pos copy start position
* @param n copy length
* @returns part of the `s` in range of `[pos...rlast]` (`rlast` is the smaller of `pos + n` and `std.size(s)`)
* @throws {RangeError} When `pos` or `n` is negative or `pos` > `std.size(s)`
*/
export declare const substr: (s: string, pos?: number, n?: number | undefined) => string;
/**
* A count of the number of codepoint currently in the string.
*
* Complexity: O(n)
* @param s string
*/
export declare const size: (s: string) => number;

167

dist/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const includes = (target, searchString, n) => {
if (typeof n !== 'number')
return target.includes(searchString);
let i = 0;
for (const t of target) {
if (n <= i)
break;
if (t === searchString)
return true;
++i;
}
return false;
};
const findFirst = (target, pos, pred) => {
let i = 0;
for (const t of target) {
if (pos <= i && pred(t))
return i;
++i;
}
return -1;
};
const impl = require("./impl");
/**
* Determines the lowest position `xpos`, if possible, such that both of the following conditions hold:
*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -37,15 +16,7 @@ *

*/
exports.findFirstOf = (target, key, pos = 0, n) => findFirst(target, pos, t => includes(key, t, n));
const findLast = (target, pos, pred) => {
const targetArr = Array.from(target);
// if (targetArr.length <= pos) return -1;
targetArr.reverse();
pos = -1 === pos || targetArr.length <= pos ? 0 : targetArr.length - 1 - pos;
const re = findFirst(targetArr, pos, pred);
return -1 === re ? -1 : targetArr.length - 1 - re;
};
exports.findFirstOf = (target, key, pos = 0, n) => impl.findFirst(target, pos, t => impl.includes(key, t, n));
/**
* Determines the highest position `xpos`, if possible, such that both of the following conditions hold:
*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -59,7 +30,7 @@ *

*/
exports.findLastof = (target, key, pos = -1, n) => findLast(target, pos, t => includes(key, t, n));
exports.findLastof = (target, key, pos = -1, n) => impl.findLast(target, pos, t => impl.includes(key, t, n));
/**
* Determines the lowest position `xpos`, if possible, such that both of the following conditions hold:
*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`!k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -73,7 +44,7 @@ *

*/
exports.findFirstNotOf = (target, key, pos = 0, n) => findFirst(target, pos, t => !includes(key, t, n));
exports.findFirstNotOf = (target, key, pos = 0, n) => impl.findFirst(target, pos, t => !impl.includes(key, t, n));
/**
* Determines the highest position `xpos`, if possible, such that both of the following conditions hold:
*
* 1. `pos <= xpos` and `xpos < size(target)`
* 1. `pos <= xpos` and `xpos < std.size(target)`
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -87,2 +58,122 @@ *

*/
exports.findLastNotof = (target, key, pos = -1, n) => findLast(target, pos, t => !includes(key, t, n));
exports.findLastNotof = (target, key, pos = -1, n) => impl.findLast(target, pos, t => !impl.includes(key, t, n));
/**
* Determines the lowest position `xpos`, if possible, such that both of the following conditions hold:
* 1. `pos <= xpos` and `xpos + n <= std.size(target)`;
* 2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)
* @param target search target string
* @param key string identifying characters to search for
* @param pos position at which to begin searching
* @param n length of character string identifying characters to search for
* @returns `xpos` if the function can determine such a value for `xpos`. Otherwise, returns `-1`.
*/
exports.find = (target, key, pos = 0, n) => {
for (;; ++pos) {
let i = 0;
const it1 = target[Symbol.iterator]();
for (; i < pos; ++i) {
if (it1.next().done) {
return -1;
}
}
const it2 = key[Symbol.iterator]();
for (let j = 0;; ++j) {
const n1 = it1.next();
const n2 = it2.next();
if (n2.done || j === n) {
return i;
}
if (n1.done) {
return -1;
}
if (n1.value !== n2.value) {
break;
}
}
}
};
/**
* 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))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)
* @param target search target string
* @param key string identifying characters to search for
* @param pos position at which to begin searching
* @param n length of character string identifying characters to search for
* @returns `xpos` if the function can determine such a value for `xpos`. Otherwise, returns `-1`.
*/
exports.rfind = (target, key, pos = -1, n) => {
const t = Array.from(target);
if (typeof n === 'number' && 0 === n)
return Math.min(pos, t.length);
for (let i = -1 === pos || t.length <= pos ? t.length - 1 : pos;; --i) {
if (-1 === i)
return -1;
const it2 = key[Symbol.iterator]();
for (let j = 0;; ++j) {
const n2 = it2.next();
if (n2.done || j === n) {
return i;
}
if (t.length <= i + j) {
break;
}
if (t[i + j] !== n2.value) {
break;
}
}
}
};
/**
* Create part of the `s`
* @param s string
* @param pos copy start position
* @param n copy length
* @returns part of the `s` in range of `[pos...rlast]` (`rlast` is the smaller of `pos + n` and `std.size(s)`)
* @throws {RangeError} When `pos` or `n` is negative or `pos` > `std.size(s)`
*/
exports.substr = (s, pos = 0, n) => {
if (pos < 0) {
throw new RangeError('std.substr: pos < 0');
}
if (typeof n === 'number' && n < 0) {
throw new RangeError('std.substr: n < 0');
}
let i = 0;
let l = 0;
let begin = 0;
for (const c of s) {
if (i === pos) {
if (typeof n === 'number' && 0 === n) {
return '';
}
begin = l;
}
else if (typeof n === 'number' && i === pos + n) {
return s.substring(begin, l);
}
l += c.length;
++i;
}
if (i < pos) {
throw new RangeError(`std.substr: pos (which is ${pos}) > std.size(s) (which is ${i})`);
}
if (0 === n) {
return '';
}
return s.substring(begin);
};
/**
* A count of the number of codepoint currently in the string.
*
* Complexity: O(n)
* @param s string
*/
exports.size = (s) => {
let i = 0;
for (const _ of s) {
++i;
}
return i;
};
{
"name": "es-string-algorithm",
"version": "1.0.2",
"version": "1.1.0",
"description": "port from C++STL std::basic_string",

@@ -10,4 +10,4 @@ "main": "dist/index.js",

"test": "jest --verbose",
"lint": "tslint --project .",
"lint:fix": "tslint --fix --project ."
"lint": "eslint --ext .ts .",
"lint:fix": "eslint --fix --ext .ts ."
},

@@ -28,9 +28,13 @@ "repository": {

"devDependencies": {
"@types/jest": "^24.0.13",
"jest": "^24.8.0",
"prettier": "^1.17.1",
"ts-jest": "^24.0.2",
"tslint": "^5.17.0",
"tslint-plugin-prettier": "^2.0.1",
"typescript": "^3.5.1"
"@types/jest": "^25.1.2",
"@typescript-eslint/eslint-plugin": "^2.19.2",
"@typescript-eslint/parser": "^2.19.2",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"jest": "^25.1.0",
"jest-junit": "^10.0.0",
"prettier": "^1.19.1",
"ts-jest": "^25.2.0",
"typescript": "^3.7.5"
},

@@ -49,4 +53,14 @@ "jest": {

"node"
],
"reporters": [
"default",
[
"jest-junit",
{
"outputDirectory": "results/junit",
"outputName": "js-test-results.xml"
}
]
]
}
}
# es-string-algorithm
[![Build Status](https://travis-ci.org/yumetodo/es-string-algorithm.svg?branch=master)](https://travis-ci.org/yumetodo/es-string-algorithm)
[![CircleCI](https://circleci.com/gh/yumetodo/es-string-algorithm.svg?style=svg)](https://circleci.com/gh/yumetodo/es-string-algorithm) [![Greenkeeper badge](https://badges.greenkeeper.io/yumetodo/es-string-algorithm.svg)](https://greenkeeper.io/)
[![Greenkeeper badge](https://badges.greenkeeper.io/yumetodo/es-string-algorithm.svg)](https://greenkeeper.io/)
[![NPM](https://nodei.co/npm/es-string-algorithm.png)](https://nodei.co/npm/es-string-algorithm/)
## Introduction
C++ STL provide `find_first_of` / `find_first_not_of` / `find_last_of` / `find_last_not_of` member function.
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.
(When you want `find` / `rfind` that C++ STL provide, please use [`indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf) / [`lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf))
## Reference

@@ -21,3 +19,2 @@

- `size(s: string) => number`: Returns the length of `s`.
- `at(s: string, n: number) => string`: Returns `n`th charactor.

@@ -33,4 +30,4 @@

1. `pos <= xpos` and `xpos < size(target)`
2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to [`key.substring(0, n)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring))
1. `pos <= xpos` and `xpos < std.size(target)`
2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -68,4 +65,4 @@ #### Parameters

1. `pos <= xpos` and `xpos < size(target)`
2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to [`key.substring(0, n)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring))
1. `pos <= xpos` and `xpos < std.size(target)`
2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -103,4 +100,4 @@ #### Parameters

1. `pos <= xpos` and `xpos < size(target)`
2. [`!k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to [`key.substring(0, n)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring))
1. `pos <= xpos` and `xpos < std.size(target)`
2. [`!k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -138,4 +135,4 @@ #### Parameters

1. `pos <= xpos` and `xpos < size(target)`
2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to [`key.substring(0, n)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring))
1. `pos <= xpos` and `xpos < std.size(target)`
2. [`k.includes(at(target, xpos))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (When `n` is `undefined` (omitted), `k` is equal to `key`. Otherwise, `k` is equal to `std.substr(key, 0, n)`)

@@ -164,1 +161,107 @@ #### Parameters

```
### find
```ts
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))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (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
```js
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
```ts
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))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) (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
```js
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
```ts
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
```ts
export declare const size: (s: string) => number;
```
A count of the number of codepoint currently in the string.
#### Parameters
- `s`: string
#### Complexity
`O(n)`
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc