Skip to content

Commit

Permalink
Merge pull request #2 from jeanlescure/add-length
Browse files Browse the repository at this point in the history
Add length option
  • Loading branch information
jeanlescure authored Apr 27, 2020
2 parents 0581777 + b1088ce commit 886e5f4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions mod.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ type Options = {
dictionary: string[],
skipShuffle: boolean,
debug: boolean,
length: number,
};
10 changes: 8 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// @deno-types="./mod.d.ts"
import { version } from './version.json';

const DEFAULT_RANDOM_ID_LEN: number = 6;
const DEFAULT_ID_LENGTH: number = 6;

const DIGIT_FIRST_ASCII: number = 48;
const DIGIT_LAST_ASCII: number = 58;
Expand All @@ -21,6 +21,7 @@ const DEFAULT_OPTIONS: Options = {
dictionary: [],
skipShuffle: false,
debug: false,
length: DEFAULT_ID_LENGTH,
};

class ShortUniqueId extends Function {
Expand All @@ -42,6 +43,8 @@ class ShortUniqueId extends Function {

dictLength: number = 0;

uuidLength: number;

/* tslint:disable consistent-return */
log(...args: any[]) {
const finalArgs = [...args];
Expand Down Expand Up @@ -72,8 +75,11 @@ class ShortUniqueId extends Function {
const {
dictionary: userDict,
skipShuffle,
length,
} = options;

this.uuidLength = length;

if (userDict && userDict.length > 1) {
this.setDictionary(userDict);
} else {
Expand Down Expand Up @@ -158,7 +164,7 @@ class ShortUniqueId extends Function {
}

/* Generates UUID by creating each part randomly. */
randomUUID(uuidLength: number = DEFAULT_RANDOM_ID_LEN) {
randomUUID(uuidLength: number = this.uuidLength) {
let id;
let randomPartIdx;
let j;
Expand Down
3 changes: 2 additions & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ test({
const uid: ShortUniqueId = new ShortUniqueId({
dictionary: ['a', '1'],
skipShuffle: true,
length: 2,
});
/* tslint:disable no-magic-numbers */
assert((/^[a1][a1]$/).test(uid(2)));
assert((/^[a1][a1]$/).test(uid()));
/* tslint:enable no-magic-numbers */
assertEquals(
[uid.sequentialUUID(), uid.sequentialUUID()].join(''),
Expand Down

0 comments on commit 886e5f4

Please sign in to comment.