-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (111 loc) · 3.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/** Simple constructor to demonstrate the observer design pattern */
function Observer() {
const subscribers = {};
/** Subscribe a function to an event, this method takes an a event and
* callback function, every function subscibed to an event is called when
* the event is published
* @param{string} event - Event name
* @param{function} cb - Callback method
* @returns{number} length of functions currently subscribed to the passed event
*/
this.subscibe = function (event, cb) {
if (subscribers[event]) {
return subscribers[event].push(cb);
}
subscribers[event] = [cb];
return subscribers[event].length;
};
/** Call every function subscribed to the passed in event
* @param{string} event - Name of event to call methods on
* @param{array} args - Arguments to pass to the listening functions
* @returns{boolean} Returns true if any function was ran or false
* otherwise
*/
this.publish = function (event, ...args) {
if (!subscribers[event]) {
return false;
}
const subs = subscribers[event];
if (subs.length < 0) {
return false;
}
subs.forEach((subscriber) => {
return subscriber(...args);
});
return true;
};
/** Unsubscribe a function from an event
* @param{string} event - Name of event to remove the function from
* @param{function} cb - Function to unsubscribe from the passed event
*/
this.unsubscribe = function (event, cb) {
if (!subscribers[event]) {
return false;
}
subs = subscribers[event];
if (subs.length < 0) {
return false;
}
subs.forEach((func, index) => {
if (func === cb) {
console.log(
"Observer found. Unsubscribing this observer for this event ..."
);
return subscribers[event].splice(index, 1);
}
});
return true;
};
}
/** Add a new book to library
* @param {string} bookname - Name of book to add
* @param {array} arr - Array to add book to
* @returns {number} New length of array
*/
const addbook = (bookname, arr) => {
const newbook = { bookname, available: true };
return arr.push(newbook);
};
/** Lend out a book
* @param {string} bookname - Name of book to lend out
* @param {array } arr - Array to lend out book from
* @returns {object} Lent out book
*/
const lendbook = (bookname, arr) => {
let a;
arr.map((book) => {
if (book.bookname === bookname) {
console.log(`${book.bookname} found in books, renting ...`);
book.available = false;
return (a = book);
}
});
return a;
};
/** Return stats of books currently in the library
* @param {string} type - Which details you'd like to get
* @returns {number} Appropriate number of books for passed type
*/
const bookdetails = (type, arr) => {
if (type === "full") {
return arr.length;
} else if (type === "rented") {
const rentedBooks = arr.filter((book) => {
return book.available === false;
});
return rentedBooks.length;
} else if (type === "available") {
const availableBooks = arr.filter((book) => {
return book.available === true;
});
return availableBooks.length;
} else {
throw new TypeError("Passed parameter type is not recognised");
}
};
module.exports = {
Observer,
addbook,
lendbook,
bookdetails,
};