Skip to content

Commit

Permalink
✨ Nano ID catmod of the same-named tiny library by Andrei Sitnik
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoMyzrailGorynych committed Feb 20, 2021
1 parent b67429f commit 21caa7c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/data/ct.libs/nanoid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Nano ID for ct.js

This catmod is a bit modified and minified `nanoid` [npm module](https://github.com/ai/nanoid). The original module is made by Andrey Sitnik and dozens of contributors, and is distributed under the MIT license.

Nano ID is an URL-friendly, secure ID generator that is shorter but more collision-resilient than GUID.

# Usage

```js
// Regular ID
var id = ct.nanoid.generate() //=> "V1StGXR8_Z5jdHi6B-myT"
// An ID of a specific size
var shortId = ct.nanoid.generate(10) //=> "IRFa-VaY2b"
```
26 changes: 26 additions & 0 deletions app/data/ct.libs/nanoid/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Nano ID (https://github.com/ai/nanoid)
The MIT License (MIT)
Copyright 2017 Andrey Sitnik <[email protected]>
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.
*/

(function(exports){'use strict';let urlAlphabet='ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';if(typeof crypto==='undefined'){throw new Error('Your browser does not have secure random generator. If you don’t need unpredictable IDs, you can use nanoid/non-secure.')}let random=bytes=>crypto.getRandomValues(new Uint8Array(bytes));let customRandom=(alphabet,size,getRandom)=>{let mask=(2<<(Math.log(alphabet.length-1)/Math.LN2))-1;let step= - ~((1.6*mask*size)/alphabet.length);return()=>{let id='';while(true){let bytes=getRandom(step);let j=step;while(j--){id+=alphabet[bytes[j]&mask]||'';if(id.length===size){return id}}}}};let customAlphabet=(alphabet,size)=>customRandom(alphabet,size,random);let nanoid=(size=21)=>{let id='';let bytes=crypto.getRandomValues(new Uint8Array(size));while(size--){let byte=bytes[size]&63;if(byte<36){id+=byte.toString(36)}else if(byte<62){id+=(byte-26).toString(36).toUpperCase()}else if(byte<63){id+='_'}else{id+='-'}}return id};exports.customAlphabet=customAlphabet;exports.customRandom=customRandom;exports.generate=nanoid;exports.random=random;exports.urlAlphabet=urlAlphabet;return exports}(ct.nanoid={}));
14 changes: 14 additions & 0 deletions app/data/ct.libs/nanoid/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"main": {
"name": "Nano ID",
"tagline": "A tiny, secure, URL-friendly, unique string ID generator for JavaScript, bundled for ct.js.",
"version": "3.1.20",
"packageName": "nanoid",
"authors": [{
"name": "Andrey Sitnik and contributors"
}],
"categories": [
"utilities"
]
}
}

0 comments on commit 21caa7c

Please sign in to comment.