Created
June 17, 2025 13:51
-
-
Save L8D/d21cfd7578538871fdffe818915f79b7 to your computer and use it in GitHub Desktop.
Utilities for clean usage of flickrBase58-encoded identifiers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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