nanoid
Advanced tools
Comparing version 3.1.20 to 3.1.21
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
export function nanoid (size?: number): Promise<string> | ||
export function nanoid(size?: number): Promise<string> | ||
@@ -39,3 +39,3 @@ /** | ||
*/ | ||
export function customAlphabet ( | ||
export function customAlphabet( | ||
alphabet: string, | ||
@@ -58,2 +58,2 @@ size: number | ||
*/ | ||
export function random (bytes: number): Promise<Uint8Array> | ||
export function random(bytes: number): Promise<Uint8Array> |
@@ -17,4 +17,3 @@ // This file replaces `index.js` in bundlers like webpack or Rollup, | ||
'For secure IDs, import `react-native-get-random-values` ' + | ||
'before Nano ID. If you use Expo, install `expo-random` ' + | ||
'and use `nanoid/async`.' | ||
'before Nano ID.' | ||
) | ||
@@ -21,0 +20,0 @@ } |
@@ -15,3 +15,3 @@ /** | ||
*/ | ||
export function nanoid (size?: number): string | ||
export function nanoid(size?: number): string | ||
@@ -34,3 +34,3 @@ /** | ||
*/ | ||
export function customAlphabet (alphabet: string, size: number): () => string | ||
export function customAlphabet(alphabet: string, size: number): () => string | ||
@@ -62,3 +62,3 @@ /** | ||
*/ | ||
export function customRandom ( | ||
export function customRandom( | ||
alphabet: string, | ||
@@ -74,3 +74,3 @@ size: number, | ||
* import { urlAlphabet } from 'nanoid' | ||
* const nanoid = customRandom(urlAlphabet, 10) | ||
* const nanoid = customAlphabet(urlAlphabet, 10) | ||
* nanoid() //=> "Uakgb_J5m9" | ||
@@ -92,2 +92,2 @@ * ``` | ||
*/ | ||
export function random (bytes: number): Uint8Array | ||
export function random(bytes: number): Uint8Array |
@@ -17,4 +17,3 @@ // This file replaces `index.js` in bundlers like webpack or Rollup, | ||
'For secure IDs, import `react-native-get-random-values` ' + | ||
'before Nano ID. If you use Expo, install `expo-random` ' + | ||
'and use `nanoid/async`.' | ||
'before Nano ID.' | ||
) | ||
@@ -21,0 +20,0 @@ } |
@@ -52,3 +52,3 @@ import crypto from 'crypto' | ||
let bytes = getRandom(step) | ||
// A compact alternative for `for (var i = 0; i < step; i++)`. | ||
// A compact alternative for `for (let i = 0; i < step; i++)`. | ||
let i = step | ||
@@ -69,3 +69,3 @@ while (i--) { | ||
let id = '' | ||
// A compact alternative for `for (var i = 0; i < step; i++)`. | ||
// A compact alternative for `for (let i = 0; i < size; i++)`. | ||
while (size--) { | ||
@@ -72,0 +72,0 @@ // It is incorrect to use bytes exceeding the alphabet size. |
@@ -17,4 +17,3 @@ // This file replaces `index.js` in bundlers like webpack or Rollup, | ||
'For secure IDs, import `react-native-get-random-values` ' + | ||
'before Nano ID. If you use Expo, install `expo-random` ' + | ||
'and use `nanoid/async`.' | ||
'before Nano ID.' | ||
) | ||
@@ -21,0 +20,0 @@ } |
@@ -13,3 +13,3 @@ /** | ||
*/ | ||
export function nanoid (size?: number): string | ||
export function nanoid(size?: number): string | ||
@@ -31,2 +31,2 @@ /** | ||
*/ | ||
export function customAlphabet (alphabet: string, size: number): () => string | ||
export function customAlphabet(alphabet: string, size: number): () => string |
{ | ||
"name": "nanoid", | ||
"version": "3.1.20", | ||
"version": "3.1.21", | ||
"description": "A tiny (108 bytes), secure URL-friendly unique string ID generator", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -50,3 +50,2 @@ # Nano ID | ||
* [Rollup](#rollup) | ||
* [Expo](#expo) | ||
* [PouchDB and CouchDB](#pouchdb-and-couchdb) | ||
@@ -143,3 +142,3 @@ * [Mongoose](#mongoose) | ||
[Secure random values (in Node.js)]: https://gist.github.com/joepie91/7105003c3b26e65efcea63f3db82dfba | ||
[better algorithm]: https://github.com/ai/nanoid/blob/master/index.js | ||
[better algorithm]: https://github.com/ai/nanoid/blob/main/index.js | ||
@@ -159,2 +158,8 @@ | ||
In Node.js you can use CommonJS import: | ||
```js | ||
const { nanoid } = require('nanoid') | ||
``` | ||
If you want to reduce the ID size (and increase collisions probability), | ||
@@ -198,18 +203,39 @@ you can pass the size as an argument. | ||
**Do not** call `nanoid` in the `key` prop. In React, `key` should be consistent | ||
among renders. | ||
There’s currently no correct way to use nanoid for React `key` prop | ||
since it should be consistent among renders. | ||
This is the bad example: | ||
```jsx | ||
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. | ||
```jsx | ||
<Item key={nanoid()} /> /* DON’T DO IT */ | ||
const todoItems = todos.map((todo) => | ||
<li key={todo.id}> | ||
{todo.text} | ||
</li> | ||
) | ||
``` | ||
This is the good example (`id` will be generated only once): | ||
In case you don’t have stable ids you'd rather use index as `key` | ||
instead of `nanoid()`: | ||
```jsx | ||
const Element = () => { | ||
const [id] = React.useState(nanoid) | ||
return <Item key={id} /> | ||
} | ||
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> | ||
) | ||
``` | ||
@@ -242,3 +268,4 @@ | ||
React Native does not have built-in random generator. | ||
React Native does not have built-in random generator. The following polyfill | ||
works for plain React Native and Expo starting with `39.x`. | ||
@@ -260,3 +287,4 @@ 1. Check [`react-native-get-random-values`] docs and install it. | ||
For Rollup you will need [`@rollup/plugin-replace`] to replace | ||
For Rollup you will need [`@rollup/plugin-node-resolve`] to bundle browser version | ||
of this library and [`@rollup/plugin-replace`] to replace | ||
`process.env.NODE_ENV`: | ||
@@ -266,4 +294,7 @@ | ||
plugins: [ | ||
nodeResolve({ | ||
browser: true | ||
}), | ||
replace({ | ||
'process.env.NODE_ENV': JSON.stringify(process.env.NODE) | ||
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) | ||
}) | ||
@@ -273,24 +304,6 @@ ] | ||
[`@rollup/plugin-node-resolve`]: https://github.com/rollup/plugins/tree/master/packages/node-resolve | ||
[`@rollup/plugin-replace`]: https://github.com/rollup/plugins/tree/master/packages/replace | ||
### Expo | ||
If you use Expo in React Native, you need a different workaround. | ||
1. Install [`expo-random`](https://www.npmjs.com/package/expo-random). | ||
2. Use `nanoid/async` instead of `nanoid`. | ||
3. Import `index.native.js` file directly. | ||
```js | ||
import { nanoid } from 'nanoid/async/index.native' | ||
async function createUser () { | ||
user.id = await nanoid() | ||
} | ||
``` | ||
[`expo-random`]: https://www.npmjs.com/package/expo-random | ||
### PouchDB and CouchDB | ||
@@ -394,2 +407,3 @@ | ||
* [Nim](https://github.com/icyphox/nanoid.nim) | ||
* [Perl](https://github.com/tkzwtks/Nanoid-perl) | ||
* [PHP](https://github.com/hidehalo/nanoid-php) | ||
@@ -396,0 +410,0 @@ * [Python](https://github.com/puyuan/py-nanoid) |
Sorry, the diff of this file is not supported yet
518
52591
24
882