-
-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(string): adds support for generating ULID (#2524)
- Loading branch information
1 parent
2f93d9d
commit 5b1c858
Showing
9 changed files
with
182 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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); | ||
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 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 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 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