8000 fix(table): default sort compare logic for date strings (#6153) · bootstrap-vue/bootstrap-vue@3696a1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3696a1f

Browse files
authored
fix(table): default sort compare logic for date strings (#6153)
1 parent 5bf6733 commit 3696a1f

File tree

5 files changed

+57
-22
lines changed

5 files changed

+57
-22
lines changed

src/components/alert/alert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ export const BAlert = /*#__PURE__*/ Vue.extend({
8787
countDown(newValue) {
8888
this.clearCountDownInterval()
8989
const show = this[MODEL_PROP_NAME]
90+
// Ignore if `show` transitions to a boolean value
9091
if (isNumeric(show)) {
91-
// Ignore if this.show transitions to a boolean value.
9292
this.$emit(EVENT_NAME_DISMISS_COUNT_DOWN, newValue)
93+
// Update the v-model if needed
9394
if (show !== newValue) {
94-
// Update the v-model if needed
9595
this.$emit(MODEL_EVENT_NAME, newValue)
9696
}
9797
if (newValue > 0) {

src/components/avatar/avatar.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
88
PROP_TYPE_STRING
99
} from '../../constants/props'
1010
import { SLOT_NAME_BADGE } from '../../constants/slots'
11-
import { RX_NUMBER } from '../../constants/regex'
12-
import { isNumber, isString } from '../../utils/inspect'
11+
import { isNumber, isNumeric, isString } from '../../utils/inspect'
1312
import { toFloat } from '../../utils/number'
1413
import { omit, sortKeys } from '../../utils/object'
1514
import { makeProp, makePropsConfigurable, pluckProps } from '../../utils/props'
@@ -33,7 +32,7 @@ const BADGE_FONT_SIZE_SCALE = FONT_SIZE_SCALE * 0.7
3332

3433
export const computeSize = value => {
3534
// Parse to number when value is a float-like string
36-
value = isString(value) && RX_NUMBER.test(value) ? toFloat(value, 0) : value
35+
value = isString(value) && isNumeric(value) ? toFloat(value, 0) : value
3736
// Convert all numbers to pixel values
3837
return isNumber(value) ? `${value}px` : value || null
3938
}

src/components/table/helpers/default-sort-compare.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ describe('table/helpers/default-sort-compare', () => {
3030
expect(defaultSortCompare(date4, date4, options)).toBe(0)
3131
})
3232

33+
it('sorts date strings correctly', async () => {
34+
const date1 = { a: new Date(2020, 1, 1).toISOString() }
35+
const date2 = { a: new Date(1999, 11, 31).toISOString() }
36+
const date3 = { a: new Date(1999, 1, 1).toISOString() }
37+
const date4 = { a: new Date(1999, 1, 1, 12, 12, 12, 12).toISOString() }
38+
const options = { sortBy: 'a' }
39+
40+
expect(defaultSortCompare(date1, date2, options)).toBe(1)
41+
expect(defaultSortCompare(date1, date1, options)).toBe(0)
42+
expect(defaultSortCompare(date2, date1, options)).toBe(-1)
43+
expect(defaultSortCompare(date2, date3, options)).toBe(1)
44+
expect(defaultSortCompare(date3, date2, options)).toBe(-1)
45+
expect(defaultSortCompare(date3, date4, options)).toBe(-1)
46+
expect(defaultSortCompare(date4, date3, options)).toBe(1)
47+
expect(defaultSortCompare(date4, date4, options)).toBe(0)
48+
})
49+
3350
it('sorts strings correctly', async () => {
3451
const options = { sortBy: 'a' }
3552

src/utils/inspect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RX_NUMBER } from '../constants/regex'
12
import { File } from '../constants/safe-types'
23

34
// --- Convenience inspection utilities ---
@@ -26,8 +27,7 @@ export const isString = value => toType(value) === 'string'
2627

2728
export const isNumber = value => toType(value) === 'number'
2829

29-
// Is a value number like (i.e. a number or a number as string)
30-
export const isNumeric = value => !isNaN(parseInt(value, 10))
30+
export const isNumeric = value => RX_NUMBER.test(String(value))
3131

3232
export const isPrimitive = value => isBoolean(value) || isString(value) || isNumber(value)
3333

src/utils/inspect.spec.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isBoolean,
1010
isString,
1111
isNumber,
12+
isNumeric,
1213
isPrimitive,
1314
isDate,
1415
isEvent,
@@ -17,7 +18,7 @@ import {
1718
} from './inspect'
1819

1920
describe('utils/inspect', () => {
20-
it('toType', async () => {
21+
it('toType()', async () => {
2122
expect(toType(123)).toEqual('number')
2223
expect(toType('123')).toEqual('string')
2324
expect(toType(true)).toEqual('boolean')
@@ -31,7 +32,7 @@ describe('utils/inspect', () => {
3132
expect(toType(null)).toEqual('object')
3233
})
3334

34-
it('toRawType', async () => {
35+
it('toRawType()', async () => {
3536
expect(toRawType(123)).toEqual('Number')
3637
expect(toRawType('123')).toEqual('String')
3738
expect(toRawType(true)).toEqual('Boolean')
@@ -45,7 +46,7 @@ describe('utils/inspect', () => {
4546
expect(toRawType(null)).toEqual('Null')
4647
})
4748

48-
it('toRawTypeLC', async () => {
49+
it('toRawTypeLC()', async () => {
4950
expect(toRawTypeLC(123)).toEqual('number')
5051
expect(toRawTypeLC('123')).toEqual('string')
5152
expect(toRawTypeLC(true)).toEqual('boolean')
@@ -59,7 +60,7 @@ describe('utils/inspect', () => {
5960
expect(toRawTypeLC(null)).toEqual('null')
6061
})
6162

62-
it('isUndefined', async () => {
63+
it('isUndefined()', async () => {
6364
expect(isUndefined(123)).toEqual(false)
6465
expect(isUndefined('123')).toEqual(false)
6566
expect(isUndefined(true)).toEqual(false)
@@ -73,7 +74,7 @@ describe('utils/inspect', () => {
7374
expect(isUndefined(null)).toEqual(false)
7475
})
7576

76-
it('isNull', async () => {
77+
it('isNull()', async () => {
7778
expect(isNull(123)).toEqual(false)
7879
expect(isNull('123')).toEqual(false)
7980
expect(isNull(true)).toEqual(false)
@@ -87,7 +88,7 @@ describe('utils/inspect', () => {
8788
expect(isNull(null)).toEqual(true)
8889
})
8990

90-
it('isUndefinedOrNull', async () => {
91+
it('isUndefinedOrNull()', async () => {
9192
expect(isUndefinedOrNull(123)).toEqual(false)
9293
expect(isUndefinedOrNull('123')).toEqual(false)
9394
expect(isUndefinedOrNull(true)).toEqual(false)
@@ -101,7 +102,7 @@ describe('utils/inspect', () => {
101102
expect(isUndefinedOrNull(null)).toEqual(true)
102103
})
103104

104-
it('isFunction', async () => {
105+
it('isFunction()', async () => {
105106
expect(isFunction(123)).toEqual(false)
106107
expect(isFunction('123')).toEqual(false)
107108
expect(isFunction(true)).toEqual(false)
@@ -115,7 +116,7 @@ describe('utils/inspect', () => {
115116
expect(isFunction(null)).toEqual(false)
116117
})
117118

118-
it('isBoolean', async () => {
119+
it('isBoolean()', async () => {
119120
expect(isBoolean(123)).toEqual(false)
120121
expect(isBoolean('123')).toEqual(false)
121122
expect(isBoolean(true)).toEqual(true)
@@ -129,7 +130,7 @@ describe('utils/inspect', () => {
129130
expect(isBoolean(null)).toEqual(false)
130131
})
131132

132-
it('isString', async () => {
133+
it('isString()', async () => {
133134
expect(isString(123)).toEqual(false)
134135
expect(isString('123')).toEqual(true)
135136
expect(isString(true)).toEqual(false)
@@ -143,8 +144,9 @@ describe('utils/inspect', () => {
143144
expect(isString(null)).toEqual(false)
144145
})
145146

146-
it('isNumber', async () => {
147+
it('isNumber()', async () => {
147148
expect(isNumber(123)).toEqual(true)
149+
expect(isNumber(123.5)).toEqual(true)
148150
expect(isNumber('123')).toEqual(false)
149151
expect(isNumber(true)).toEqual(false)
150152
expect(isNumber({})).toEqual(false)
@@ -157,7 +159,24 @@ describe('utils/inspect', () => {
157159
expect(isNumber(null)).toEqual(false)
158160
})
159161

160-
it('isPrimitive', async () => {
162+
it('isNumeric()', async () => {
163+
expect(isNumeric(123)).toEqual(true)
164+
expect(isNumeric(123.5)).toEqual(true)
165+
expect(isNumeric('123')).toEqual(true)
166+
expect(isNumeric('123.5')).toEqual(true)
167+
expect(isNumeric('123,5')).toEqual(false)
168+
expect(isNumeric(true)).toEqual(false)
169+
expect(isNumeric({})).toEqual(false)
170+
expect(isNumeric([])).toEqual(false)
171+
expect(isNumeric(/abc/)).toEqual(false)
172+
expect(isNumeric(() => {})).toEqual(false)
173+
expect(isNumeric(Date)).toEqual(false)
174+
expect(isNumeric(new Date())).toEqual(false)
175+
expect(isNumeric(undefined)).toEqual(false)
176+
expect(isNumeric(null)).toEqual(false)
177+
})
178+
179+
it('isPrimitive()', async () => {
161180
expect(isPrimitive(123)).toEqual(true)
162181
expect(isPrimitive('123')).toEqual(true)
163182
expect(isPrimitive(true)).toEqual(true)
@@ -171,7 +190,7 @@ describe('utils/inspect', () => {
171190
expect(isPrimitive(null)).toEqual(false)
172191
})
173192

174-
it('isDate', async () => {
193+
it('isDate()', async () => {
175194
expect(isDate(123)).toEqual(false)
176195
expect(isDate('123')).toEqual(false)
177196
expect(isDate(true)).toEqual(false)
@@ -185,7 +204,7 @@ describe('utils/inspect', () => {
185204
expect(isDate(null)).toEqual(false)
186205
})
187206

188-
it('isEvent', async () => {
207+
it('isEvent()', async () => {
189208
expect(isEvent(123)).toEqual(false)
190209
expect(isEvent('123')).toEqual(false)
191210
expect(isEvent(true)).toEqual(false)
@@ -201,7 +220,7 @@ describe('utils/inspect', () => {
201220
expect(isEvent(null)).toEqual(false)
202221
})
203222

204-
it('isRegExp', async () => {
223+
it('isRegExp()', async () => {
205224
expect(isRegExp(123)).toEqual(false)
206225
expect(isRegExp('123')).toEqual(false)
207226
expect(isRegExp(true)).toEqual(false)
@@ -215,7 +234,7 @@ describe('utils/inspect', () => {
215234
expect(isRegExp(null)).toEqual(false)
216235
})
217236

218-
it('isPromise', async () => {
237+
it('isPromise()', async () => {
219238
expect(isPromise(123)).toEqual(false)
220239
expect(isPromise('123')).toEqual(false)
221240
expect(isPromise(true)).toEqual(false)

0 commit comments

Comments
 (0)
0