Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

arnautov-anton
Copy link
Contributor

@arnautov-anton arnautov-anton commented Sep 19, 2024

This PR removes and replaces binarySearchByDateEqualOrNearestGreater with universal findIndexInSortedArray function and enables exact matching by checking midpoint.

Changes to channel and channel_state tests are to ensure proper order of generated messages (added created_at to each generated message with proper offset).

Copy link
Contributor

github-actions bot commented Sep 19, 2024

Size Change: -343 B (-0.08%)

Total Size: 427 kB

Filename Size Change
dist/browser.es.js 92.6 kB -77 B (-0.08%)
dist/browser.full-bundle.min.js 54.1 kB -33 B (-0.06%)
dist/browser.js 93.7 kB -82 B (-0.09%)
dist/index.es.js 92.6 kB -73 B (-0.08%)
dist/index.js 93.7 kB -78 B (-0.08%)

compressed-size-action

Comment on lines 113 to 114
expect(state.messages[7].id).to.be.equal('id');
expect(state.messages[8].id).to.be.equal('7');
Copy link
Contributor Author

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 same created_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.

Comment on lines -2694 to -2712
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);
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to utils.test.ts with additions.

@arnautov-anton arnautov-anton force-pushed the fix/find-index-in-sorted-array-midpoint-match branch from 398af35 to 544753a Compare September 19, 2024 13:48
Comment on lines 95 to +106
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);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I messed up here previously.

@arnautov-anton arnautov-anton changed the title fix: replace binarySearchByDateEqualOrNearestGreater with findIndexInSortedArray, add midpoint match fix: replace binarySearchByDateEqualOrNearestGreater with findIndexInSortedArray \w midpoint match Sep 20, 2024
@MartinCupela MartinCupela force-pushed the fix/find-index-in-sorted-array-midpoint-match branch from efd7c38 to d009882 Compare October 21, 2024 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants