ogamejs 4.0.1 → 4.0.2

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/README.md CHANGED
@@ -366,7 +366,7 @@ Ogame.Trader.sellCrystal(10000); // → { deut, metal }
366
366
 
367
367
  #### `parseRate(rate = '2:1.5:1', type = 'deut')`
368
368
 
369
- Normalizes a rate relative to a reference resource (`'metal'`, `'crystal'` or `'deut'`). Throws if the rate is malformed.
369
+ Normalizes a rate relative to a reference resource (`'metal'`, `'crystal'` or `'deut'`). Throws if the rate is malformed — that includes a term that is not a number, but also a `0`, an empty one (`'2::1'`) and a negative one, none of which describe a trade.
370
370
 
371
371
  ```javascript
372
372
  Ogame.Trader.parseRate('3:2:1', 'deut');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ogamejs",
3
- "version": "4.0.1",
3
+ "version": "4.0.2",
4
4
  "description": "OGame formulas as a dependency-free ESM library: building and research costs, production, build times, fleet debris and marketplace rates.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1,4 +1,4 @@
1
- import hasNaN from './utils.js';
1
+ import isUsableRate from './utils.js';
2
2
 
3
3
  function extract(rate, type) {
4
4
  let res = {};
@@ -46,7 +46,10 @@ export default function parseRate(rate = '2:1.5:1', type = 'deut') {
46
46
 
47
47
  const res = extract(rate, type);
48
48
 
49
- if (hasNaN(Object.values(res))) {
49
+ // Checked after normalizing rather than on the raw terms: whatever makes a
50
+ // rate unusable — a term that is not a number, `0`, empty, negative — shows
51
+ // up here as a NaN, an Infinity, a 0 or a negative ratio.
52
+ if (!isUsableRate(Object.values(res))) {
50
53
  throw new Error('rate not parsed correctly');
51
54
  }
52
55
 
@@ -1,9 +1,8 @@
1
- export default function hasNaN(args) {
2
- let response = false;
3
- args.forEach((val) => {
4
- if (Number.isNaN(Number(val))) {
5
- response = true;
6
- }
7
- });
8
- return response;
1
+ // A normalized rate is usable when every term is a real, strictly positive
2
+ // number. Checking only for `NaN` let two kinds of nonsense through as if they
3
+ // were answers: `Infinity`, produced by dividing into a `0` term, and `0`
4
+ // itself, which is what an empty term (`'2::1'`) parses to. A negative worth is
5
+ // not something the game can express either.
6
+ export default function isUsableRate(values) {
7
+ return values.every((value) => Number.isFinite(value) && value > 0);
9
8
  }
@@ -1 +1 @@
1
- {"version":3,"file":"parseRate.d.ts","sourceRoot":"","sources":["../../src/trades/parseRate.js"],"names":[],"mappings":"AAuCA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,SAAY,EAAE,IAAI,SAAS,MAchE"}
1
+ {"version":3,"file":"parseRate.d.ts","sourceRoot":"","sources":["../../src/trades/parseRate.js"],"names":[],"mappings":"AAuCA,MAAM,CAAC,OAAO,UAAU,SAAS,CAAC,IAAI,SAAY,EAAE,IAAI,SAAS,MAiBhE"}
@@ -1,2 +1,2 @@
1
- export default function hasNaN(args: any): boolean;
1
+ export default function isUsableRate(values: any): any;
2
2
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/trades/utils.js"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,IAAI,KAAA,WAQlC"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/trades/utils.js"],"names":[],"mappings":"AAKA,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,MAAM,KAAA,OAE1C"}