@js-draw/math 1.11.1 → 1.17.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/Vec3.d.ts +21 -0
- package/dist/cjs/Vec3.js +28 -0
- package/dist/cjs/lib.d.ts +2 -2
- package/dist/cjs/lib.js +16 -3
- package/dist/cjs/rounding/cleanUpNumber.d.ts +3 -0
- package/dist/cjs/rounding/cleanUpNumber.js +35 -0
- package/dist/cjs/rounding/constants.d.ts +1 -0
- package/dist/cjs/rounding/constants.js +4 -0
- package/dist/cjs/rounding/getLenAfterDecimal.d.ts +10 -0
- package/dist/cjs/rounding/getLenAfterDecimal.js +30 -0
- package/dist/cjs/rounding/lib.d.ts +1 -0
- package/dist/cjs/rounding/lib.js +5 -0
- package/dist/cjs/{rounding.d.ts → rounding/toRoundedString.d.ts} +1 -3
- package/dist/cjs/rounding/toRoundedString.js +54 -0
- package/dist/cjs/rounding/toStringOfSamePrecision.d.ts +2 -0
- package/dist/cjs/rounding/toStringOfSamePrecision.js +58 -0
- package/dist/cjs/rounding/toStringOfSamePrecision.test.d.ts +1 -0
- package/dist/cjs/shapes/Abstract2DShape.d.ts +3 -0
- package/dist/cjs/shapes/BezierJSWrapper.d.ts +15 -5
- package/dist/cjs/shapes/BezierJSWrapper.js +135 -18
- package/dist/cjs/shapes/LineSegment2.d.ts +34 -5
- package/dist/cjs/shapes/LineSegment2.js +63 -10
- package/dist/cjs/shapes/Parameterized2DShape.d.ts +31 -0
- package/dist/cjs/shapes/Parameterized2DShape.js +15 -0
- package/dist/cjs/shapes/Path.d.ts +40 -6
- package/dist/cjs/shapes/Path.js +181 -22
- package/dist/cjs/shapes/PointShape2D.d.ts +14 -3
- package/dist/cjs/shapes/PointShape2D.js +28 -5
- package/dist/cjs/shapes/QuadraticBezier.d.ts +4 -0
- package/dist/cjs/shapes/QuadraticBezier.js +19 -4
- package/dist/cjs/shapes/Rect2.d.ts +3 -0
- package/dist/cjs/shapes/Rect2.js +4 -1
- package/dist/mjs/Vec3.d.ts +21 -0
- package/dist/mjs/Vec3.mjs +28 -0
- package/dist/mjs/lib.d.ts +2 -2
- package/dist/mjs/lib.mjs +1 -1
- package/dist/mjs/rounding/cleanUpNumber.d.ts +3 -0
- package/dist/mjs/rounding/cleanUpNumber.mjs +31 -0
- package/dist/mjs/rounding/cleanUpNumber.test.d.ts +1 -0
- package/dist/mjs/rounding/constants.d.ts +1 -0
- package/dist/mjs/rounding/constants.mjs +1 -0
- package/dist/mjs/rounding/getLenAfterDecimal.d.ts +10 -0
- package/dist/mjs/rounding/getLenAfterDecimal.mjs +26 -0
- package/dist/mjs/rounding/lib.d.ts +1 -0
- package/dist/mjs/rounding/lib.mjs +1 -0
- package/dist/mjs/{rounding.d.ts → rounding/toRoundedString.d.ts} +1 -3
- package/dist/mjs/rounding/toRoundedString.mjs +47 -0
- package/dist/mjs/rounding/toRoundedString.test.d.ts +1 -0
- package/dist/mjs/rounding/toStringOfSamePrecision.d.ts +2 -0
- package/dist/mjs/rounding/toStringOfSamePrecision.mjs +51 -0
- package/dist/mjs/rounding/toStringOfSamePrecision.test.d.ts +1 -0
- package/dist/mjs/shapes/Abstract2DShape.d.ts +3 -0
- package/dist/mjs/shapes/BezierJSWrapper.d.ts +15 -5
- package/dist/mjs/shapes/BezierJSWrapper.mjs +133 -18
- package/dist/mjs/shapes/LineSegment2.d.ts +34 -5
- package/dist/mjs/shapes/LineSegment2.mjs +63 -10
- package/dist/mjs/shapes/Parameterized2DShape.d.ts +31 -0
- package/dist/mjs/shapes/Parameterized2DShape.mjs +8 -0
- package/dist/mjs/shapes/Path.d.ts +40 -6
- package/dist/mjs/shapes/Path.mjs +175 -16
- package/dist/mjs/shapes/PointShape2D.d.ts +14 -3
- package/dist/mjs/shapes/PointShape2D.mjs +28 -5
- package/dist/mjs/shapes/QuadraticBezier.d.ts +4 -0
- package/dist/mjs/shapes/QuadraticBezier.mjs +19 -4
- package/dist/mjs/shapes/Rect2.d.ts +3 -0
- package/dist/mjs/shapes/Rect2.mjs +4 -1
- package/package.json +5 -5
- package/src/Vec3.test.ts +26 -7
- package/src/Vec3.ts +30 -0
- package/src/lib.ts +3 -1
- package/src/rounding/cleanUpNumber.test.ts +15 -0
- package/src/rounding/cleanUpNumber.ts +38 -0
- package/src/rounding/constants.ts +3 -0
- package/src/rounding/getLenAfterDecimal.ts +29 -0
- package/src/rounding/lib.ts +2 -0
- package/src/rounding/toRoundedString.test.ts +32 -0
- package/src/rounding/toRoundedString.ts +57 -0
- package/src/rounding/toStringOfSamePrecision.test.ts +21 -0
- package/src/rounding/toStringOfSamePrecision.ts +63 -0
- package/src/shapes/Abstract2DShape.ts +3 -0
- package/src/shapes/BezierJSWrapper.ts +154 -14
- package/src/shapes/LineSegment2.test.ts +35 -1
- package/src/shapes/LineSegment2.ts +79 -11
- package/src/shapes/Parameterized2DShape.ts +39 -0
- package/src/shapes/Path.test.ts +63 -3
- package/src/shapes/Path.ts +211 -26
- package/src/shapes/PointShape2D.ts +33 -6
- package/src/shapes/QuadraticBezier.test.ts +48 -12
- package/src/shapes/QuadraticBezier.ts +23 -5
- package/src/shapes/Rect2.ts +4 -1
- package/dist/cjs/rounding.js +0 -146
- package/dist/mjs/rounding.mjs +0 -139
- package/src/rounding.test.ts +0 -65
- package/src/rounding.ts +0 -168
- /package/dist/cjs/{rounding.test.d.ts → rounding/cleanUpNumber.test.d.ts} +0 -0
- /package/dist/{mjs/rounding.test.d.ts → cjs/rounding/toRoundedString.test.d.ts} +0 -0
package/src/shapes/Rect2.ts
CHANGED
@@ -67,6 +67,9 @@ export class Rect2 extends Abstract2DShape {
|
|
67
67
|
&& this.y + this.h >= other.y + other.h;
|
68
68
|
}
|
69
69
|
|
70
|
+
/**
|
71
|
+
* @returns true iff this and `other` overlap
|
72
|
+
*/
|
70
73
|
public intersects(other: Rect2): boolean {
|
71
74
|
// Project along x/y axes.
|
72
75
|
const thisMinX = this.x;
|
@@ -181,7 +184,7 @@ export class Rect2 extends Abstract2DShape {
|
|
181
184
|
let closest: Point2|null = null;
|
182
185
|
let closestDist: number|null = null;
|
183
186
|
for (const point of closestEdgePoints) {
|
184
|
-
const dist = point.
|
187
|
+
const dist = point.distanceTo(target);
|
185
188
|
if (closestDist === null || dist < closestDist) {
|
186
189
|
closest = point;
|
187
190
|
closestDist = dist;
|
package/dist/cjs/rounding.js
DELETED
@@ -1,146 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
// @packageDocumentation @internal
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
4
|
-
exports.toStringOfSamePrecision = exports.getLenAfterDecimal = exports.toRoundedString = exports.cleanUpNumber = void 0;
|
5
|
-
// Clean up stringified numbers
|
6
|
-
const cleanUpNumber = (text) => {
|
7
|
-
// Regular expression substitions can be somewhat expensive. Only do them
|
8
|
-
// if necessary.
|
9
|
-
if (text.indexOf('e') > 0) {
|
10
|
-
// Round to zero.
|
11
|
-
if (text.match(/[eE][-]\d{2,}$/)) {
|
12
|
-
return '0';
|
13
|
-
}
|
14
|
-
}
|
15
|
-
const lastChar = text.charAt(text.length - 1);
|
16
|
-
if (lastChar === '0' || lastChar === '.') {
|
17
|
-
// Remove trailing zeroes
|
18
|
-
text = text.replace(/([.]\d*[^0]+)0+$/, '$1');
|
19
|
-
text = text.replace(/[.]0+$/, '.');
|
20
|
-
// Remove trailing period
|
21
|
-
text = text.replace(/[.]$/, '');
|
22
|
-
}
|
23
|
-
const firstChar = text.charAt(0);
|
24
|
-
if (firstChar === '0' || firstChar === '-') {
|
25
|
-
// Remove unnecessary leading zeroes.
|
26
|
-
text = text.replace(/^(0+)[.]/, '.');
|
27
|
-
text = text.replace(/^-(0+)[.]/, '-.');
|
28
|
-
text = text.replace(/^(-?)0+$/, '$10');
|
29
|
-
}
|
30
|
-
if (text === '-0') {
|
31
|
-
return '0';
|
32
|
-
}
|
33
|
-
return text;
|
34
|
-
};
|
35
|
-
exports.cleanUpNumber = cleanUpNumber;
|
36
|
-
/**
|
37
|
-
* Converts `num` to a string, removing trailing digits that were likely caused by
|
38
|
-
* precision errors.
|
39
|
-
*
|
40
|
-
* @example
|
41
|
-
* ```ts,runnable,console
|
42
|
-
* import { toRoundedString } from '@js-draw/math';
|
43
|
-
*
|
44
|
-
* console.log('Rounded: ', toRoundedString(1.000000011));
|
45
|
-
* ```
|
46
|
-
*/
|
47
|
-
const toRoundedString = (num) => {
|
48
|
-
// Try to remove rounding errors. If the number ends in at least three/four zeroes
|
49
|
-
// (or nines) just one or two digits, it's probably a rounding error.
|
50
|
-
const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d{1,4}$/;
|
51
|
-
const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,})\d{1,4}$/;
|
52
|
-
let text = num.toString(10);
|
53
|
-
if (text.indexOf('.') === -1) {
|
54
|
-
return text;
|
55
|
-
}
|
56
|
-
const roundingDownMatch = hasRoundingDownExp.exec(text);
|
57
|
-
if (roundingDownMatch) {
|
58
|
-
const negativeSign = roundingDownMatch[1];
|
59
|
-
const postDecimalString = roundingDownMatch[3];
|
60
|
-
const lastDigit = parseInt(postDecimalString.charAt(postDecimalString.length - 1), 10);
|
61
|
-
const postDecimal = parseInt(postDecimalString, 10);
|
62
|
-
const preDecimal = parseInt(roundingDownMatch[2], 10);
|
63
|
-
const origPostDecimalString = roundingDownMatch[3];
|
64
|
-
let newPostDecimal = (postDecimal + 10 - lastDigit).toString();
|
65
|
-
let carry = 0;
|
66
|
-
if (newPostDecimal.length > postDecimal.toString().length) {
|
67
|
-
// Left-shift
|
68
|
-
newPostDecimal = newPostDecimal.substring(1);
|
69
|
-
carry = 1;
|
70
|
-
}
|
71
|
-
// parseInt(...).toString() removes leading zeroes. Add them back.
|
72
|
-
while (newPostDecimal.length < origPostDecimalString.length) {
|
73
|
-
newPostDecimal = carry.toString(10) + newPostDecimal;
|
74
|
-
carry = 0;
|
75
|
-
}
|
76
|
-
text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
|
77
|
-
}
|
78
|
-
text = text.replace(fixRoundingUpExp, '$1');
|
79
|
-
return (0, exports.cleanUpNumber)(text);
|
80
|
-
};
|
81
|
-
exports.toRoundedString = toRoundedString;
|
82
|
-
const numberExp = /^([-]?)(\d*)[.](\d+)$/;
|
83
|
-
const getLenAfterDecimal = (numberAsString) => {
|
84
|
-
const numberMatch = numberExp.exec(numberAsString);
|
85
|
-
if (!numberMatch) {
|
86
|
-
// If not a match, either the number is exponential notation (or is something
|
87
|
-
// like NaN or Infinity)
|
88
|
-
if (numberAsString.search(/[eE]/) !== -1 || /^[a-zA-Z]+$/.exec(numberAsString)) {
|
89
|
-
return -1;
|
90
|
-
// Or it has no decimal point
|
91
|
-
}
|
92
|
-
else {
|
93
|
-
return 0;
|
94
|
-
}
|
95
|
-
}
|
96
|
-
const afterDecimalLen = numberMatch[3].length;
|
97
|
-
return afterDecimalLen;
|
98
|
-
};
|
99
|
-
exports.getLenAfterDecimal = getLenAfterDecimal;
|
100
|
-
// [reference] should be a string representation of a base-10 number (no exponential (e.g. 10e10))
|
101
|
-
const toStringOfSamePrecision = (num, ...references) => {
|
102
|
-
const text = num.toString(10);
|
103
|
-
const textMatch = numberExp.exec(text);
|
104
|
-
if (!textMatch) {
|
105
|
-
return text;
|
106
|
-
}
|
107
|
-
let decimalPlaces = -1;
|
108
|
-
for (const reference of references) {
|
109
|
-
decimalPlaces = Math.max((0, exports.getLenAfterDecimal)(reference), decimalPlaces);
|
110
|
-
}
|
111
|
-
if (decimalPlaces === -1) {
|
112
|
-
return (0, exports.toRoundedString)(num);
|
113
|
-
}
|
114
|
-
// Make text's after decimal length match [afterDecimalLen].
|
115
|
-
let postDecimal = textMatch[3].substring(0, decimalPlaces);
|
116
|
-
let preDecimal = textMatch[2];
|
117
|
-
const nextDigit = textMatch[3].charAt(decimalPlaces);
|
118
|
-
if (nextDigit !== '') {
|
119
|
-
const asNumber = parseInt(nextDigit, 10);
|
120
|
-
if (asNumber >= 5) {
|
121
|
-
// Don't attempt to parseInt() an empty string.
|
122
|
-
if (postDecimal.length > 0) {
|
123
|
-
const leadingZeroMatch = /^(0+)(\d*)$/.exec(postDecimal);
|
124
|
-
let leadingZeroes = '';
|
125
|
-
let postLeading = postDecimal;
|
126
|
-
if (leadingZeroMatch) {
|
127
|
-
leadingZeroes = leadingZeroMatch[1];
|
128
|
-
postLeading = leadingZeroMatch[2];
|
129
|
-
}
|
130
|
-
postDecimal = (parseInt(postDecimal) + 1).toString();
|
131
|
-
// If postDecimal got longer, remove leading zeroes if possible
|
132
|
-
if (postDecimal.length > postLeading.length && leadingZeroes.length > 0) {
|
133
|
-
leadingZeroes = leadingZeroes.substring(1);
|
134
|
-
}
|
135
|
-
postDecimal = leadingZeroes + postDecimal;
|
136
|
-
}
|
137
|
-
if (postDecimal.length === 0 || postDecimal.length > decimalPlaces) {
|
138
|
-
preDecimal = (parseInt(preDecimal) + 1).toString();
|
139
|
-
postDecimal = postDecimal.substring(1);
|
140
|
-
}
|
141
|
-
}
|
142
|
-
}
|
143
|
-
const negativeSign = textMatch[1];
|
144
|
-
return (0, exports.cleanUpNumber)(`${negativeSign}${preDecimal}.${postDecimal}`);
|
145
|
-
};
|
146
|
-
exports.toStringOfSamePrecision = toStringOfSamePrecision;
|
package/dist/mjs/rounding.mjs
DELETED
@@ -1,139 +0,0 @@
|
|
1
|
-
// @packageDocumentation @internal
|
2
|
-
// Clean up stringified numbers
|
3
|
-
export const cleanUpNumber = (text) => {
|
4
|
-
// Regular expression substitions can be somewhat expensive. Only do them
|
5
|
-
// if necessary.
|
6
|
-
if (text.indexOf('e') > 0) {
|
7
|
-
// Round to zero.
|
8
|
-
if (text.match(/[eE][-]\d{2,}$/)) {
|
9
|
-
return '0';
|
10
|
-
}
|
11
|
-
}
|
12
|
-
const lastChar = text.charAt(text.length - 1);
|
13
|
-
if (lastChar === '0' || lastChar === '.') {
|
14
|
-
// Remove trailing zeroes
|
15
|
-
text = text.replace(/([.]\d*[^0]+)0+$/, '$1');
|
16
|
-
text = text.replace(/[.]0+$/, '.');
|
17
|
-
// Remove trailing period
|
18
|
-
text = text.replace(/[.]$/, '');
|
19
|
-
}
|
20
|
-
const firstChar = text.charAt(0);
|
21
|
-
if (firstChar === '0' || firstChar === '-') {
|
22
|
-
// Remove unnecessary leading zeroes.
|
23
|
-
text = text.replace(/^(0+)[.]/, '.');
|
24
|
-
text = text.replace(/^-(0+)[.]/, '-.');
|
25
|
-
text = text.replace(/^(-?)0+$/, '$10');
|
26
|
-
}
|
27
|
-
if (text === '-0') {
|
28
|
-
return '0';
|
29
|
-
}
|
30
|
-
return text;
|
31
|
-
};
|
32
|
-
/**
|
33
|
-
* Converts `num` to a string, removing trailing digits that were likely caused by
|
34
|
-
* precision errors.
|
35
|
-
*
|
36
|
-
* @example
|
37
|
-
* ```ts,runnable,console
|
38
|
-
* import { toRoundedString } from '@js-draw/math';
|
39
|
-
*
|
40
|
-
* console.log('Rounded: ', toRoundedString(1.000000011));
|
41
|
-
* ```
|
42
|
-
*/
|
43
|
-
export const toRoundedString = (num) => {
|
44
|
-
// Try to remove rounding errors. If the number ends in at least three/four zeroes
|
45
|
-
// (or nines) just one or two digits, it's probably a rounding error.
|
46
|
-
const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d{1,4}$/;
|
47
|
-
const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,})\d{1,4}$/;
|
48
|
-
let text = num.toString(10);
|
49
|
-
if (text.indexOf('.') === -1) {
|
50
|
-
return text;
|
51
|
-
}
|
52
|
-
const roundingDownMatch = hasRoundingDownExp.exec(text);
|
53
|
-
if (roundingDownMatch) {
|
54
|
-
const negativeSign = roundingDownMatch[1];
|
55
|
-
const postDecimalString = roundingDownMatch[3];
|
56
|
-
const lastDigit = parseInt(postDecimalString.charAt(postDecimalString.length - 1), 10);
|
57
|
-
const postDecimal = parseInt(postDecimalString, 10);
|
58
|
-
const preDecimal = parseInt(roundingDownMatch[2], 10);
|
59
|
-
const origPostDecimalString = roundingDownMatch[3];
|
60
|
-
let newPostDecimal = (postDecimal + 10 - lastDigit).toString();
|
61
|
-
let carry = 0;
|
62
|
-
if (newPostDecimal.length > postDecimal.toString().length) {
|
63
|
-
// Left-shift
|
64
|
-
newPostDecimal = newPostDecimal.substring(1);
|
65
|
-
carry = 1;
|
66
|
-
}
|
67
|
-
// parseInt(...).toString() removes leading zeroes. Add them back.
|
68
|
-
while (newPostDecimal.length < origPostDecimalString.length) {
|
69
|
-
newPostDecimal = carry.toString(10) + newPostDecimal;
|
70
|
-
carry = 0;
|
71
|
-
}
|
72
|
-
text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
|
73
|
-
}
|
74
|
-
text = text.replace(fixRoundingUpExp, '$1');
|
75
|
-
return cleanUpNumber(text);
|
76
|
-
};
|
77
|
-
const numberExp = /^([-]?)(\d*)[.](\d+)$/;
|
78
|
-
export const getLenAfterDecimal = (numberAsString) => {
|
79
|
-
const numberMatch = numberExp.exec(numberAsString);
|
80
|
-
if (!numberMatch) {
|
81
|
-
// If not a match, either the number is exponential notation (or is something
|
82
|
-
// like NaN or Infinity)
|
83
|
-
if (numberAsString.search(/[eE]/) !== -1 || /^[a-zA-Z]+$/.exec(numberAsString)) {
|
84
|
-
return -1;
|
85
|
-
// Or it has no decimal point
|
86
|
-
}
|
87
|
-
else {
|
88
|
-
return 0;
|
89
|
-
}
|
90
|
-
}
|
91
|
-
const afterDecimalLen = numberMatch[3].length;
|
92
|
-
return afterDecimalLen;
|
93
|
-
};
|
94
|
-
// [reference] should be a string representation of a base-10 number (no exponential (e.g. 10e10))
|
95
|
-
export const toStringOfSamePrecision = (num, ...references) => {
|
96
|
-
const text = num.toString(10);
|
97
|
-
const textMatch = numberExp.exec(text);
|
98
|
-
if (!textMatch) {
|
99
|
-
return text;
|
100
|
-
}
|
101
|
-
let decimalPlaces = -1;
|
102
|
-
for (const reference of references) {
|
103
|
-
decimalPlaces = Math.max(getLenAfterDecimal(reference), decimalPlaces);
|
104
|
-
}
|
105
|
-
if (decimalPlaces === -1) {
|
106
|
-
return toRoundedString(num);
|
107
|
-
}
|
108
|
-
// Make text's after decimal length match [afterDecimalLen].
|
109
|
-
let postDecimal = textMatch[3].substring(0, decimalPlaces);
|
110
|
-
let preDecimal = textMatch[2];
|
111
|
-
const nextDigit = textMatch[3].charAt(decimalPlaces);
|
112
|
-
if (nextDigit !== '') {
|
113
|
-
const asNumber = parseInt(nextDigit, 10);
|
114
|
-
if (asNumber >= 5) {
|
115
|
-
// Don't attempt to parseInt() an empty string.
|
116
|
-
if (postDecimal.length > 0) {
|
117
|
-
const leadingZeroMatch = /^(0+)(\d*)$/.exec(postDecimal);
|
118
|
-
let leadingZeroes = '';
|
119
|
-
let postLeading = postDecimal;
|
120
|
-
if (leadingZeroMatch) {
|
121
|
-
leadingZeroes = leadingZeroMatch[1];
|
122
|
-
postLeading = leadingZeroMatch[2];
|
123
|
-
}
|
124
|
-
postDecimal = (parseInt(postDecimal) + 1).toString();
|
125
|
-
// If postDecimal got longer, remove leading zeroes if possible
|
126
|
-
if (postDecimal.length > postLeading.length && leadingZeroes.length > 0) {
|
127
|
-
leadingZeroes = leadingZeroes.substring(1);
|
128
|
-
}
|
129
|
-
postDecimal = leadingZeroes + postDecimal;
|
130
|
-
}
|
131
|
-
if (postDecimal.length === 0 || postDecimal.length > decimalPlaces) {
|
132
|
-
preDecimal = (parseInt(preDecimal) + 1).toString();
|
133
|
-
postDecimal = postDecimal.substring(1);
|
134
|
-
}
|
135
|
-
}
|
136
|
-
}
|
137
|
-
const negativeSign = textMatch[1];
|
138
|
-
return cleanUpNumber(`${negativeSign}${preDecimal}.${postDecimal}`);
|
139
|
-
};
|
package/src/rounding.test.ts
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
import { cleanUpNumber, toRoundedString, toStringOfSamePrecision } from './rounding';
|
2
|
-
|
3
|
-
describe('toRoundedString', () => {
|
4
|
-
it('should round up numbers endings similar to .999999999999999', () => {
|
5
|
-
expect(toRoundedString(0.999999999)).toBe('1');
|
6
|
-
expect(toRoundedString(0.899999999)).toBe('.9');
|
7
|
-
expect(toRoundedString(9.999999999)).toBe('10');
|
8
|
-
expect(toRoundedString(-10.999999999)).toBe('-11');
|
9
|
-
});
|
10
|
-
|
11
|
-
it('should round up numbers similar to 10.999999998', () => {
|
12
|
-
expect(toRoundedString(10.999999998)).toBe('11');
|
13
|
-
});
|
14
|
-
|
15
|
-
it('should round strings with multiple digits after the ending decimal points', () => {
|
16
|
-
expect(toRoundedString(292.2 - 292.8)).toBe('-.6');
|
17
|
-
expect(toRoundedString(4.06425600000023)).toBe('4.064256');
|
18
|
-
});
|
19
|
-
|
20
|
-
it('should round down strings ending endings similar to .00000001', () => {
|
21
|
-
expect(toRoundedString(10.00000001)).toBe('10');
|
22
|
-
expect(toRoundedString(-30.00000001)).toBe('-30');
|
23
|
-
expect(toRoundedString(-14.20000000000002)).toBe('-14.2');
|
24
|
-
});
|
25
|
-
|
26
|
-
it('should not round numbers insufficiently close to the next', () => {
|
27
|
-
expect(toRoundedString(-10.9999)).toBe('-10.9999');
|
28
|
-
expect(toRoundedString(-10.0001)).toBe('-10.0001');
|
29
|
-
expect(toRoundedString(-10.123499)).toBe('-10.123499');
|
30
|
-
expect(toRoundedString(0.00123499)).toBe('.00123499');
|
31
|
-
});
|
32
|
-
});
|
33
|
-
|
34
|
-
it('toStringOfSamePrecision', () => {
|
35
|
-
expect(toStringOfSamePrecision(1.23456, '1.12')).toBe('1.23');
|
36
|
-
expect(toStringOfSamePrecision(1.23456, '1.120')).toBe('1.235');
|
37
|
-
expect(toStringOfSamePrecision(1.23456, '1.1')).toBe('1.2');
|
38
|
-
expect(toStringOfSamePrecision(1.23456, '1.1', '5.32')).toBe('1.23');
|
39
|
-
expect(toStringOfSamePrecision(-1.23456, '1.1', '5.32')).toBe('-1.23');
|
40
|
-
expect(toStringOfSamePrecision(-1.99999, '1.1', '5.32')).toBe('-2');
|
41
|
-
expect(toStringOfSamePrecision(1.99999, '1.1', '5.32')).toBe('2');
|
42
|
-
expect(toStringOfSamePrecision(1.89999, '1.1', '5.32')).toBe('1.9');
|
43
|
-
expect(toStringOfSamePrecision(9.99999999, '-1.1234')).toBe('10');
|
44
|
-
expect(toStringOfSamePrecision(9.999999998999996, '100')).toBe('10');
|
45
|
-
expect(toStringOfSamePrecision(0.000012345, '0.000012')).toBe('.000012');
|
46
|
-
expect(toStringOfSamePrecision(0.000012645, '.000012')).toBe('.000013');
|
47
|
-
expect(toStringOfSamePrecision(-0.09999999999999432, '291.3')).toBe('-.1');
|
48
|
-
expect(toStringOfSamePrecision(-0.9999999999999432, '291.3')).toBe('-1');
|
49
|
-
expect(toStringOfSamePrecision(9998.9, '.1', '-11')).toBe('9998.9');
|
50
|
-
expect(toStringOfSamePrecision(-14.20000000000002, '.000001', '-11')).toBe('-14.2');
|
51
|
-
});
|
52
|
-
|
53
|
-
it('cleanUpNumber', () => {
|
54
|
-
expect(cleanUpNumber('000.0000')).toBe('0');
|
55
|
-
expect(cleanUpNumber('-000.0000')).toBe('0');
|
56
|
-
expect(cleanUpNumber('0.0000')).toBe('0');
|
57
|
-
expect(cleanUpNumber('0.001')).toBe('.001');
|
58
|
-
expect(cleanUpNumber('-0.001')).toBe('-.001');
|
59
|
-
expect(cleanUpNumber('-0.000000001')).toBe('-.000000001');
|
60
|
-
expect(cleanUpNumber('-0.00000000100')).toBe('-.000000001');
|
61
|
-
expect(cleanUpNumber('1234')).toBe('1234');
|
62
|
-
expect(cleanUpNumber('1234.5')).toBe('1234.5');
|
63
|
-
expect(cleanUpNumber('1234.500')).toBe('1234.5');
|
64
|
-
expect(cleanUpNumber('1.1368683772161603e-13')).toBe('0');
|
65
|
-
});
|
package/src/rounding.ts
DELETED
@@ -1,168 +0,0 @@
|
|
1
|
-
// @packageDocumentation @internal
|
2
|
-
|
3
|
-
// Clean up stringified numbers
|
4
|
-
export const cleanUpNumber = (text: string) => {
|
5
|
-
// Regular expression substitions can be somewhat expensive. Only do them
|
6
|
-
// if necessary.
|
7
|
-
|
8
|
-
if (text.indexOf('e') > 0) {
|
9
|
-
// Round to zero.
|
10
|
-
if (text.match(/[eE][-]\d{2,}$/)) {
|
11
|
-
return '0';
|
12
|
-
}
|
13
|
-
}
|
14
|
-
|
15
|
-
const lastChar = text.charAt(text.length - 1);
|
16
|
-
if (lastChar === '0' || lastChar === '.') {
|
17
|
-
// Remove trailing zeroes
|
18
|
-
text = text.replace(/([.]\d*[^0]+)0+$/, '$1');
|
19
|
-
text = text.replace(/[.]0+$/, '.');
|
20
|
-
|
21
|
-
// Remove trailing period
|
22
|
-
text = text.replace(/[.]$/, '');
|
23
|
-
}
|
24
|
-
|
25
|
-
const firstChar = text.charAt(0);
|
26
|
-
if (firstChar === '0' || firstChar === '-') {
|
27
|
-
// Remove unnecessary leading zeroes.
|
28
|
-
text = text.replace(/^(0+)[.]/, '.');
|
29
|
-
text = text.replace(/^-(0+)[.]/, '-.');
|
30
|
-
text = text.replace(/^(-?)0+$/, '$10');
|
31
|
-
}
|
32
|
-
|
33
|
-
if (text === '-0') {
|
34
|
-
return '0';
|
35
|
-
}
|
36
|
-
|
37
|
-
return text;
|
38
|
-
};
|
39
|
-
|
40
|
-
/**
|
41
|
-
* Converts `num` to a string, removing trailing digits that were likely caused by
|
42
|
-
* precision errors.
|
43
|
-
*
|
44
|
-
* @example
|
45
|
-
* ```ts,runnable,console
|
46
|
-
* import { toRoundedString } from '@js-draw/math';
|
47
|
-
*
|
48
|
-
* console.log('Rounded: ', toRoundedString(1.000000011));
|
49
|
-
* ```
|
50
|
-
*/
|
51
|
-
export const toRoundedString = (num: number): string => {
|
52
|
-
// Try to remove rounding errors. If the number ends in at least three/four zeroes
|
53
|
-
// (or nines) just one or two digits, it's probably a rounding error.
|
54
|
-
const fixRoundingUpExp = /^([-]?\d*\.\d{3,})0{4,}\d{1,4}$/;
|
55
|
-
const hasRoundingDownExp = /^([-]?)(\d*)\.(\d{3,}9{4,})\d{1,4}$/;
|
56
|
-
|
57
|
-
let text = num.toString(10);
|
58
|
-
if (text.indexOf('.') === -1) {
|
59
|
-
return text;
|
60
|
-
}
|
61
|
-
|
62
|
-
const roundingDownMatch = hasRoundingDownExp.exec(text);
|
63
|
-
if (roundingDownMatch) {
|
64
|
-
const negativeSign = roundingDownMatch[1];
|
65
|
-
const postDecimalString = roundingDownMatch[3];
|
66
|
-
const lastDigit = parseInt(postDecimalString.charAt(postDecimalString.length - 1), 10);
|
67
|
-
const postDecimal = parseInt(postDecimalString, 10);
|
68
|
-
const preDecimal = parseInt(roundingDownMatch[2], 10);
|
69
|
-
|
70
|
-
const origPostDecimalString = roundingDownMatch[3];
|
71
|
-
|
72
|
-
let newPostDecimal = (postDecimal + 10 - lastDigit).toString();
|
73
|
-
let carry = 0;
|
74
|
-
if (newPostDecimal.length > postDecimal.toString().length) {
|
75
|
-
// Left-shift
|
76
|
-
newPostDecimal = newPostDecimal.substring(1);
|
77
|
-
carry = 1;
|
78
|
-
}
|
79
|
-
|
80
|
-
// parseInt(...).toString() removes leading zeroes. Add them back.
|
81
|
-
while (newPostDecimal.length < origPostDecimalString.length) {
|
82
|
-
newPostDecimal = carry.toString(10) + newPostDecimal;
|
83
|
-
carry = 0;
|
84
|
-
}
|
85
|
-
|
86
|
-
text = `${negativeSign + (preDecimal + carry).toString()}.${newPostDecimal}`;
|
87
|
-
}
|
88
|
-
|
89
|
-
text = text.replace(fixRoundingUpExp, '$1');
|
90
|
-
|
91
|
-
return cleanUpNumber(text);
|
92
|
-
};
|
93
|
-
|
94
|
-
const numberExp = /^([-]?)(\d*)[.](\d+)$/;
|
95
|
-
export const getLenAfterDecimal = (numberAsString: string) => {
|
96
|
-
const numberMatch = numberExp.exec(numberAsString);
|
97
|
-
if (!numberMatch) {
|
98
|
-
// If not a match, either the number is exponential notation (or is something
|
99
|
-
// like NaN or Infinity)
|
100
|
-
if (numberAsString.search(/[eE]/) !== -1 || /^[a-zA-Z]+$/.exec(numberAsString)) {
|
101
|
-
return -1;
|
102
|
-
// Or it has no decimal point
|
103
|
-
} else {
|
104
|
-
return 0;
|
105
|
-
}
|
106
|
-
}
|
107
|
-
|
108
|
-
const afterDecimalLen = numberMatch[3].length;
|
109
|
-
return afterDecimalLen;
|
110
|
-
};
|
111
|
-
|
112
|
-
// [reference] should be a string representation of a base-10 number (no exponential (e.g. 10e10))
|
113
|
-
export const toStringOfSamePrecision = (num: number, ...references: string[]): string => {
|
114
|
-
const text = num.toString(10);
|
115
|
-
const textMatch = numberExp.exec(text);
|
116
|
-
if (!textMatch) {
|
117
|
-
return text;
|
118
|
-
}
|
119
|
-
|
120
|
-
let decimalPlaces = -1;
|
121
|
-
for (const reference of references) {
|
122
|
-
decimalPlaces = Math.max(getLenAfterDecimal(reference), decimalPlaces);
|
123
|
-
}
|
124
|
-
|
125
|
-
if (decimalPlaces === -1) {
|
126
|
-
return toRoundedString(num);
|
127
|
-
}
|
128
|
-
|
129
|
-
// Make text's after decimal length match [afterDecimalLen].
|
130
|
-
let postDecimal = textMatch[3].substring(0, decimalPlaces);
|
131
|
-
let preDecimal = textMatch[2];
|
132
|
-
const nextDigit = textMatch[3].charAt(decimalPlaces);
|
133
|
-
|
134
|
-
if (nextDigit !== '') {
|
135
|
-
const asNumber = parseInt(nextDigit, 10);
|
136
|
-
if (asNumber >= 5) {
|
137
|
-
// Don't attempt to parseInt() an empty string.
|
138
|
-
if (postDecimal.length > 0) {
|
139
|
-
const leadingZeroMatch = /^(0+)(\d*)$/.exec(postDecimal);
|
140
|
-
|
141
|
-
let leadingZeroes = '';
|
142
|
-
let postLeading = postDecimal;
|
143
|
-
if (leadingZeroMatch) {
|
144
|
-
leadingZeroes = leadingZeroMatch[1];
|
145
|
-
postLeading = leadingZeroMatch[2];
|
146
|
-
}
|
147
|
-
|
148
|
-
postDecimal = (parseInt(postDecimal) + 1).toString();
|
149
|
-
|
150
|
-
// If postDecimal got longer, remove leading zeroes if possible
|
151
|
-
if (postDecimal.length > postLeading.length && leadingZeroes.length > 0) {
|
152
|
-
leadingZeroes = leadingZeroes.substring(1);
|
153
|
-
}
|
154
|
-
|
155
|
-
postDecimal = leadingZeroes + postDecimal;
|
156
|
-
}
|
157
|
-
|
158
|
-
if (postDecimal.length === 0 || postDecimal.length > decimalPlaces) {
|
159
|
-
preDecimal = (parseInt(preDecimal) + 1).toString();
|
160
|
-
postDecimal = postDecimal.substring(1);
|
161
|
-
}
|
162
|
-
}
|
163
|
-
}
|
164
|
-
|
165
|
-
const negativeSign = textMatch[1];
|
166
|
-
return cleanUpNumber(`${negativeSign}${preDecimal}.${postDecimal}`);
|
167
|
-
};
|
168
|
-
|
File without changes
|
File without changes
|