-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add filter which rounds number with defined precision.
additionally - fixed input-type directive for escape, tab and enter to be allowed; - make demo-components controller compliant with typescript; Signed-off-by: Oleksii Kurinnyi <okurinnyi@codenvy.com>
- Loading branch information
1 parent
0a624b4
commit 3be62a2
Showing
9 changed files
with
190 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) 2015-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*/ | ||
'use strict'; | ||
import {CheNumberRoundFilter} from './number-round/number-round.filter'; | ||
|
||
export class FilterConfig { | ||
|
||
constructor(register: che.IRegisterService) { | ||
new CheNumberRoundFilter(register); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
dashboard/src/components/filter/number-round/number-round.filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (c) 2015-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*/ | ||
'use strict'; | ||
|
||
export class CheNumberRoundFilter { | ||
|
||
constructor(register: che.IRegisterService) { | ||
register.filter('cheNumberRoundFilter', () => { | ||
return (number: number, precision: number) => { | ||
number = parseFloat(number); | ||
precision = parseInt(precision, 10); | ||
|
||
if (isNaN(number)) { | ||
return 'NaN'; | ||
} | ||
if (isNaN(precision)) { | ||
precision = 0; | ||
} | ||
|
||
let factor = Math.pow(10, precision); | ||
let tempNumber = number * factor; | ||
let roundedTempNumber = Math.round(tempNumber); | ||
return (roundedTempNumber / factor).toString(); | ||
}; | ||
}); | ||
} | ||
} | ||
|
91 changes: 91 additions & 0 deletions
91
dashboard/src/components/filter/number-round/number-round.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2015-2017 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*/ | ||
'use strict'; | ||
import {CheHttpBackend} from '../../api/test/che-http-backend'; | ||
|
||
/** | ||
* Test for CheNumberRoundFilter | ||
* | ||
* @author Oleksii Kurinnyi | ||
*/ | ||
|
||
describe('CheNumberRoundFilter', () => { | ||
let $scope, $compile; | ||
|
||
let testNumbers = [ | ||
{ | ||
number: -1234.5678, | ||
precision: -2, | ||
result: -1200 | ||
}, | ||
{ | ||
number: -1234.5678, | ||
precision: 0, | ||
result: -1235 | ||
}, | ||
{ | ||
number: -1234.5678, | ||
precision: 2, | ||
result: -1234.57 | ||
}, | ||
{ | ||
number: 1234.5678, | ||
precision: -2, | ||
result: 1200 | ||
}, | ||
{ | ||
number: 1234.5678, | ||
precision: 0, | ||
result: 1235 | ||
}, | ||
{ | ||
number: 1234.5678, | ||
precision: 2, | ||
result: 1234.57 | ||
} | ||
]; | ||
|
||
/** | ||
* Backend for handling http operations | ||
*/ | ||
let httpBackend; | ||
|
||
beforeEach(angular.mock.module('userDashboard')); | ||
|
||
beforeEach(inject((_$compile_: ng.ICompileService, $rootScope: ng.IRootScopeService, cheHttpBackend: CheHttpBackend) => { | ||
$scope = $rootScope; | ||
$compile = _$compile_; | ||
|
||
httpBackend = cheHttpBackend.getHttpBackend(); | ||
// avoid tracking requests from branding controller | ||
httpBackend.whenGET(/.*/).respond(200, ''); | ||
httpBackend.when('OPTIONS', '/api/').respond({}); | ||
})); | ||
|
||
testNumbers.forEach((entry: {number: number, precision: number, result: number}) => { | ||
|
||
(function (entry: {number: number, precision: number, result: number}) { | ||
it(`Number ${entry.number} with precision ${entry.precision} should be equal to ${entry.result}`, () => { | ||
$scope.entry = entry; | ||
|
||
let element = angular.element( | ||
'<div>{{entry.number | cheNumberRoundFilter:entry.precision}}</div>' | ||
); | ||
$compile(element)($scope); | ||
$scope.$digest(); | ||
|
||
expect(element.text()).toEqual(entry.result.toString()); | ||
}); | ||
})(entry); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters