-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: replace binarySearchByDateEqualOrNearestGreater with findIndexInSortedArray \w midpoint match #1370
Open
arnautov-anton
wants to merge
3
commits into
master
Choose a base branch
from
fix/find-index-in-sorted-array-midpoint-match
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+254
−94
Open
fix: replace binarySearchByDateEqualOrNearestGreater with findIndexInSortedArray \w midpoint match #1370
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
|
@@ -2690,23 +2690,3 @@ describe('messageSetPagination', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('', () => { | ||
const messages = [ | ||
{ created_at: '2024-08-05T08:55:00.199808Z', id: '0' }, | ||
{ created_at: '2024-08-05T08:55:01.199808Z', id: '1' }, | ||
{ created_at: '2024-08-05T08:55:02.199808Z', id: '2' }, | ||
{ created_at: '2024-08-05T08:55:03.199808Z', id: '3' }, | ||
{ created_at: '2024-08-05T08:55:04.199808Z', id: '4' }, | ||
{ created_at: '2024-08-05T08:55:05.199808Z', id: '5' }, | ||
{ created_at: '2024-08-05T08:55:06.199808Z', id: '6' }, | ||
{ created_at: '2024-08-05T08:55:07.199808Z', id: '7' }, | ||
{ created_at: '2024-08-05T08:55:08.199808Z', id: '8' }, | ||
]; | ||
it('finds the nearest newer item', () => { | ||
expect(binarySearchByDateEqualOrNearestGreater(messages, new Date('2024-08-05T08:55:02.299808Z'))).to.eql(3); | ||
}); | ||
it('finds the nearest matching item', () => { | ||
expect(binarySearchByDateEqualOrNearestGreater(messages, new Date('2024-08-05T08:55:07.199808Z'))).to.eql(7); | ||
}); | ||
}); | ||
Comment on lines
-2694
to
-2712
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
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 |
---|---|---|
|
@@ -3,12 +3,12 @@ import { v4 as uuidv4 } from 'uuid'; | |
|
||
import { generateMsg } from './test-utils/generateMessage'; | ||
|
||
import { addToMessageList, formatMessage } from '../../src/utils'; | ||
import { addToMessageList, findIndexInSortedArray, formatMessage } from '../../src/utils'; | ||
|
||
import type { FormatMessageResponse, MessageResponse } from '../../src'; | ||
|
||
describe('addToMessageList', () => { | ||
const timestamp = new Date('2024-09-18T15:30:00.000Z').getTime(); | ||
const timestamp = new Date('2024-01-01T00:00:00.000Z').getTime(); | ||
// messages with each created_at 10 seconds apart | ||
let messagesBefore: FormatMessageResponse[]; | ||
|
||
|
@@ -93,15 +93,91 @@ describe('addToMessageList', () => { | |
}); | ||
|
||
it("updates an existing message that wasn't filtered due to changed timestamp (timestampChanged)", () => { | ||
const newMessage = getNewFormattedMessage({ timeOffset: 30 * 1000, id: messagesBefore[4].id }); | ||
const newMessage = getNewFormattedMessage({ timeOffset: 30 * 1000, id: messagesBefore[3].id }); | ||
|
||
expect(messagesBefore[4].id).to.equal(newMessage.id); | ||
expect(messagesBefore[4].text).to.not.equal(newMessage.text); | ||
expect(messagesBefore[4]).to.not.equal(newMessage); | ||
expect(messagesBefore[3].id).to.equal(newMessage.id); | ||
expect(messagesBefore[3].text).to.not.equal(newMessage.text); | ||
expect(messagesBefore[3]).to.not.equal(newMessage); | ||
|
||
const messagesAfter = addToMessageList(messagesBefore, newMessage, false, 'created_at', false); | ||
const messagesAfter = addToMessageList(messagesBefore, newMessage, false); | ||
|
||
expect(messagesAfter).to.have.length(5); | ||
expect(messagesAfter[4]).to.equal(newMessage); | ||
expect(messagesAfter[3]).to.equal(newMessage); | ||
}); | ||
Comment on lines
95
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I messed up here previously. |
||
}); | ||
|
||
describe('findIndexInSortedArray', () => { | ||
const timestamp = new Date('2024-01-01T00:00:00.000Z').getTime(); | ||
|
||
const generateMessages = ({ count = 10, sort = 'desc' }: { count?: number; sort?: 'asc' | 'desc' } = {}) => { | ||
const messages = Array.from({ length: count }, (_, index) => | ||
generateMsg({ created_at: new Date(timestamp + index * 10 * 1000).toISOString() }), | ||
); | ||
|
||
if (sort === 'desc') { | ||
messages.reverse(); | ||
} | ||
|
||
return messages as MessageResponse[]; | ||
}; | ||
|
||
describe('ascending order', () => { | ||
const messages = generateMessages({ sort: 'asc' }).map(formatMessage); | ||
|
||
it('finds index of the message with closest matching created_at', () => { | ||
const newMessage = formatMessage(generateMsg({ created_at: new Date(timestamp + 22 * 1000) }) as MessageResponse); | ||
|
||
const index = findIndexInSortedArray({ | ||
needle: newMessage, | ||
sortedArray: messages, | ||
sortDirection: 'ascending', | ||
selectValueToCompare: (v) => v.created_at.getTime(), | ||
}); | ||
|
||
expect(index).to.equal(3); | ||
}); | ||
|
||
it('finds exact index', () => { | ||
const newMessage = formatMessage(generateMsg({ created_at: new Date(timestamp + 20 * 1000) }) as MessageResponse); | ||
|
||
const index = findIndexInSortedArray({ | ||
needle: newMessage, | ||
sortedArray: messages, | ||
sortDirection: 'ascending', | ||
selectValueToCompare: (v) => v.created_at.getTime(), | ||
}); | ||
|
||
expect(index).to.equal(2); | ||
}); | ||
}); | ||
|
||
describe('descending order', () => { | ||
const messages = generateMessages({ sort: 'desc' }).map(formatMessage); | ||
|
||
it('finds index of the message with closest matching created_at', () => { | ||
const newMessage = formatMessage(generateMsg({ created_at: new Date(timestamp + 22 * 1000) }) as MessageResponse); | ||
|
||
const index = findIndexInSortedArray({ | ||
needle: newMessage, | ||
sortedArray: messages, | ||
sortDirection: 'descending', | ||
selectValueToCompare: (v) => v.created_at.getTime(), | ||
}); | ||
|
||
expect(index).to.equal(7); | ||
}); | ||
|
||
it('finds exact index', () => { | ||
const newMessage = formatMessage(generateMsg({ created_at: new Date(timestamp + 10 * 1000) }) as MessageResponse); | ||
|
||
const index = findIndexInSortedArray({ | ||
needle: newMessage, | ||
sortedArray: messages, | ||
sortDirection: 'descending', | ||
selectValueToCompare: (v) => v.created_at.getTime(), | ||
}); | ||
|
||
expect(index).to.equal(8); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
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.
One hurdle of the midpoint check addition is that message with the same
created_at
will be added before existing one but since the existence of the messages with the samecreated_at
in real-world applications is almost impossible - I adjusted the test.Even without midpoint check, the previous solution - in case of multiple messages with the same
created_at
- the new message would be placed at most one index after the already existing one which is still incorrect.