-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
61 lines (51 loc) · 996 Bytes
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
function replaceAt(word, index, character) {
if (!word) {
return word;
}
return word.slice(0, index) + character + word.slice(index + 1, word.length);
}
function hasPrefix(word, prefix) {
if (!word) {
return false;
}
if (prefix instanceof RegExp) {
if (word.match(prefix)) {
return true;
}
} else {
if (word.indexOf(prefix) === 0) {
return true;
}
}
return false;
}
function hasSuffix(word, suffix) {
if (!word) {
return false;
}
if (word.lastIndexOf(suffix) == word.length - suffix.length) {
return true;
}
return false;
}
function countSyllables(word) {
if (!word) {
return 0;
}
let match = word
.toString() //For handling regex
.replace('(?![aeiou])', '') //For regex. Removes negativelookahead for vowels
.match(/[aeiou]/g);
if (match) {
return match.length;
} else {
return 0;
}
}
module.exports = {
replaceAt,
hasPrefix,
hasSuffix,
countSyllables
};