{"id":15486014,"url":"https://github.com/ianfabs/nanoid","last_synced_at":"2025-04-15T20:24:26.010Z","repository":{"id":40341990,"uuid":"237105553","full_name":"ianfabs/nanoid","owner":"ianfabs","description":"A NanoID implementation for Deno","archived":false,"fork":false,"pushed_at":"2022-04-25T06:59:30.000Z","size":26,"stargazers_count":72,"open_issues_count":4,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-14T21:37:52.023Z","etag":null,"topics":["deno","javascript","js","nanoid","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ianfabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-29T23:50:21.000Z","updated_at":"2024-01-12T06:46:46.000Z","dependencies_parsed_at":"2022-08-09T17:41:09.559Z","dependency_job_id":null,"html_url":"https://github.com/ianfabs/nanoid","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianfabs%2Fnanoid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianfabs%2Fnanoid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianfabs%2Fnanoid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ianfabs%2Fnanoid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ianfabs","download_url":"https://codeload.github.com/ianfabs/nanoid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249146584,"owners_count":21220169,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deno","javascript","js","nanoid","typescript"],"created_at":"2024-10-02T06:05:42.084Z","updated_at":"2025-04-15T20:24:25.971Z","avatar_url":"https://github.com/ianfabs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nano ID\nA port of [nanoid] to Deno\n\nA tiny, secure, URL-friendly, unique string ID generator for JavaScript.\n\n```js\nimport { nanoid } from \"https://deno.land/x/nanoid/mod.ts\"\nnanoid() //=\u003e \"lQLTBJKVRCuc\"\n```\n\n## Table of Contents\n\n1. [Comparison with UUID](#comparison-with-uuid)\n4. [Tools](#tools)\n3. [Security](#security)\n6. Usage\n   1. [JS](#js)\n   2. [Other Programming Languages](#other-programming-languages)\n5. CLI\n   1. [Installation](#installation)\n   2. [Usage](#usage-1)\n7. API\n   1. [Async](#async)\n   3. [Custom Alphabet or Length](#custom-alphabet-or-length)\n   4. [Custom Random Bytes Generator](#custom-random-bytes-generator)\n\n\n## Comparison with UUID\n\nNano ID is quite comparable to UUID v4 (random-based).\nIt has a similar number of random bits in the ID\n(126 in Nano ID and 122 in UUID), so it has a similar collision probability:\n\n\u003e For there to be a one in a billion chance of duplication,\n\u003e 103 trillion version 4 IDs must be generated.\n\nThere are three main differences between Nano ID and UUID v4:\n\n1. Nano ID uses a bigger alphabet, so a similar number of random bits\n   are packed in just 21 symbols instead of 36.\n2. Nano ID code is 4 times less than `uuid/v4` package:\n   127 bytes instead of 435.\n3. Because of memory allocation tricks, Nano ID is 16% faster than UUID.\n\n## Tools\n\n* [ID size calculator] to choice smaller ID size depends on your case.\n* [`nanoid-dictionary`] with popular alphabets to use with `nanoid/generate`.\n\n[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary\n[ID size calculator]:  https://zelark.github.io/nano-id-cc/\n\n\n## Security\n\n*See a good article about random generators theory:\n[Secure random values (in Node.js)]*\n\n\n### Unpredictability\n\nInstead of using the unsafe `Math.random()`, Nano ID uses the Web Crypto API. These modules use unpredictable\nhardware random generator.\n\n## Usage\n\n### JS\n\nThe main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID\nwith 21 characters (to have a collision probability similar to UUID v4).\n\n```js\nimport { nanoid } from \"https://deno.land/x/nanoid/mod.ts\"\nnanoid() //=\u003e \"lQLTBJKVRCuc\"\n```\n\nIf you want to reduce ID length (and increase collisions probability),\nyou can pass the length as an argument.\n\n```js\nnanoid(10) //=\u003e \"IRFa-VaY2b\"\n```\n\nDon’t forget to check the safety of your ID length\nin our [ID collision probability] calculator.\n\n[ID collision probability]: https://zelark.github.io/nano-id-cc/\n[nanoid]: https://github.com/ai/nanoid\n\n\n### Other Programming Languages\n\nNano ID was ported to many languages. You can use these ports to have the same\nID generators on client and server side.\n\n* [Node.js](https://github.com/ai/nanoid)\n* [C#](https://github.com/codeyu/nanoid-net)\n* [Clojure and ClojureScript](https://github.com/zelark/nano-id)\n* [Crystal](https://github.com/mamantoha/nanoid.cr)\n* [Dart](https://github.com/pd4d10/nanoid-dart)\n* [Go](https://github.com/matoous/go-nanoid)\n* [Elixir](https://github.com/railsmechanic/nanoid)\n* [Haskell](https://github.com/4e6/nanoid-hs)\n* [Java](https://github.com/aventrix/jnanoid)\n* [Nim](https://github.com/icyphox/nanoid.nim)\n* [PHP](https://github.com/hidehalo/nanoid-php)\n* [Python](https://github.com/puyuan/py-nanoid) with [dictionaries](https://pypi.org/project/nanoid-dictionary)\n* [Ruby](https://github.com/radeno/nanoid.rb)\n* [Rust](https://github.com/nikolay-govorov/nanoid)\n* [Swift](https://github.com/antiflasher/NanoID)\n\nAlso, CLI tool is available to generate IDs from a command line.\n\n```sh\n$ deno run https://deno.land/x/nanoid/cli.ts\n```\n\n## CLI\n\n### Installation\n\n```sh\n$ deno install --name nanoid https://deno.land/x/nanoid/cli.ts\n```\n\n### Usage\n\n```\n  Usage:   nanoid\n  Version: v2.0.0\n\n  Description:\n\n    A CLI for generating cryptographically-secure random IDs.\n\n  Options:\n\n    -h, --help                         - Show this help.                                                             \n    -V, --version                      - Show the version number for this program.                                   \n    -s, --size      \u003csize:number\u003e      - The desired length of IDs to be generated.                                  \n    -a, --alphabet  \u003calphabet:string\u003e  - The alphabet that IDs should be generated with.                             \n    -n, --number    \u003cn:number\u003e         - The number of IDs to generate, if you would like more than one  (Default: 1)\n\n  Commands:\n\n    completions  - Generate shell completions.\n```\n\n## API\n\n### Async\n\nTo generate hardware random bytes, CPU will collect electromagnetic noise.\nDuring the collection, CPU doesn’t work.\n\nIf we will use asynchronous API for random generator,\nanother code could be executed during the entropy collection.\n\n```js\nimport { nanoid } from \"https://deno.land/x/nanoid/async.ts\";\n\nasync function createUser () {\n  user.id = await nanoid();\n}\n```\n\n\n### Custom Alphabet or Length\n\nIf you want to change the ID's alphabet or length\nyou can use the low-level `generate` module.\n\n```js\nimport { customAlphabet } from \"https://deno.land/x/nanoid/customAlphabet.ts\";\nconst nanoid = customAlphabet('1234567890abcdef', 10)\nnanoid()  // =\u003e \"4f90d13a42\"\n```\n\nAlphabet must contain 256 symbols or less.\nOtherwise, the generator will not be secure.\n\nAsynchronous API is also available:\n\n```js\nimport { customAlphabet } from \"https://deno.land/x/nanoid/async.ts\";\nconst nanoid = await customAlphabet('1234567890abcdef', 10);\nasync function createUser() {\n  let id = nanoid();\n}\n```\n\n### Custom Random Bytes Generator\n\nYou can replace the default safe random generator using the `format` module.\nFor instance, to use a seed-based generator.\n\n```js\nimport { customRandom } from \"https://deno.land/x/nanoid/customRandom.ts\";\n\nfunction random (size) {\n  const result = []\n  for (let i = 0; i \u003c size; i++) {\n    result.push(randomByte())\n  }\n  return result;\n}\n\nconst nanoid = customRandom(random, \"abcdef\", 10);\nnanoid(); // =\u003e \"fbaefaadeb\"\n```\n\n`random` callback must accept the array size and return an array\nwith random numbers.\n\nIf you want to use the same URL-friendly symbols with `format`,\nyou can get the default alphabet from the `url` file.\n\n```js\nimport { customRandom } from \"https://deno.land/x/nanoid/customRandom.ts\";\nimport { urlAlphabet } from \"https://deno.land/x/nanoid/urlAlphabet.ts\";\n\nconst nanoid = customRandom(random, chars, 10);\nnanoid(); // =\u003e \"93ce_Ltuub\"\n```\n\nAsynchronous API is also available:\n\n```js\nimport { customRandom } from 'https://deno.land/x/nanoid/async.ts';\nimport { urlAlphabet } from \"https://deno.land/x/nanoid/urlAlphabet.ts\";\n\nfunction random (size) {\n  return Promise.resolve(/*…*/)\n}\nconst nanoid = await customRandom(random, url, 10);\nasync function createUser () {\n  let id = nanoid(); // =\u003e \"93ce_Ltuub\"\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianfabs%2Fnanoid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fianfabs%2Fnanoid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fianfabs%2Fnanoid/lists"}