Skip to content

Commit

Permalink
feat: add ability to validate uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlescure committed Apr 29, 2024
1 parent 92ecf98 commit f9f5d45
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ console.log(result);
// Time: 63d5e631 ID: 0b-aaab
```

Ability to validate UUIDs against the instance dictionary or a provided dictionary for improved data integrity and consistency.

Example of using .validate() method:

```js
// Instantiate using one of the default dictionary strings
const uid = new ShortUniqueId({
dictionary: 'hex',
});

const uuid = uid.stamp(32); // Generate a UUID

// Validate the generated UUID against the instance dictionary
const isValid = uid.validate(uuid);

console.log(`Is the UUID valid? ${isValid}`);
``

### Use in CLI

```sh
Expand Down
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ export default class ShortUniqueId {
};
/* tslint:enable consistent-return */

/** Change the dictionary after initialization. */
setDictionary = (dictionary: string[] | ShortUniqueIdDefaultDictionaries, shuffle?: boolean): void => {
protected _normalizeDictionary = (dictionary: string[] | ShortUniqueIdDefaultDictionaries, shuffle?: boolean): string[] => {
let finalDict: string[];

if (dictionary && Array.isArray(dictionary) && dictionary.length > 1) {
Expand Down Expand Up @@ -248,7 +247,12 @@ export default class ShortUniqueId {
finalDict = finalDict.sort(() => Math.random() - PROBABILITY);
}

this.dict = finalDict;
return finalDict;
}

/** Change the dictionary after initialization. */
setDictionary = (dictionary: string[] | ShortUniqueIdDefaultDictionaries, shuffle?: boolean): void => {
this.dict = this._normalizeDictionary(dictionary, shuffle);

// Cache Dictionary Length for future usage.
this.dictLength = this.dict.length;
Expand Down Expand Up @@ -587,6 +591,15 @@ export default class ShortUniqueId {
this.counter = counter;
};

/**
* Validate given UID contains only characters from the instanced dictionary or optionally provided dictionary.
*/
validate = (uid: string, dictionary?: string[] | ShortUniqueIdDefaultDictionaries): boolean => {
const finalDictionary = dictionary ? this._normalizeDictionary(dictionary) : this.dict;

return uid.split('').every((c) => finalDictionary.includes(c));
};

constructor(argOptions: Partial<ShortUniqueIdOptions> = {}) {
const options: ShortUniqueIdOptions = {
...DEFAULT_OPTIONS,
Expand Down
52 changes: 52 additions & 0 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,55 @@ test({
assert(+parsedStamp === nowStamp);
},
});

test({
name: 'ability to validate UUID against instance dictionary',
fn(): void {
const { validate, fmt } = new ShortUniqueId({
dictionary: ['a', 'b'],
});

assert(validate('ab'));

assert(validate('b'));

assert(validate('abba'));

assert(!validate('abc'));

// Formatted UIDs are custom, thus user should implement their own validation
assert(!validate(fmt('$r2-$t12')));
},
});

test({
name: 'ability to validate UUID against custom dictionary',
fn(): void {
const { validate } = new ShortUniqueId({
dictionary: ['a', 'b'],
});

assert(validate('dc', ['c', 'd']));

assert(validate('dccd', ['c', 'd']));

assert(!validate('dccb', ['c', 'd']));
},
});

test({
name: 'ability to validate UUID against internal dictionary',
fn(): void {
const { validate, stamp } = new ShortUniqueId({
dictionary: ['a', 'b'],
});

assert(validate(stamp(32), 'hex'));

assert(!validate(stamp(32)));

assert(validate('0987654321fedcba', 'hex'));

assert(!validate('0987654321fedcbag', 'hex'));
},
});

0 comments on commit f9f5d45

Please sign in to comment.