diff --git a/app/common/lib/formatUtil.js b/app/common/lib/formatUtil.js index 9d917ad4ee4..fd952b3bce6 100644 --- a/app/common/lib/formatUtil.js +++ b/app/common/lib/formatUtil.js @@ -84,6 +84,20 @@ module.exports.formatAccelerator = (accelerator) => { return result } +module.exports.toLocaleString = (epoch, defaultValue) => { + if (epoch && typeof epoch === 'number') { + try { + const date = new Date(epoch).toLocaleString() + if (date !== 'Invalid Date') { + return date + } + } catch (e) { + console.log('Error parsing date: ', e) + } + } + return defaultValue || '' +} + /** * Clamp values down to a given range (min/max). * Value is wrapped when out of bounds. ex: diff --git a/js/about/bookmarks.js b/js/about/bookmarks.js index 73c2ad279bd..a6253426a0c 100644 --- a/js/about/bookmarks.js +++ b/js/about/bookmarks.js @@ -14,6 +14,7 @@ const dndData = require('../dndData') const cx = require('../lib/classSet') const SortableTable = require('../components/sortableTable') const siteUtil = require('../state/siteUtil') +const formatUtil = require('../../app/common/lib/formatUtil') const iconSize = require('../../app/common/lib/faviconUtil').iconSize const ipc = window.chrome.ipc @@ -340,8 +341,8 @@ class BookmarksList extends ImmutableComponent { value: entry.get('customTitle') || entry.get('title') || entry.get('location') }, { - html: new Date(entry.get('lastAccessedTime')).toLocaleString(), - value: entry.get('lastAccessedTime') + html: formatUtil.toLocaleString(entry.get('lastAccessedTime'), ''), + value: entry.get('lastAccessedTime') || 0 } ])} rowObjects={this.props.bookmarks} diff --git a/test/unit/lib/formatUtilTest.js b/test/unit/lib/formatUtilTest.js index 134f55efa4e..7d9d404e5b6 100644 --- a/test/unit/lib/formatUtilTest.js +++ b/test/unit/lib/formatUtilTest.js @@ -47,6 +47,30 @@ describe('formatUtil', function () { }) }) + describe('toLocaleString', function () { + it('formats the date as a string', function () { + const result = formatUtil.toLocaleString(1479159834005) + assert.equal(typeof result, 'string') + assert.equal(result.length > 0, true) + }) + it('falls back to provided default value if epoch is falsey', function () { + const result = formatUtil.toLocaleString(null, 'abc') + assert.equal(result, 'abc') + }) + it('falls back to empty string if no default provided and epoch is falsey', function () { + const result = formatUtil.toLocaleString() + assert.equal(result, '') + }) + it('falls back to default value if invalid date is passed', function () { + const result = formatUtil.toLocaleString(NaN, 'def') + assert.equal(result, 'def') + }) + it('falls back to default value if a non-number is passed', function () { + const result = formatUtil.toLocaleString('this is not a number', 'ghi') + assert.equal(result, 'ghi') + }) + }) + describe('wrappingClamp', function () { it('does not change value if within bounds', function () { assert.equal(formatUtil.wrappingClamp(5, 1, 10), 5)