split some store func and add test

This commit is contained in:
pa
2026-03-04 20:21:17 +09:00
parent ea82825823
commit 1f1b996239
15 changed files with 1172 additions and 1307 deletions

34
src/shared/resolveRef.js Normal file
View File

@@ -0,0 +1,34 @@
/**
* Generic resolver for user/world/group references.
* Normalises the input, optionally fetches the display name if missing.
*
* @param {string|object|null|undefined} input
* @param {object} opts
* @param {object} opts.emptyDefault - value to return when input is falsy
* @param {string} opts.idAlias - alternative id key on input (e.g. 'userId')
* @param {string} opts.nameKey - name property key (e.g. 'displayName' or 'name')
* @param {(id: string) => Promise<{ref: object}>} opts.fetchFn - fetch function
* @returns {Promise<object>}
*/
async function resolveRef(input, { emptyDefault, idAlias, nameKey, fetchFn }) {
if (!input) {
return emptyDefault;
}
if (typeof input === 'string') {
input = { id: input, [nameKey]: '' };
}
const id = input.id || input[idAlias] || '';
let name = input[nameKey] || '';
if (id && !name) {
try {
const args = await fetchFn(id);
name = args?.ref?.[nameKey] || name;
return { ...args.ref, id, [nameKey]: name };
} catch {
return { ...input, id, [nameKey]: name };
}
}
return { ...input, id, [nameKey]: name };
}
export { resolveRef };