-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
136 lines (101 loc) · 3.54 KB
/
main.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
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
(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.OLSKString = global.OLSKString || {})));
}(this, (function(exports) { 'use strict';
const mod = {
OLSKStringFormatted (inputData) {
if (typeof inputData !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
var substitutions = Object.values(arguments).slice(1);
if (!substitutions.length) {
return inputData;
}
var formattedString = inputData;
(inputData.match(/%@/g) || []).forEach(function(e, i) {
formattedString = formattedString.replace(e, substitutions[i]);
});
mod._OLSKStringAllMatches(/%\$(\d*)@/g, inputData).forEach(function(e) {
formattedString = formattedString.replace(e[0], substitutions[e[1] - 1]);
});
return formattedString;
},
_OLSKStringAllMatches (regex, string) {
var matches = [];
var match = regex.exec(string);
while (match != null) {
matches.push(match);
match = regex.exec(string);
}
return matches;
},
OLSKStringReplaceTokens (param1, param2) {
if (typeof param1 !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof param2 !== 'object' || param2 === null) {
throw new Error('OLSKErrorInputNotValid');
}
return Object.entries(param2).reduce(function (coll, item) {
return coll.replace(new RegExp(item.shift(), 'g'), item.pop());
}, param1);
},
OLSKStringPatch (param1, param2, param3) {
if (typeof param1 !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof param2 !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof param3 !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
if (!param1.includes(param2) && !param1.includes(param3)) {
console.error(`source includes neither of "${ param2 }" or "${ param3 }"`);
throw new Error('OLSKErrorInputNotValid');
}
if (param3.includes(param2)) {
throw new Error('OLSKErrorInputNotValid');
}
return param1.split(param2).join(param3);
},
OLSKStringMatch (param1, param2, param3 = 'includes') {
if (typeof param1 !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof param2 !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
if (typeof param3 !== 'undefined') {
if (typeof param3 !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
}
// Searching and sorting text with diacritical marks in JavaScript | Thread Engineering https://thread.engineering/2018-08-29-searching-and-sorting-text-with-diacritical-marks-in-javascript/
return param2.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, '')[param3](param1.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, ''));
},
OLSKStringSnippet (inputData) {
if (typeof inputData !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
return inputData.length <= 100 ? inputData : inputData.slice(0, 100).split(' ').slice(0, -1).join(' ').concat('…');
},
OLSKStringEncode (inputData) {
if (typeof inputData !== 'string') {
throw new Error('OLSKErrorInputNotValid');
}
return mod.OLSKStringReplaceTokens(encodeURIComponent(inputData), {
'\\(': '%28',
'\\)': '%29',
});
},
};
Object.assign(exports, mod);
Object.defineProperty(exports, '__esModule', {
value: true
});
})));
if (typeof exports === 'object') {
exports.OLSKFormatted = exports.OLSKStringFormatted;
}