Last active
June 28, 2016 12:35
-
-
Save anatomic/5f243e36957c25bf7a12e2e7bbe0f66f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {compose, divide, multiply, add, curry, reduce, map, __, split, props, apply} from "ramda"; | |
declare interface Outcome { | |
outcomeId: number, | |
marketId: number, | |
eventId: number, | |
boosted: boolean, | |
cashoutable: boolean, | |
displayOrder: number, | |
displayStatus: "active" | "suspended", | |
price: Price | |
} | |
declare interface Price { | |
num: number, | |
den: number, | |
} | |
const toArray = (...args) => args; | |
const toPrice = ([num, den]: [number, number]): Price => ({num, den}); | |
const toInt = (x: string): number => parseInt(x, 10); | |
const parseAsPrice: (p: string) => Price = compose(toPrice, map(toInt), split('/')); | |
const asFraction: (p: Price) => string = compose(([num, den]) => `${num}/${den}`, props(['num','den'])); | |
const toFixed: (places: number, n: number) => string = curry((places: number, x: number): string => x.toFixed(places)); | |
const asDecimal: (p: Price) => number = compose(add(1), apply(divide), props(['num', 'den'])) | |
const multiplyPrice: (p1: Price, p2: Price) => Price = (a: Price, b: Price): Price => ({num: a.num * b.num, den: a.den * b.den}); | |
const BASE_PRICE: Price = { num: 1, den: 1 }; | |
const p1 = {num: 12, den: 1}; | |
const p2 = {num: 2, den: 1}; | |
const p3 = parseAsPrice("3/2"); | |
const multipleDecimalPrice = compose(toFixed(2), asDecimal, reduce(multiplyPrice, BASE_PRICE), toArray); | |
console.log(multipleDecimalPrice(p1, p2, p3)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare interface IOutcome { | |
outcomeId: number, | |
marketId: number, | |
eventId: number, | |
boosted: boolean, | |
cashoutable: boolean, | |
displayOrder: number, | |
displayStatus: "active" | "suspended", | |
price: IPrice | |
} | |
declare interface IPrice { | |
num: number, | |
den: number, | |
concat: (p: IPrice) => IPrice, | |
decimal: () => string, | |
fraction: () => string | |
} | |
export class Price implements IPrice { | |
constructor( public num: number, public den: number ) {} | |
static of(fractionalString: string): IPrice { | |
const [num, den] = fractionalString.split('/'); | |
return new Price(parseInt(num, 10), parseInt(den, 10)); | |
} | |
concat(p: Price) { | |
return new Price(this.num * p.num, this.den * p.den); | |
} | |
decimal() { | |
return ((this.num / this.den) + 1).toFixed(2); | |
} | |
fraction() { | |
return `${this.num}/${this.den}`; | |
} | |
} | |
const p1 = new Price(12, 1); | |
const p2 = new Price(2, 1); | |
const p3 = Price.of("3/2"); | |
console.log(p1.concat(p2).concat(p3).decimal()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment