-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateFormat.js
More file actions
56 lines (45 loc) · 1.48 KB
/
dateFormat.js
File metadata and controls
56 lines (45 loc) · 1.48 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
(function (window, undefined) {
"use strict";
// get a list of all tokens on the source that exist on the target,
// ordered by reverse token length
var getSortedTokens = function (sourceDefinition, targetDefinition) {
var tokens = [],
id = 0;
for (var key in sourceDefinition) {
if (!sourceDefinition.hasOwnProperty(key)) {
continue;
}
//skip things that we can't replace
if (!targetDefinition[key]) {
continue;
}
tokens.push({
value: sourceDefinition[key],
key: key,
id: id++
});
}
tokens.sort(function (lhs, rhs) {
return rhs.value.length - lhs.value.length;
});
return tokens;
},
// main dateFormatConverter function
convert = function (format, from, to) {
var sourceTokens = getSortedTokens(from, to);
for (var i = 0; i < sourceTokens.length; i++) {
format = format.replace(
new RegExp(sourceTokens[i].value, "g"),
"{" + sourceTokens[i].id + "}");
}
for (var j = 0; j < sourceTokens.length; j++) {
format = format.replace(
new RegExp("\\{" + sourceTokens[j].id + "\\}", "g"),
to[sourceTokens[j].key]);
}
return format;
};
window.dateFormat = {
convert: convert
};
}(window));