Skip to content

Commit

Permalink
Update the package to Deno
Browse files Browse the repository at this point in the history
Move the build system to Deno, export both to npm and deno/x
  • Loading branch information
claudiuandrei committed Oct 20, 2022
1 parent 6ebc3df commit de3ff95
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 9,911 deletions.
13 changes: 0 additions & 13 deletions .editorconfig

This file was deleted.

14 changes: 2 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
node_modules
coverage
.nyc_output
.DS_Store
*.log
.vscode
.idea
dist
compiled
.awcache
.rpt2_cache
docs
# NPM build directory
npm
20 changes: 0 additions & 20 deletions .travis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
31 changes: 31 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import nhash from "https://deno.land/x/[email protected]/mod.ts";

// Create a uuid
// RFC 4122 Version 4 compliant pattern
const from = (h: () => string): string =>
"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
.replace(/x/g, h)
.replace(/y/g, () =>
Math.floor((parseInt(h(), 16) / 16) * 4 + 8).toString(16)
);

// Uuid regex
const pattern =
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

// Check if it is a uuid
export const check = (uuid: string): boolean => pattern.test(uuid);

// Create a random uuid
export const create = (): string =>
from(() => Math.floor(Math.random() * 16).toString(16));

// Create a uuid hash
export const hash = (s: string): string => {
// Create a hash
const h = nhash(s, { size: 128 }).hex();

// Return the hash
let i = 0;
return from(() => h[i++]);
};
86 changes: 86 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {
assertEquals,
assertNotEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { check, create, hash } from "./mod.ts";

// Check
Deno.test("Check", async (t) => {
// Valid UUID
const vuuid = "01234567-abcd-4abc-8def-0123456789ab";
const iuuid = "01234567-abcd-4abc-4def-0123456789ab";

// Valid UUID should pass the check
await t.step("Valid UUID should pass the check", () => {
// Check the valid uuid
const v = check(vuuid);

// Expect rules
assertEquals(v, true);
});

// Invaid UUID should not pass the check
await t.step("Inalid UUID should not pass the check", () => {
// Check invalid uuid
const v = check(iuuid);

// Expect rules
assertEquals(v, false);
});
});

// Random
Deno.test("Random", async (t) => {
// UUID should be between a valid UUID
await t.step("Random UUID should be a valid UUID", () => {
// Check random uuid
const u = create();
const v = check(u);

// Expect rules
assertEquals(v, true);
});

// Two random UUIDs should be different
await t.step("Two random UUIDs should be different", () => {
// Check random uuids
const u1 = create();
const u2 = create();

// Expect rules
assertNotEquals(u1, u2);
});
});

// Hash
Deno.test("Hash", async (t) => {
// Hashed UUID should be between a valid UUID
await t.step("Hashed UUID should be a valid UUID", () => {
// Get a hashed uuid
const u = hash("Hash");
const v = check(u);

// Expect rules
assertEquals(v, true);
});

// Same hashed input should generate same UUID
await t.step("Same hashed input should generate same UUID", () => {
// Get hashed uuids
const u1 = hash("Hash");
const u2 = hash("Hash");

// Expect rules
assertEquals(u1, u2);
});

// Different hashed input should generate different UUIDs
await t.step("Different hashed input should generate different UUIDs", () => {
// Get hashed uuids
const u1 = hash("Hash1");
const u2 = hash("Hash2");

// Expect rules
assertNotEquals(u1, u2);
});
});
129 changes: 0 additions & 129 deletions package.json

This file was deleted.

38 changes: 0 additions & 38 deletions rollup.config.ts

This file was deleted.

40 changes: 40 additions & 0 deletions scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { build, emptyDir } from "https://deno.land/x/[email protected]/mod.ts";

await emptyDir("./npm");

await build({
entryPoints: ["./mod.ts"],
outDir: "./npm",
shims: {
// see JS docs for overview and more options
deno: {
test: "dev",
},
},
compilerOptions: {
lib: ["es2021", "dom"],
},
package: {
// package.json properties
name: "huid",
version: Deno.args[0],
description: "Utility for generating, checking or hashing UUIDs.",
license: "MIT",
keywords: ["uuid", "guid", "hash", "hashing"],
author: "Claudiu Andrei <[email protected]>",
repository: {
type: "git",
url: "git+https://github.com/claudiuandrei/huid.git",
},
bugs: {
url: "https://github.com/claudiuandrei/huid/issues",
},
engines: {
node: ">=11.0.0",
},
},
});

// post build steps
Deno.copyFileSync("LICENSE", "npm/LICENSE");
Deno.copyFileSync("README.md", "npm/README.md");
Loading

0 comments on commit de3ff95

Please sign in to comment.