-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed the VmDisks sort so bootable disks sort first on the VmDetail page #547
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise LGTM
src/components/VmDisks/index.js
Outdated
|
||
return aBoot && !bBoot ? -1 | ||
: !aBoot && bBoot ? 1 | ||
: a.get('name').localeCompare(b.get('name')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please use number aware sorting:
const b = ['r', 'ř10', 'ř9', 'ř']
b.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))
// b is Array [ "r", "ř", "ř9", "ř10" ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent point. I've added that and some tests so it's a bit more obvious what is intended.
I moved the disk sort out to its own file to keep the component simple. Adding in explicit locales to the compare is easier to see in the utils.js. The test make it explicit what we are expecting the sort to look like on screen. package.json got a tweak to fix a jest complaint. |
src/components/VmDisks/utils.js
Outdated
import { locale as appLocale, DEFAULT_LOCALE } from '../../intl' | ||
|
||
function localeCompare (a, b, locale = appLocale) { | ||
return a.localeCompare(b, [ locale, DEFAULT_LOCALE ], { numeric: true }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ locale, DEFAULT_LOCALE ]
IIUIC this is already done at
ovirt-web-ui/src/intl/index.js
Line 29 in e6f8c1f
export const locale: string = getLocaleFromUrl() || getBrowserLocale() || DEFAULT_LOCALE |
locale
from intl
should always provide valid locale string. So using DEFAULT_LOCALE
and exporting it seems redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did notice that locale coming from intl.js will fallback to DEFAULT_LOCALE. However, the sort function accepts a locale as a parameter, primarily for testing purposes. When the locale passed in isn't correct, and there isn't a fallback given the behavior is inconsistent across browsers.
In short, it is there to protect against edge cases if the function is ever used elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- There is no test for invalid locale code so reproducibility of test isn't in risk.
- Current production code won't provide wrong locale code.
- Inconsistent sorting in case of potential future usage and wrong language code isn't much of an issue.
... which makes me find the second item of locales array redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a bit more reading and it is redundant and doesn't protect against bad locales passed in via function argument. I'm ok with letting the locale functions throw the errors if the locale isn't correct. I'll adjust accordingly.
Sorry for my confusion, it's been awhile since I dealt with the ECMA Intl APIs.
@@ -117,7 +117,9 @@ | |||
"^.+\\.css$": "<rootDir>/config/jest/CSSStub.js", | |||
"ovirtapi": "<rootDir>/src/mock/ovirtapi.mock.js" | |||
}, | |||
"scriptPreprocessor": "<rootDir>/config/jest/transform.js", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not able to find doc for scriptPreprocessor
property. What is the motivation for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is exactly why I changed it. scriptPreprocessor
was migrated to transform
. When I ran the tests, jest reported the change. It is talked about a bit here [1].
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, optional minor comments
src/components/VmDisks/utils.test.js
Outdated
|
||
{ | ||
testTitle: 'name with number', | ||
locale: 'cs', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note, action not required
['r', 'ř10', 'ř9', 'ř'].sort((a, b) => a.localeCompare(b, 'cs', { numeric: true }))
['r', 'ř10', 'ř9', 'ř'].sort((a, b) => a.localeCompare(b, 'en', { numeric: true }))
both returns the same result Array [ "r", "ř", "ř9", "ř10" ]
. So this isn't actually testing that other sorting algorithm is used. The distiguishing difference might be the fact that Czech language consider sequence 'ch' as one char that is sorted between 'h' and 'i':
['cecil', 'chris', 'david', 'harry'].sort((a, b) => a.localeCompare(b, 'cs', { numeric: true }))
// Array [ "cecil", "david", "harry", "chris" ]
['cecil', 'chris', 'david', 'harry'].sort((a, b) => a.localeCompare(b, 'en', { numeric: true }))
// Array [ "cecil", "chris", "david", "harry" ]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the language lesson! I'll adjust the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh fun. The normal build of nodejs, at least for node 8 and newer, appears to enable the Intl API but only includes English (en). There is a way to include the full locale support. I'll update package.json to do that, however that will also require updating ovirt-engine-nodejs-modules
for full CI and build support.
src/components/VmDisks/utils.js
Outdated
import { locale as appLocale, DEFAULT_LOCALE } from '../../intl' | ||
|
||
function localeCompare (a, b, locale = appLocale) { | ||
return a.localeCompare(b, [ locale, DEFAULT_LOCALE ], { numeric: true }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- There is no test for invalid locale code so reproducibility of test isn't in risk.
- Current production code won't provide wrong locale code.
- Inconsistent sorting in case of potential future usage and wrong language code isn't much of an issue.
... which makes me find the second item of locales array redundant.
Add full-icu to devDependencies to enable testing in multiple locales.
How are new libraries/version handled in ovirt-web-ui? I added |
@sjd78 , |
Introduced by: #547 Breaks recent offline-build by using runtime-determined npm dependencies. The npm package "full-icu" does post-installation setup consisting of downloading "icu4c-data" _in a version corresponding to running nodejs_ which is determined at runtime. Can be fixed by either including all potential icu4c-data versions in ovirt-engine-nodejs-modules or using clean full-icu npm dependency mechanism.
Introduced by: #547 Breaks recent offline-build by using runtime-determined npm dependencies. The npm package "full-icu" does post-installation setup consisting of downloading "icu4c-data" _in a version corresponding to running nodejs_ which is determined at runtime. Can be fixed by either including all potential icu4c-data versions in ovirt-engine-nodejs-modules or using clean full-icu npm dependency mechanism.
Resolves issue #511
This change is