From c048f0d8e8385e96e9fff0b4ff733cecfa726d93 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Mon, 8 Nov 2010 20:34:18 -0800 Subject: Added formatter documentation. --- src/formatters.js | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) (limited to 'src/formatters.js') diff --git a/src/formatters.js b/src/formatters.js index ba57a3ed..5c602cc8 100644 --- a/src/formatters.js +++ b/src/formatters.js @@ -6,8 +6,76 @@ function toString(obj) { var NUMBER = /^\s*[-+]?\d*(\.\d*)?\s*$/; angularFormatter.noop = formatter(identity, identity); + +/** + * @ngdoc formatter + * @name angular.formatter.json + * + * @description + * Formats the user input as JSON text. + * + * @returns {string} A JSON string representation of the model. + * + * @example + *
+ * + *
data={{data}}
+ *
+ * + * @scenario + * it('should format json', function(){ + * expect(binding('data')).toEqual('data={\n \"name\":\"misko\",\n \"project\":\"angular\"}'); + * input('data').enter('{}'); + * expect(binding('data')).toEqual('data={\n }'); + * }); + */ angularFormatter.json = formatter(toJson, fromJson); + +/** + * @ngdoc formatter + * @name angular.formatter.boolean + * + * @description + * Use boolean formatter if you wish to store the data as boolean. + * + * @returns Convert to `true` unless user enters (blank), `f`, `false`, `0`, `no`, `[]`. + * + * @example + * Enter truthy text: + * + * + *
value={{value}}
+ * + * @scenario + * it('should format boolean', function(){ + * expect(binding('value')).toEqual('value=false'); + * input('value').enter('truthy'); + * expect(binding('value')).toEqual('value=true'); + * }); + */ angularFormatter['boolean'] = formatter(toString, toBoolean); + +/** + * @ngdoc formatter + * @name angular.formatter.number + * + * @description + * Use number formatter if you wish to convert the user entered string to a number. + * + * @returns parse string to number. + * + * @example + * Enter valid number: + * + *
value={{value}}
+ * + * @scenario + * it('should format numbers', function(){ + * expect(binding('value')).toEqual('value=1234'); + * input('value').enter('5678'); + * expect(binding('value')).toEqual('value=5678'); + * }); + */ angularFormatter.number = formatter(toString, function(obj){ if (obj == _null || NUMBER.exec(obj)) { return obj===_null || obj === '' ? _null : 1*obj; @@ -16,6 +84,31 @@ angularFormatter.number = formatter(toString, function(obj){ } }); +/** + * @ngdoc formatter + * @name angular.formatter.list + * + * @description + * Use number formatter if you wish to convert the user entered string to a number. + * + * @returns parse string to number. + * + * @example + * Enter a list of items: + * + * + *
value={{value}}
+ * + * @scenario + * it('should format lists', function(){ + * expect(binding('value')).toEqual('value=["chair","table"]'); + * this.addFutureAction('change to XYZ', function($window, $document, done){ + * $document.elements('.example :input:last').val(',,a,b,').trigger('change'); + * done(); + * }); + * expect(binding('value')).toEqual('value=["a","b"]'); + * }); + */ angularFormatter.list = formatter( function(obj) { return obj ? obj.join(", ") : obj; }, function(value) { @@ -28,6 +121,31 @@ angularFormatter.list = formatter( } ); +/** + * @ngdoc formatter + * @name angular.formatter.trim + * + * @description + * Use trim formatter if you wish to trim extra spaces in user text. + * + * @returns {String} Trim excess leading and trailing space. + * + * @example + * Enter text with leading/trailing spaces: + * + * + *
value={{value|json}}
+ * + * @scenario + * it('should format trim', function(){ + * expect(binding('value')).toEqual('value="book"'); + * this.addFutureAction('change to XYZ', function($window, $document, done){ + * $document.elements('.example :input:last').val(' text ').trigger('change'); + * done(); + * }); + * expect(binding('value')).toEqual('value="text"'); + * }); + */ angularFormatter.trim = formatter( function(obj) { return obj ? trim("" + obj) : ""; } ); -- cgit v1.2.3