Skip to content

Commit

Permalink
Add checks for multiple matte orders per customer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsw committed Mar 7, 2018
1 parent dd77a4e commit c150a62
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export enum ParseErrorCode {
InvalidPaintFormat,
InvalidColor,
CustomerPaintOutOfRange,
OverlappingPaintType
OverlappingPaintType,
MultipleMatte
}

export class ParseError extends Error {
Expand Down Expand Up @@ -94,14 +95,22 @@ export const parse = (input: string): Order => {
};
});

// validating each customer's paint
const invalidCustomers = customers.filter((customer) => {
// validating each customer's paint to be within the given range
const paintOutOfRangeCustomers = customers.filter((customer) => {
return customer.paints.filter(
(paint) => !(paint.color > 0 && paint.color <= colorCount)
).length > 0;
});
if (invalidCustomers.length > 0) {
throw new ParseError(`Order has ${invalidCustomers.length} customers with invalid paint colors!`, ParseErrorCode.CustomerPaintOutOfRange);
if (paintOutOfRangeCustomers.length > 0) {
throw new ParseError(`Order has ${paintOutOfRangeCustomers.length} customers with invalid paint colors!`, ParseErrorCode.CustomerPaintOutOfRange);
}

// validating each customer's paint listing to not have multiple orders of matte
const multipleMatteCustomers = customers.filter((customer) => {
return customer.paints.filter((paint) => paint.type === "M").length > 1;
});
if (multipleMatteCustomers.length > 0) {
throw new ParseError(`Order has ${multipleMatteCustomers.length} customers with >1 orders of matte paint!`, ParseErrorCode.MultipleMatte);
}

// resolving the list of colors
Expand Down
8 changes: 8 additions & 0 deletions src/test/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ describe("Parser", () => {
(err: ParseError) => err instanceof ParseError && err.code === ParseErrorCode.OverlappingPaintType
);
});

it("Should fail on multiple matte paints for one customer", () => {
const multipleMatteExample = fs.readFileSync("./test-fixtures/example-9-multiple-matte").toString();
assert.throws(
() => parse(multipleMatteExample),
(err: ParseError) => err instanceof ParseError && err.code === ParseErrorCode.MultipleMatte
);
});
});
2 changes: 2 additions & 0 deletions test-fixtures/example-9-multiple-matte
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2
1 M 2 M

0 comments on commit c150a62

Please sign in to comment.