libram 0.11.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,17 +35,17 @@ export declare function get(name: BooleanModifier, subject?: string | Item | Eff
35
35
  export declare function get(name: NumericModifier, subject?: string | Item | Effect | Skill | Familiar): number;
36
36
  export declare function get(name: StringModifier, subject?: string | Effect | Item): string;
37
37
  export declare function get(name: MultiStringModifier, subject?: string | Item | Effect | Skill | Familiar): string[];
38
- export type ModifierValue<T> = T extends BooleanModifier ? boolean : T extends NumericModifier ? number : T extends StringModifier ? string : string;
38
+ export type ModifierValue<T> = T extends BooleanModifier ? boolean : T extends NumericModifier ? number : T extends MultiStringModifier ? string[] : string;
39
39
  export type Modifiers<T extends ModifierType = ModifierType> = Partial<{
40
40
  [K in T]: ModifierValue<K>;
41
41
  }>;
42
42
  /**
43
43
  * Merge arbitrarily many Modifiers objects into one, summing all numeric modifiers, and ||ing all boolean modifiers.
44
44
  *
45
- * @param modifierss Modifiers objects to be merged together.
45
+ * @param modifiers Modifiers objects to be merged together.
46
46
  * @returns A single Modifiers object obtained by merging.
47
47
  */
48
- export declare function mergeModifiers(...modifierss: Modifiers[]): Modifiers;
48
+ export declare function mergeModifiers(...modifiers: Modifiers[]): Modifiers;
49
49
  /**
50
50
  * Prints the modtrace to the log.
51
51
  * Example: printModtrace("Meat Drop") or printModtrace(["Item Drop", "Booze Drop"])
@@ -70,21 +70,19 @@ export type ModifierParser = {
70
70
  numeric: (value: string) => number;
71
71
  str: (value: string) => string;
72
72
  bool: (value: string) => boolean;
73
+ multiString: (value: string) => string[];
73
74
  };
74
75
  /**
75
76
  * Translate a pref into a `Modifiers` object by wrapping mafia's `splitModifiers`
76
77
  * @param pref The name of the mafia preference in question
77
- * @param translator Optional object to help translate fields into their appropriate values
78
- * @param translator.numeric How to translate the values from `splitModifiers` into numbers for numeric modifiers; defaults to Number
79
- * @param translator.str How to translate the values from `splitModifiers` into strings for string modifiers; defaults to String
80
- * @param translator.bool How to translate the values from `splitModifiers` into booleans for boolean modifiers; defaults to comparing to the string `"true"`
78
+ * @param parsers Optional object to help translate fields into their appropriate values
79
+ * @param parsers.numeric How to translate the values from `splitModifiers` into numbers for numeric modifiers; defaults to Number
80
+ * @param parsers.str How to translate the values from `splitModifiers` into strings for string modifiers; defaults to String
81
+ * @param parsers.bool How to translate the values from `splitModifiers` into booleans for boolean modifiers; defaults to comparing to the string `"true"`
82
+ * @param parsers.multiString How to translate the values from `splitModifiers` into string[]s for multistring modifiers; defaults to splitting by a comma
81
83
  * @returns A `Modifiers` object corresponding to the given preference.
82
84
  */
83
- export declare function parseModifiers(pref: StringProperty, { numeric, str, bool, }?: Partial<{
84
- numeric: (value: string) => number;
85
- str: (value: string) => string;
86
- bool: (value: string) => boolean;
87
- }>): Modifiers;
85
+ export declare function parseModifiers(pref: StringProperty, parsers?: Partial<ModifierParser>): Modifiers;
88
86
  /**
89
87
  * Compile together all `Modifiers` something has
90
88
  * @param thing An Item, Effect, or string to check all modifiers of
package/dist/modifier.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { booleanModifier, cliExecuteOutput, Effect, familiarWeight, getProperty, Item, myFamiliar, numericModifier, print, splitModifiers, stringModifier, stringsModifier, } from "kolmafia";
2
2
  import { have } from "./lib.js";
3
- import { booleanModifiersSet, numericModifiersSet, stringModifiersSet, } from "./modifierTypes.js";
3
+ import { booleanModifiersSet, multiStringModifiersSet, numericModifiersSet, stringModifiersSet, } from "./modifierTypes.js";
4
4
  import { $effect } from "./template-string.js";
5
5
  import { sum } from "./utils.js";
6
6
  /**
@@ -33,7 +33,7 @@ export function isStringModifier(modifier) {
33
33
  * @returns Whether the string in question is a valid multistring modifier
34
34
  */
35
35
  export function isMultiStringModifier(modifier) {
36
- return stringModifiersSet.has(modifier);
36
+ return multiStringModifiersSet.has(modifier);
37
37
  }
38
38
  /**
39
39
  * Type guard that determines if a given string is a valid modifier
@@ -43,7 +43,8 @@ export function isMultiStringModifier(modifier) {
43
43
  export function isValidModifier(modifier) {
44
44
  return (isNumericModifier(modifier) ||
45
45
  isBooleanModifier(modifier) ||
46
- isStringModifier(modifier));
46
+ isStringModifier(modifier) ||
47
+ isMultiStringModifier(modifier));
47
48
  }
48
49
  /**
49
50
  * Get the value of a modifier
@@ -93,6 +94,12 @@ function pairwiseMerge(modifiers1, modifiers2) {
93
94
  returnValue[modifier] =
94
95
  (modifiers1[modifier] ?? false) || (modifiers2[modifier] ?? false);
95
96
  }
97
+ if (isMultiStringModifier(modifier)) {
98
+ returnValue[modifier] = [
99
+ ...(modifiers1[modifier] ?? []),
100
+ ...(modifiers2[modifier] ?? []),
101
+ ];
102
+ }
96
103
  }
97
104
  }
98
105
  return returnValue;
@@ -100,11 +107,11 @@ function pairwiseMerge(modifiers1, modifiers2) {
100
107
  /**
101
108
  * Merge arbitrarily many Modifiers objects into one, summing all numeric modifiers, and ||ing all boolean modifiers.
102
109
  *
103
- * @param modifierss Modifiers objects to be merged together.
110
+ * @param modifiers Modifiers objects to be merged together.
104
111
  * @returns A single Modifiers object obtained by merging.
105
112
  */
106
- export function mergeModifiers(...modifierss) {
107
- return modifierss.reduce((a, b) => pairwiseMerge(a, b), {});
113
+ export function mergeModifiers(...modifiers) {
114
+ return modifiers.reduce((a, b) => pairwiseMerge(a, b), {});
108
115
  }
109
116
  /**
110
117
  * Prints the modtrace to the log.
@@ -190,27 +197,30 @@ baseModifier, componentColor = "purple", totalColor = "blue") {
190
197
  export function getTotalModifier(modifier, ...subjects) {
191
198
  return sum(subjects, (subject) => get(modifier, subject));
192
199
  }
193
- function parseModifierString(modifiers, { numeric = Number, str = String, bool = (val) => val === "true", } = {}) {
200
+ function parseModifierString(modifiers, { numeric = Number, str = String, bool = (val) => val === "true", multiString = (val) => val.split(","), } = {}) {
194
201
  return Object.entries(splitModifiers(modifiers)).reduce((acc, [key, value]) => ({
195
202
  ...acc,
196
203
  [key]: isBooleanModifier(key)
197
204
  ? bool(value)
198
205
  : isNumericModifier(key)
199
206
  ? numeric(value)
200
- : str(value),
207
+ : isMultiStringModifier(key)
208
+ ? multiString(value)
209
+ : str(value),
201
210
  }), {});
202
211
  }
203
212
  /**
204
213
  * Translate a pref into a `Modifiers` object by wrapping mafia's `splitModifiers`
205
214
  * @param pref The name of the mafia preference in question
206
- * @param translator Optional object to help translate fields into their appropriate values
207
- * @param translator.numeric How to translate the values from `splitModifiers` into numbers for numeric modifiers; defaults to Number
208
- * @param translator.str How to translate the values from `splitModifiers` into strings for string modifiers; defaults to String
209
- * @param translator.bool How to translate the values from `splitModifiers` into booleans for boolean modifiers; defaults to comparing to the string `"true"`
215
+ * @param parsers Optional object to help translate fields into their appropriate values
216
+ * @param parsers.numeric How to translate the values from `splitModifiers` into numbers for numeric modifiers; defaults to Number
217
+ * @param parsers.str How to translate the values from `splitModifiers` into strings for string modifiers; defaults to String
218
+ * @param parsers.bool How to translate the values from `splitModifiers` into booleans for boolean modifiers; defaults to comparing to the string `"true"`
219
+ * @param parsers.multiString How to translate the values from `splitModifiers` into string[]s for multistring modifiers; defaults to splitting by a comma
210
220
  * @returns A `Modifiers` object corresponding to the given preference.
211
221
  */
212
- export function parseModifiers(pref, { numeric = Number, str = String, bool = (val) => val === "true", } = {}) {
213
- return parseModifierString(getProperty(pref), { numeric, str, bool });
222
+ export function parseModifiers(pref, parsers = {}) {
223
+ return parseModifierString(getProperty(pref), parsers);
214
224
  }
215
225
  const overloadedStringModifier = (thing, modifier) => thing instanceof Effect
216
226
  ? stringModifier(thing, modifier)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libram",
3
- "version": "0.11.0",
3
+ "version": "0.11.1",
4
4
  "description": "JavaScript helper library for KoLmafia",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",