-
-
Notifications
You must be signed in to change notification settings - Fork 980
feat(string): adds support for generating ULID #2524
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
Merged
Shinigami92
merged 49 commits into
faker-js:next
from
brunocleite:feat/adds-support-for-ulid-generation
Oct 10, 2024
Merged
Changes from 45 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
4bb549a
feat(string): adds support for generating ULID
brunocleite 493c3e0
Merge branch 'next' into feat/adds-support-for-ulid-generation
brunocleite b6dc97b
Merge branch 'next' into feat/adds-support-for-ulid-generation
brunocleite b63d07b
Merge branch 'next' into feat/adds-support-for-ulid-generation
ST-DDT 9ba1bbe
fix(string): update ulid values (#2648)
brunocleite 12fed6e
Merge remote-tracking branch 'origin/feat/adds-support-for-ulid-gener…
brunocleite f41fd27
Merge branch 'next' into feat/adds-support-for-ulid-generation
ST-DDT 6e7a967
feat(string): updates ulid implementation using refDate
brunocleite 70a4bf4
Merge remote-tracking branch 'origin/feat/adds-support-for-ulid-gener…
brunocleite 702061d
feat(string): updates ulid refDate documentation
brunocleite 6fcba2c
fix(string): updates ulid regex to match all possible ULID values
brunocleite 805cfbd
Merge branch 'next' into feat/adds-support-for-ulid-generation
ST-DDT 23bbf60
fix(string): uses up-to-date standard on handling date errors and upd…
brunocleite 1ca84cc
Update src/modules/string/index.ts
brunocleite e762041
feat(internal): move 'toDate' function into an internal helper and up…
brunocleite 10794e1
Merge remote-tracking branch 'origin/feat/adds-support-for-ulid-gener…
brunocleite 66158c3
feat(string): adds ulid example with refDate
brunocleite 8d16f2b
feat(string): extract base32 methods into its own file
brunocleite 9c53fb5
feat(internal): adds tests for toDate
brunocleite ccadadf
feat(internal): adds tests for base32
brunocleite 43141fb
feat(string): fix typo on ulid jsdoc
brunocleite 56c57ea
feat(string): fix linting errors
brunocleite 54096d7
Update src/internal/base32.ts
brunocleite 1c4d39d
fix(string): fix ulid comment
brunocleite 0a824fa
Update src/modules/string/index.ts
brunocleite 49654c2
Merge remote-tracking branch 'origin/feat/adds-support-for-ulid-gener…
brunocleite 2f5aa42
fix(string): adds test to match base32 characters and toMatchSnapshot()
brunocleite 34316a6
Update src/internal/base32.ts
brunocleite c3b8697
Update src/internal/base32.ts
brunocleite ec1fd5b
Update test/internal/base32.spec.ts
brunocleite 7e6ba0a
fix(string): refactoring and lint fixes
brunocleite 09b6523
Merge remote-tracking branch 'origin/feat/adds-support-for-ulid-gener…
brunocleite eb25568
Merge branch 'next' into feat/adds-support-for-ulid-generation
brunocleite d1d3d23
Update src/modules/string/index.ts
brunocleite 1d318b4
Update src/modules/string/index.ts
brunocleite 9941efa
Merge branch 'next' into feat/adds-support-for-ulid-generation
ST-DDT c7080e1
Update test/internal/base32.spec.ts
brunocleite 52c2872
Update src/modules/string/index.ts
brunocleite af7376d
Update test/internal/date.spec.ts
brunocleite 1d63041
Update test/modules/string.spec.ts
brunocleite b02d751
fix(string): import fix and test snapshot
brunocleite d7ee011
Merge branch 'next' into feat/adds-support-for-ulid-generation
brunocleite 8f9aecc
Merge branch 'next' into feat/adds-support-for-ulid-generation
ST-DDT 8acad7a
Merge branch 'next' into feat/adds-support-for-ulid-generation
ST-DDT ddd7e59
Merge branch 'next' into feat/adds-support-for-ulid-generation
ST-DDT 9646279
Merge remote-tracking branch 'faker/next' into feat/adds-support-for-…
brunocleite f809eef
🎉 feat(snapshot): add snapshot for dateToBase32 encoding test
brunocleite 16517df
Merge branch 'next' into feat/adds-support-for-ulid-generation
xDivisionByZerox c8347cd
Merge branch 'next' into feat/adds-support-for-ulid-generation
Shinigami92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Crockford's Base32 - Excludes I, L, O, and U which may be confused with numbers | ||
*/ | ||
export const CROCKFORDS_BASE32 = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; | ||
|
||
/** | ||
* Encodes a Date into 10 characters base32 string. | ||
* | ||
* @param date The Date to encode. | ||
*/ | ||
export function dateToBase32(date: Date): string { | ||
let value = date.valueOf(); | ||
let result = ''; | ||
for (let len = 10; len > 0; len--) { | ||
const mod = value % 32; | ||
result = CROCKFORDS_BASE32[mod] + result; | ||
value = (value - mod) / 32; | ||
} | ||
|
||
return result; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { FakerError } from '../errors/faker-error'; | ||
|
||
/** | ||
* Converts a date passed as a `string`, `number` or `Date` to a valid `Date` object. | ||
* | ||
* @param date The date to convert. | ||
* @param name The reference name used for error messages. Defaults to `'refDate'`. | ||
* | ||
* @throws If the given date is invalid. | ||
*/ | ||
export function toDate( | ||
date: string | Date | number, | ||
name: string = 'refDate' | ||
): Date { | ||
const converted = new Date(date); | ||
|
||
if (Number.isNaN(converted.valueOf())) { | ||
throw new FakerError(`Invalid ${name} date: ${date.toString()}`); | ||
} | ||
|
||
return converted; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`dateToBase32() > encodes current date correctly 1`] = `"01GWX1T800"`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { CROCKFORDS_BASE32, dateToBase32 } from '../../src/internal/base32'; | ||
|
||
describe('dateToBase32()', () => { | ||
it('encodes current date correctly', () => { | ||
const date = new Date('2023-04-01T00:00:00Z'); | ||
const encoded = dateToBase32(date); | ||
expect(encoded).toHaveLength(10); | ||
brunocleite marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(encoded).toMatchSnapshot(); | ||
for (const char of encoded) { | ||
expect(CROCKFORDS_BASE32).toContain(char); | ||
} | ||
}); | ||
|
||
it('encodes epoch start date correctly', () => { | ||
const date = new Date('1970-01-01T00:00:00Z'); | ||
const encoded = dateToBase32(date); | ||
expect(encoded).toBe('0000000000'); | ||
}); | ||
|
||
it('returns different encodings for dates one millisecond apart', () => { | ||
const date1 = new Date('2023-04-01T00:00:00.000Z'); | ||
const date2 = new Date('2023-04-01T00:00:00.001Z'); | ||
const encoded1 = dateToBase32(date1); | ||
const encoded2 = dateToBase32(date2); | ||
expect(encoded1).not.toBe(encoded2); | ||
}); | ||
|
||
it('encodes same date consistently', () => { | ||
const date = new Date('2023-04-01T00:00:00Z'); | ||
const encoded1 = dateToBase32(date); | ||
const encoded2 = dateToBase32(date); | ||
expect(encoded1).toBe(encoded2); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { FakerError } from '../../src'; | ||
import { toDate } from '../../src/internal/date'; | ||
|
||
describe('toDate()', () => { | ||
it('should convert a string date to a valid Date object', () => { | ||
const dateString = '2024-07-05'; | ||
expect(toDate(dateString)).toEqual(new Date(dateString)); | ||
}); | ||
|
||
it('should convert a string datetime to a valid Date object', () => { | ||
const timestamp = '2024-07-05T15:49:19+0000'; | ||
expect(toDate(timestamp)).toEqual(new Date(timestamp)); | ||
}); | ||
|
||
it('should throw a FakerError for an invalid date string', () => { | ||
const timestamp = 'aaaa-07-05T15:49:19+0000'; | ||
expect(() => toDate(timestamp)).toThrow( | ||
new FakerError(`Invalid refDate date: ${timestamp}`) | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.