diff --git a/product-inventory/.gitignore b/product-inventory/.gitignore new file mode 100644 index 00000000..25c8fdba --- /dev/null +++ b/product-inventory/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/product-inventory/package.json b/product-inventory/package.json new file mode 100644 index 00000000..b87a0fbb --- /dev/null +++ b/product-inventory/package.json @@ -0,0 +1,15 @@ +{ + "name": "product-inventory", + "version": "1.0.0", + "main": "index.js", + "scripts": { + "test": "jest" + }, + "keywords": [], + "author": "", + "license": "ISC", + "description": "", + "devDependencies": { + "jest": "^29.7.0" + } +} diff --git a/product-inventory/products.js b/product-inventory/products.js new file mode 100644 index 00000000..bab4eac3 --- /dev/null +++ b/product-inventory/products.js @@ -0,0 +1,43 @@ +const products = [ + {Item: 'Laptop', Quantity: 50, Price: 1000}, + {Item: 'Smartphone', Quantity: 200, Price: 1200}, + {Item: 'Headphones', Quantity: 150, Price: 500}, + {Item: 'Keyboard', Quantity: 75, Price: 200}, + {Item: 'Mouse', Quantity: 120, Price: 50}, +]; + +function calculateDiscount(price, discountRate) { + if (typeof price !== 'number' || typeof discountRate !== 'number') { + return null; + } + if (discountRate < 0 || discountRate > 1) { + return null; + } + + return price-price*discountRate; +} + +console.log(calculateDiscount(100, 0.1)); + +function filterProducts(products, callback) { + if (!Array.isArray(products) || typeof callback !== 'function'){ + + return []; + } + return products.filter(callback); +} +console.log(filterProducts(products, product => product.price<500)); + +function sortInventory(inventory, key) { +if (!Array.isArray(inventory) || typeof key !== 'string'){ + return []; + } + return inventory.sort((a,b) => (b[key]-a[key])); + }; +console.log(sortInventory(products, "quantity")); + +module.exports = { + calculateDiscount, + filterProducts, + sortInventory +} \ No newline at end of file diff --git a/product-inventory/tests/products.test.js b/product-inventory/tests/products.test.js new file mode 100644 index 00000000..82fcb2a6 --- /dev/null +++ b/product-inventory/tests/products.test.js @@ -0,0 +1,66 @@ +const { calculateDiscount, filterProducts, sortInventory } = require('../products.js'); + +const products = [ + { Item: 'Laptop', Quantity: 50, Price: 1000 }, + { Item: 'Smartphone', Quantity: 200, Price: 1200 }, + { Item: 'Headphones', Quantity: 150, Price: 500 }, + { Item: 'Keyboard', Quantity: 75, Price: 200 }, + { Item: 'Mouse', Quantity: 120, Price: 50 }, +]; + +describe("apply dicount function", () => { + test("applies a valid discount rate", () => { + expect(calculateDiscount(100, 0.1)).toBe(90); + }); + + test("handles an invalid discount rate gracefully", () => { + expect(calculateDiscount(100, -0.1)).toBe(null); + }); + + test("handles edge case with price of 0", () => { + expect(calculateDiscount(0, 0.2)).toBe(0); + }); + +//This test case was added later + test("accepts numbers for price not strings",() =>{ + expect(calculateDiscount(100,"string")).toBe(null); + }) +}) + +const cat = "Nick"; +describe("filter products by price", () => { + test("filters products less than 500", () => { + expect(filterProducts(products, product => product.Price < 500)).toEqual([ + { Item: 'Keyboard', Quantity: 75, Price: 200 }, + { Item: 'Mouse', Quantity: 120, Price: 50 }, + ]) + }) + test("Negative Test Case: Should give empty array for non-array first argument", () => { + expect(filterProducts(cat, product => product.Price < 500)).toEqual([]); + }); + + test("Edge Test Case: Should handle filtering everything out of array", () => { + expect(filterProducts(products, product => product.Price < 20)).toEqual([]); + }); +}); + +//sortInventory +describe("inventoryFunctions.sortInventory", function () { + test("Sort by Quantity", () => { + expect(sortInventory(products, "quantity")).toEqual([ + { Item: 'Laptop', Quantity: 50, Price: 1000 }, + { Item: 'Smartphone', Quantity: 200, Price: 1200 }, + { Item: 'Headphones', Quantity: 150, Price: 500 }, + { Item: 'Keyboard', Quantity: 75, Price: 200 }, + { Item: 'Mouse', Quantity: 120, Price: 50 } + ]); + }); + + test("Number input instead of a string", () => { + expect(sortInventory(products, 587609)).toEqual([]); + }); + + test("Empty array input", () => { + expect(sortInventory([], "quantity")).toEqual([]); + }); +}); \ No newline at end of file