aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVojta Jina2012-03-12 22:56:36 -0700
committerIgor Minar2012-03-13 17:51:05 -0700
commit13f31602f396bc269076ab4d389cfd8ca94b20ba (patch)
treec6837581b3b965d762fb7a1dee7321d7dee0b4db
parent7b52586f7c139ea5dfe694f9667bad673d7ca5f1 (diff)
downloadangular.js-13f31602f396bc269076ab4d389cfd8ca94b20ba.tar.bz2
feat(ng-list): Allow custom separator
-rw-r--r--src/directive/input.js9
-rw-r--r--test/directive/inputSpec.js22
2 files changed, 29 insertions, 2 deletions
diff --git a/src/directive/input.js b/src/directive/input.js
index 740f3ba6..631e3a36 100644
--- a/src/directive/input.js
+++ b/src/directive/input.js
@@ -1101,6 +1101,8 @@ var requiredDirective = [function() {
* Text input that converts between comma-seperated string into an array of strings.
*
* @element input
+ * @param {string=} ng-list optional delimiter that should be used to split the value. If
+ * specified in form `/something/` then the value will be converted into a regular expression.
*
* @example
<doc:example>
@@ -1139,12 +1141,15 @@ var ngListDirective = function() {
return {
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
+ var match = /\/(.*)\//.exec(attr.ngList),
+ separator = match && new RegExp(match[1]) || attr.ngList || ',';
+
var parse = function(viewValue) {
var list = [];
if (viewValue) {
- forEach(viewValue.split(/\s*,\s*/), function(value) {
- if (value) list.push(value);
+ forEach(viewValue.split(separator), function(value) {
+ if (value) list.push(trim(value));
});
}
diff --git a/test/directive/inputSpec.js b/test/directive/inputSpec.js
index a9aafd04..ad1670f9 100644
--- a/test/directive/inputSpec.js
+++ b/test/directive/inputSpec.js
@@ -871,6 +871,28 @@ describe('input', function() {
changeInputValueTo('');
expect(scope.list).toEqual([]);
});
+
+
+ it('should allow custom separator', function() {
+ compileInput('<input type="text" ng-model="list" ng-list=":" />');
+
+ changeInputValueTo('a,a');
+ expect(scope.list).toEqual(['a,a']);
+
+ changeInputValueTo('a:b');
+ expect(scope.list).toEqual(['a', 'b']);
+ });
+
+
+ it('should allow regexp as a separator', function() {
+ compileInput('<input type="text" ng-model="list" ng-list="/:|,/" />');
+
+ changeInputValueTo('a,b');
+ expect(scope.list).toEqual(['a', 'b']);
+
+ changeInputValueTo('a,b: c');
+ expect(scope.list).toEqual(['a', 'b', 'c']);
+ });
});
describe('required', function() {