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

feat: hash() - Generate a unique number based on input #2272

Merged
merged 1 commit into from
Mar 20, 2024

Conversation

holroy
Copy link
Contributor

@holroy holroy commented Mar 17, 2024

Based on a unique combination of a seed, text and a variant number, it generates a fixed number which can be used for sorting in a random order. Typical seed could be the date of today, the text a file name, and the variant could be the line number of a list/task item.

As discussed in #2252 this is the alternate PR just having a hash() function, which is a simple logic, but still provides the needs necessary for sorting any list in a random order, but with fixed value for a given input sequence.

Given a date as the seed, this can be used to sort lists of files/tasks/lists, and then return a random item of that list, and the item for that given date will remain the same (as long as the other parameters stay the same). This deals with an issue of using dataview to receive random items, where it'll change for every refresh of the random query.

See also https://jsperf.app/bowuze for the reasoning why this particular hash function was used.

Based on a unique combination of a seed, text and a variant number, it generates a fixed number which can be used for sorting in a random order. Typical seed could be the date of today, the text a file name, and the variant could be the line number of a list/task item.
@holroy
Copy link
Contributor Author

holroy commented Mar 17, 2024

Attached here is a self-contained test file (which I added to my local test-vault in the untracked folder):

Testing hash().md

@GottZ
Copy link
Contributor

GottZ commented Mar 17, 2024

you are aware this is conflicting with #2271 due to the lint fix?

@holroy
Copy link
Contributor Author

holroy commented Mar 17, 2024

Why do you think it's in conflict with #2271? Please explain your reasoning.

Even though the same fix has been made in both versions, they should cancel each other out when properly merged which is why this PR also has all of its checked passed.

@GottZ
Copy link
Contributor

GottZ commented Mar 18, 2024

Why do you think it's in conflict with #2271? Please explain your reasoning.

Even though the same fix has been made in both versions, they should cancel each other out when properly merged which is why this PR also has all of its checked passed.

@blacksmithgu usually does squash merges.
after one is merged, a conflict is likely to happen.
either he resolves it then, or you pull master and resolve it in your branch yourself.
we will see.
if it does come to a conflict, it's pretty easy to solve.

@blacksmithgu
Copy link
Owner

I can resolve both merge conflicts.

@blacksmithgu blacksmithgu merged commit 4f70e92 into blacksmithgu:master Mar 20, 2024
3 checks passed
@blacksmithgu
Copy link
Owner

Good functionality! Thanks for the contribution.

@six8
Copy link

six8 commented Jun 11, 2024

A simple use case to get a new list of random files (excluding notes in the Daily folder) each day:

LIST
FROM !"Daily"
WHERE file.name != this.file.name
SORT hash(dateformat(date(today), "YYYY-MM-DD"), file.name)
LIMIT 5

tip: YYYY-MM-DD-HH if you want new notes each hour.

I'm adding this use case because it took me a while to figure out how to piece all the parts together, and someone else might find it useful. I'm new to DQL, so Googling led me all over the place since this function is so new. I eventually landed here.

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.

4 participants