@allmaps/id
Uses the SHA-1 algorithm to generate IDs from strings, random IDs from UUIDs and checksums from JSON objects.
Allmaps uses this module to create IDs from IIIF URIs. For example:
Usage
This is an ESM-only module that works in browsers or in Node.js.
Node.js:
The Node.js version of @allmaps/id usescrypto.createHash()
and crypto.randomUUID()
from the crypto
module. First, run npm install @allmaps/id
.
import { generateId } from '@allmaps/id'
const url =
'https://orka.bibliothek.uni-kassel.de/viewer/rest/iiif/manifests/1535113582549/manifest/'
const id = await generateId(url)
console.log(id)
Browser:
The browser version of @allmaps/id uses the SubtleCrypto.digest()
and Crypto.randomUUID()
Web APIs.
<script type="module">
import { generateId } from 'https://unpkg.com/@allmaps/id?module'
const url =
'https://orka.bibliothek.uni-kassel.de/viewer/rest/iiif/manifests/1535113582549/manifest/'
const id = await generateId(url)
console.log(id)
</script>
API
Table of Contents
generateId
Generates an ID from a string using the SHA-1 algorithm. Given the same input, the ID will always be the same.
Parameters
str
string Input string.length
number Length of returned hash. The maximum length of the hash is 40 characters. (optional, default 16
)
Returns string First length
characters of the SHA-1 hash of str
.
generateRandomId
Generates a random ID.
Parameters
length
number Length of returned hash. The maximum length of the hash is 40 characters. (optional, default 16
)
Returns string First length
characters of the SHA-1 hash of a random UUID.
generateChecksum
Generates a checksum of a JSON object.
Parameters
obj
Object JSON object.length
number Length of returned hash. The maximum length of the hash is 40 characters. (optional, default 16
)
Returns string First length
characters of the SHA-1 hash of sorted and serialized version of obj
.