@@ -11,39 +11,36 @@ import { tolerance as TOLERANCE } from "./constants";
1111const EPSILON = 1e-8 ;
1212/**
1313 * @summary Returns scalar product of vectors
14- * @param v1 {Number[]} Vector 1
15- * @param v2 {Number[]} Vector 2
16- * @return {Number }
14+ * @param v1 Vector 1
15+ * @param v2 Vector 2
1716 */
18-
19- const product = ( v1 , v2 ) => {
17+ const product = ( v1 : number [ ] , v2 : number [ ] ) => {
2018 return math . multiply ( v1 , math . transpose ( v2 ) ) ;
2119} ;
2220
2321/**
2422 * @summary Returns length of a vector.
25- * @param v {Number[]} Vector
26- * @return {Number }
23+ * @param v Vector
2724 */
28- const vlen = ( v ) => {
25+ const vlen = ( v : number [ ] ) => {
2926 return math . sqrt ( product ( v , v ) ) ;
3027} ;
3128
3229/**
3330 * @summary Returns angle between `a` and `b` vectors.
34- * @param a {Number[]} Vector a
35- * @param b {Number[]} Vector b
36- * @param [unit] {String} `rad`, `deg`
37- * @return {Number }
31+ * @param a Vector a
32+ * @param b Vector b
33+ * @param unit `rad`, `deg`
3834 */
39- const angle = ( a , b , unit ) => {
35+ const angle = ( a : number [ ] , b : number [ ] , unit : string ) => {
4036 const lenA = vlen ( a ) ;
4137 const lenB = vlen ( b ) ;
38+ // @ts -ignore
4239 return math . unit ( math . acos ( product ( a , b ) / ( lenA * lenB ) ) , "rad" ) . toNumber ( unit || "deg" ) ;
4340} ;
4441
45- const angleUpTo90 = ( ... args ) => {
46- const angleUpTo180 = angle ( ... args ) ;
42+ const angleUpTo90 = ( a : number [ ] , b : number [ ] , unit : string ) => {
43+ const angleUpTo180 = angle ( a , b , unit ) ;
4744 return angleUpTo180 < 90 ? angleUpTo180 : 180 - angleUpTo180 ;
4845} ;
4946
@@ -53,7 +50,7 @@ const angleUpTo90 = (...args) => {
5350 * @param v2 {Number[]} Vector
5451 * @return {Number }
5552 */
56- const vDist = ( v1 , v2 ) => {
53+ const vDist = ( v1 : number [ ] , v2 : number [ ] ) => {
5754 if ( v1 . length !== v2 . length ) {
5855 console . error (
5956 "Attempting to calculate distance between vectors of different dimensionality" ,
@@ -70,35 +67,36 @@ const vDist = (v1, v2) => {
7067 * @param tolerance {Number} Tolerance
7168 * @return {Number }
7269 */
73- const vEqualWithTolerance = ( vec1 , vec2 , tolerance = TOLERANCE . pointsDistance ) =>
74- vDist ( vec1 , vec2 ) <= tolerance ;
70+ const vEqualWithTolerance = ( vec1 :number [ ] , vec2 :number [ ] , tolerance = TOLERANCE . pointsDistance ) : boolean => {
71+ const val = vDist ( vec1 , vec2 ) ;
72+ if ( typeof val === "undefined" ) {
73+ return false ;
74+ }
75+ // @ts -ignore
76+ return val <= tolerance ;
77+ }
78+
7579
7680/**
7781 * @summary Returns 0 if passed number is less than Made.math.EPSILON.
7882 * @param n {Number}
7983 * @return {Number }
8084 */
81- const roundToZero = ( n ) => {
85+ const roundToZero = ( n : number ) => {
8286 return Math . abs ( n ) < EPSILON ? 0 : n ;
8387} ;
8488
8589/**
8690 * @summary Returns number with specified precision.
87- * @param x {Number}
88- * @param n {Number}
89- * @return {Number }
9091 */
91- const precise = ( x , n = 7 ) => {
92+ const precise = ( x : number , n = 7 ) => {
9293 return Number ( x . toPrecision ( n ) ) ;
9394} ;
9495
9596/**
9697 * @summary Returns mod of the passed value with the specified tolerance.
97- * @param num {Number}
98- * @param tolerance {Number}
99- * @return {Number }
10098 */
101- const mod = ( num , tolerance = 0.001 ) => {
99+ const mod = ( num : number , tolerance = 0.001 ) : number => {
102100 const m = num % 1 ;
103101 const x = num >= 0 ? m : 1 + m ;
104102
@@ -112,11 +110,11 @@ const mod = (num, tolerance = 0.001) => {
112110 * @summary Returns cartesian of passed arrays.
113111 * @example combinations([1,2], [4,5], [6]) = [[1,4,6], [1,5,6], [2,4,6], [2,5,6]];
114112 */
115- const cartesianProduct = ( ...arg ) => {
116- const r = [ ] ;
113+ const cartesianProduct = ( ...arg : number [ ] [ ] ) : number [ ] [ ] => {
114+ const r : number [ ] [ ] = [ ] ;
117115 const max = arg . length - 1 ;
118116
119- const helper = ( arr , i ) => {
117+ const helper = ( arr : number [ ] , i : number ) => {
120118 for ( let j = 0 , l = arg [ i ] . length ; j < l ; j ++ ) {
121119 const a = arr . slice ( 0 ) ; // clone arr
122120 a . push ( arg [ i ] [ j ] ) ;
@@ -134,20 +132,16 @@ const cartesianProduct = (...arg) => {
134132
135133/**
136134 * @summary Returns all possible positive integer combinations where each value changes from 0 to a, b, c.
137- * @param a {Number}
138- * @param b {Number}
139- * @param tolerance {Number}
140135 */
141- const almostEqual = ( a , b , tolerance = TOLERANCE . pointsDistance ) => {
136+ const almostEqual = ( a : number , b : number , tolerance = TOLERANCE . pointsDistance ) : boolean => {
142137 return Math . abs ( a - b ) < tolerance ;
143138} ;
144139
145140/**
146141 * @summary Returns true if number is 0 <= x < 1, inclusive, otherwise false.
147142 * Helper to deal with JS arithmetic artifacts.
148- * @number number {Number}
149143 */
150- const isBetweenZeroInclusiveAndOne = ( number , tolerance = TOLERANCE . length ) => {
144+ const isBetweenZeroInclusiveAndOne = ( number : number , tolerance = TOLERANCE . length ) : boolean => {
151145 return roundToZero ( number ) >= 0 && ! almostEqual ( number , 1 , tolerance ) && number < 1 ;
152146} ;
153147
@@ -160,7 +154,7 @@ const isBetweenZeroInclusiveAndOne = (number, tolerance = TOLERANCE.length) => {
160154 * @param b
161155 * @param c
162156 */
163- const combinations = ( a , b , c ) => {
157+ const combinations = ( a : number , b : number , c : number ) => {
164158 const combs = [ ] ;
165159 for ( let i = 0 ; i <= a ; i ++ ) {
166160 for ( let j = 0 ; j <= b ; j ++ ) {
@@ -175,7 +169,7 @@ const combinations = (a, b, c) => {
175169/*
176170 * @summary Same as `combinations` but accepting intervals (tuples) of integers: eg. [-3, 4]
177171 */
178- const combinationsFromIntervals = ( arrA , arrB , arrC ) => {
172+ const combinationsFromIntervals = ( arrA : number [ ] , arrB : number [ ] , arrC : number [ ] ) => {
179173 const combs = [ ] ;
180174 for ( let i = arrA [ 0 ] ; i <= arrA [ 1 ] ; i ++ ) {
181175 for ( let j = arrB [ 0 ] ; j <= arrB [ 1 ] ; j ++ ) {
@@ -187,21 +181,18 @@ const combinationsFromIntervals = (arrA, arrB, arrC) => {
187181 return combs ;
188182} ;
189183
190- const roundValueToNDecimals = ( value , decimals = 3 ) => {
184+ const roundValueToNDecimals = ( value : number , decimals = 3 ) => {
191185 return parseFloat ( value . toFixed ( decimals ) ) ;
192186} ;
193187
194188/**
195189 * @summary Returns n splits of the passed segment.
196- * @param point1 {Number[]}
197- * @param point2 {Number[]}
198- * @param n {Number}
199190 */
200- const calculateSegmentsBetweenPoints3D = ( point1 , point2 , n ) => {
191+ const calculateSegmentsBetweenPoints3D = ( point1 : ( string | number ) [ ] , point2 : ( string | number ) [ ] , n : number | string ) => {
201192 // safely parse if passed strings
202- const point1_ = point1 . map ( ( x ) => parseFloat ( x ) ) ;
203- const point2_ = point2 . map ( ( x ) => parseFloat ( x ) ) ;
204- const n_ = parseInt ( n ) ;
193+ const point1_ = point1 . map ( ( x ) => typeof x === "string" ? parseFloat ( x ) : x ) ;
194+ const point2_ = point2 . map ( ( x ) => typeof x === "string" ? parseFloat ( x ) : x ) ;
195+ const n_ = typeof n === "string" ? parseInt ( n ) : n ;
205196
206197 const result = [ ] ;
207198 for ( let i = 1 ; i < n_ ; i ++ ) {
@@ -223,10 +214,10 @@ const calculateSegmentsBetweenPoints3D = (point1, point2, n) => {
223214 * @locus Client
224215 * @method
225216 * @name toPrecision
226- * @param { Number } number
227- * @param { Number } precision Optional. An integer specifying the number of significant digits.
217+ * @param number
218+ * @param precision Optional. An integer specifying the number of significant digits.
228219 */
229- export function numberToPrecision ( number , precision ) {
220+ export function numberToPrecision ( number : number | string , precision ?: number ) : string {
230221 if ( _ . isNumber ( number ) ) {
231222 return number . toPrecision ( precision ) ;
232223 }
0 commit comments