Comments (4)
Upon further investigation, it seems fairly straightforward, without the use of monotonicFactory()
.
// 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.
from javascript.
The "smallest" ulid for a particular timestamp will always be xxxxxxxx0000000000000000
, and the "largest" will always be xxxxxxxxxxZZZZZZZZZZZZZZZZ
from javascript.
Yep. That's what my code does, just dynamically using the ENCODING
string.
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.
from javascript.
It would also be nice ulid's TIME_LEN
variable was exported, so calling encodeTime()
could be guaranteed to use the same parameters as ulid's internals.
from javascript.
Related Issues (20)
- isUlid
- dist _index.js#L59
- License violation HOT 20
- Where is ulid.js HOT 3
- Support for RFC 4648 base32? HOT 2
- Is there a way to get the timestamp from a given ULID? HOT 3
- PRNG inconsistency HOT 1
- Crypto API isn't detected on alternative platforms HOT 5
- Monotonically Increasing Guarantee
- is_ulid(x) HOT 3
- Generate time from shortened ulid?
- Random bias?
- Not available in web workers
- Maintainers HOT 4
- Please Upgrade Your Package HOT 1
- Error in worker HOT 2
- issue 1
- issue2
- Decoding ULID string into bytes HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript.