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(string): adds support for generating ULID #2524

Open
wants to merge 11 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/modules/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,21 @@ export class StringModule extends SimpleModuleBase {
.replaceAll('y', () => this.faker.number.hex({ min: 0x8, max: 0xb }));
}

/**
* Returns a ULID ([Universally Unique Lexicographically Sortable Identifier](https://github.com/ulid/spec)).
*
* @example
* faker.string.ulid() // '01ARZ3NDEKTSV4RRFFQ69G5FAV'
*
* @since 8.2.0
*/
ulid(): string {
return (
this.fromCharacters('012', 1) +
Copy link
Contributor

Choose a reason for hiding this comment

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

perhaps add a comment here that ULIDs starting with 0, 1 or 2 cover the years 1970-5314

Copy link
Author

Choose a reason for hiding this comment

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

I've updated the PR with a new solution proposal addressing the suggestions by @Shinigami92.
It should now generate the timestamp part of the ULID from refDate.

this.fromCharacters('0123456789ABCDEFGHJKMNPQRSTVWXYZ', 25)
);
}

/**
* Generates a [Nano ID](https://github.com/ai/nanoid).
*
Expand Down
30 changes: 30 additions & 0 deletions test/modules/__snapshots__/string.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ exports[`string > 42 > symbol > with length parameter 5`] = `">%*,/"`;

exports[`string > 42 > symbol > with length range 1`] = `"}\\>%%"\`>[!~_'&"`;

exports[`string > 42 > ulid 1`] = `"1YQK441VKP0ZT6559GD9K49BES"`;

exports[`string > 42 > ulid 2`] = `"0GJ1K52YYS93NE3F1X8N9GH5ZR"`;

exports[`string > 42 > ulid 3`] = `"2WKX261AC8TB8H4S2ZR60TPQR2"`;

exports[`string > 42 > ulid 4`] = `"13VKA29AQMWF3PRHRFGD031MAG"`;

exports[`string > 42 > ulid 5`] = `"27DR7295XSMVS5WHSWA37DTV0G"`;

exports[`string > 42 > uuid 1`] = `"5fb9220d-9b0f-4d32-a248-6492457c3890"`;

exports[`string > 42 > uuid 2`] = `"21ffc41a-7170-4e4a-9488-2fcfe9e13056"`;
Expand Down Expand Up @@ -338,6 +348,16 @@ exports[`string > 1211 > symbol > with length parameter 5`] = `"~]-|<"`;

exports[`string > 1211 > symbol > with length range 1`] = `"{(~@@],[_]?_.\`\`'=',~"`;

exports[`string > 1211 > ulid 1`] = `"2W7ZNNRBPTRMTDVV6J6BZRCXHZ"`;

exports[`string > 1211 > ulid 2`] = `"09PGBVC895HP4PF7D6VJF1P5P8"`;

exports[`string > 1211 > ulid 3`] = `"2GE6KV3VECFJSHXZTV7PTAW0HR"`;

exports[`string > 1211 > ulid 4`] = `"128486XZD6KJJKTMTRVXHAHEBT"`;

exports[`string > 1211 > ulid 5`] = `"0Y371XDXKYZBKRW4GPQ8TX4P2D"`;

exports[`string > 1211 > uuid 1`] = `"ee3faac5-bdca-4d6d-9d39-35fc6e8f34b8"`;

exports[`string > 1211 > uuid 2`] = `"d64428b2-b736-43d9-970b-2b4c8739d1d7"`;
Expand Down Expand Up @@ -512,6 +532,16 @@ exports[`string > 1337 > symbol > with length parameter 5`] = `"]'*@:"`;

exports[`string > 1337 > symbol > with length range 1`] = `"&)/+;)~\\$-?%"`;

exports[`string > 1337 > ulid 1`] = `"058EAG8ZQ3CM4ZESSBDJR69NF5"`;

exports[`string > 1337 > ulid 2`] = `"16HT5YDGG0QZ54BP0B1SBPFZTK"`;

exports[`string > 1337 > ulid 3`] = `"1Z802YZFAQ0RN6NX5XAP8G5EEC"`;

exports[`string > 1337 > ulid 4`] = `"2FQSBXPWDEWMH7YHR7DCG4N07Q"`;

exports[`string > 1337 > ulid 5`] = `"2KDVZ9CPB1BF1XQR31847NFXJ5"`;

exports[`string > 1337 > uuid 1`] = `"4247584f-b16a-42f7-8cc5-69c34a72638d"`;

exports[`string > 1337 > uuid 2`] = `"f6880bf2-25b0-450c-a5b7-fd99f401ff75"`;
Expand Down
10 changes: 10 additions & 0 deletions test/modules/string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ describe('string', () => {

t.itRepeated('uuid', 5);

t.itRepeated('ulid', 5);

t.describe('nanoid', (t) => {
t.itRepeated('noArgs', 5)
.it('with length parameter', 30)
Expand Down Expand Up @@ -750,6 +752,14 @@ describe('string', () => {
});
});

describe(`ulid`, () => {
it('generates a valid ULID', () => {
const ulid = faker.string.ulid();
const regex = /^[0-2][0-9A-HJKMNP-TV-Z]{25}$/;
matthewmayer marked this conversation as resolved.
Show resolved Hide resolved
expect(ulid).toMatch(regex);
});
});

describe(`nanoid`, () => {
it('generates a valid Nano ID', () => {
const id = faker.string.nanoid();
Expand Down