@courseproof/utils
v0.1.1
Published
[](https://opensource.org/licenses/MIT)
Maintainers
Readme
@courseproof/utils
Common utility functions for URL handling, input detection, and data manipulation used across CourseProof packages.
Exports
URL Utilities
| Function | Description |
|----------|-------------|
| normalizeUrl(url) | Adds https:// if missing, lowercases hostname, strips trailing slash |
| extractDomain(url) | Extracts domain without www. prefix |
| extractHostname(url) | Extracts full hostname from a URL |
| domainMatches(url, domain) | Checks if a URL domain matches or is a subdomain of a given domain |
| extractPathSegments(url) | Splits URL pathname into segments |
| extractCredentialId(url) | Attempts to extract a credential ID from URL path segments |
| isValidUrl(str) | Returns true if the string is a valid URL |
Input Detection
| Function | Description |
|----------|-------------|
| detectInputFormat(input) | Returns "url", "credential-id", or "unknown" |
General Helpers
| Function | Description |
|----------|-------------|
| groupBy(items, keyFn) | Groups an array by a key function |
| uniqueBy(items, keyFn) | Filters array to unique items by key |
| slugify(text) | Converts text to a URL-friendly slug |
| formatDate(date) | Formats a date string to YYYY-MM-DD |
| calculateDuration(start, end) | Returns human-readable duration between two dates |
| truncate(str, maxLen) | Truncates a string with ... |
| safeJsonParse(str, fallback) | JSON.parse with a fallback on failure |
| delay(ms) | Returns a promise that resolves after ms milliseconds |
Usage
import { normalizeUrl, extractDomain, detectInputFormat, delay } from "@courseproof/utils";
const url = normalizeUrl("example.com/cert/123");
// → "https://example.com/cert/123"
const domain = extractDomain(url);
// → "example.com"
const format = detectInputFormat("UC-abc123");
// → "credential-id"
await delay(1000); // waits 1 secondInstallation
npm install @courseproof/utilsLicense
MIT — see LICENSE for details.
Part of the CourseProof monorepo.
