Skip to content

Commit

Permalink
Fix hang with number input
Browse files Browse the repository at this point in the history
* number converter was failing on NaN - Submitted tests
* Fixed issue with filter text getting set for improper managed list such that it is restored on refresh incorrectly.
* Completed the catalogue replacement functionality

Fixes #64
  • Loading branch information
jadrake75 committed Oct 8, 2023
1 parent 4412730 commit 054cb00
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ The following is a list of test statistics for the project by date and commit
| 2022-06-08 | [a798ac3](https://github.com/stamp-web/stamp-web-aurelia/commit/a798ac36ac61a06258729173d8fa5cacf6a0ff24) | 102 | 18.41% |
| 2022-06-08 | [e64d964](https://github.com/stamp-web/stamp-web-aurelia/commit/e64d964202e6b9930dda0712837682a71ad0d1db) | 104 | 18.59% |
| 2023-01-04 | [359948b](https://github.com/stamp-web/stamp-web-aurelia/commit/359948b689f088ec8c8554044cab96c24ffe1a77) | 107 | 18.80% |
| 2023-09-21 | | 123 | 20.27% |
| 2023-09-21 | [4412730](https://github.com/stamp-web/stamp-web-aurelia/commit/441273055dc1af57257aba29f929923799563325) | 123 | 20.27% |
| 2023-10-08 | | 134 | 20.68% |

## Optimizing for Browsers

Expand Down
20 changes: 8 additions & 12 deletions src/resources/value-converters/as-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,26 @@
limitations under the License.
*/
import {valueConverter} from 'aurelia-framework';
import _ from 'lodash';

@valueConverter("asNumber")
export class asNumberValueConverter {

toView(value, asFloat = false) {
if (value) {
try {
value = asFloat ? parseFloat(value.toString()) : parseInt(value.toString());
} catch (err) {
console.log("Could not parse '" + value + "' to a number.");
value = -1;
}
value = asFloat ? parseFloat(value.toString()) : parseInt(value.toString());
}
if (_.isNaN(value)) {
value = undefined;
}
return value;
}

fromView(value, asFloat = false) {
if(value) {
try {
value = (asFloat) ? parseFloat(value) : parseInt(value);
} catch( err ) {
console.log("Could not parse '" + value + "' to a number.");
value = -1;
}
value = (asFloat) ? parseFloat(value) : parseInt(value);
} if (_.isNaN(value)) {
value = undefined;
}
return value;
}
Expand Down
2 changes: 2 additions & 0 deletions src/resources/views/manage/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export class EntityListManage {
var cacheVal = JSON.parse(filterCache);
if(cacheVal[fieldDef.field]) {
this.filterText = cacheVal[fieldDef.field];
} else {
this.filterText = '';
}
}
}
Expand Down
91 changes: 91 additions & 0 deletions test/unit/resources/value-converters/as-number.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
Copyright 2023 Jason Drake
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {asNumberValueConverter} from 'resources/value-converters/as-number';

describe('asNumberValueConverter test suite', () => {

describe('toView tests', () => {
let cmp;

beforeEach(() => {
cmp = new asNumberValueConverter();
});

it('handles NaN', () => {
let value = cmp.toView(NaN);
expect(value).toBeUndefined();
});

it('handles integer', () => {
let value = cmp.toView(53);
expect(value).toBe(53);
});

it('handles real number as integer', () => {
let value = cmp.toView(21.25, false);
expect(value).toBe(21);
});

it('handles string real number as integer ', () => {
let value = cmp.toView('42.75', false);
expect(value).toBe(42);
});

it('handles string real number as real number ', () => {
let value = cmp.toView('63.75', true);
expect(value).toBe(63.75);
});
});

describe('fromView tests', () => {

let cmp;

beforeEach(() => {
cmp = new asNumberValueConverter();
});

it('handles NaN', () => {
let value = cmp.fromView(NaN);
expect(value).toBeUndefined();
});

it('handles positive integer value', () => {
let value = cmp.fromView('54');
expect(value).toBe(54);
});

it('handles positive real number value as integer', () => {
let value = cmp.fromView('52.5');
expect(value).toBe(52);
});

it('handles negative number value', () => {
let value = cmp.fromView('-23');
expect(value).toBe(-23);
});

it('handles postive real number value as float', () => {
let value = cmp.fromView('35.25', true);
expect(value).toBe(35.25);
});

it('handles String invalid number', () => {
let value = cmp.fromView('ThisIsNotANumber');
expect(value).toBeUndefined();
});
});
});

0 comments on commit 054cb00

Please sign in to comment.