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

Add ability to open a random note #440

Merged
merged 4 commits into from
Jan 12, 2021

Conversation

MCluck90
Copy link
Contributor

Adds a Foam: Open Random Note command. It will open any random note in the knowledge base. It is guaranteed to not open the note that you're already viewing

This will implement the basic case outlined in #422 but more work will be needed for any sort of prioritization.

const notes = foam.notes
.getNotes()
.map(note => note.uri.path)
.filter(notePath => notePath !== currentFile);
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of filtering the whole array of notes to remove one, which can be expensive (O(n)) as your knowledge base grows, we could optimistically pick a note, and if it's the current one pick another random note, wdyt?
You can still check that there is more than 1 note in the array below to keep that check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a great idea

Change random note if optimistic choice was the current file
Copy link
Collaborator

@riccardoferretti riccardoferretti left a comment

Choose a reason for hiding this comment

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

LTGM!

.map(note => note.uri.path)
.filter(notePath => notePath !== currentFile);
if (notes.length === 0) {
const notes = foam.notes.getNotes().map(note => note.uri.path);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think you need to do the mapping (same issue as with the filter before, we'd be doing an extra scan of the array) - you can access the field you need when you need it


let randomNoteIndex = Math.floor(Math.random() * notes.length);
if (notes[randomNoteIndex] === currentFile) {
notes.splice(randomNoteIndex, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

splice can also be expensive on big arrays, how about getting randomNoteIndex + 1 % notes.length, which would also turn O(n) into O(1)?

Copy link
Contributor Author

@MCluck90 MCluck90 Jan 12, 2021

Choose a reason for hiding this comment

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

Sure, that works. splice only takes 2ms to randomly remove one item in an array of 1,000,000 items on my computer so I wasn't really concerned 😂 I'll push up the fix in a second

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hahah sorry about being pedantic there, I wasn't expecting it to be that fast (should have tested!) 😂😂😂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries! You're just keeping me honest 😂

Copy link
Collaborator

@riccardoferretti riccardoferretti left a comment

Choose a reason for hiding this comment

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

Great, thx!

@riccardoferretti riccardoferretti merged commit e5589f0 into foambubble:master Jan 12, 2021
@riccardoferretti
Copy link
Collaborator

@allcontributors add @MCluck90 for code

@allcontributors
Copy link
Contributor

@riccardoferretti

I've put up a pull request to add @MCluck90! 🎉

@MCluck90 MCluck90 deleted the feature/open-random-note branch January 12, 2021 22:03
@riccardoferretti riccardoferretti added this to the 0.8.0 milestone Jan 15, 2021
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