declaration
map
Map characters to their special meanings.
var map = {
d: 'day',
m: 'month',
y: 'year',
'-': 'divider',
'/': 'divider',
' ': 'space'
};
function
getValues()
Option name | Type | Description |
---|---|---|
format | Object | |
return | Function |
Given a format and a string, get the day, month and year values from that string.
function getValues(format) {
return function(value) {
var i = 0;
var index = 0;
var len = format.length;
var values = {};
// Loop through all format pieces
for (; i < len; i++) {
// Only worry about date values
if (['day', 'month', 'year'].indexOf(format[i].name) !== -1) {
// If the passed value doesn't contain a format piece, it's invalid.
if (value.length < index + format[i].length)
return;
values[format[i].name] = parseInt(value.substr(index, format[i].length), 10);
}
index += format[i].length;
}
return dateHelper.create(values);
};
}
function
getString()
Option name | Type | Description |
---|---|---|
format | Object | |
return | Function |
Create a formatted date string given an object of values.
function getString(format) {
function
parseDateFormat()
Option name | Type | Description |
---|---|---|
format | String | |
return | Object |
function parseDateFormat(format) {
var f = format.toLowerCase();
var i = 0;
var len = f.length;
var obj = {
parts: []
};
for (; i < len; i++) {
// If there is a matching character mapping and the last part was of the same name, increment its length
// and add to its content.
if (map[f[i]] && obj.parts.length && obj.parts[obj.parts.length - 1].name === map[f[i]]) {
obj.parts[obj.parts.length - 1].length++;
obj.parts[obj.parts.length - 1].value += format[i];
continue;
}
obj.parts.push({
name: map[f[i]] ? map[f[i]] : '',
value: format[i],
length: 1
});
}
// Add a way to convert the parsed date into a regex-ish string that works with the Typeahead implementation.
obj.getValues = getValues(obj.parts);
obj.getString = getString(obj.parts);
return obj;
}
Base.exportjQuery(parseDateFormat, 'parseDateFormat');
return parseDateFormat;
}));