xero-hero
Version:
Heroic utilities to simplify and enable your progress with the [xero-node](https://www.npmjs.com/package/xero-node) SDK.
190 lines (182 loc) • 8.29 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/accounting/invoices/__tests__/lineItems.test.ts
var import_xero_node = require("xero-node");
// 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");
var filterInvoiceLineItems = (lineItems, minCode, maxCode) => {
if (typeof minCode === "function") {
return (lineItems || []).filter(minCode);
}
const parsedMinCode = (0, import_deep_cuts2.isNil)(minCode) ? minCode : Number.parseInt(minCode, 10);
const parsedMaxCode = (0, import_deep_cuts2.isNil)(maxCode) ? maxCode : Number.parseInt(maxCode, 10);
if (parsedMinCode || parsedMaxCode) {
return (lineItems || []).filter(({ itemCode }) => {
const parsedItemCode = (0, import_deep_cuts2.isNil)(itemCode) ? itemCode : Number.parseInt(itemCode, 10);
if (parsedItemCode) {
const greaterThanOrEqualToMinCode = (0, import_deep_cuts2.isNil)(parsedMinCode) ? true : parsedItemCode >= parsedMinCode;
const lessThanOrEqualToMaxCode = (0, import_deep_cuts2.isNil)(parsedMaxCode) ? true : parsedItemCode <= (parsedMaxCode || 0);
return greaterThanOrEqualToMinCode && lessThanOrEqualToMaxCode;
}
return false;
});
}
return lineItems || [];
};
// src/accounting/journals/links.ts
var import_qs2 = __toESM(require("qs"));
// src/projects/timeEntries.ts
var import_deep_cuts3 = require("deep-cuts");
// src/accounting/invoices/__tests__/lineItems.test.ts
var generateLineItemsWithCodes = (itemCodes) => {
return itemCodes.map((itemCode) => {
const lineItem = new import_xero_node.LineItem();
lineItem.itemCode = String(itemCode);
return lineItem;
});
};
describe("invoices/lineItems", () => {
describe("filterInvoiceLineItems()", () => {
it("should return an empty array when passed undefined", () => {
expect(filterInvoiceLineItems(void 0, 1e3)).toEqual([]);
});
it("should return an empty array when passed null", () => {
expect(filterInvoiceLineItems(null, String(0))).toEqual([]);
});
it("should return an empty array when passed an empty array", () => {
expect(filterInvoiceLineItems([], 5500)).toEqual([]);
});
it("should return all given line items when no min, max, or decision function is provided", () => {
const lineItems = generateLineItemsWithCodes([1, 50, 5e3]);
const filteredLineItems = filterInvoiceLineItems(lineItems);
expect(filteredLineItems).toHaveLength(3);
expect(filteredLineItems[0].itemCode).toBe("1");
expect(filteredLineItems[1].itemCode).toBe("50");
expect(filteredLineItems[2].itemCode).toBe("5000");
});
it("should remove line items without an item code in the case of an item code match", () => {
const lineItems = generateLineItemsWithCodes([1, 50, 5e3]);
const emptyLineItem = new import_xero_node.LineItem();
const filteredLineItems = filterInvoiceLineItems(
lineItems.concat(emptyLineItem),
0,
1e4
);
expect(filteredLineItems).toHaveLength(3);
expect(filteredLineItems[0].itemCode).toBe("1");
expect(filteredLineItems[1].itemCode).toBe("50");
expect(filteredLineItems[2].itemCode).toBe("5000");
});
it("should return line items above the min if only given a min", () => {
const lineItems = generateLineItemsWithCodes([1, 50, 5e3]);
const filteredLineItems = filterInvoiceLineItems(lineItems, 50);
expect(filteredLineItems).toHaveLength(2);
expect(filteredLineItems[0].itemCode).toBe("50");
expect(filteredLineItems[1].itemCode).toBe("5000");
});
it("should return line items above the min if only given a min of string type", () => {
const lineItems = generateLineItemsWithCodes([280, 7001, 2400]);
const filteredLineItems = filterInvoiceLineItems(lineItems, "7000");
expect(filteredLineItems).toHaveLength(1);
expect(filteredLineItems[0].itemCode).toBe("7001");
});
it("should return line items below the max if only given a max", () => {
const lineItems = generateLineItemsWithCodes([280, 7001, 2400]);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
// @ts-expect-error - This is an invalid type for the function.
void 0,
2500
);
expect(filteredLineItems).toHaveLength(2);
expect(filteredLineItems[0].itemCode).toBe("280");
expect(filteredLineItems[1].itemCode).toBe("2400");
});
it("should return line items below the max if only given a max of string type", () => {
const lineItems = generateLineItemsWithCodes([280, 7001, 2400]);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
// @ts-expect-error - This is an invalid type for the function.
void 0,
"280"
);
expect(filteredLineItems).toHaveLength(1);
expect(filteredLineItems[0].itemCode).toBe("280");
});
it("should return line items in between the min and the max inclusive", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]);
const filteredLineItems = filterInvoiceLineItems(lineItems, 1, 2726);
expect(filteredLineItems).toHaveLength(2);
expect(filteredLineItems[0].itemCode).toBe("1");
expect(filteredLineItems[1].itemCode).toBe("2726");
});
it("should return line items in between the min and the max inclusive, when string types", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
"2726",
"29000"
);
expect(filteredLineItems).toHaveLength(3);
expect(filteredLineItems[0].itemCode).toBe("29000");
expect(filteredLineItems[1].itemCode).toBe("2726");
expect(filteredLineItems[2].itemCode).toBe("4846");
});
it("should be able to return line items based on a decision function", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]).map(
(lineItem, index) => {
lineItem.lineAmount = index * 100;
return lineItem;
}
);
const filteredLineItems = filterInvoiceLineItems(lineItems, (lineItem) => {
return (lineItem.lineAmount || 0) > 199;
});
expect(filteredLineItems).toHaveLength(2);
});
it("should ignore the max when a decision function is provided", () => {
const lineItems = generateLineItemsWithCodes([1, 29e3, 2726, 4846]).map(
(lineItem, index) => {
lineItem.lineAmount = index * 100;
return lineItem;
}
);
const filteredLineItems = filterInvoiceLineItems(
lineItems,
(lineItem) => {
return (lineItem.lineAmount || 0) > 199;
},
10
);
expect(filteredLineItems).toHaveLength(2);
});
});
});
//# sourceMappingURL=lineItems.test.js.map