-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (38 loc) · 991 Bytes
/
index.js
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
33
34
35
36
37
38
39
40
41
42
/*
* angular-to-sentence v1.0.0
* (c) 2015 Rhys Causey
* License: MIT
*/
'use strict';
angular.module('toSentence', []).
directive('toSentence', function() {
return {
restrict: 'EA',
scope: {
wordsConnector: '=',
twoWordsConnector: '=',
lastWordConnector: '=',
items: '='
},
link: function(scope, element, attr) {
var wordsConnector = scope.wordsConnector || ', ',
twoWordsConnector = scope.twoWordsConnector || ' and ',
lastWordConnector = scope.lastWordConnector || ', and ',
items = scope.items || [],
elementText;
var len = items.length;
if (len == 1) {
elementText = items[0];
} else if (len == 2) {
elementText = items[0] +
twoWordsConnector +
items[1];
} else if (len > 2) {
elementText =
items.slice(0, -1).join(wordsConnector) +
lastWordConnector + items[len - 1];
}
element.text(elementText || '');
}
}
});