npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jasmine-float-equals

v1.0.4

Published

A Jasmine matcher for comparing objects containing floats with a tolerance

Downloads

140

Readme

jasmine-float-equals

jasmine-float-equals is a custom matcher for Jasmine deep comparing two objects while taking into account a tolerance for float comparison.

It can compare floats of type number but also floats that are embedded in a string, alone (ex: '10.01') or within a text (ex: 'My 1st is 10.01 and my second is 25.1').

Usage

Install the package :

npm install --save-dev jasmine-float-equals

Import the package before your tests and use the toEqualWithFloatTolerance(expected: any, epsilon: number, handleStringEmbeddedFloats: boolean) matcher.

  • epsilon: the maximum difference allowed between two floats
  • handleStringEnbeddedFloats (false by default): whether to also compare with a tolerance floats that are embedded in string
import 'jasmine-float-equals';

describe('my suit', function() {
    it('should pass with tolerance', function() {
        const expected = {
            hello: 10,
            world: {
                foo: 25
            }
        };
        const actual = {
            hello: 10.00001,
            world: {
                foo: 25.00005
            }
        };
        
        expect(actual).toEqualWithFloatTolerance(expected, 10e-6); // pass
        expect(actual).toEqualWithFloatTolerance(expected, 10e-7); // DON'T PASS
    });
    
    it('should pass with tolerance when embedded inside a string', function() {
        const expected = 'The result is 10.00001 units and 42.00001 subunits';
        const actual = 'The result is 10.00002 units and 42.00002 subunits';
        expect(actual).toEqualWithFloatTolerance(expected, 10e-6, true); // pass

        expect('10.00001').toEqualWithFloatTolerance('10.00002', 10e-6, true) // pass
    });
});

If you need to register the matcher at a given time, you can use the registerEqualWithFloatToleranceMatcher function.

import { registerEqualWithFloatToleranceMatcher } from 'jasmine-float-equals';

// at my specific time that I need it
registerEqualWithFloatToleranceMatcher();

This will register the matcher in a beforeAll block.

If you need to, you can also manually register the matcher to Jasmine using :

import { toEqualWithFloatToleranceMatcher } from 'jasmine-float-equals';


beforeAll(function () {
    jasmine.addMatchers({
        toEqualWithFloatTolerance: toEqualWithFloatToleranceMatcher
    });
});

Or execute the function manually:

let floatsAreEqual: boolean = toEqualWithFloatTolerance().compare(25, 25.00001, 10e-6).pass;

Credits

Crafted with ❤️ by Bastien MARSAUD.