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:
- Given the following manifest URL: https://polona.pl/iiif/item/NTU5NTE4OTg/manifest.json,
- The output of the
generateId
function will be:4ac2770ae8624e38
, - We can use this ID to view the annotation for the georeferenced maps in the manifest in Allmaps: https://dev.annotations.allmaps.org/manifests/4ac2770ae8624e38.
To see @allmaps/id in action, view this Observable notebook.
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>
Generates an ID from a string using the SHA-1 algorithm. Given the same input, the ID will always be the same.
str
string Input string.length
number Length of returned hash. The maximum length of the hash is 40 characters. (optional, default16
)
Returns string First length
characters of the SHA-1 hash of str
.
Generates a random ID.
length
number Length of returned hash. The maximum length of the hash is 40 characters. (optional, default16
)
Returns string First length
characters of the SHA-1 hash of a random UUID.
Generates a checksum of a JSON object.
obj
Object JSON object.length
number Length of returned hash. The maximum length of the hash is 40 characters. (optional, default16
)
Returns string First length
characters of the SHA-1 hash of sorted and serialized version of obj
.