Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 76ea8c3

Browse files
authored
Merge pull request #5621 from bsclifton/fix-import-date
Added safe wrapper for toLocateDate() to handle null/invalid dates an…
2 parents dd20048 + 349990d commit 76ea8c3

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

app/common/lib/formatUtil.js

+14
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ module.exports.formatAccelerator = (accelerator) => {
8484
return result
8585
}
8686

87+
module.exports.toLocaleString = (epoch, defaultValue) => {
88+
if (epoch && typeof epoch === 'number') {
89+
try {
90+
const date = new Date(epoch).toLocaleString()
91+
if (date !== 'Invalid Date') {
92+
return date
93+
}
94+
} catch (e) {
95+
console.log('Error parsing date: ', e)
96+
}
97+
}
98+
return defaultValue || ''
99+
}
100+
87101
/**
88102
* Clamp values down to a given range (min/max).
89103
* Value is wrapped when out of bounds. ex:

js/about/bookmarks.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const dndData = require('../dndData')
1414
const cx = require('../lib/classSet')
1515
const SortableTable = require('../components/sortableTable')
1616
const siteUtil = require('../state/siteUtil')
17+
const formatUtil = require('../../app/common/lib/formatUtil')
1718
const iconSize = require('../../app/common/lib/faviconUtil').iconSize
1819

1920
const ipc = window.chrome.ipc
@@ -340,8 +341,8 @@ class BookmarksList extends ImmutableComponent {
340341
value: entry.get('customTitle') || entry.get('title') || entry.get('location')
341342
},
342343
{
343-
html: new Date(entry.get('lastAccessedTime')).toLocaleString(),
344-
value: entry.get('lastAccessedTime')
344+
html: formatUtil.toLocaleString(entry.get('lastAccessedTime'), ''),
345+
value: entry.get('lastAccessedTime') || 0
345346
}
346347
])}
347348
rowObjects={this.props.bookmarks}

test/unit/lib/formatUtilTest.js

+24
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ describe('formatUtil', function () {
4747
})
4848
})
4949

50+
describe('toLocaleString', function () {
51+
it('formats the date as a string', function () {
52+
const result = formatUtil.toLocaleString(1479159834005)
53+
assert.equal(typeof result, 'string')
54+
assert.equal(result.length > 0, true)
55+
})
56+
it('falls back to provided default value if epoch is falsey', function () {
57+
const result = formatUtil.toLocaleString(null, 'abc')
58+
assert.equal(result, 'abc')
59+
})
60+
it('falls back to empty string if no default provided and epoch is falsey', function () {
61+
const result = formatUtil.toLocaleString()
62+
assert.equal(result, '')
63+
})
64+
it('falls back to default value if invalid date is passed', function () {
65+
const result = formatUtil.toLocaleString(NaN, 'def')
66+
assert.equal(result, 'def')
67+
})
68+
it('falls back to default value if a non-number is passed', function () {
69+
const result = formatUtil.toLocaleString('this is not a number', 'ghi')
70+
assert.equal(result, 'ghi')
71+
})
72+
})
73+
5074
describe('wrappingClamp', function () {
5175
it('does not change value if within bounds', function () {
5276
assert.equal(formatUtil.wrappingClamp(5, 1, 10), 5)

0 commit comments

Comments
 (0)