-
Notifications
You must be signed in to change notification settings - Fork 2
IPW_IP 2324 1 A1
Bachelor in Informatics, Networks and Telecommunications Engineering
Winter Semester of 2023/2024 – **1st practical assignment**
This first practical assignment has 2 parts, and it should be implemented and delivered individually by each student. The delivery dates are the following:
- Part1: 15/10/2023-23h59.
- Part2: 22/10/2023-23h59.
The delivery method is done in 2 steps:
- In Github Classroom, use this invitation link. Following this link, login with your github user account and select your student id to make the correspondence between them, so that a private Github repository is created. The assignment should be delivered in this repository, by creating a tag called
A1-P1. If you need to make some amend to the delivery, just create another tag namedA1-P1.1,A1-P1.2, etc.
Implement the following JavaScript functions and create tests that confirm the described behavior:
-
Implement the function
validateProperty(obj, propValidator), that returnstrueif it confirms the existence and validity of a given property inobj, orfalseotherwise. The validity specification is provided inpropValidatorargument.Example
const validator = {name : "p1" , validators: [s => typeof s == 'string' && s.length > 2, s => s[0]=="a"] } const obj1 = { p1 : "abc" } const obj2 = { p2 : 123 } const obj3 = { p1 : "a" , p2 : 123 } validateProperty(obj1, validator) //true validateProperty(obj2, validator) //false validateProperty(obj3, validator) //false
Arguments
-
obj: an object to validate a property -
propValidator: an object that validates a property onobj, having the following properties:-
name- the name of the property to validate -
validators- an array of predicate functions that must be called with the property value. If any returnsfalsethe property is invalid.
-
Conditions
- Do not create any unnecessary functions e.g. helpers.
- Create more object and validator usage examples, than the provided ones.
-
-
Implement the function
copyProperties(obj, propValidators), that returns a new object with properties with the same name and value to the ones existing inobj. The properties to copy are defined and validated bypropValidatorsarray argument. Each property that does not exists inobjor the validation fails, will not be present in the returned object. If no property exists or is valid and empty object is returned.Example
const validators = [ { name: "p1", validators: [s => typeof s == 'string' && s.length > 2, s => s[0] == "a"] }, { name: "p2", validators: [s => Number.isInteger(s)] } ] const obj1 = { p1: "a" } const obj2 = { p1: 123 } const obj3 = { p1: "abc", p2: 123 } console.log(copyProperties(obj1, validators)) // {} console.log(copyProperties(obj2, validators)) // {} console.log(copyProperties(obj3, validators)) // { p1: 'abc', p2: 123 }
Arguments
-
obj: an object to validate the properties -
propValidators: an array of property validators, as defined in the previous exercise to validate the properties inobj
Conditions
- Do not use any for/while loops or
Array#forEach. - Do not create any unnecessary functions e.g. helpers.
- Create more object and validator usage examples, than the provided ones.
Hints
- The function must call
validatePropertyfor each validator
-
- Add a
copyPropertiesmethod toObjecttype. This method implementation should call thecopyPropertiesfunction implemented in the previous exercise.
Example
const validators = [
{
name: "p1", validators: [s => typeof s == 'string' && s.length > 2, s => s[0] == "a"]
},
{
name: "p2", validators: [s => Number.isInteger(s)]
}
]
const obj1 = { p1: "a" }
const obj2 = { p1: 123 }
const obj3 = { p1: "abc", p2: 123 }
console.log(obj1.copyProperties(validators)) // {}
console.log(obj2.copyProperties(validators)) // {}
console.log(obj3.copyProperties(validators)) // { p1: 'abc', p2: 123 } -
Add the
associateWith(transformation)method to the Array type. This function returns anObjectin which the elements values of the original Array are properties names, and the corresponding values values are produced from them by the giventransformationfunction. If two elements are equal, only the last one remains in the object. Example:let numbers = ["one", "two", "three", "four"] console.log(numbers.associateWith( str => str.length )) // { one: 3, two: 3, three: 5, four: 4}
-
Implement
checkUsersValid(goodUsers)function that returns a function. The returned function, takes a list of valid users ingoodUsers, and returnstrueif all of the supplied users exist in the original list of users, orfalseotherwise.You only need to check that the user ids match. Example:
var goodUsers = [ { id: 1 }, { id: 2 }, { id: 3 } ] // `checkUsersValid` is the function you'll define var testAllValid = checkUsersValid(goodUsers) testAllValid([ { id: 2 }, { id: 1 } ]) // => true testAllValid([ { id: 2 }, { id: 4 }, { id: 1 } ]) // => false
Arguments
-
goodUsers: a list of valid users
-
-
Override a specified method of an object with new functionality while still maintaining all of the old behavior.
Create a
Spyfunction that returns an object that keeps track of how many times a function is called.Example
var spy = Spy(console, 'error') console.error('calling console.error') console.error('calling console.error') console.error('calling console.error') console.log(spy.count) // 3
Arguments
-
target: an object containing the methodmethod -
method: a string with the name of the method ontargetto spy on.
Conditions
- Do not use any for/while loops or
Array#forEach. - Do not create any unnecessary functions e.g. helpers.
Hint
- Functions have context, input and output. Make sure you consider the context, input to and output from the function you are spying on.
-
This part requires that your code makes HTTP requests to the Ticketmaster API. To access this API, each student must request the API key that identifies the student application's requests. That key must be included in the URL of each HTTP request. The description of the necessary steps to create the token are described here. Note that this is a free API, and therefore it has rate limits for your requests. Be sure you understand and comply to these limits.
This application reads event ids from a json file, with the following format:
{
"event-ids" : [
"Z698xZ2qZa6y5",
"Z698xZ2qZaFHl",
"G5vYZ9svBCs1O",
]
}
and produces another JSON file containing each event name, date and venue location (if one exists), as shown in following example:
{
"events": [
{
"id": "Z698xZ2qZa6y5",
"name": "HAUSER - Rebel with a Cello - Meet&Greet Package",
"date": "2023-10-11T19:00:00Z",
"venue": {
"name": "WiZink Center",
"country": "Spain",
"city": "Madrid"
}
},
{
"id": "Z698xZ2qZaFHl",
"name": "Masood Boomgaard",
"date": "2023-10-10T19:00:00Z",
"venue": {
"name": "Teatro Luchana (Sala 2)",
"country": "Spain",
"city": "Madrid"
}
},
{
"id": "G5vYZ9svBCs1O",
"name": "Golden State Warriors vs. Phoenix Suns",
"date": "2023-10-25T02:00:00Z",
"venue": {
"name": "Chase Center",
"country": "United States Of America",
"city": "San Francisco"
}
}
]
}
Implement two versions of the application:
- Using Promises explicitly
- Using the async/await style
REMARK: The students should consider and find solutions for the limitations of this free API, namely the Rate Limits in terms of the number of requests per day and per second.