Điểm qua ai/nanoid: 26.9k sao trên GitHub, viết chủ yếu bằng JavaScript. A tiny (118 bytes), secure, URL-friendly, unique string ID generator for JavaScript
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Nano ID is built by Evil Martians, an American design and engineering consultancy for developer tools, AI, and cybersecurity startups.
Table of Contents
Table of Contents
Comparison with UUID
Benchmark
Security
Install
JSR
CDN
API
Custom Alphabet or Size
Custom Random Bytes Generator
Non-Secure
Usage
React
React Native
PouchDB and CouchDB
CLI
TypeScript
Other Programming Languages
Tools
Comparison with UUID
Nano ID is quite comparable to UUID v4 (random-based).
It has a similar number of random bits in the ID
(126 in Nano ID and 122 in UUID), so it has a similar collision probability:
For there to be a one in a billion chance of duplication,
103 trillion version 4 IDs must be generated.
There are two main differences between Nano ID and UUID v4:
Nano ID uses a bigger alphabet, so a similar number of random bits
are packed in just 21 symbols instead of 36.
Nano ID is faster than crypto.randomUUID and uuid/v4.
See a good article about random generators theory:
Secure random values (in Node.js)
Unpredictability. Instead of using the unsafe Math.random(), Nano ID
uses the crypto module in Node.js and the Web Crypto API in browsers.
These modules use unpredictable hardware random generator.
Uniformity.random % alphabet is a popular mistake to make when coding
an ID generator. The distribution will not be even; there will be a lower
chance for some symbols to appear compared to others. So, it will reduce
the number of tries when brute-forcing. Nano ID uses a better algorithm
and is tested for uniformity.
Well-documented: all Nano ID hacks are documented. See comments
in the source.
Vulnerabilities: to report a security vulnerability, please use
the Tidelift security contact.
Tidelift will coordinate the fix and disclosure.
Install
npm install nanoid
JSR
JSR is a replacement for npm with open governance
and active development (in contrast to npm).
npx jsr add @sitnik/nanoid
You can use it in Node.js, Deno, Bun, etc.
// Replace `nanoid` to `@sitnik/nanoid` in all imports
import { nanoid } from '@sitnik/nanoid'
For Deno install it by deno add jsr:@sitnik/nanoid or import
from jsr:@sitnik/nanoid.
CDN
For quick hacks, you can load Nano ID from CDN. Though, it is not recommended
to be used in production because of the lower loading performance.
import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'
API
By default, Nano ID uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID
with 21 characters (to have a collision probability similar to UUID v4).
Check the safety of your custom alphabet and ID size in our
ID collision probability calculator. For more alphabets, check out the options
in nanoid-dictionary.
Alphabet must contain 256 symbols or less.
Otherwise, the security of the internal generator algorithm is not guaranteed.
In addition to setting a default size, you can change the ID size when calling
the function:
Note, that between Nano ID versions we may change random generator
call sequence. If you are using seed-based generators, we do not guarantee
the same result.
Non-Secure
Nano ID uses hardware random bytes generation for security and low collision
probability. If you are not so concerned with security, you can use it
for environments without hardware random generators.
import { nanoid } from 'nanoid/non-secure'
const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
Note, that non-secure version is slower than secure.
Use it only if you have to.
Usage
React
There’s no correct way to use Nano ID for React key prop
since it should be consistent among renders.
function Todos({ todos }) {
return (
<ul>
{todos.map(todo => (
<li key={nanoid()}>
{' '}
/* DON’T DO IT */
{todo.text}
</li>
))}
</ul>
)
}
You should rather try to reach for stable ID inside your list item.
In case you don’t have stable IDs you'd rather use index as key
instead of nanoid():
const todoItems = todos.map((text, index) => (
<li key={index}>
{' '}
/* Still not recommended but preferred over nanoid(). Only do this if items
have no stable IDs. */
{text}
</li>
))
In case you just need random IDs to link elements like labels
and input fields together, useId is recommended.
That hook was added in React 18.
React Native
React Native does not have built-in random generator. The following polyfill
works for plain React Native and Expo starting with 39.x.
Check react-native-get-random-values docs and install it.
Import it before Nano ID.
import 'react-native-get-random-values'
import { nanoid } from 'nanoid'
PouchDB and CouchDB
In PouchDB and CouchDB, IDs can’t start with an underscore _.
A prefix is required to prevent this issue, as Nano ID might use a _
at the start of the ID by default.
Override the default ID with the following option:
db.put({
_id: 'id' + nanoid(),
…
})
CLI
You can get unique ID in terminal by calling npx nanoid. You need only
Node.js in the system. You do not need Nano ID to be installed anywhere.
$ npx nanoid
npx: installed 1 in 0.63s
LZfXLFzPPR4NNrgjlWDxn
Size of generated ID can be specified with --size (or -s) option:
$ npx nanoid --size 10
L3til0JS4z
Custom alphabet can be specified with --alphabet (or -a) option
(note that in this case --size is required):
Có — ai/nanoid phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/ai/nanoid.
ai/nanoid có trang demo không?
Dự án có trang chủ ở https://zelark.github.io/nano-id-cc/. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
ai/nanoid dùng license gì?
ai/nanoid phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
ai/nanoid là gì?
ai/nanoid (ai/nanoid) là dự án JavaScript trên GitHub. Theo mô tả gốc: A tiny (118 bytes), secure, URL-friendly, unique string ID generator for JavaScript
Đọc thêm về ai/nanoid ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/ai/nanoid là nguồn chính thức.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về nanoid?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về nanoid.