This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 972
Pulled code from about:history into a new historyUtil module #5370
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
const Immutable = require('immutable') | ||
|
||
const getDayString = (entry, locale) => { | ||
const lastAccessedTime = entry.get('lastAccessedTime') | ||
return lastAccessedTime | ||
? new Date(lastAccessedTime).toLocaleDateString(locale, { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' }) | ||
: '' | ||
} | ||
|
||
module.exports.groupEntriesByDay = (history, locale) => { | ||
const reduced = history.reduce((previousValue, currentValue, currentIndex, array) => { | ||
const result = currentIndex === 1 ? [] : previousValue | ||
if (currentIndex === 1) { | ||
const firstDate = getDayString(currentValue, locale) | ||
result.push({date: firstDate, entries: [previousValue]}) | ||
} | ||
const date = getDayString(currentValue, locale) | ||
const dateIndex = result.findIndex((entryByDate) => entryByDate.date === date) | ||
if (dateIndex !== -1) { | ||
result[dateIndex].entries.push(currentValue) | ||
} else { | ||
result.push({date: date, entries: [currentValue]}) | ||
} | ||
return result | ||
}) | ||
if (reduced) { | ||
return Immutable.fromJS( | ||
Array.isArray(reduced) | ||
? reduced | ||
: [{date: getDayString(reduced, locale), entries: [reduced]}] | ||
) | ||
} | ||
return Immutable.fromJS([]) | ||
} | ||
|
||
/** | ||
* Return an array with ALL entries. | ||
* Format is expected to be array containing one array per day. | ||
*/ | ||
module.exports.totalEntries = (entriesByDay) => { | ||
let result = new Immutable.List() | ||
entriesByDay.forEach((entry) => { | ||
result = result.push(entry.get('entries')) | ||
}) | ||
return result | ||
} | ||
|
||
module.exports.sortTimeDescending = (left, right) => { | ||
if (left.get('lastAccessedTime') < right.get('lastAccessedTime')) return 1 | ||
if (left.get('lastAccessedTime') > right.get('lastAccessedTime')) return -1 | ||
return 0 | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* global describe, it */ | ||
const assert = require('assert') | ||
const Immutable = require('immutable') | ||
const historyUtil = require('../../../app/common/lib/historyUtil') | ||
require('../braveUnit') | ||
|
||
describe('historyUtil', function () { | ||
const historyDayOne = Immutable.fromJS({ | ||
lastAccessedTime: 1477944718876, | ||
location: 'https://brave.com/page1', | ||
title: 'sample 1', | ||
tags: [] | ||
}) | ||
const historyDayTwo = Immutable.fromJS({ | ||
lastAccessedTime: 1478079042097, | ||
location: 'https://brave.com/page2', | ||
title: 'sample 2', | ||
tags: [] | ||
}) | ||
const historyDayThree = Immutable.fromJS([{ | ||
lastAccessedTime: 1478157051910, | ||
location: 'https://brave.com/page3', | ||
title: 'sample 3', | ||
tags: [] | ||
}, { | ||
lastAccessedTime: 1478157051921, | ||
location: 'https://brave.com/page4', | ||
title: 'sample 4', | ||
tags: [] | ||
}, { | ||
lastAccessedTime: 1478157051932, | ||
location: 'https://brave.com/page5', | ||
title: 'sample 5', | ||
tags: [] | ||
}]) | ||
const historyMultipleDays = historyDayThree.push(historyDayTwo, historyDayOne) | ||
|
||
describe('groupEntriesByDay', function () { | ||
it('returns the result as an Immutable.List', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree) | ||
assert.equal(Immutable.List.isList(result), true) | ||
}) | ||
it('has one object for each day', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree) | ||
assert.equal(result.size, 1) | ||
}) | ||
it('can handle multiple days', function () { | ||
const result = historyUtil.groupEntriesByDay(historyMultipleDays) | ||
assert.equal(result.size, 3) | ||
}) | ||
describe('with the object representing a day', function () { | ||
it('formats a readable `date` field', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree, 'en-US') | ||
assert.equal(result.getIn([0, 'date']), 'Thursday, November 3, 2016') | ||
}) | ||
it('has an entry for each history item', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree, 'en-US') | ||
const entries = result.getIn([0, 'entries']) | ||
assert.equal(entries && entries.size, historyDayThree.size) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('totalEntries', function () { | ||
it('returns the result as an Immutable.List', function () { | ||
const result1 = historyUtil.groupEntriesByDay(historyMultipleDays) | ||
const result2 = historyUtil.totalEntries(result1) | ||
assert.equal(Immutable.List.isList(result2), true) | ||
}) | ||
it('combines entries for multiple days into one response', function () { | ||
const result1 = historyUtil.groupEntriesByDay(historyMultipleDays) | ||
const result2 = historyUtil.totalEntries(result1) | ||
const expectedResult = [ | ||
historyDayThree.toJS(), | ||
[historyDayTwo.toJS()], | ||
[historyDayOne.toJS()] | ||
] | ||
assert.deepEqual(result2.toJS(), expectedResult) | ||
}) | ||
}) | ||
|
||
describe('sortTimeDescending', function () { | ||
it('sorts the items by date/time DESC', function () { | ||
const result = historyMultipleDays.sort(historyUtil.sortTimeDescending) | ||
const expectedResult = historyDayThree.toJS().reverse() | ||
expectedResult.push(historyDayTwo.toJS()) | ||
expectedResult.push(historyDayOne.toJS()) | ||
|
||
assert.deepEqual(result.toJS(), expectedResult) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love to see you pulling things like this out so they are more testable. ++