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

Added safe wrapper for toLocateDate() to handle null/invalid dates an… #5621

Merged
merged 1 commit into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/common/lib/formatUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions js/about/bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
24 changes: 24 additions & 0 deletions test/unit/lib/formatUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down