core-functions
Advanced tools
Comparing version 2.0.3 to 2.0.4
{ | ||
"name": "core-functions", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including strings, booleans, Promises, base 64, Arrays, Objects, standard AppErrors, etc.", | ||
@@ -5,0 +5,0 @@ "author": "Byron du Preez", |
@@ -1,2 +0,2 @@ | ||
# core-functions v2.0.3 | ||
# core-functions v2.0.4 | ||
@@ -106,2 +106,6 @@ Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including | ||
### 2.0.4 | ||
- Change to `strings.js`: | ||
- Added an `nthIndexOf` function that finds index of nth occurrence of a search value in a string | ||
### 2.0.3 | ||
@@ -108,0 +112,0 @@ - Change to `promises.js`: |
@@ -28,2 +28,3 @@ 'use strict'; | ||
stringify: stringify, | ||
nthIndexOf: nthIndexOf | ||
}; | ||
@@ -95,1 +96,25 @@ | ||
} | ||
/** | ||
* Returns the index of the nth occurrence of the given searchValue in the given string (if any); otherwise returns -1. | ||
* @param {string} s - the string to search | ||
* @param {string} searchValue - the value to search for in the string | ||
* @param {number} nth - the number of occurrences to traverse through to find the nth occurrence | ||
* @returns {number} the index of the nth occurrence of the given searchValue in the given string (if any); otherwise returns -1 | ||
*/ | ||
function nthIndexOf(s, searchValue, nth) { | ||
if (nth < 1) return -1; | ||
let index = 0; | ||
for (let i = 0; i < nth; ++i) { | ||
if (i > 0) { | ||
index += searchValue.length; | ||
} | ||
index = s.indexOf(searchValue, index); | ||
if (index === -1) { | ||
break; | ||
} | ||
} | ||
return index; | ||
} |
{ | ||
"name": "core-functions-tests", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"author": "Byron du Preez", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -526,1 +526,35 @@ 'use strict'; | ||
}); | ||
test('nthIndexOf', t => { | ||
const s = 'arn:aws:dynamodb:us-east-1:111111111111:table/test/stream/2020-10-10T08:18:22.385'; | ||
// 0----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 | ||
t.equal(Strings.nthIndexOf(s, ':', -1), -1, `nthIndexOf(':', -1) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, ':', 0), -1, `nthIndexOf(':', 0) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, ':', 1), 3, `nthIndexOf(':', 1) must be 3`); | ||
t.equal(Strings.nthIndexOf(s, ':', 2), 7, `nthIndexOf(':', 2) must be 7`); | ||
t.equal(Strings.nthIndexOf(s, ':', 3), 16, `nthIndexOf(':', 3) must be 16`); | ||
t.equal(Strings.nthIndexOf(s, ':', 4), 26, `nthIndexOf(':', 4) must be 26`); | ||
t.equal(Strings.nthIndexOf(s, ':', 5), 39, `nthIndexOf(':', 5) must be 39`); | ||
t.equal(Strings.nthIndexOf(s, ':', 6), 71, `nthIndexOf(':', 6) must be 71`); | ||
t.equal(Strings.nthIndexOf(s, ':', 7), 74, `nthIndexOf(':', 7) must be 74`); | ||
t.equal(Strings.nthIndexOf(s, ':', 8), -1, `nthIndexOf(':', 8) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, ':', 9), -1, `nthIndexOf(':', 9) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, 'table', 0), -1, `nthIndexOf('table', 0) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, 'table', 1), 40, `nthIndexOf('table', 1) must be 40`); | ||
t.equal(Strings.nthIndexOf(s, 'table', 2), -1, `nthIndexOf('table', 2) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, '/', 0), -1, `nthIndexOf('/', 0) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, '/', 1), 45, `nthIndexOf('/', 1) must be 45`); | ||
t.equal(Strings.nthIndexOf(s, '/', 2), 50, `nthIndexOf('/', 2) must be 50`); | ||
t.equal(Strings.nthIndexOf(s, '/', 3), 57, `nthIndexOf('/', 3) must be 57`); | ||
t.equal(Strings.nthIndexOf(s, '/', 4), -1, `nthIndexOf('/', 4) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, '', -1), -1, `nthIndexOf('', -1) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, '', 0), -1, `nthIndexOf('', 0) must be -1`); | ||
t.equal(Strings.nthIndexOf(s, '', 1), 0, `nthIndexOf('', 1) must be 0`); | ||
t.equal(Strings.nthIndexOf(s, '', 2), 0, `nthIndexOf('', 2) must be 0`); | ||
t.equal(Strings.nthIndexOf(s, '', 100), 0, `nthIndexOf('', 100) must be 0`); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
168032
3352
150