UNPKG

xero-hero

Version:

Heroic utilities to simplify and enable your progress with the [xero-node](https://www.npmjs.com/package/xero-node) SDK.

91 lines (85 loc) 2.7 kB
// src/common/instance/operations.ts import { isObject } from "deep-cuts"; // src/accounting/attachments/requests.ts import { bufferToStream } from "tranquil-stream"; // src/accounting/contacts/links.ts import qs from "qs"; // src/accounting/invoices/lineItems.ts import { isNil } from "deep-cuts"; // src/accounting/journals/links.ts import qs2 from "qs"; // src/projects/timeEntries.ts import { roundToNearestFraction } from "deep-cuts"; var hoursFromTimeEntries = (timeEntries, denominator = 4, maxDecimalPlaces = 2) => { const totalMinutes = timeEntries.reduce((totalMinutes2, timeEntry) => { const duration = timeEntry.duration || 0; return totalMinutes2 + duration; }, 0); return roundToNearestFraction( totalMinutes / 60, denominator, maxDecimalPlaces ); }; // src/projects/__tests__/timeEntries.test.ts describe("projects/timeEntries", () => { describe("hoursFromTimeEntries()", () => { it("should throw an error if passed undefined", () => { expect(() => { return hoursFromTimeEntries(); }).toThrowError(); }); it("should throw an error if passed null", () => { expect(() => { return hoursFromTimeEntries(null); }).toThrowError(); }); it("should return 0 if passed an empty array", () => { expect(hoursFromTimeEntries([])).toBe(0); }); it("should return the total hours from the given TimeEntries, defaulting to the nearest 15 minutes", () => { const timeEntries = [ { duration: 60 }, { duration: 30 }, { duration: 18 } ]; expect(hoursFromTimeEntries(timeEntries)).toBe(1.75); }); it("should play nice with a non-default denominator for rounding", () => { const timeEntryA = {}; timeEntryA.duration = 60; const timeEntryB = {}; timeEntryB.duration = 23; const timeEntries = [timeEntryA, timeEntryB]; expect(hoursFromTimeEntries(timeEntries, 10)).toBe(1.4); }); it("should play nice with non-standard decimal places for rounding / fixed numbers", () => { const timeEntries = [ { duration: 60 }, { duration: 30 }, { duration: 18 } ]; expect(hoursFromTimeEntries(timeEntries, 2, 0)).toBe(2); }); it("should sub in 0 when a Time Entry is missing a duration", () => { const timeEntryA = {}; const timeEntryB = {}; timeEntryB.duration = 23; const timeEntries = [timeEntryA, timeEntryB]; expect(hoursFromTimeEntries(timeEntries, 10)).toBe(0.4); }); }); }); //# sourceMappingURL=timeEntries.test.mjs.map