-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutil.js
105 lines (105 loc) · 3.39 KB
/
util.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*
* Private : Utils
* */
const BasePatterns_1 = require("./pattern/BasePatterns");
const Text = {
escapeRegex(rx) {
return rx.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
},
removeAllSpaces(target) {
return target.replace(/[\n\r\t\s]/g, '');
},
urisToOneRxStr(uris) {
let re = '';
for (let a = 0; a < uris.length; a++) {
let re_partial = '';
for (let b = 0; b < uris[a].length; b++) {
if (!(uris[a][b] && typeof uris[a][b] === 'string')) {
throw new Error('A value not in a string type has been found : ' + uris[a][b] + ' / loc in for clause : a=' + a + ' / b=' + b);
}
uris[a][b] = this.removeAllSpaces(uris[a][b]);
if (b === 0) {
if (new RegExp('^' + BasePatterns_1.BasePatterns.noLangCharNum, 'i').test(uris[a][b])) {
if (uris[a][b] !== "{number}") {
throw new Error('The first letter of the first URI part must not be a meta char : not valid : ' + uris[a][b]);
}
}
}
if (b < uris[a].length - 1) {
re_partial += uris[a][b] + '/';
}
else {
re_partial += uris[a][b];
}
}
if (a < uris.length - 1) {
re += this.escapeRegex(re_partial) + '|';
}
else {
re += this.escapeRegex(re_partial);
}
}
re = re.replace(/\\\{number\\\}/gi, '[0-9]+');
return re;
},
orConditionRxToArr(rxStr) {
rxStr = rxStr.replace(/^\(\?:|\)$/gi, '');
return rxStr.split('|');
},
similarity(s1, s2) {
let longer = s1;
let shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
let longerLength = longer.length;
if (longerLength === 0) {
return 1.0;
}
return (longerLength - this.editDistance(longer, shorter)) / parseFloat(String(longerLength));
},
editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
let costs = [];
for (let i = 0; i <= s1.length; i++) {
let lastValue = i;
for (let j = 0; j <= s2.length; j++) {
if (i === 0)
costs[j] = j;
else {
if (j > 0) {
let newValue = costs[j - 1];
if (s1.charAt(i - 1) !== s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
},
indexOfMax(arr) {
if (arr.length === 0) {
return -1;
}
let max = arr[0];
let maxIndex = 0;
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
maxIndex = i;
max = arr[i];
}
}
return maxIndex;
}
};
exports.default = {
Text
};