This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(occurrences): add occurrences function with tests (#187)
Making my first pull request: add occurrences function.
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Original Source: https://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string | ||
* | ||
* Function that count occurrences of a substring in a string | ||
* | ||
* @param {String} string The string | ||
* @param {String} subString The sub string to search for | ||
* @param {Boolean} [allowOverlapping] Optional. (Default:false) | ||
*/ | ||
|
||
function occurrences(string, subString, allowOverlapping) { | ||
string += '' | ||
subString += '' | ||
if (subString.length <= 0) { | ||
return (string.length + 1) | ||
} | ||
|
||
let n = 0 | ||
let pos = 0 | ||
const step = allowOverlapping ? 1 : subString.length | ||
let flag = true | ||
|
||
while (flag) { | ||
pos = string.indexOf(subString, pos) | ||
if (pos >= 0) { | ||
++n | ||
pos += step | ||
} else { | ||
flag = false | ||
} | ||
} | ||
return n | ||
} | ||
|
||
export default occurrences |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import test from 'ava' | ||
import { occurrences } from '../src' | ||
|
||
test('empty substring', t => { | ||
t.deepEqual(occurrences('', ''), 1) | ||
t.deepEqual(occurrences('abc', ''), 4) | ||
}) | ||
|
||
test('single occurences', t => { | ||
t.deepEqual(occurrences('foo', 'foo'), 1) | ||
t.deepEqual(occurrences('blahfooblah', 'foo'), 1) | ||
t.deepEqual(occurrences('foo', 'f'), 1) | ||
}) | ||
|
||
test('multiple occurrences', t => { | ||
t.deepEqual(occurrences('foofoofoofoo', 'foo'), 4) | ||
t.deepEqual(occurrences('foofoofoofoo', 'foofoo'), 2) | ||
t.deepEqual(occurrences('blafooblahfooblah', 'foo'), 2) | ||
t.deepEqual(occurrences('foofoofooooofo', 'foo'), 3) | ||
}) | ||
|
||
test('no occurrences', t => { | ||
t.deepEqual(occurrences('', 'foo'), 0) | ||
t.deepEqual(occurrences('abc', 'foo'), 0) | ||
t.deepEqual(occurrences('boo', 'foo'), 0) | ||
}) | ||
|
||
test('overlap', t => { | ||
t.deepEqual(occurrences('', '', true), 1) | ||
t.deepEqual(occurrences('abc', '', true), 4) | ||
t.deepEqual(occurrences('foofoofoofoo', 'foofoo', true), 3) | ||
t.deepEqual(occurrences('blafooblahfooblah', 'foo', true), 2) | ||
t.deepEqual(occurrences('foofoofooooofo', 'foo', true), 3) | ||
}) | ||
|
||
test('overlap no occurrences', t => { | ||
t.deepEqual(occurrences('', 'foo', true), 0); | ||
t.deepEqual(occurrences('abc', 'foo', true), 0); | ||
t.deepEqual(occurrences('boo', 'foo', true), 0); | ||
t.deepEqual(occurrences('fooofooofooofoo', 'foofoo', true), 0); | ||
t.deepEqual(occurrences('blafobooblahfoboblah', 'foo', true), 0); | ||
t.deepEqual(occurrences('fofofofaooooofo', 'foo', true), 0); | ||
}) |