Git Product home page Git Product logo

oddslib's Introduction

This library is stable and being used in production (as of Apr 2021).

A JS library to convert and format odds.

Usage

Odds are constructed by calling the public from(format, odds) constructor:

import * as oddslib from 'oddslib';

// Create from decimal/European odds
let odds = oddslib.from('decimal', 1.20);

// Create from American/Moneyline odds
let odds = oddslib.from('moneyline', -500);

// Create from Hong Kong odds
let odds = oddslib.from('hongKong', .20);

// Create from implied probability
let odds = oddslib.from('impliedProbability', .5)
         = oddslib.from('impliedProbability', '50%');

// Create from UK/fractional odds
let odds = oddslib.from('fractional', 5/2)
         = oddslib.from('fractional', '5/2')
         = oddslib.from('fractional', 2.5);

// Create from Malay odds
let odds = oddslib.from('malay', 0.5);

// Create from Indonesian odds
let odds = oddslib.from('indonesian', -5.0);

Odds can then be converted using the to(format, options?) instance method:

let odds = oddslib.from('decimal', 1.20);

odds.to('decimal');            // == 1.2
odds.to('moneyline');          // == -500
odds.to('hongKong');           // == 0.2
odds.to('impliedProbability'); // == 0.83ฬ…
odds.to('fractional');         // == "1/5"
odds.to('malay');              // == 0.2
odds.to('indonesian');         // == -5.0

The default options are:

{
   precision: null,   // Return a rounded value correct to the nth digit. Do not round if null.
   percentage: false, // Return IP odds as a percentage string. No effect on other formats.
}

Fractions are also approximated if the precision option is specified, by using the algorithm at http://www.mindspring.com/~alanh/fracs.html.

// No approximation
oddslib.from('decimal', 2.33).to('fractional');                  // === "133/100"

// Approximate up to 2nd decimal digit
oddslib.from('decimal', 2.33).to('fractional', {precision: 2});  // === "4/3"

oddslib's People

Contributors

1player avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.