Skip to content

Instantly share code, notes, and snippets.

@L8D
Created June 17, 2025 13:51
Show Gist options
  • Save L8D/d21cfd7578538871fdffe818915f79b7 to your computer and use it in GitHub Desktop.
Save L8D/d21cfd7578538871fdffe818915f79b7 to your computer and use it in GitHub Desktop.
Utilities for clean usage of flickrBase58-encoded identifiers
import makeTranslator from 'short-uuid'
const translator = makeTranslator()
export const convertId = (
params:
| {
from: 'uuid'
to: 'flickrBase58'
value: string
}
| {
from: 'flickrBase58'
to: 'uuid'
value: string
},
) => {
switch (params.to) {
case 'uuid': {
return translator.toUUID(params.value)
}
case 'flickrBase58': {
return translator.fromUUID(params.value)
}
}
}
import { v4, v5 } from 'uuid'
import { convertId } from './convertId'
export const generateId = (
params:
| {
mode: 'deterministic'
namespace?: string
input: string
}
| {
mode: 'random'
},
) => {
switch (params.mode) {
case 'deterministic': {
const { namespace = '00000000-0000-0000-0000-000000000000', input } =
params
return convertId({
from: 'uuid',
to: 'flickrBase58',
value: v5(input, namespace),
})
}
case 'random': {
return convertId({
from: 'uuid',
to: 'flickrBase58',
value: v4(),
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment