mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 15:40:22 +00:00
32 lines
877 B
JavaScript
32 lines
877 B
JavaScript
function formatter(format, parse) {return {'format':format, 'parse':parse || format};}
|
|
function toString(obj) {return (isDefined(obj) && obj !== null) ? "" + obj : obj;}
|
|
|
|
var NUMBER = /^\s*[-+]?\d*(\.\d*)?\s*$/;
|
|
|
|
extend(angularFormatter, {
|
|
'noop':formatter(identity, identity),
|
|
'boolean':formatter(toString, toBoolean),
|
|
'number':formatter(toString,
|
|
function(obj){
|
|
if (isString(obj) && NUMBER.exec(obj)) {
|
|
return obj ? 1*obj : null;
|
|
}
|
|
throw "Not a number";
|
|
}),
|
|
|
|
'list':formatter(
|
|
function(obj) { return obj ? obj.join(", ") : obj; },
|
|
function(value) {
|
|
var list = [];
|
|
foreach((value || '').split(','), function(item){
|
|
item = trim(item);
|
|
if (item) list.push(item);
|
|
});
|
|
return list;
|
|
}
|
|
),
|
|
|
|
'trim':formatter(
|
|
function(obj) { return obj ? trim("" + obj) : ""; }
|
|
)
|
|
});
|