From f7b505fda33776f17c9e0e9f8cb99eade86c8608 Mon Sep 17 00:00:00 2001 From: shayan Date: Fri, 24 Dec 2021 01:26:57 +0330 Subject: [PATCH] add closeTo function --- index.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/index.js b/index.js index 04d8371..6340e18 100644 --- a/index.js +++ b/index.js @@ -491,6 +491,25 @@ return this; }; + /** + * Assert being within [center-eps, center+eps] inclusive + * + * @param {Number} center + * @param {Number} eps + * @api public + */ + + Assertion.prototype.closeTo = function (center, eps) { + let start = center - eps; + let end = center + eps; + let range = start + '..' + end; + this.assert( + start <= this.obj && this.obj <= end + , function(){ return 'expected ' + i(this.obj) + ' to be within ' + range } + , function(){ return 'expected ' + i(this.obj) + ' to not be within ' + range }); + return this; + }; + /** * Function bind implementation. */