This repository has been archived by the owner on Feb 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
51 lines (46 loc) · 1.45 KB
/
index.ts
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
interface CountCharactersOption {
withSpaces: boolean;
}
export function countWords(inputString: string) {
inputString += "";
if (!inputString) return 0;
let splitWords = inputString.split(" ");
return splitWords.length;
}
export function countSpaces(inputString: string) {
inputString += "";
if (!inputString) return 0;
let splitWords = inputString.split(" ");
return splitWords.length - 1;
}
export function countCharacters(inputString: string, options: CountCharactersOption = { withSpaces: true }) {
inputString += "";
if (!inputString) return 0;
let inputCharacterSize = inputString.length;
let inputSpaceCharacterSize = inputString.split(" ").length - 1;
let inputCharacterSizeWithoutSpaces = inputCharacterSize - inputSpaceCharacterSize;
if (options.withSpaces) return inputCharacterSize;
return inputCharacterSizeWithoutSpaces;
}
export function countVowels(inputString: string) {
inputString += "";
if (!inputString) return 0;
return (inputString.match(/[aeiou]/gi) || []).length;
}
export function countOccurences(inputString, stringToMatch) {
inputString += "";
stringToMatch += "";
if (!inputString || !stringToMatch) return 0;
if(stringToMatch.length <= 0)
return 0;
let countOfOccurences = 0;
let position = 0;
while(true) {
position = inputString.indexOf(stringToMatch, position);
if(position >= 0) {
++countOfOccurences;
position += 1;
} else break;
}
return countOfOccurences;
}