UNB/ CS/ David Bremner/ teaching/ cs2613/ assignments/ CS2613 Assignment 3

What to hand in, and how to hand it in

Marking

Part 1: Flatten

Write a function flatten that produces an array containing all of the elements of all of the nested arrays in an array. For full marks your solution should use recursion. Your function should pass the following jasmine tests

describe("flatten",
     function () {
         it("base case",
        function () {
            expect(ms.flatten([])).toEqual([]);
        });
         it("single element",
        function () {
            expect(ms.flatten([1])).toEqual([1]);
            expect(ms.flatten(["foo"])).toEqual(["foo"]);
            expect(ms.flatten([{foo: 1}])).toEqual([{foo: 1}]);                     
        });
         it("longer list, no mutation",
        function () {
            var data = [[[1],2,[3]],4,[5,6]];
            var orig_data = data;
            expect(ms.flatten(data)).toEqual([1,2,3,4,5,6]);
            expect(data).toEqual(orig_data);
        });
     });

Part 2: Mail Summarize, Again

describe("summarize_mail",
     function() {
         it("no headers",
        function () {
            expect(ms.summarize_mail("example1.json")).toEqual([{}, {}, {}]);
            expect(ms.summarize_mail("example2.json")).toEqual(Array(10).fill({}));
        });
         it("Subject",
        function () {
            expect(ms.summarize_mail("example1.json",'Subject')).toEqual([{Subject: "[notmuch] archive"},
                                          {Subject: "Re: [notmuch] archive"},
                                          {Subject: "Re: [notmuch] archive"}]);
        });
         it("Subject, Date",
        function () {
            expect(ms.summarize_mail("example1.json",'Subject','Date')).toEqual(
            [{Subject: "[notmuch] archive", Date: 'Tue, 17 Nov 2009 18:21:38 -0500'},
             {Subject: "Re: [notmuch] archive", Date: 'Tue, 17 Nov 2009 18:04:31 -0800'},
             {Subject: "Re: [notmuch] archive", Date: 'Wed, 18 Nov 2009 02:22:12 -0800'}]);
        });
     });

Part 3: Objects and Constructors

Constructor

Referring to L11, create a 'Message' constructor that takes as input one of the objects returned by read_json_file and creates an object providing lower-cased a lower-cased accessor for the 'Subject', 'Date', 'From', and 'To' headers. Your constructor should pass pass the following tests; you should also add one or more extra tests to show that your Message class works with data read with read_json_file

let testMsg = new Message({'headers': {'Subject' : "lunch", 'Date' : "now"}});
describe("Message class",
         function() {
             it("new message is not null",
                function () {
                    expect(testMsg).not.toEqual(null);
                });
             it("subject accessor",
                function () {
                    expect(testMsg.subject).toEqual("lunch");
                });
         });

equals method

Write an equals method for your Message class. You may assume that each property of the Message object is a string or a number, but you should not hardcode the list of properties in your equals method (i.e. your equals method should continue to work if more string or number valued properties are added to the Message object).

Your method should pass at least the following tests:

let otherMsg = new Message({'headers': {'Subject' : "dinner", 'Date' : "now"}});
it("message is equal to itself",
   function () {
       expect(testMsg.equals(testMsg)).toEqual(true);
   });
it("message is not equal if field changes",
   function () {
       expect(testMsg.equals(otherMsg)).toEqual(false);
   });