Last active
November 5, 2020 07:09
-
-
Save kossnocorp/1262c2b9bf2e73c387c7f5a2d284c5bc to your computer and use it in GitHub Desktop.
Source code with an example of hoisting usage
This file contains 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 { | |
defaultNumberingPosition, | |
defaultNumberingStyle, | |
NumberingFormatOptions, | |
NumberingPosition, | |
NumberingStyle, | |
} from '#app/core/split/parse' | |
export function stripNumbering( | |
text: string, | |
numberingOptions: Pick< | |
NumberingFormatOptions, | |
'numberingStyle' | 'numberingPosition' | |
> | |
): string { | |
const regEx = | |
numberingRegEx[numberingOptions.numberingStyle || defaultNumberingStyle][ | |
numberingOptions.numberingPosition || defaultNumberingPosition | |
] | |
const captures = text.match(regEx) | |
if (!captures) return text | |
return captures[1] | |
} | |
export function stripEllipsis(text: string) { | |
const captures = text.match(ellipsisRegExp.start) | |
return (captures ? captures[1] : text).replace(ellipsisRegExp.end, '') | |
} | |
var numberingRegEx: { | |
[style in NumberingStyle]: { | |
[position in NumberingPosition]: RegExp | |
} | |
} = { | |
'n/': { | |
start: /^\d+\/ ([\s\S]*)$/, | |
end: /^([\s\S]*) \d+\/$/, | |
}, | |
'/n': { | |
start: /^\/\d+ ([\s\S]*)$/, | |
end: /^([\s\S]*) \/\d+$/, | |
}, | |
'n/max': { | |
start: /^\d+\/\d+ ([\s\S]*)$/, | |
end: /^([\s\S]*) \d+\/\d+$/, | |
}, | |
} | |
var ellipsisRegExp = { start: /^…?([\s\S]*)$/, end: /…$/ } |
This file contains 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 { calculateHistoryBalance, calculateLedgerState } from "."; | |
import { LedgerExpenseAction, LedgerRegisterAction } from "../../db"; | |
describe("Calculate balance command", () => { | |
describe("calculateHistoryBalance", () => { | |
it("calculates simple ledges", () => { | |
const result = calculateHistoryBalance([ | |
registerSasha, | |
registerNadi, | |
expense("nadi", 100), | |
expense("sasha", 10), | |
]); | |
expect(result.balance).toEqual([ | |
{ | |
who: ["sasha"], | |
whom: ["nadi"], | |
value: 45, | |
}, | |
]); | |
}); | |
it("allows to sum dept", () => { | |
const result = calculateHistoryBalance([ | |
registerSasha, | |
registerNadi, | |
expense("nadi", 100), | |
expense("nadi", 100), | |
]); | |
expect(result.balance).toEqual([ | |
{ | |
who: ["sasha"], | |
whom: ["nadi"], | |
value: 100, | |
}, | |
]); | |
}); | |
it("allows to repay dept", () => { | |
const result = calculateHistoryBalance([ | |
registerSasha, | |
registerNadi, | |
expense("nadi", 100), | |
expense("nadi", 100), | |
expense("sasha", 200), | |
]); | |
expect(result.balance).toEqual([]); | |
}); | |
it("allows to update dept", () => { | |
const result = calculateHistoryBalance([ | |
registerSasha, | |
registerNadi, | |
expense("nadi", 100), | |
expense("nadi", 100), | |
expense("sasha", 300), | |
]); | |
expect(result.balance).toEqual([ | |
{ | |
who: ["nadi"], | |
whom: ["sasha"], | |
value: 50, | |
}, | |
]); | |
}); | |
}); | |
describe("calculateLedgerState", () => { | |
it("calculates simple ledge state", () => { | |
const result = calculateLedgerState([ | |
registerSasha, | |
registerNadi, | |
expense("nadi", 100), | |
expense("sasha", 10), | |
]); | |
expect(result).toEqual({ | |
members: { sasha, nadi }, | |
joints: [], | |
splits: [ | |
{ who: ["sasha"], whom: ["nadi"], value: 50 }, | |
{ who: ["nadi"], whom: ["sasha"], value: 5 }, | |
], | |
}); | |
}); | |
}); | |
}); | |
var sasha = { | |
telegramId: 123, | |
firstName: "Sasha", | |
lastName: "Koss", | |
username: "kossnocorp", | |
since: new Date(), | |
}; | |
var registerSasha: LedgerRegisterAction = { | |
type: "register", | |
memberId: "sasha", | |
member: sasha, | |
createdAt: new Date(), | |
}; | |
var nadi = { | |
telegramId: 456, | |
firstName: "Nadi", | |
since: new Date(), | |
}; | |
var registerNadi: LedgerRegisterAction = { | |
type: "register", | |
memberId: "nadi", | |
member: nadi, | |
createdAt: new Date(), | |
}; | |
function expense(memberId: string, value: number): LedgerExpenseAction { | |
return { | |
type: "expense", | |
memberId, | |
value, | |
currency: "USD", | |
valueUSD: value, | |
exchangeRate: { | |
base: "USD", | |
rate: 1, | |
date: new Date(), | |
}, | |
createdAt: new Date(), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍