Socket
Socket
Sign inDemoInstall

nanoid

Package Overview
Dependencies
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanoid - npm Package Compare versions

Comparing version 2.0.3 to 2.0.4

3

CHANGELOG.md
# Change Log
This project adheres to [Semantic Versioning](http://semver.org/).
## 2.0.4
* Improve error text for React Native (by Sebastian Werner).
## 2.0.3

@@ -5,0 +8,0 @@ * Fix freeze on string in ID length.

if (process.env.NODE_ENV !== 'production') {
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
throw new Error(
'React Native does not have a built-in secure random generator. ' +
'If you don’t need unpredictable IDs, you can use `nanoid/non-secure`. ' +
'If you need secure ID, see https://github.com/ai/nanoid/#react-native.'
)
}
if (typeof self === 'undefined' || (!self.crypto && !self.msCrypto)) {

@@ -3,0 +10,0 @@ throw new Error(

{
"name": "nanoid",
"version": "2.0.3",
"version": "2.0.4",
"description": "A tiny (141 bytes), secure URL-friendly unique string ID generator",

@@ -20,3 +20,10 @@ "keywords": [

},
"sideEffects": false
"sideEffects": false,
"eslintIgnore": [
"node_modules",
"test/demo/build"
],
"sharec": {
"version": "0.4.1"
}
}

130

README.md

@@ -31,3 +31,22 @@ # Nano ID

## Table of Contents
1. [Comparison with UUID](#comparison-with-uuid)
2. [Benchmark](#benchmark)
3. [Security](#security)
4. [Tools](#tools)
6. Usage
1. [JS](#js)
2. [React](#react)
3. [React Native](#react-native)
4. [Web Workers](#web-workers)
5. [PouchDB and CouchDB](#pouchdb-and-couchdb)
5. [Mongoose](#mongoose)
6. [Other Programming Languages](#other-programming-languages)
7. API
1. [Async](#async)
2. [Custom Alphabet or Length](#custom-alphabet-or-length)
3. [Custom Random Bytes Generator](#custom-random-bytes-generator)
## Comparison with UUID

@@ -48,3 +67,3 @@

141 bytes instead of 435.
3. Because of memory allocation tricks, Nano ID 16% faster than UUID.
3. Because of memory allocation tricks, Nano ID is 16% faster than UUID.

@@ -117,26 +136,6 @@

## Other Programming Languages
## Usage
* [C#](https://github.com/codeyu/nanoid-net)
* [Clojure and ClojureScript](https://github.com/zelark/nano-id)
* [Crystal](https://github.com/mamantoha/nanoid.cr)
* [Dart](https://github.com/pd4d10/nanoid)
* [Go](https://github.com/matoous/go-nanoid)
* [Elixir](https://github.com/railsmechanic/nanoid)
* [Haskell](https://github.com/4e6/nanoid-hs)
* [Java](https://github.com/aventrix/jnanoid)
* [Nim](https://github.com/icyphox/nanoid.nim)
* [PHP](https://github.com/hidehalo/nanoid-php)
* [Python](https://github.com/puyuan/py-nanoid) with [dictionaries](https://github.com/aidarkhanov/py-nanoid-dictionary)
* [Ruby](https://github.com/radeno/nanoid.rb)
* [Rust](https://github.com/nikolay-govorov/nanoid)
* [Swift](https://github.com/antiflasher/NanoID)
### JS
Also, [CLI tool] is available to generate IDs from a command line.
[CLI tool]: https://github.com/twhitbeck/nanoid-cli
## Usage
The main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID

@@ -162,2 +161,3 @@ with 21 characters (to have a collision probability similar to UUID v4).

### React

@@ -172,14 +172,8 @@

This is good code. Note, that we added `"input"` string in front of `id`,
because Nano ID could be started from number. HTML ID can’t be started
from the number.
This is good code. `this.id` will be generated only once:
```jsx
id = 'input' + nanoid()
id = nanoid()
render () {
return <>
<label htmlFor={this.id}>Label text</label>
<input id={this.id} type="text"/>
</>;
return <Item key={this.id}>;
}

@@ -189,9 +183,18 @@ }

If you want to use Nano ID for `id`, you must to set some string prefix.
Nano ID could be started from number. HTML ID can’t be started from the number.
```jsx
<input id={'id' + this.id} type="text"/>
```
### React Native
To generate secure random IDs in React Native, you must use
[a native random generator] and asynchronous API:
To generate secure random IDs in React Native, you must use a native random
generator (like [`expo-random`] or [`react-native-securerandom`]) and the
asynchronous API:
```js
const generateSecureRandom = require('react-native-securerandom').generateSecureRandom
const getRandomBytesAsync = require('expo-random').getRandomBytesAsync
const format = require('nanoid/async/format')

@@ -201,9 +204,26 @@ const url = require('nanoid/url')

async function createUser () {
user.id = await format(generateSecureRandom, url, 21);
user.id = await format(getRandomBytesAsync, url, 21);
}
```
[a native random generator]: https://github.com/rh389/react-native-securerandom
[`expo-random`]: https://github.com/expo/expo/tree/master/packages/expo-random
[`react-native-securerandom`]: https://github.com/rh389/react-native-securerandom
### 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:
```js
db.put({
_id: 'id' + nanoid(),
})
```
### Mongoose

@@ -215,3 +235,3 @@

type: String,
default: () => nanoid(10)
default: () => nanoid()
}

@@ -238,2 +258,29 @@ })

### Other Programming Languages
Nano ID was ported to many languages. You can use these ports to have the same
ID generators on client and server side.
* [C#](https://github.com/codeyu/nanoid-net)
* [Clojure and ClojureScript](https://github.com/zelark/nano-id)
* [Crystal](https://github.com/mamantoha/nanoid.cr)
* [Dart](https://github.com/pd4d10/nanoid)
* [Go](https://github.com/matoous/go-nanoid)
* [Elixir](https://github.com/railsmechanic/nanoid)
* [Haskell](https://github.com/4e6/nanoid-hs)
* [Java](https://github.com/aventrix/jnanoid)
* [Nim](https://github.com/icyphox/nanoid.nim)
* [PHP](https://github.com/hidehalo/nanoid-php)
* [Python](https://github.com/puyuan/py-nanoid) with [dictionaries](https://pypi.org/project/nanoid-dictionary)
* [Ruby](https://github.com/radeno/nanoid.rb)
* [Rust](https://github.com/nikolay-govorov/nanoid)
* [Swift](https://github.com/antiflasher/NanoID)
Also, [CLI tool] is available to generate IDs from a command line.
[CLI tool]: https://github.com/twhitbeck/nanoid-cli
## API
### Async

@@ -339,1 +386,8 @@

```
## Security
To report a security vulnerability, please use the
[Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc