forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (147 loc) · 4.65 KB
/
Copy pathindex.js
File metadata and controls
188 lines (147 loc) · 4.65 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const LOWERCASE_TRANSFORMER = function (fragments, separator) {
return fragments.join(separator).toLowerCase()
};
const SENTENCECASE_TRANSFORMER = function (fragments, separator) {
const sentence = fragments.join(separator);
return sentence.charAt(0).toUpperCase() + sentence.slice(1).toLowerCase()
};
const TITLECASE_TRANSFORMER = function (fragments, separator) {
const buffer = [];
for (let index = 0; index < fragments.length; index++) {
buffer.push(
fragments[index].charAt(0).toUpperCase() +
fragments[index].slice(1).toLowerCase()
);
}
return buffer.join(separator)
};
const UPPERCASE_TRANSFORMER = function (fragments, separator) {
return fragments.join(separator).toUpperCase()
};
const INVALID_SEPARATOR = /[^-._~!$&'()*+,;=]/;
const CAMELCASE_REGEXP_PATTERN = '(?:[a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))';
function validate (options, extra) {
const camelCase = options.camelCase;
if (camelCase !== undefined && typeof camelCase !== 'boolean') {
throw new Error('camelCase must be a boolean: "' + camelCase + '".')
}
const separator = options.separator;
extra = extra || {};
if (
separator !== undefined &&
!('separator' in extra && separator === extra.separator)
) {
if (typeof separator !== 'string') {
throw new Error(
'separator must be a string' +
('separator' in extra ? ' or ' + extra.separator : '') + ': "' +
separator + '".'
)
} else if (INVALID_SEPARATOR.test(separator)) {
throw new Error(
'separator has an invalid character: "' +
separator.match(INVALID_SEPARATOR)[0] + '".'
)
}
}
const transformer = options.transformer;
if (
transformer !== undefined &&
transformer !== false &&
typeof transformer !== 'function'
) {
throw new Error(
'transformer must be false or a function: "' +
transformer + '".'
)
}
}
function replace (string, definitions) {
for (let index = 0, length = string.length; index < length; index++) {
const char = string[index];
const replacement = definitions[char];
if (replacement !== undefined) {
string = string.substr(0, index) +
replacement +
string.substr(index + 1);
const addedCharsCount = String(replacement).length - 1;
index += addedCharsCount;
length += addedCharsCount;
}
}
return string
}
// eslint-disable-next-line no-misleading-character-class
const COMBINING_CHARS = /[\u0300-\u036F\u1AB0-\u1AFF\u1DC0-\u1DFF]+/g;
const CONVERT = /[A-Za-z\d]+/g;
const CONVERT_CAMELCASE = new RegExp(
'[A-Za-z\\d]*?' + CAMELCASE_REGEXP_PATTERN + '|[A-Za-z\\d]+',
'g'
);
function convert (string, options) {
options = options || {};
if (process.env.NODE_ENV !== 'production') {
validate(options);
}
const camelCase = options.camelCase !== undefined
? options.camelCase
: true;
const separator = options.separator !== undefined
? options.separator
: '-';
const transformer = options.transformer !== undefined
? options.transformer
: LOWERCASE_TRANSFORMER;
const fragments = (
options.dictionary
? replace(String(string), options.dictionary)
: String(string)
)
.normalize('NFKD')
.replace(COMBINING_CHARS, '')
.match(camelCase ? CONVERT_CAMELCASE : CONVERT);
if (!fragments) {
return ''
}
return transformer
? transformer(fragments, separator)
: fragments.join(separator)
}
const REVERT = /[^-._~!$&'()*+,;=]+/g;
const REVERT_CAMELCASE = new RegExp(
'[^-._~!$&\'()*+,;=]*?' + CAMELCASE_REGEXP_PATTERN + '|[^-._~!$&\'()*+,;=]+',
'g'
);
const REVERT_CAMELCASE_ONLY = new RegExp(
'.*?' + CAMELCASE_REGEXP_PATTERN + '|.+',
'g'
);
function revert (slug, options) {
options = options || {};
if (process.env.NODE_ENV !== 'production') {
validate(options, { separator: null });
}
const camelCase = options.camelCase !== undefined
? options.camelCase
: false;
const separator = options.separator;
const transformer = options.transformer !== undefined
? options.transformer
: false;
let fragments;
slug = String(slug);
/* Determine which method will be used split the slug */
if (separator === '') {
fragments = camelCase ? slug.match(REVERT_CAMELCASE_ONLY) : [String(slug)];
} else if (typeof separator === 'string') {
fragments = slug.split(separator);
} else {
fragments = slug.match(camelCase ? REVERT_CAMELCASE : REVERT);
}
if (!fragments) {
return ''
}
return transformer ? transformer(fragments, ' ') : fragments.join(' ')
}
export default convert;
export { LOWERCASE_TRANSFORMER, SENTENCECASE_TRANSFORMER, TITLECASE_TRANSFORMER, UPPERCASE_TRANSFORMER, convert, revert };