Skip to content

Commit

Permalink
resources: allow for readonly string[] netregex params (quisquous#5548)
Browse files Browse the repository at this point in the history
  • Loading branch information
quisquous authored Jun 12, 2023
1 parent 50edf60 commit 3274b49
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions resources/netregexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ type RepeatingFieldsMapType<

type ParseHelperType<T extends LogDefinitionTypes> =
& {
[field in keyof NetFields[T]]?: string | string[] | RepeatingFieldsMapType<T, field>;
[field in keyof NetFields[T]]?: string | readonly string[] | RepeatingFieldsMapType<T, field>;
}
& { capture?: boolean };

const isRepeatingField = <
T extends LogDefinitionTypes,
>(
repeating: boolean | undefined,
value: string | string[] | RepeatingFieldsMap<T> | undefined,
value: string | readonly string[] | RepeatingFieldsMap<T> | undefined,
): value is RepeatingFieldsMap<T> => {
if (repeating !== true)
return false;
Expand Down
23 changes: 12 additions & 11 deletions resources/regexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ type RepeatingFieldsMapType<

type ParseHelperType<T extends LogDefinitionTypes> =
& {
[field in keyof NetFields[T]]?: string | string[] | RepeatingFieldsMapType<T, field>;
[field in keyof NetFields[T]]?: string | readonly string[] | RepeatingFieldsMapType<T, field>;
}
& { capture?: boolean };

const isRepeatingField = <
T extends LogDefinitionTypes,
>(
repeating: boolean | undefined,
value: string | string[] | RepeatingFieldsMap<T> | undefined,
value: string | readonly string[] | RepeatingFieldsMap<T> | undefined,
): value is RepeatingFieldsMap<T> => {
if (repeating !== true)
return false;
Expand Down Expand Up @@ -584,7 +584,7 @@ export default class Regexes {
static maybeCapture(
capture: boolean,
name: string,
value: string | string[] | undefined,
value: string | readonly string[] | undefined,
defaultValue?: string,
): string {
if (value === undefined)
Expand Down Expand Up @@ -614,24 +614,25 @@ export default class Regexes {
* args may be strings or RegExp, although any additional markers to RegExp
* like /insensitive/i are dropped.
*/
static anyOf(...args: (string | string[] | RegExp)[]): string {
const anyOfArray = (array: (string | RegExp)[]): string => {
static anyOf(...args: (string | readonly string[] | RegExp)[]): string {
const anyOfArray = (array: readonly (string | RegExp)[]): string => {
const [elem] = array;
if (elem !== undefined && array.length === 1)
return `${elem instanceof RegExp ? elem.source : elem}`;
return `(?:${array.map((elem) => elem instanceof RegExp ? elem.source : elem).join('|')})`;
};
let array: (string | RegExp)[] = [];
let array: readonly (string | RegExp)[] = [];
const [firstArg] = args;
if (args.length === 1) {
if (Array.isArray(args[0]))
array = args[0];
else if (args[0] !== undefined)
array = [args[0]];
if (typeof firstArg === 'string' || firstArg instanceof RegExp)
array = [firstArg];
else if (Array.isArray(firstArg))
array = firstArg;
else
array = [];
} else {
// TODO: more accurate type instead of `as` cast
array = args as string[];
array = args as readonly string[];
}
return anyOfArray(array);
}
Expand Down
4 changes: 3 additions & 1 deletion resources/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ export const translateRegexBuildParam = <T extends TriggerTypes>(
replaceLang: Lang,
replacements?: TimelineReplacement[],
): NetParams[T] => {
type AnonymousParams = { [name: string]: string | string[] | boolean | undefined | unknown[] };
type AnonymousParams = {
[name: string]: string | readonly string[] | boolean | undefined | unknown[];
};
const anonParams: AnonymousParams = params;
for (const key of keysThatRequireTranslation) {
const value = anonParams[key];
Expand Down
9 changes: 7 additions & 2 deletions test/helper/test_trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,10 @@ const testTriggerFile = (file: string, info: TriggerSetInfo) => {
).wasTranslated;
};

const checkIfFieldHasTranslation = (field: string | string[], fieldName: string) => {
const checkIfFieldHasTranslation = (
field: string | readonly string[],
fieldName: string,
) => {
if (typeof field === 'string') {
assert.isTrue(
textHasTranslation(field),
Expand All @@ -731,7 +734,9 @@ const testTriggerFile = (file: string, info: TriggerSetInfo) => {
};

for (const key of keysThatRequireTranslation) {
type AnonymousParams = { [name: string]: string | string[] | boolean | undefined };
type AnonymousParams = {
[name: string]: string | readonly string[] | boolean | undefined;
};
const anonTriggerFields: AnonymousParams = trigger.netRegex;
const value = anonTriggerFields[key];
if (value !== undefined && typeof value !== 'boolean')
Expand Down
2 changes: 1 addition & 1 deletion types/net_props.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type RepeatingFieldsParams<
: never;

type Params<T extends string> = Partial<
& Record<Exclude<T, 'timestamp' | 'capture'>, string | string[]>
& Record<Exclude<T, 'timestamp' | 'capture'>, string | readonly string[]>
& { 'timestamp': string; 'capture': boolean }
>;

Expand Down
3 changes: 1 addition & 2 deletions ui/raidboss/data/06-ew/raid/p12s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ const limitCutMap: { [id: string]: number } = {

const limitCutIds: readonly string[] = Object.keys(limitCutMap);
const wingIds: readonly string[] = Object.values(wings);
// TODO: make it possible for this to be readonly
const superchainNpcBaseIds: string[] = Object.values(superchainNpcBaseIdMap);
const superchainNpcBaseIds: readonly string[] = Object.values(superchainNpcBaseIdMap);

const getHeadmarkerId = (data: Data, matches: NetMatches['HeadMarker']) => {
if (data.decOffset === undefined) {
Expand Down

0 comments on commit 3274b49

Please sign in to comment.