mirror of
https://git.um-react.app/um/um-react.git
synced 2025-11-28 03:23:02 +00:00
feat: kwm v2 key import ui
This commit is contained in:
67
src/features/settings/keyFormats.ts
Normal file
67
src/features/settings/keyFormats.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { nanoid } from 'nanoid';
|
||||
import { objectify } from 'radash';
|
||||
|
||||
export function productionKeyToStaging<S, P extends Record<string, unknown>>(
|
||||
src: P,
|
||||
make: (k: keyof P, v: P[keyof P]) => null | S
|
||||
): S[] {
|
||||
const result: S[] = [];
|
||||
for (const [key, value] of Object.entries(src)) {
|
||||
const item = make(key, value as P[keyof P]);
|
||||
if (item) {
|
||||
result.push(item);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
export function stagingKeyToProduction<S, P>(src: S[], toKey: (s: S) => keyof P, toValue: (s: S) => P[keyof P]): P {
|
||||
return objectify(src, toKey, toValue) as P;
|
||||
}
|
||||
|
||||
// QMCv2 (QQ)
|
||||
export interface StagingQMCv2Key {
|
||||
id: string;
|
||||
name: string;
|
||||
ekey: string;
|
||||
}
|
||||
|
||||
export type ProductionQMCv2Keys = Record<string /* filename */, string /* ekey */>;
|
||||
|
||||
export const qmc2StagingToProductionKey = (key: StagingQMCv2Key) => key.name.normalize();
|
||||
export const qmc2StagingToProductionValue = (key: StagingQMCv2Key) => key.ekey.trim();
|
||||
export const qmc2ProductionToStaging = (
|
||||
key: keyof ProductionQMCv2Keys,
|
||||
value: ProductionQMCv2Keys[keyof ProductionQMCv2Keys]
|
||||
): StagingQMCv2Key => {
|
||||
return {
|
||||
id: nanoid(),
|
||||
name: key.normalize(),
|
||||
ekey: value.trim(),
|
||||
};
|
||||
};
|
||||
|
||||
// KWMv2 (KuWo)
|
||||
|
||||
export interface StagingKWMv2Key {
|
||||
id: string;
|
||||
rid: string;
|
||||
quality: string;
|
||||
ekey: string;
|
||||
}
|
||||
|
||||
export type ProductionKWMv2Keys = Record<string /* `${rid}-${quality}` */, string /* ekey */>;
|
||||
|
||||
export const kwm2StagingToProductionKey = (key: StagingKWMv2Key) => `${key.rid}-${key.quality}`;
|
||||
export const kwm2StagingToProductionValue = (key: StagingKWMv2Key) => key.ekey;
|
||||
export const kwm2ProductionToStaging = (
|
||||
key: keyof ProductionKWMv2Keys,
|
||||
value: ProductionKWMv2Keys[keyof ProductionKWMv2Keys]
|
||||
): null | StagingKWMv2Key => {
|
||||
if (typeof value !== 'string') return null;
|
||||
|
||||
const m = key.match(/^(\d+)-(\w+)$/);
|
||||
if (!m) return null;
|
||||
|
||||
const [_, rid, quality] = m;
|
||||
return { id: nanoid(), rid, quality, ekey: value };
|
||||
};
|
||||
Reference in New Issue
Block a user