Giter VIP home page Giter VIP logo

Comments (4)

ChrisTalman avatar ChrisTalman commented on July 17, 2024 1

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.

alizain avatar alizain commented on July 17, 2024 1

The "smallest" ulid for a particular timestamp will always be xxxxxxxx0000000000000000, and the "largest" will always be xxxxxxxxxxZZZZZZZZZZZZZZZZ

from javascript.

ChrisTalman avatar ChrisTalman commented on July 17, 2024

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.

spiffytech avatar spiffytech commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.