libram 0.7.10 → 0.7.11

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.
package/dist/utils.d.ts CHANGED
@@ -21,7 +21,15 @@ export declare function countedMapToString<T>(map: Map<T, number>): string;
21
21
  /**
22
22
  * Sum an array of numbers.
23
23
  * @param addends Addends to sum.
24
- * @param mappingFunction function to turn elements into numbers
24
+ * @param property Property of the elements to be summing
25
+ */
26
+ export declare function sum<S extends string | number | symbol, T extends {
27
+ [s in S]: number;
28
+ }>(addends: T[], property: S): number;
29
+ /**
30
+ * Sum an array of numbers.
31
+ * @param addends Addends to sum.
32
+ * @param mappingFunction Mapping function to turn addends into actual numbers.
25
33
  */
26
34
  export declare function sum<T>(addends: T[], mappingFunction: (element: T) => number): number;
27
35
  export declare function sumNumbers(addends: number[]): number;
@@ -44,16 +52,23 @@ export declare function setEqual<T>(a: T[], b: T[]): boolean;
44
52
  * @param map Map to invert
45
53
  */
46
54
  export declare function invertMap<T1, T2>(map: Map<T1, T2>): Map<T2, T1>;
47
- /**
48
- * Creates a Type Guard function for a string union type defined via an array as const.
49
- */
50
- export declare function createStringUnionTypeGuardFunction<T extends string>(array: readonly T[]): (x: string) => x is T;
51
55
  /**
52
56
  * Splits a string by commas while also respecting escaping commas with a backslash
53
57
  * @param str String to split
54
58
  * @returns List of tokens
55
59
  */
56
60
  export declare function splitByCommasWithEscapes(str: string): string[];
61
+ /**
62
+ * Find the best element of an array, where "best" is defined by some given criteria.
63
+ * @param array The array to traverse and find the best element of.
64
+ * @param optimizer Either a key on the objects we're looking at that corresponds to numerical values, or a function for mapping these objects to numbers. Essentially, some way of assigning value to the elements of the array.
65
+ * @param reverse Make this true to find the worst element of the array, and false to find the best. Defaults to false.
66
+ */
67
+ export declare function maxBy<T>(array: T[] | readonly T[], optimizer: (element: T) => number, reverse?: boolean): T;
68
+ export declare function maxBy<S extends string | number | symbol, T extends {
69
+ [x in S]: number;
70
+ }>(array: T[] | readonly T[], key: S, reverse?: boolean): T;
57
71
  export declare type Tuple<T, N extends number> = N extends N ? number extends N ? T[] : _tupleOf<T, N, []> : never;
58
72
  declare type _tupleOf<T, N extends number, R extends unknown[]> = R["length"] extends N ? R : _tupleOf<T, N, [T, ...R]>;
73
+ export declare function arrayEquals<T>(left: T[] | readonly T[], right: T[] | readonly T[]): boolean;
59
74
  export {};
package/dist/utils.js CHANGED
@@ -42,13 +42,8 @@ export function countedMapToArray(map) {
42
42
  export function countedMapToString(map) {
43
43
  return [...map].map(([item, quantity]) => `${quantity} x ${item}`).join(", ");
44
44
  }
45
- /**
46
- * Sum an array of numbers.
47
- * @param addends Addends to sum.
48
- * @param mappingFunction function to turn elements into numbers
49
- */
50
- export function sum(addends, mappingFunction) {
51
- return addends.reduce((subtotal, element) => subtotal + mappingFunction(element), 0);
45
+ export function sum(addends, x) {
46
+ return addends.reduce((subtotal, element) => subtotal + (typeof x === "function" ? x(element) : element[x]), 0);
52
47
  }
53
48
  export function sumNumbers(addends) {
54
49
  return sum(addends, (x) => x);
@@ -85,14 +80,6 @@ export function invertMap(map) {
85
80
  }
86
81
  return returnValue;
87
82
  }
88
- /**
89
- * Creates a Type Guard function for a string union type defined via an array as const.
90
- */
91
- export function createStringUnionTypeGuardFunction(array) {
92
- return function (x) {
93
- return array.includes(x);
94
- };
95
- }
96
83
  /**
97
84
  * Splits a string by commas while also respecting escaping commas with a backslash
98
85
  * @param str String to split
@@ -120,3 +107,23 @@ export function splitByCommasWithEscapes(str) {
120
107
  returnValue.push(currentString.trim());
121
108
  return returnValue;
122
109
  }
110
+ export function maxBy(array, optimizer, reverse = false) {
111
+ if (!array.length)
112
+ throw new Error("Cannot call maxBy on an empty array!");
113
+ if (typeof optimizer === "function") {
114
+ return [...array].reduce(({ value, item }, other) => {
115
+ const otherValue = optimizer(other);
116
+ return value >= otherValue !== reverse
117
+ ? { value, item }
118
+ : { value: otherValue, item: other };
119
+ }, { item: array[0], value: optimizer(array[0]) }).item;
120
+ }
121
+ else {
122
+ return array.reduce((a, b) => a[optimizer] >= b[optimizer] !== reverse ? a : b);
123
+ }
124
+ }
125
+ export function arrayEquals(left, right) {
126
+ if (left.length !== right.length)
127
+ return false;
128
+ return left.every((element, index) => element === right[index]);
129
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libram",
3
- "version": "0.7.10",
3
+ "version": "0.7.11",
4
4
  "description": "JavaScript helper library for KoLmafia",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,8 +11,7 @@
11
11
  "scripts": {
12
12
  "build": "yarn run build:tsc && yarn run build:bundled",
13
13
  "build:tsc": "tsc",
14
- "build:bundled": "webpack",
15
- "build:watch": "tsc --watch",
14
+ "build:bundled": "node build.mjs",
16
15
  "clean": "rm -rf dist",
17
16
  "docs": "yarn run typedoc",
18
17
  "format": "yarn run prettier --write .",
@@ -27,7 +26,6 @@
27
26
  "devDependencies": {
28
27
  "@babel/compat-data": "^7.17.0",
29
28
  "@babel/core": "^7.17.2",
30
- "@babel/plugin-proposal-class-properties": "^7.14.5",
31
29
  "@babel/plugin-proposal-object-rest-spread": "^7.16.7",
32
30
  "@babel/plugin-transform-runtime": "^7.15.0",
33
31
  "@babel/preset-env": "^7.16.11",
@@ -40,6 +38,8 @@
40
38
  "@typescript-eslint/eslint-plugin": "^5.5.0",
41
39
  "@typescript-eslint/parser": "^5.5.0",
42
40
  "babel-loader": "^8.2.3",
41
+ "esbuild": "^0.17.0",
42
+ "esbuild-plugin-babel": "^0.2.3",
43
43
  "eslint": "^7.16.0",
44
44
  "eslint-config-prettier": "^8.3.0",
45
45
  "eslint-import-resolver-typescript": "^2.5.0",
@@ -56,14 +56,11 @@
56
56
  "ts-jest": "^27.0.5",
57
57
  "ts-node": "^10.4.0",
58
58
  "typedoc": "^0.22.10",
59
- "typescript": "^4.5.2",
60
- "webpack": "^5.67.0",
61
- "webpack-cli": "^4.9.2"
59
+ "typescript": "^4.5.2"
62
60
  },
63
61
  "dependencies": {
64
62
  "@babel/runtime-corejs3": "^7.17.2",
65
- "core-js": "^3.21.0",
66
- "lodash": "^4.17.21"
63
+ "core-js": "^3.21.0"
67
64
  },
68
65
  "peerDependencies": {
69
66
  "kolmafia": "^5.26781.0"