Skip to content

Commit ca1724a

Browse files
committed
feat: within
1 parent b1c7383 commit ca1724a

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ https://unpkg.com/math-toolbox/dist/math-toolbox.umd.min.js
7575
| isOdd | Returns true if the number given is odd. |
7676
| isEven | Returns true if the number given is even. |
7777
| difference | Returns the absolute difference between two values. |
78+
| within | Checks if two values are within the given tolerance of each other |
7879

7980
## Plans
8081
See GitHub Wiki:

docs/index.html

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ <h3 class='mb0 no-anchor'></h3>
293293

294294
</li>
295295

296+
297+
<li><a
298+
href='#within'
299+
class="">
300+
within
301+
302+
</a>
303+
304+
</li>
305+
296306
</ul>
297307
</div>
298308
<div class='mt1 h6 quiet'>
@@ -2371,6 +2381,87 @@ <h3 class='fl m0' id='difference'>
23712381

23722382

23732383

2384+
</section>
2385+
2386+
2387+
2388+
2389+
<section class='p2 mb2 clearfix bg-white minishadow'>
2390+
2391+
2392+
<div class='clearfix'>
2393+
<h3 class='fl m0' id='within'>
2394+
within
2395+
</h3>
2396+
2397+
</div>
2398+
2399+
2400+
<p>Checks if two values are within the given tolerance of each other.</p>
2401+
2402+
2403+
<div class='pre p1 fill-light mt0'>within(a: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>, b: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>, tolerance: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>): <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></div>
2404+
2405+
2406+
2407+
2408+
2409+
2410+
2411+
2412+
2413+
2414+
<div class='py1 quiet mt1 prose-big'>Parameters</div>
2415+
<div class='prose'>
2416+
2417+
<div class='space-bottom0'>
2418+
<div>
2419+
<span class='code bold'>a</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>)</code> The first number to check
2420+
2421+
</div>
2422+
2423+
</div>
2424+
2425+
<div class='space-bottom0'>
2426+
<div>
2427+
<span class='code bold'>b</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>)</code> The second number to check
2428+
2429+
</div>
2430+
2431+
</div>
2432+
2433+
<div class='space-bottom0'>
2434+
<div>
2435+
<span class='code bold'>tolerance</span> <code class='quiet'>(<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">number</a>)</code> The tolerance. Anything equal to or less than this is considered within the range.
2436+
2437+
</div>
2438+
2439+
</div>
2440+
2441+
</div>
2442+
2443+
2444+
2445+
2446+
2447+
2448+
<div class='py1 quiet mt1 prose-big'>Returns</div>
2449+
<code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">boolean</a></code>:
2450+
True if a is &lt;= tolerance of b.
2451+
2452+
2453+
2454+
2455+
2456+
2457+
2458+
2459+
2460+
2461+
2462+
2463+
2464+
23742465
</section>
23752466

23762467

src/math-toolbox.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export { isOdd } from './is-odd'
2525
export { isEven } from './is-even'
2626
export { percent } from './percent'
2727
export { difference } from './difference'
28+
export { within } from './within'

src/within.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Checks if two values are within the given tolerance of each other.
3+
*
4+
* @param {number} a - The first number to check
5+
* @param {number} b - The second number to check
6+
* @param {number} tolerance - The tolerance. Anything equal to or less than this is considered within the range.
7+
* @return {boolean} True if a is <= tolerance of b.
8+
*/
9+
function within (a, b, tolerance) {
10+
return (Math.abs(a - b) <= tolerance)
11+
}
12+
13+
export { within }

test/within.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { within } from '../src/math-toolbox'
2+
3+
describe('Checks if two values are within the given tolerance of each other', () => {
4+
it('Expect 20.5 and 21 to be within each other with the tolerance 0.5', () => {
5+
expect(within(20.5, 21, 0.50)).toBe(true)
6+
})
7+
})

0 commit comments

Comments
 (0)