diff options
Diffstat (limited to 'src/formatters.js')
| -rw-r--r-- | src/formatters.js | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/formatters.js b/src/formatters.js new file mode 100644 index 00000000..40462cf3 --- /dev/null +++ b/src/formatters.js @@ -0,0 +1,32 @@ +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) : ""; } + ) +}); |
