-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcldr-2-php-convertor.js
110 lines (94 loc) · 3.08 KB
/
cldr-2-php-convertor.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
/**
* Created by Niels de Feyter on 29/01/15.
*/
$(document).ready(function () {
// Change event
$("#convert").click(function () {
// Get input value.
var cldrValue = $("#cldr-format").val().trim();
// Get converted value
var phpValue = convertCLDRtoPHP(cldrValue, "*");
$("#php-format").text(phpValue);
// Get converted value (compliance mode)
var phpValue = convertCLDRtoPHP(cldrValue, "");
$("#php-format-compliance").text(phpValue);
});
});
// Converter function
function convertCLDRtoPHP(cldrValue, fallback_replacement) {
// Get an array with partials that can be replaced sorted by length.
var replacements = delimitCLDRDateTimeString(cldrValue);
// Replace each partial with its corresponding PHP format.
$(replacements).each(function () {
cldrValue = cldrValue.replace(this, returnMapping(this, fallback_replacement));
});
// Remove spaces before commas.
cldrValue = cldrValue.replace(" ,", ",");
// Return result.
return cldrValue;
}
// Delimit function
function delimitCLDRDateTimeString(text) {
var splitters = [", ", " ", ",", "-", "/", "\\.", "'", ":"];
var textSplitted = text.split(new RegExp(splitters.join('|'), ''));
// Sort by value length (long first).
textSplitted.sort(function (a, b) {
return b.length - a.length; // ASC -> a - b; DESC -> b - a
});
return textSplitted;
}
// Replace with mapping.
function returnMapping(partial, fallback_replacement) {
if (fallback_replacement === undefined) {
fallback_replacement = "*";
}
var map =
{
// Era is not implemented.
"GGGGG": fallback_replacement,
"GGGG": fallback_replacement,
"GGG": fallback_replacement,
"GG": fallback_replacement,
"G": fallback_replacement,
// Year.
"yyyy": "Y", // 1999
"yy": "y", // 99
"y": "Y", // 1999
// Month.
"MMMM": "F",
"MMM": "M",
"MM": "m",
"M": "m",
// Day.
"dd": "d",
"d": "j",
// Day of week.
"EEEEEE": fallback_replacement, // Tu
"EEEEE": fallback_replacement, // T
"EEEE": "l", // Tuesday
"EEE": "D", // Tue
"EE": "D", // Tue
"E": "D", // Tue
// Am/PM
"a": "a",
// hours
"HH": "H", // 24-hour format of an hour with leading zeros
"H": "G", // 24-hour format of an hour without leading zeros
"h": "h", // 12-hour format of an hour with leading zeros
"K": "g", // 12-hour format of an hour without leading zero
// minutes
"mm": "i", // Minutes with leading zeros
"ss": "s", // Seconds, with leading zeros
// timezone.
"z": "T", // Timezone abbreviation
"zz": "T", // Timezone abbreviation
"zzz": "T", // Timezone abbreviation
"zzzz": "e", // Timezone
};
// Return replacement if needle is found.
if (partial in map) {
return map[partial];
}
// Return fallback by default.
return fallback_replacement;
}