Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit f58b735

Browse files
lwd369Kent C. Dodds
authored andcommitted
feat(occurrences): add occurrences function with tests (#187)
Making my first pull request: add occurrences function.
1 parent 97551ae commit f58b735

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import tail from './tail'
6767
import makeObjectIterable from './makeObjectIterable'
6868
import fibonacciSum from './fibonacciSum'
6969
import lcm from './lcm'
70+
import occurrences from './occurrences'
7071

7172
export {
7273
reverseArrayInPlace,
@@ -138,4 +139,5 @@ export {
138139
makeObjectIterable,
139140
fibonacciSum,
140141
lcm,
142+
occurrences,
141143
}

src/occurrences.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Original Source: https://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string
3+
*
4+
* Function that count occurrences of a substring in a string
5+
*
6+
* @param {String} string The string
7+
* @param {String} subString The sub string to search for
8+
* @param {Boolean} [allowOverlapping] Optional. (Default:false)
9+
*/
10+
11+
function occurrences(string, subString, allowOverlapping) {
12+
string += ''
13+
subString += ''
14+
if (subString.length <= 0) {
15+
return (string.length + 1)
16+
}
17+
18+
let n = 0
19+
let pos = 0
20+
const step = allowOverlapping ? 1 : subString.length
21+
let flag = true
22+
23+
while (flag) {
24+
pos = string.indexOf(subString, pos)
25+
if (pos >= 0) {
26+
++n
27+
pos += step
28+
} else {
29+
flag = false
30+
}
31+
}
32+
return n
33+
}
34+
35+
export default occurrences

test/occurrences.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import test from 'ava'
2+
import { occurrences } from '../src'
3+
4+
test('empty substring', t => {
5+
t.deepEqual(occurrences('', ''), 1)
6+
t.deepEqual(occurrences('abc', ''), 4)
7+
})
8+
9+
test('single occurences', t => {
10+
t.deepEqual(occurrences('foo', 'foo'), 1)
11+
t.deepEqual(occurrences('blahfooblah', 'foo'), 1)
12+
t.deepEqual(occurrences('foo', 'f'), 1)
13+
})
14+
15+
test('multiple occurrences', t => {
16+
t.deepEqual(occurrences('foofoofoofoo', 'foo'), 4)
17+
t.deepEqual(occurrences('foofoofoofoo', 'foofoo'), 2)
18+
t.deepEqual(occurrences('blafooblahfooblah', 'foo'), 2)
19+
t.deepEqual(occurrences('foofoofooooofo', 'foo'), 3)
20+
})
21+
22+
test('no occurrences', t => {
23+
t.deepEqual(occurrences('', 'foo'), 0)
24+
t.deepEqual(occurrences('abc', 'foo'), 0)
25+
t.deepEqual(occurrences('boo', 'foo'), 0)
26+
})
27+
28+
test('overlap', t => {
29+
t.deepEqual(occurrences('', '', true), 1)
30+
t.deepEqual(occurrences('abc', '', true), 4)
31+
t.deepEqual(occurrences('foofoofoofoo', 'foofoo', true), 3)
32+
t.deepEqual(occurrences('blafooblahfooblah', 'foo', true), 2)
33+
t.deepEqual(occurrences('foofoofooooofo', 'foo', true), 3)
34+
})
35+
36+
test('overlap no occurrences', t => {
37+
t.deepEqual(occurrences('', 'foo', true), 0);
38+
t.deepEqual(occurrences('abc', 'foo', true), 0);
39+
t.deepEqual(occurrences('boo', 'foo', true), 0);
40+
t.deepEqual(occurrences('fooofooofooofoo', 'foofoo', true), 0);
41+
t.deepEqual(occurrences('blafobooblahfoboblah', 'foo', true), 0);
42+
t.deepEqual(occurrences('fofofofaooooofo', 'foo', true), 0);
43+
})

0 commit comments

Comments
 (0)