aboutsummaryrefslogtreecommitdiffstats
path: root/src/formatters.js
blob: 40462cf3aba8959cfda949c4391069794dd45cb4 (plain)
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
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) : ""; }
  )
});