-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathtr.js
More file actions
82 lines (79 loc) · 2.33 KB
/
tr.js
File metadata and controls
82 lines (79 loc) · 2.33 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
import coerceToString from 'helper/string/coerce_to_string';
import isString from 'query/is_string';
import nilDefault from 'helper/undefined/nil_default';
/**
* Translates characters or replaces substrings in `subject`.
*
* @function tr
* @static
* @since 1.3.0
* @memberOf Manipulate
* @param {string} [subject=''] The string to translate.
* @param {string|Object} from The string of characters to translate from. Or an object, then the object keys are replaced with corresponding values (longest keys are tried first).
* @param {string} to The string of characters to translate to. Ignored when `from` is an object.
* @return {string} Returns the translated string.
* @example
* v.tr('hello', 'el', 'ip');
* // => 'hippo'
*
* v.tr('légèreté', 'éè', 'ee');
* // => 'legerete'
*
* v.tr('Yes. The fire rises.', {
* 'Yes': 'Awesome',
* 'fire': 'flame'
* })
* // => 'Awesome. The flame rises.'
*
* v.tr(':where is the birthplace of :what', {
* ':where': 'Africa',
* ':what': 'Humanity'
* });
* // => 'Africa is the birthplace of Humanity'
*
*/
export default function tr(subject, from, to) {
const subjectString = coerceToString(subject);
let keys;
let values;
if (isString(from) && isString(to)) {
keys = from.split('');
values = to.split('');
} else {
[keys, values] = extractKeysAndValues(nilDefault(from, {}));
}
const keysLength = keys.length;
if (keysLength === 0) {
return subjectString;
}
let result = '';
const valuesLength = values.length;
for (let index = 0; index < subjectString.length; index++) {
let isMatch = false;
let matchValue;
for (let keyIndex = 0; keyIndex < keysLength && keyIndex < valuesLength; keyIndex++) {
const key = keys[keyIndex];
if (subjectString.substr(index, key.length) === key) {
isMatch = true;
matchValue = values[keyIndex];
index = index + key.length - 1;
break;
}
}
result += isMatch ? matchValue : subjectString[index];
}
return result;
}
function extractKeysAndValues(object) {
const keys = Object.keys(object);
const values = keys.sort(sortStringByLength).map(function(key) {
return object[key];
});
return [keys, values];
}
function sortStringByLength(str1, str2) {
if (str1.length === str2.length) {
return 0;
}
return str1.length < str2.length ? 1 : -1;
}