Skip to content

Commit c138086

Browse files
committed
Merge pull request siddii#132 from sanderaido/master
New plural unit decision functions
2 parents d181c18 + b869609 commit c138086

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

app/js/timer.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,20 @@ var timerModule = angular.module('timer', [])
153153
$scope.months = Math.floor((($scope.millis / (3600000)) / 24 / 30) % 12);
154154
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 365);
155155
}
156-
// plural - singular unit decision
157-
$scope.secondsS = ($scope.seconds === 1 || $scope.seconds === 0) ? '' : 's';
158-
$scope.minutesS = ($scope.minutes === 1 || $scope.minutes === 0) ? '' : 's';
159-
$scope.hoursS = ($scope.hours === 1 || $scope.hours === 0) ? '' : 's';
160-
$scope.daysS = ($scope.days === 1 || $scope.days === 0)? '' : 's';
161-
$scope.monthsS = ($scope.months === 1 || $scope.months === 0)? '' : 's';
162-
$scope.yearsS = ($scope.years === 1 || $scope.years === 0)? '' : 's';
156+
// plural - singular unit decision (old syntax, for backwards compatibility and English only, could be deprecated!)
157+
$scope.secondsS = ($scope.seconds === 1) ? '' : 's';
158+
$scope.minutesS = ($scope.minutes === 1) ? '' : 's';
159+
$scope.hoursS = ($scope.hours === 1) ? '' : 's';
160+
$scope.daysS = ($scope.days === 1)? '' : 's';
161+
$scope.monthsS = ($scope.months === 1)? '' : 's';
162+
$scope.yearsS = ($scope.years === 1)? '' : 's';
163+
// new plural-singular unit decision functions (for custom units and multilingual support)
164+
$scope.secondUnit = function(singleSecond, pluralSecond){if($scope.seconds === 1){if(singleSecond){return singleSecond;} return 'second';} if(pluralSecond){return pluralSecond;} return 'seconds';};
165+
$scope.minuteUnit = function(singleMinute, pluralMinute){if($scope.minutes === 1){if(singleMinute){return singleMinute;} return 'minute';} if(pluralMinute){return pluralMinute;} return 'minutes';};
166+
$scope.hourUnit = function(singleHour, pluralHour){if($scope.hours === 1){if(singleHour){return singleHour;} return 'hour';} if(pluralHour){return pluralHour;} return 'hours';};
167+
$scope.dayUnit = function(singleDay, pluralDay){if($scope.days === 1){if(singleDay){return singleDay;} return 'day';} if(pluralDay){return pluralDay;} return 'days';};
168+
$scope.monthUnit = function(singleMonth, pluralMonth){if($scope.months === 1){if(singleMonth){return singleMonth;} return 'month';} if(pluralMonth){return pluralMonth;} return 'months';};
169+
$scope.yearUnit = function(singleYear, pluralYear){if($scope.years === 1){if(singleYear){return singleYear;} return 'year';} if(pluralYear){return pluralYear;} return 'years';};
163170
//add leading zero if number is smaller than 10
164171
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
165172
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;

dist/angular-timer.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,20 @@ var timerModule = angular.module('timer', [])
160160
$scope.months = Math.floor((($scope.millis / (3600000)) / 24 / 30) % 12);
161161
$scope.years = Math.floor(($scope.millis / (3600000)) / 24 / 365);
162162
}
163-
// plural - singular unit decision
164-
$scope.secondsS = ($scope.seconds === 1 || $scope.seconds === 0) ? '' : 's';
165-
$scope.minutesS = ($scope.minutes === 1 || $scope.minutes === 0) ? '' : 's';
166-
$scope.hoursS = ($scope.hours === 1 || $scope.hours === 0) ? '' : 's';
167-
$scope.daysS = ($scope.days === 1 || $scope.days === 0)? '' : 's';
168-
$scope.monthsS = ($scope.months === 1 || $scope.months === 0)? '' : 's';
169-
$scope.yearsS = ($scope.years === 1 || $scope.years === 0)? '' : 's';
163+
// plural - singular unit decision (old syntax, for backwards compatibility and English only, could be deprecated!)
164+
$scope.secondsS = ($scope.seconds === 1) ? '' : 's';
165+
$scope.minutesS = ($scope.minutes === 1) ? '' : 's';
166+
$scope.hoursS = ($scope.hours === 1) ? '' : 's';
167+
$scope.daysS = ($scope.days === 1)? '' : 's';
168+
$scope.monthsS = ($scope.months === 1)? '' : 's';
169+
$scope.yearsS = ($scope.years === 1)? '' : 's';
170+
// new plural-singular unit decision functions (for custom units and multilingual support)
171+
$scope.secondUnit = function(singleSecond, pluralSecond){if($scope.seconds === 1){if(singleSecond){return singleSecond;} return 'second';} if(pluralSecond){return pluralSecond;} return 'seconds';};
172+
$scope.minuteUnit = function(singleMinute, pluralMinute){if($scope.minutes === 1){if(singleMinute){return singleMinute;} return 'minute';} if(pluralMinute){return pluralMinute;} return 'minutes';};
173+
$scope.hourUnit = function(singleHour, pluralHour){if($scope.hours === 1){if(singleHour){return singleHour;} return 'hour';} if(pluralHour){return pluralHour;} return 'hours';};
174+
$scope.dayUnit = function(singleDay, pluralDay){if($scope.days === 1){if(singleDay){return singleDay;} return 'day';} if(pluralDay){return pluralDay;} return 'days';};
175+
$scope.monthUnit = function(singleMonth, pluralMonth){if($scope.months === 1){if(singleMonth){return singleMonth;} return 'month';} if(pluralMonth){return pluralMonth;} return 'months';};
176+
$scope.yearUnit = function(singleYear, pluralYear){if($scope.years === 1){if(singleYear){return singleYear;} return 'year';} if(pluralYear){return pluralYear;} return 'years';};
170177
//add leading zero if number is smaller than 10
171178
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
172179
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;

test/e2e/scenarios.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ describe('Angular Timer E2E Tests', function () {
9090

9191
it('Clock Timer - with hours, minutes & seconds', function () {
9292
sleep(3);
93-
expect(element('#clock-timer timer').text()).toMatch(/0 hour/);
94-
expect(element('#clock-timer timer').text()).toMatch(/0 minute/);
93+
expect(element('#clock-timer timer').text()).toMatch(/0 hours/);
94+
expect(element('#clock-timer timer').text()).toMatch(/0 minutes/);
9595
expect(element('#clock-timer timer').text()).toMatch(/3 seconds./); //because of sleep(3);
9696
});
9797

@@ -134,12 +134,12 @@ describe('Angular Timer E2E Tests', function () {
134134

135135
it('Leading zero timer - should add a leading zero if number is smaller than 10', function() {
136136
sleep(1);
137-
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 hour,/);
138-
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 minute,/);
137+
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 hours,/);
138+
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 minutes,/);
139139
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/01 second/);
140140
sleep(10);
141-
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 hour,/);
142-
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 minute,/);
141+
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 hours,/);
142+
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/00 minutes,/);
143143
expect(element('#clock-timer-leading-zero timer').text()).toMatch(/11 seconds/);
144144
});
145145

0 commit comments

Comments
 (0)