Skip to content

Commit

Permalink
feat: add immutable default dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlescure committed May 4, 2021
1 parent f36f00a commit 3dd1026
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 172 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Also, the following changes might generate errors in some edge-cases:

### New Features 🥳

We have added the ability to generate UUIDs that contain a timestamp which can be extracted:
The ability to generate UUIDs that contain a timestamp which can be extracted:

```js
const uid = new ShortUniqueId();
Expand All @@ -65,6 +65,34 @@ console.log(uid.parseStamp(uidWithTimestamp));
// 2021-05-03T06:24:58.000Z
```

Default dictionaries (generated on the spot to reduce memory footprint and
avoid dictionary injection vulnerabilities):

- number
- alpha
- alpha_lower
- alpha_upper
- **alphanum** _(default when no dictionary is provided to `new ShortUniqueId()`)_
- alphanum_lower
- alphanum_upper
- hex

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

console.log(uid.dict.join());
// 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f

// or change the dictionary after instantiation
uid.setDictionary('alpha_upper');

console.log(uid.dict.join());
// A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
```

### Use in CLI

```sh
Expand All @@ -85,16 +113,22 @@ $ short-unique-id -h

### Use as module

Add to your project:

```js
// Deno (web module) Import
import ShortUniqueId from 'https://cdn.jsdelivr.net/npm/short-unique-id@latest/src/index.ts';

// ES6 / TypeScript Import
import ShortUniqueId from 'short-unique-id';

//or Node.js require
// Node.js require
const ShortUniqueId = require('short-unique-id');
```

Instantiate and use:

```js
//Instantiate
const uid = new ShortUniqueId();

Expand All @@ -108,7 +142,7 @@ console.log(uid.seq());
### Use in browser

```html
<!-- Import -->
<!-- Add source (minified 6.9K) -->
<script src="https://cdn.jsdelivr.net/npm/short-unique-id@latest/dist/short-unique-id.min.js"></script>

<!-- Usage -->
Expand Down
2 changes: 1 addition & 1 deletion bin/short-unique-id
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
const fs = require('fs');
const assert = require('assert');
const { default: ShortUniqueId } = require('short-unique-id');
const ShortUniqueId = require('short-unique-id');

const [,, ...args] = process.argv;

Expand Down
2 changes: 2 additions & 0 deletions dist/dist.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import ShortUniqueId from './';
export default ShortUniqueId;
24 changes: 22 additions & 2 deletions dist/short-unique-id.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
interface ShortUniqueIdRanges {
[k: string]: [number, number];
}
declare type defaultDictionaries = 'number' | 'alpha' | 'alpha_lower' | 'alpha_upper' | 'alphanum' | 'alphanum_lower' | 'alphanum_upper' | 'hex';
/**
* ```js
* {
Expand All @@ -10,7 +14,7 @@
*/
export interface ShortUniqueIdOptions {
/** User-defined character dictionary */
dictionary: string[];
dictionary: string[] | defaultDictionaries;
/** If true, sequentialUUID use the dictionary in the given order */
shuffle: boolean;
/** If true the instance will console.log useful info */
Expand Down Expand Up @@ -96,9 +100,24 @@ export default class ShortUniqueId extends Function {
upperBound: number;
dictLength: number;
uuidLength: number;
protected _digit_first_ascii: number;
protected _digit_last_ascii: number;
protected _alpha_lower_first_ascii: number;
protected _alpha_lower_last_ascii: number;
protected _hex_last_ascii: number;
protected _alpha_upper_first_ascii: number;
protected _alpha_upper_last_ascii: number;
protected _number_dict_ranges: ShortUniqueIdRanges;
protected _alpha_dict_ranges: ShortUniqueIdRanges;
protected _alpha_lower_dict_ranges: ShortUniqueIdRanges;
protected _alpha_upper_dict_ranges: ShortUniqueIdRanges;
protected _alphanum_dict_ranges: ShortUniqueIdRanges;
protected _alphanum_lower_dict_ranges: ShortUniqueIdRanges;
protected _alphanum_upper_dict_ranges: ShortUniqueIdRanges;
protected _hex_dict_ranges: ShortUniqueIdRanges;
protected log: (...args: any[]) => void;
/** Change the dictionary after initialization. */
setDictionary: (dictionary: string[]) => void;
setDictionary: (dictionary: string[] | defaultDictionaries, shuffle?: boolean | undefined) => void;
seq: () => string;
/**
* Generates UUID based on internal counter that's incremented after each ID generation.
Expand Down Expand Up @@ -219,3 +238,4 @@ export default class ShortUniqueId extends Function {
parseStamp: (stamp: string) => Date;
constructor(argOptions?: Partial<ShortUniqueIdOptions>);
}
export {};
119 changes: 72 additions & 47 deletions dist/short-unique-id.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/short-unique-id.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 3dd1026

Please sign in to comment.