-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyphenex.js
96 lines (83 loc) · 1.92 KB
/
hyphenex.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// usable functions for functions to work with.
function countWords(str) {
return str.split("-").length;
}
function countWordsDynamic(str, separator) {
return str.split(separator).length;
}
function checkHyphenated(string) {
let wordno = countWords(string);
let hyphencount = (string.split("-").length - 1);
let wordnos = wordno - 1;
if (hyphencount == wordnos) {
return true;
}
else {
return false;
}
}
function checkDynamicHyphenated(string, separator) {
let wordno = countWordsDynamic(string, separator);
let separatorCount = (string.split(separator).length - 1);
let wordnos = wordno - 1;
if (separatorCount == wordnos ) {
return true;
}
else {
return false;
}
}
// User usable functions.
function hyphenate(string) {
for (let i = 0; i < string.length; i++) {
if (string[i] == ' ') {
string = string.replace(' ', '-');
}
}
return string;
}
function deHyphenate(string) {
let check = checkHyphenated(string);
if (check == true) {
for (let i = 0; i < string.length; i++) {
if (string[i] == '-') {
string = string.replace('-', ' ');
}
}
return string;
}
else {
console.error("String is not hyphenated");
throw new Error;
}
}
function hyphenateDynamically(string, separator) {
for (let i = 0; i < string.length; i++) {
if (string[i] == ' ') {
string = string.replace(' ', separator);
}
}
return string;
}
function deHyphenateDynamically(string, separator) {
let check = checkDynamicHyphenated(string, separator);
if (check == true) {
for (let i = 0; i < string.length; i++) {
if (string[i] == separator) {
string = string.replace(separator, ' ');
}
}
return string;
}
else {
console.error("String is not hyphenated");
throw new Error;
}
}
// exporting functions.
module.exports = {
hyphenate,
deHyphenate,
hyphenateDynamically,
deHyphenateDynamically
}