@allmaps/id
Advanced tools
Comparing version 1.0.0-alpha.3 to 1.0.0-alpha.4
@@ -28,2 +28,6 @@ 'use strict'; | ||
function randomString () { | ||
return String(Math.random()) | ||
} | ||
/* global crypto, TextEncoder, btoa */ | ||
@@ -50,2 +54,7 @@ | ||
async function createRandomId (length) { | ||
const id = await createId(randomString(), length); | ||
return id | ||
} | ||
async function createChecksum (obj, length) { | ||
@@ -58,2 +67,3 @@ const hash = await createBase64Hash(serialize(obj)); | ||
exports.createId = createId; | ||
exports.createRandomId = createRandomId; | ||
//# sourceMappingURL=web.js.map |
@@ -24,2 +24,6 @@ function hashToId (hash, length = 16) { | ||
function randomString () { | ||
return String(Math.random()) | ||
} | ||
/* global crypto, TextEncoder, btoa */ | ||
@@ -46,2 +50,7 @@ | ||
async function createRandomId (length) { | ||
const id = await createId(randomString(), length); | ||
return id | ||
} | ||
async function createChecksum (obj, length) { | ||
@@ -52,3 +61,3 @@ const hash = await createBase64Hash(serialize(obj)); | ||
export { createChecksum, createId }; | ||
export { createChecksum, createId, createRandomId }; | ||
//# sourceMappingURL=web.js.map |
@@ -1,3 +0,3 @@ | ||
// https://allmaps.org v1.0.0-alpha.3 Copyright 2021 @nelsonic | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("crypto")):"function"==typeof define&&define.amd?define(["exports","crypto"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).id={},e.crypto)}(this,(function(e,t){"use strict";function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t);function o(e,t=16){return e.replace(/[Il0oO=\/\+]/g,"").substring(0,t)}function i(e){return Array.isArray(e)?JSON.stringify(e.map((e=>i(e)))):"string"==typeof e?`"${e}"`:"object"==typeof e&&null!==e?Object.keys(e).sort().map((t=>`${t}:${i(e[t])}`)).join("|"):e}function u(e){return r.default.createHash("sha512").update(e).digest("base64")}e.createChecksum=async function(e,t){return o(u(i(e)),t)},e.createId=async function(e,t){return o(await u(String(e)),t)}})); | ||
// https://allmaps.org v1.0.0-alpha.4 Copyright 2021 @nelsonic | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("crypto")):"function"==typeof define&&define.amd?define(["exports","crypto"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).id={},t.crypto)}(this,(function(t,e){"use strict";function n(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=n(e);function o(t,e=16){return t.replace(/[Il0oO=\/\+]/g,"").substring(0,e)}function i(t){return Array.isArray(t)?JSON.stringify(t.map((t=>i(t)))):"string"==typeof t?`"${t}"`:"object"==typeof t&&null!==t?Object.keys(t).sort().map((e=>`${e}:${i(t[e])}`)).join("|"):t}function a(t){return r.default.createHash("sha512").update(t).digest("base64")}async function u(t,e){return o(await a(String(t)),e)}t.createChecksum=async function(t,e){return o(a(i(t)),e)},t.createId=u,t.createRandomId=async function(t){return await u(String(Math.random()),t)}})); | ||
//# sourceMappingURL=id.min.js.map |
@@ -1,2 +0,2 @@ | ||
import { hashToId, serialize } from './src/id.js' | ||
import { hashToId, serialize, randomString } from './src/id.js' | ||
import crypto from 'crypto' | ||
@@ -15,2 +15,7 @@ | ||
export async function createRandomId (length) { | ||
const id = await createId(randomString(), length) | ||
return id | ||
} | ||
export async function createChecksum (obj, length) { | ||
@@ -17,0 +22,0 @@ const hash = createBase64Hash(serialize(obj)) |
{ | ||
"name": "@allmaps/id", | ||
"version": "1.0.0-alpha.3", | ||
"version": "1.0.0-alpha.4", | ||
"description": "IDs for Allmaps", | ||
@@ -44,3 +44,22 @@ "type": "module", | ||
"rollup-plugin-terser": "^7.0.2" | ||
}, | ||
"eslintConfig": { | ||
"root": true, | ||
"env": { | ||
"node": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended" | ||
], | ||
"rules": { | ||
"no-console": "off", | ||
"no-unused-vars": "off" | ||
}, | ||
"parserOptions": { | ||
"parser": "babel-eslint" | ||
} | ||
}, | ||
"engines": { | ||
"node": ">=12" | ||
} | ||
} |
@@ -23,1 +23,5 @@ export function hashToId (hash, length = 16) { | ||
} | ||
export function randomString () { | ||
return String(Math.random()) | ||
} |
import test from 'tape' | ||
import { createId, createChecksum } from '../nodejs.js' | ||
import { createId, createRandomId, createChecksum } from '../nodejs.js' | ||
test('Create id for url: 1234', async (t) => { | ||
test('Create ID for url: 1234', async (t) => { | ||
const id = await createId(1234) | ||
@@ -26,2 +26,10 @@ t.equal(id.length, 16, 'Worked as expected ' + id) | ||
test('Create random IDs', async (t) => { | ||
const id1 = await createRandomId() | ||
const id2 = await createRandomId() | ||
t.equal(id1.length, 16, 'Worked as expected ' + id1) | ||
t.notEqual(id1, id2, 'IDs are different') | ||
t.end() | ||
}) | ||
test('Full Length Hash', async (t) => { | ||
@@ -52,3 +60,2 @@ const id = await createId('qwertyuiopasdfghjklzxcvbnm', 50) | ||
test('Semantically equal objects produce equal checksums', async (t) => { | ||
@@ -60,2 +67,1 @@ const checksum1 = await createChecksum(obj1) | ||
}) | ||
/* global crypto, TextEncoder, btoa */ | ||
import { hashToId, serialize } from './src/id' | ||
import { hashToId, serialize, randomString } from './src/id' | ||
@@ -24,2 +24,7 @@ function sha512 (str) { | ||
export async function createRandomId (length) { | ||
const id = await createId(randomString(), length) | ||
return id | ||
} | ||
export async function createChecksum (obj, length) { | ||
@@ -26,0 +31,0 @@ const hash = await createBase64Hash(serialize(obj)) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
20797
277