-
Notifications
You must be signed in to change notification settings - Fork 107
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
First and Last IDs for Timestamp #64
Comments
Upon further investigation, it seems fairly straightforward, without the use of // External Modules
import { encodeTime } from 'ulid';
// Constants
const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
const TIME_LEN = 10;
const RANDOM_LEN = 16;
function generateTimeBounds(timestamp: number)
{
const timeComponent = encodeTime(timestamp, TIME_LEN);
const first = timeComponent + generateRandomnessBound('first');
const last = timeComponent + generateRandomnessBound('last');
return { first, last };
};
function generateRandomnessBound(bound: 'first' | 'last')
{
const boundCharacter = bound === 'first' ? ENCODING[0] : ENCODING[ENCODING.length - 1];
let randomness = '';
for (let character = 0; character < RANDOM_LEN; character++)
{
randomness += boundCharacter;
};
return randomness;
}; Perhaps this could be a useful addition as a helper method in the library? My use case is to retrieve records from a database which, according to their ID, occur within a time period. For instance, records which occur between two dates represented by timestamps. |
The "smallest" ulid for a particular timestamp will always be |
Yep. That's what my code does, just dynamically using the This is a very useful helper function when working with time ranges. It would be nice to have it as part of the module, rather than having to redeclare it in every project where it is needed. If it would be welcome, I could submit a pull request for this. |
It would also be nice ulid's |
Is it possible to obtain the first and last lexicography sorted IDs for a timestamp?
If I understand correctly, it seems possible to get the first ID with
monotonicFactory()
.However, I'm not sure how to go about obtaining the last ID.
Any help would be much appreciated. 👍
The text was updated successfully, but these errors were encountered: