forked from vindecodex/counting-str
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
22 lines (21 loc) · 786 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
interface CountCharactersOption {
withSpaces: boolean;
}
export function countWords(inputString: string) {
if (!inputString) return 0;
let splitWords = inputString.split(" ");
return splitWords.length;
}
export function countSpaces(inputString: string) {
if (!inputString) return 0;
let splitWords = inputString.split(" ");
return splitWords.length - 1;
}
export function countCharacters(inputString: string, options: CountCharactersOption) {
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;
}