-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-functions.js
69 lines (66 loc) · 2.25 KB
/
string-functions.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
/*
─────────────────────────────────────────────────────────────────────────────
Author: @RockStarwind
Date: 2020-01-31
Repo: https://github.com/RockStarwind/js-functions
License: MIT
─────────────────────────────────────────────────────────────────────────────
*/
/*
# isLowerCase()
How to use:
"string".isLowerCase();
If result is true: All alphabetical characters in the string are in lowercase.
If result is false: All alphabetical characters in the string aren't in lowercase.
If result is null: There are no alphabetical characters in the string.
*/
String.prototype.isLowerCase = function() {
'use strict';
if ((/([a-zA-Z\u00BF-\u1FFF\u2C00-\uD7FF])/i).test(this)) {
return this === this.toLowerCase()
} else {
return null
}
}
/*
# isUpperCase()
How to use:
"string".isUpperCase();
If result is true: All alphabetical characters in the string are in uppercase.
If result is false: All alphabetical characters in the string aren't in uppercase.
If result is null: There are no alphabetical characters in the string.
*/
String.prototype.isUpperCase = function() {
'use strict';
if ((/([a-zA-Z\u00BF-\u1FFF\u2C00-\uD7FF])/i).test(this)) {
return this === this.toUpperCase()
} else {
return null
}
}
/*
# toAltCase()
How to use:
"string".toAltCase(lowercase);
* lowercase options:
* true: Start altcasing with lowercase letters
* false: Start altcasing with uppercase letters
* (blank): First alphabetical character decides altcasing
*/
String.prototype.toAltCase = function(lowercase) {
'use strict';
if (typeof(lowercase) !== "undefined") {
lowercase = (["true", true].includes(lowercase));
}
var str = this.split("");
for (var i = 0; i < str.length; i++) {
if ((/[a-z]/i).test(str[i])) {
if (typeof(lowercase) === "undefined") {
lowercase = (str[i]).isLowerCase();
}
str[i] = (lowercase) ? str[i].toLowerCase() : str[i].toUpperCase()
lowercase = !lowercase;
}
}
return str.join("");
}