Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
…6105) * fix(b-table): sort handling for numeric string values * Update stringify-object-values.spec.js * Update stringify-object-values.spec.js
- Loading branch information
1 parent
2700ebd
commit 29fbcb5
Showing
7 changed files
with
180 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,62 @@ | ||
import get from '../../../utils/get' | ||
import { isDate, isFunction, isNumber, isUndefinedOrNull } from '../../../utils/inspect' | ||
import stringifyObjectValues from './stringify-object-values' | ||
import stringifyObjectValues from '../../../utils/stringify-object-values' | ||
import { isDate, isFunction, isNumber, isNumeric, isUndefinedOrNull } from '../../../utils/inspect' | ||
import { toFloat } from '../../../utils/number' | ||
|
||
const normalizeValue = value => { | ||
if (isUndefinedOrNull(value)) { | ||
return '' | ||
} | ||
if (isNumeric(value)) { | ||
return toFloat(value) | ||
} | ||
return value | ||
} | ||
|
||
// Default sort compare routine | ||
// | ||
// TODO: Add option to sort by multiple columns (tri-state per column, | ||
// plus order of columns in sort) where sortBy could be an array | ||
// of objects `[ {key: 'foo', sortDir: 'asc'}, {key:'bar', sortDir: 'desc'} ...]` | ||
// or an array of arrays `[ ['foo','asc'], ['bar','desc'] ]` | ||
// Multisort will most likely be handled in mixin-sort.js by | ||
// calling this method for each sortBy | ||
const defaultSortCompare = (a, b, sortBy, sortDesc, formatter, localeOpts, locale, nullLast) => { | ||
// TODO: | ||
// Add option to sort by multiple columns (tri-state per column, | ||
// plus order of columns in sort) where `sortBy` could be an array | ||
// of objects `[ {key: 'foo', sortDir: 'asc'}, {key:'bar', sortDir: 'desc'} ...]` | ||
// or an array of arrays `[ ['foo','asc'], ['bar','desc'] ]` | ||
// Multisort will most likely be handled in `mixin-sort.js` by | ||
// calling this method for each sortBy | ||
const defaultSortCompare = ( | ||
a, | ||
b, | ||
{ sortBy = null, formatter = null, locale = undefined, localeOptions = {}, nullLast = false } = {} | ||
) => { | ||
// Get the value by `sortBy` | ||
let aa = get(a, sortBy, null) | ||
let bb = get(b, sortBy, null) | ||
|
||
// Apply user-provided formatter | ||
if (isFunction(formatter)) { | ||
aa = formatter(aa, sortBy, a) | ||
bb = formatter(bb, sortBy, b) | ||
} | ||
aa = isUndefinedOrNull(aa) ? '' : aa | ||
bb = isUndefinedOrNull(bb) ? '' : bb | ||
|
||
// Internally normalize value | ||
// `null` / `undefined` => '' | ||
// `'0'` => `0` | ||
aa = normalizeValue(aa) | ||
bb = normalizeValue(bb) | ||
|
||
if ((isDate(aa) && isDate(bb)) || (isNumber(aa) && isNumber(bb))) { | ||
// Special case for comparing dates and numbers | ||
// Internally dates are compared via their epoch number values | ||
return aa < bb ? -1 : aa > bb ? 1 : 0 | ||
} else if (nullLast && aa === '' && bb !== '') { | ||
// Special case when sorting null/undefined/empty string last | ||
// Special case when sorting `null` / `undefined` / '' last | ||
return 1 | ||
} else if (nullLast && aa !== '' && bb === '') { | ||
// Special case when sorting null/undefined/empty string last | ||
// Special case when sorting `null` / `undefined` / '' last | ||
return -1 | ||
} | ||
|
||
// Do localized string comparison | ||
return stringifyObjectValues(aa).localeCompare(stringifyObjectValues(bb), locale, localeOpts) | ||
return stringifyObjectValues(aa).localeCompare(stringifyObjectValues(bb), locale, localeOptions) | ||
} | ||
|
||
export default defaultSortCompare |
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
Oops, something went wrong.