Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dailyrandomphoto committed Oct 30, 2019
0 parents commit 6135875
Show file tree
Hide file tree
Showing 15 changed files with 458 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
package-lock.json
yarn.lock
.nyc_output/
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js

node_js:
- "8"
- "10"
- "12"
- "13"

script:
- npm run test-cov

after_script:
- npm install coveralls
- nyc report --reporter=text-lcov | coveralls
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 dailyrandomphoto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# id-generators

[![NPM Version][npm-version-image]][npm-url]
[![LICENSE][license-image]][license-url]
[![Build Status][travis-image]][travis-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![dependencies Status][dependencies-image]][dependencies-url]
[![devDependencies Status][devDependencies-image]][devDependencies-url]

**id-generators** is small JavaScript library to generate ID with awesome Unique ID libraries.

## Installation

```sh
npm install id-generators
```

## Usages

```js
const generators = require('id-generators');
const generator = generators.get('nanoid');
const generate = generator();

console.log('ID: ' + generate());
// ID: gCa0wL_8ElTje5cwOci1d
console.log('ID: ' + generate());
// ID: 5C_YAjgQl4iM3zK-TValY
console.log('ID: ' + generate());
// ID: gQOOwCoQyfCuGK7fgINLd
```

If you want to customize ID, you can pass an option as an argument to the `generator` function.
```js
const generators = require('id-generators');
const generator = generators.get('nanoid-simple');
const generate = generator({size: 25});

console.log('ID: ' + generate());
// ID: oa9wj0kfm50gv2qse8l5xup0u
console.log('ID: ' + generate());
// ID: xn5ff5odiylg4jehhhp9vtlxv
console.log('ID: ' + generate());
// ID: dff7ay5x38k1pkc2pxjv7elky
```

generator type | ID length | character set |options/default | description
--- | --- | --- | --- | ---
cuid (default) | 25 | `a-z0-9`, start with `c` | | use [cuid()](https://github.com/ericelliott/cuid) generated string. <br>e.g. `ck2bi7fxf00013ryng5jr1rer`
cuid-slug | 7-10 | `a-z0-9` | | use [cuid.slug()](https://github.com/ericelliott/cuid) generated string. <br>e.g. `xh23npi`
nanoid | 21 | `A-Za-z0-9_-` | size/21 | use [nanoid()](https://github.com/ai/nanoid) generated string. <br>e.g. `EwUTt2eoka-oEV5kf-o0O`
nanoid-simple | 24 | `a-z0-9` | size/24 | use [nanoid/generate](https://github.com/ai/nanoid) generated string. <br>e.g. `pfldm3gg8h9psydphotqe71d`
nanoid-lowercase | 26 | `a-z` | size/26 | use [nanoid/generate](https://github.com/ai/nanoid) generated string. <br>e.g. `jsjxoibprplrdoitjmppotjrnm`


## Define Custom Generators
This sample shows how to register a generator function.
The generator function should return a function that returns a ID.

```js
const { register } = require('id-generators');

register('my_custom_id', function(option) {
option = option || {};
let size = option.size || 8;
let prefix = option.prefix || 'items-';
return function(title) {
return prefix + title.toLowerCase().replace(/[^\w]/g, '').substring(0, size);
};
});
```
```js
const generators = require('id-generators');
const generator = generators.get('my_custom_id');
const generate = generator({size: 6});

console.log('ID: ' + generate('Hello World!'));
// ID: items-hellow
console.log('ID: ' + generate('Foo Bar!'));
// ID: items-foobar
```

## Related
- [Awesome Unique ID](https://github.com/grantcarthew/awesome-unique-id) - A curated list of awesome Unique ID libraries and resources.
- [cuid](https://github.com/ericelliott/cuid) - Collision-resistant ids optimized for horizontal scaling and binary search lookup performance.
- [nanoid](https://github.com/ai/nanoid) - A tiny, secure, URL-friendly, unique string ID generator for JavaScript.


## License
Copyright (c) 2019 dailyrandomphoto. Licensed under the [MIT license][license-url].

[npm-url]: https://www.npmjs.com/package/id-generators
[travis-url]: https://travis-ci.org/dailyrandomphoto/id-generators
[coveralls-url]: https://coveralls.io/github/dailyrandomphoto/id-generators?branch=master
[license-url]: LICENSE
[dependencies-url]: https://david-dm.org/dailyrandomphoto/id-generators
[devDependencies-url]: https://david-dm.org/dailyrandomphoto/id-generators?type=dev

[npm-downloads-image]: https://img.shields.io/npm/dm/id-generators.svg
[npm-version-image]: https://img.shields.io/npm/v/id-generators.svg
[license-image]: https://img.shields.io/npm/l/id-generators.svg
[travis-image]: https://img.shields.io/travis/dailyrandomphoto/id-generators/master
[coveralls-image]: https://coveralls.io/repos/github/dailyrandomphoto/id-generators/badge.svg?branch=master
[dependencies-image]: https://david-dm.org/dailyrandomphoto/id-generators/status.svg
[devDependencies-image]: https://david-dm.org/dailyrandomphoto/id-generators/dev-status.svg
7 changes: 7 additions & 0 deletions lib/cuid-slug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const {slug} = require('cuid');

module.exports = function () {
return () => slug();
};
7 changes: 7 additions & 0 deletions lib/cuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const cuid = require('cuid');

module.exports = function () {
return cuid;
};
29 changes: 29 additions & 0 deletions lib/id-generators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const store = {};

function register(type, fn) {
if (typeof fn !== 'function') {
throw new TypeError('fn must be a function');
}

if (typeof store[type] === 'function') {
throw new TypeError('already registered as type: ' + type);
}

store[type] = fn;
}

function get(type) {
type = type || 'default';
return store[type] || throwIt(new TypeError('Can\'t find generator with type: ' + type));
}

function throwIt(err) {
throw err;
}

module.exports = {
register,
get
};
16 changes: 16 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const {register, get} = require('./id-generators');
const cuid = require('./cuid');

register('default', cuid);
register('cuid', cuid);
register('cuid-slug', require('./cuid-slug'));
register('nanoid', require('./nanoid'));
register('nanoid-simple', require('./nanoid-simple'));
register('nanoid-lowercase', require('./nanoid-lowercase'));

module.exports = {
register,
get
};
13 changes: 13 additions & 0 deletions lib/nanoid-lowercase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

/**
* https://github.com/ai/nanoid
*/
const generate = require('nanoid/generate');
const alphabet = 'abcdefghijklmnopqrstuvwxyz';

module.exports = function (option) {
option = option || {};
const size = option.size || 26;
return () => generate(alphabet, size);
};
13 changes: 13 additions & 0 deletions lib/nanoid-simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

/**
* https://github.com/ai/nanoid
*/
const generate = require('nanoid/generate');
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';

module.exports = function (option) {
option = option || {};
const size = option.size || 24;
return () => generate(alphabet, size);
};
12 changes: 12 additions & 0 deletions lib/nanoid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

/**
* https://github.com/ai/nanoid
*/
const nanoid = require('nanoid');

module.exports = function (option) {
option = option || {};
const size = option.size || 21;
return () => nanoid(size);
};
58 changes: 58 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "id-generators",
"version": "1.0.0",
"description": "id-generators is small JavaScript library to generate ID with awesome Unique ID libraries.",
"main": "lib/index.js",
"scripts": {
"eslint": "xo --fix",
"test": "xo && mocha test/index.js",
"test-cov": "nyc npm run test"
},
"dependencies": {
"cuid": "^2.1.6",
"nanoid": "^2.1.6"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.2.1",
"nyc": "^14.1.1",
"xo": "^0.25.3"
},
"keywords": [
"generate",
"generator",
"id",
"uuid",
"unique-id",
"cuid",
"nanoid",
"url"
],
"files": [
"lib/",
"LICENSE",
"README.md"
],
"engines": {
"node": ">=8.6.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dailyrandomphoto/id-generators.git"
},
"author": "dailyrandomphoto <[email protected]> (https://www.dailyrandomphoto.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/dailyrandomphoto/id-generators/issues"
},
"homepage": "https://github.com/dailyrandomphoto/id-generators#readme",
"xo": {
"space": 2,
"overrides": [
{
"files": "test/*.js",
"envs": ["node", "mocha"]
}
]
}
}
Loading

0 comments on commit 6135875

Please sign in to comment.