xero-hero
Version:
Heroic utilities to simplify and enable your progress with the [xero-node](https://www.npmjs.com/package/xero-node) SDK.
115 lines (108 loc) • 3.96 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
// src/common/instance/operations.ts
var import_deep_cuts = require("deep-cuts");
// src/accounting/attachments/requests.ts
var import_tranquil_stream = require("tranquil-stream");
// src/accounting/contacts/links.ts
var import_qs = __toESM(require("qs"));
// src/accounting/invoices/lineItems.ts
var import_deep_cuts2 = require("deep-cuts");
// src/accounting/journals/links.ts
var import_qs2 = __toESM(require("qs"));
// src/projects/timeEntries.ts
var import_deep_cuts3 = require("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 (0, import_deep_cuts3.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.js.map