extra-utils 5.20.0 → 5.21.0

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.
@@ -1 +1 @@
1
- {"version":3,"file":"remap-to-index.js","sourceRoot":"","sources":["../../src/number/remap-to-index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAElC,MAAM,UAAU,YAAY,CAC1B,KAAa,EACb,KAA0C,EAC1C,KAA6B;IAE7B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CACtB,KAAK,CACH,KAAK,EACL,KAAK,EACL,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAClB,CACF,CAAA;IAED,OAAO,KAAK,KAAK,KAAK,CAAC,MAAM;QACxB,CAAC,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;QAClB,CAAC,CAAC,KAAK,CAAA;AACd,CAAC"}
1
+ {"version":3,"file":"remap-to-index.js","sourceRoot":"","sources":["../../src/number/remap-to-index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAElC,MAAM,UAAU,YAAY,CAC1B,KAAa,EACb,KAA0C,EAC1C,KAA6B;IAE7B,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CACtB,KAAK,CACH,KAAK,EACL,KAAK,EACL,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAClB,CACF,CAAA;IAED,OAAO,KAAK,KAAK,KAAK,CAAC,MAAM;QACxB,CAAC,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;QAClB,CAAC,CAAC,KAAK,CAAA;AAUd,CAAC"}
@@ -1,6 +1,16 @@
1
+ import { assert } from '@blackglory/errors';
1
2
  export function remap(value, [oldMin, oldMax], [newMin, newMax]) {
2
- return (value - oldMin) / (oldMax - oldMin)
3
- * (newMax - newMin)
4
- + newMin;
3
+ assert(oldMin <= oldMax, 'The oldMin must be less than or equal to oldMax');
4
+ assert(newMin <= newMax, 'The newMin must be less than or equal to newMax');
5
+ assert(value >= oldMin && value <= oldMax, 'The parameter value must between oldMin and oldMax');
6
+ if (oldMin === oldMax) {
7
+ assert(newMin === newMax, 'The old range cannot be zero-length unless the new range is also zero-length');
8
+ return newMin;
9
+ }
10
+ else {
11
+ return (value - oldMin) / (oldMax - oldMin)
12
+ * (newMax - newMin)
13
+ + newMin;
14
+ }
5
15
  }
6
16
  //# sourceMappingURL=remap.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"remap.js","sourceRoot":"","sources":["../../src/number/remap.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,KAAK,CACnB,KAAa,EACb,CAAC,MAAM,EAAE,MAAM,CAA4C,EAC3D,CAAC,MAAM,EAAE,MAAM,CAA4C;IAE3D,OAAO,CAAC,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;UACpC,CAAC,MAAM,GAAG,MAAM,CAAC;UACjB,MAAM,CAAA;AACf,CAAC"}
1
+ {"version":3,"file":"remap.js","sourceRoot":"","sources":["../../src/number/remap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAE3C,MAAM,UAAU,KAAK,CACnB,KAAa,EACb,CAAC,MAAM,EAAE,MAAM,CAA4C,EAC3D,CAAC,MAAM,EAAE,MAAM,CAA4C;IAE3D,MAAM,CAAC,MAAM,IAAI,MAAM,EAAE,iDAAiD,CAAC,CAAA;IAC3E,MAAM,CAAC,MAAM,IAAI,MAAM,EAAE,iDAAiD,CAAC,CAAA;IAC3E,MAAM,CACJ,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,MAAM,EAClC,oDAAoD,CACrD,CAAA;IAED,IAAI,MAAM,KAAK,MAAM,EAAE;QACrB,MAAM,CACJ,MAAM,KAAK,MAAM,EACjB,8EAA8E,CAC/E,CAAA;QAED,OAAO,MAAM,CAAA;KACd;SAAM;QACL,OAAO,CAAC,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;cACpC,CAAC,MAAM,GAAG,MAAM,CAAC;cACjB,MAAM,CAAA;KACd;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-utils",
3
- "version": "5.20.0",
3
+ "version": "5.21.0",
4
4
  "description": "Utilities for JavaScript and Typescript",
5
5
  "files": [
6
6
  "src",
@@ -59,6 +59,7 @@
59
59
  "typescript-transform-paths": "^3.4.6"
60
60
  },
61
61
  "dependencies": {
62
+ "@blackglory/errors": "^3.1.0",
62
63
  "justypes": "^4.2.0",
63
64
  "lodash-es": "^4.17.21"
64
65
  }
@@ -17,4 +17,13 @@ export function remapToIndex(
17
17
  return index === items.length
18
18
  ? items.length - 1
19
19
  : index
20
+
21
+ // 上方代码在均匀性上优于使用`Math.round`的实现:
22
+ // return Math.round(
23
+ // remap(
24
+ // value
25
+ // , range
26
+ // , [0, items.length - 1]
27
+ // )
28
+ // )
20
29
  }
@@ -1,9 +1,27 @@
1
+ import { assert } from '@blackglory/errors'
2
+
1
3
  export function remap(
2
4
  value: number
3
5
  , [oldMin, oldMax]: readonly [oldMin: number, oldMax: number]
4
6
  , [newMin, newMax]: readonly [newMin: number, newMax: number]
5
7
  ): number {
6
- return (value - oldMin) / (oldMax - oldMin)
7
- * (newMax - newMin)
8
- + newMin
8
+ assert(oldMin <= oldMax, 'The oldMin must be less than or equal to oldMax')
9
+ assert(newMin <= newMax, 'The newMin must be less than or equal to newMax')
10
+ assert(
11
+ value >= oldMin && value <= oldMax
12
+ , 'The parameter value must between oldMin and oldMax'
13
+ )
14
+
15
+ if (oldMin === oldMax) {
16
+ assert(
17
+ newMin === newMax
18
+ , 'The old range cannot be zero-length unless the new range is also zero-length'
19
+ )
20
+
21
+ return newMin
22
+ } else {
23
+ return (value - oldMin) / (oldMax - oldMin)
24
+ * (newMax - newMin)
25
+ + newMin
26
+ }
9
27
  }