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

Commit e5d1a83

Browse files
ChikelKent C. Dodds
authored and
Kent C. Dodds
committed
feat: Add startsWith function (#50)
Closes #49
1 parent 739ac80 commit e5d1a83

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import isArray from './is-array'
1111
import validateEmail from './validateEmail'
1212
import hex2rgb from './hex2rgb'
1313
import isNullOrWhitespace from './is-null-or-whitespace'
14+
import startsWith from './startsWith'
1415

1516
export {
1617
flatten,
@@ -26,4 +27,5 @@ export {
2627
validateEmail,
2728
hex2rgb,
2829
isNullOrWhitespace,
30+
startsWith,
2931
}

src/startsWith.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default startsWith
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/646631/5954939
5+
*
6+
* This method will return a bollean indicating whether a string starts with a given input.
7+
*
8+
* @param {String} str - The string to validate against
9+
* @param {String} head - The input to match with str substring
10+
* @return {Bollean} - True if 'str' starts with 'head', otherwise false
11+
*/
12+
function startsWith(str, head) {
13+
const tmp = str.substring(0, head.length)
14+
const res = (tmp === head)
15+
return res
16+
}

test/startsWith.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava'
2+
import {startsWith} from '../src'
3+
4+
test('check correct input', t => {
5+
const string = 'First PR'
6+
const head = 'Fir'
7+
const result = startsWith(string, head)
8+
t.true(result)
9+
})
10+
11+
test('check wrong input', t => {
12+
const string = 'First PR'
13+
const head = 'Foo'
14+
const result = startsWith(string, head)
15+
t.false(result)
16+
})

0 commit comments

Comments
 (0)