-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (127 loc) · 4.18 KB
/
index.js
File metadata and controls
142 lines (127 loc) · 4.18 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @fileoverview String transform module from Glize library.
*
* @see https://google.github.io/styleguide/javascriptguide.xml
* @see https://developers.google.com/closure/compiler/docs/js-for-compiler
* @see https://github.com/Datamart/Glize
* @module string-transform
*/
import { uint32 } from 'uint';
/**
* Transforms the first character of each word to uppercase; other
* characters are unaffected.
* @param {string} str The string to be transformed.
* @return {string} Returns a transformed string.
* @see http://www.w3.org/wiki/CSS/Properties/text-transform
* @method
*/
export const capitalize = (str) => {
const words = str.split(/\s+/);
const length = uint32(words.length);
for (let i = 0; i < length; ++i) {
const word = words[i];
words[i] = word.charAt(0).toUpperCase() + word.slice(1);
}
return words.join(" ");
};
/**
* Converts the passed string to a hashed string.
* @param {string} str The input string.
* @return {string} Returns a hashed string.
* @method
*/
export const hash =(str) => {
const length = uint32(str.length);
let result = 0;
let j = 0;
for (let i = 0; i < length;) {
result ^= str.charCodeAt(i++) << j;
j += 8;
j %= 24;
}
return result.toString(36).toUpperCase();
};
/**
* Converts the given string into a string with a single dash as a separator.
* @param {string} str The input string.
* @return {string} Returns a transformed string.
* @method
*/
export const toKebabCase = (str) => toSpecialCase_(str, '-');
/**
* Converts the given string into a string with a single underscore as a separator.
* @param {string} str The input string.
* @return {string} Returns a transformed string.
* @see https://en.wikipedia.org/wiki/Snake_case
* @method
*/
export const toSnakeCase = (str) => toSpecialCase_(str, '_');
/**
* Converts the given string into a string of capitalized words without
* separators (aka upper camel case).
* @param {string} str The input string.
* @return {string} A string transformed into a string of capitalized words
* without separators.
* @see https://en.wikipedia.org/wiki/PascalCase
* @method
*/
export const toPascalCase = (str) => toCamelCase_(str, true);
/**
* Converts the given string into a string with the separator denoted by the
* next word capitalized (aka lower camel case).
* @param {string} str The input string.
* @return {string} A string transformed into a string with the separator
* denoted by the next word capitalized.
* @see https://en.wikipedia.org/wiki/Camel_case
* @method
*/
export const toCamelCase = (str) => toCamelCase_(str, false);
/**
* Converts the given string into a string with the separator denoted by the
* next word capitalized.
* @param {string} str The input string.
* @param {boolean} isUpperCamelCase Specifies the type of transformation of
* the first letter.
* @return {string} A string convered into a string with the separator
* denoted by the next word capitalized.
* @see https://en.wikipedia.org/wiki/Camel_case
* @private
*/
const toCamelCase_ = (str, isUpperCamelCase) => {
str = str.replace(/[-_\s.]+(.)?/g, (...args) =>
args[1] ? args[1].toUpperCase() : ''
);
return (
(isUpperCamelCase
? str.substr(0, 1).toUpperCase()
: str.substr(0, 1).toLowerCase()) + str.substr(1)
);
};
/**
* Converts the given string into special case style.
* @param {string} str The input string.
* @param {string} separator The separator to apply.
* @return {string} A string convered into special case style.
* @see https://en.wikipedia.org/wiki/Letter_case#Special_case_styles
* @private
*/
const toSpecialCase_ = (str, separator) => {
return str
.split('')
.map((letter) => {
if (/[-_\s.]/.test(letter)) {
return separator;
}
if (letter.toUpperCase() === letter) {
return separator + letter.toLowerCase();
}
return letter;
})
.join('')
// Replacing multiple separators with the single separator.
.replace(new RegExp(separator + '+', 'g'), separator)
// Deleting the separator from the beginning of the string.
.replace(new RegExp('^' + separator), '')
// Deleting the separator at the end of the string.
.replace(new RegExp(separator + '$'), '');
};