/******************************************************************************* * Problem 8: generate license text and link from license code

/*******************************************************************************
* Problem 8: generate license text and link from license code.
*
* Images, videos, and other resources on the web are governed by copyright.
* Everything you find on the web is copyright to its creator automatically, and
* you cannot reuse it unless you are granted a license to do so.
*
* Different licenses exist to allow creators to share their work. For example,
* the Creative Commons licenses are a popular way to allow people to reuse
* copyright material, see https://creativecommons.org/licenses/.
*
* Below is a list of license codes, and the associated license text explaining the code:
*
* CC-BY: Creative Commons Attribution License
* CC-BY-NC: Creative Commons Attribution-NonCommercial License
* CC-BY-SA: Creative Commons Attribution-ShareAlike License
* CC-BY-ND: Creative Commons Attribution-NoDerivs License
* CC-BY-NC-SA: Creative Commons Attribution-NonCommercial-ShareAlike License
* CC-BY-NC-ND: Creative Commons Attribution-NonCommercial-NoDerivs License
*
* NOTE: any other licenseCode should use the URL https://choosealicense.com/no-permission/
* and the explanation text, “All Rights Reserved”
*
* Write a function, generateLicenseLink(), which takes a license code, and returns
* an HTML link to the appropriate license URL, and including the explanation text.
*
* For example:
*
* generateLicenseLink(‘CC-BY-NC’) should return the following HTML string:
*
* ‘Creative Commons Attribution-NonCommercial License
*
* The URL is generated based on the license code:
*
* – remove the `CC-` prefix
* – convert to lower case
* – place formatted license code within URL https://creativecommons.org/licenses/[…here]/4.0/
*
* Your generateLicenseLink() function should also accept an optional second argument,
* `includeLicenseAttr`. If `includeLicenseAttr` is true, add ` rel=”license”` to your
* link so that it’s clear this URL points to license information.
*
* You can read more about HTML links at https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a
*
* @param {string} licenseCode – a license code
* @param {boolean|undefined} includeLicenseAttr – whether to include the rel=”license” attribute
* @returns {string}
******************************************************************************/

function generateLicenseLink(licenseCode, includeLicenseAttr) {
// Replace this comment with your code…
}

Problem test 8

const { generateLicenseLink } = require(‘./solutions’);

describe(‘Problem 8 – generateLicenseLink() function’, function () {
test(‘CC-BY license code produces correct link’, function () {
let licenseCode = ‘CC-BY’;
let result = generateLicenseLink(licenseCode);
expect(result).toBe(
Creative Commons Attribution License
);
});

test(‘CC-BY-NC license code produces correct link’, function () {
let licenseCode = ‘CC-BY-NC’;
let result = generateLicenseLink(licenseCode);
expect(result).toBe(
Creative Commons Attribution-NonCommercial License
);
});

test(‘CC-BY-SA license code produces correct link’, function () {
let licenseCode = ‘CC-BY-SA’;
let result = generateLicenseLink(licenseCode);
expect(result).toBe(
Creative Commons Attribution-ShareAlike License
);
});

test(‘CC-BY-ND license code produces correct link’, function () {
let licenseCode = ‘CC-BY-ND’;
let result = generateLicenseLink(licenseCode);
expect(result).toBe(
Creative Commons Attribution-NoDerivs License
);
});

test(‘CC-BY-NC-SA license code produces correct link’, function () {
let licenseCode = ‘CC-BY-NC-SA’;
let result = generateLicenseLink(licenseCode);
expect(result).toBe(
Creative Commons Attribution-NonCommercial-ShareAlike License
);
});

test(‘CC-BY-NC-ND license code produces correct link’, function () {
let licenseCode = ‘CC-BY-NC-ND’;
let result = generateLicenseLink(licenseCode);
expect(result).toBe(
Creative Commons Attribution-NonCommercial-NoDerivs License
);
});

test(‘Unknown license codes produces correct link’, function () {
expect(generateLicenseLink(null)).toBe(
All Rights Reserved
);

expect(generateLicenseLink()).toBe(
All Rights Reserved
);

expect(generateLicenseLink(‘copyright’)).toBe(
All Rights Reserved
);

expect(generateLicenseLink(‘Unknown’)).toBe(
All Rights Reserved
);
});

test(‘includeLicenseAttr = true adds the correct rel attribute’, function () {
let licenseCode = ‘CC-BY-NC-ND’;
let result = generateLicenseLink(licenseCode, true);
expect(result).toBe(
Creative Commons Attribution-NonCommercial-NoDerivs License
);
});

test(‘includeLicenseAttr = true works with non-CC license too’, function () {
let result = generateLicenseLink(‘invalid’, true);
expect(result).toBe(
All Rights Reserved
);
});
});

/*******************************************************************************
* Problem 9 Part 2: checking for all True or all False values in a normalized list
*
* Using your toBool() function above, create three new functions to check
* if a list of arguments are all “true”, partially “true”, or all “false” values:
*
* all(‘Y’, ‘yes’, 1) –> returns true
* some(‘Y’, ‘no’, 1) –> returns true
* some(‘N’, ‘no’, 0) –> returns false
* none(‘Y’, ‘invalid’, 1) –> returns false
*
* Use try/catch syntax to avoid having your functions throw errors when pureBool()
* throws on invalid data.
******************************************************************************/

function all() {
// Replace this comment with your code…
}

function some() {
// Replace this comment with your code…
}

function none() {
// Replace this comment with your code…
}
Problem test 9

const { toBool, all, some, none } = require(‘./solutions’);

describe(‘Problem 9 – toBool(), all(), none() functions’, function () {
describe(‘toBool()’, function () {
test(‘pure boolean values work as expected’, function () {
expect(toBool(true)).toBe(true);
expect(toBool(false)).toBe(false);
});

test(‘various forms of Yes should return true’, function () {
expect(toBool(‘Yes’)).toBe(true);
expect(toBool(‘yes’)).toBe(true);
expect(toBool(‘YES’)).toBe(true);
expect(toBool(‘YeS’)).toBe(true);
expect(toBool(‘YEs’)).toBe(true);
expect(toBool(‘Y’)).toBe(true);
expect(toBool(‘y’)).toBe(true);
});

test(‘various forms of Oui (French) should return true’, function () {
expect(toBool(‘Oui’)).toBe(true);
expect(toBool(‘oui’)).toBe(true);
expect(toBool(‘OUI’)).toBe(true);
expect(toBool(‘OuI’)).toBe(true);
expect(toBool(‘OUi’)).toBe(true);
expect(toBool(‘O’)).toBe(true);
expect(toBool(‘o’)).toBe(true);
});

test(‘various forms of No should return false’, function () {
expect(toBool(‘No’)).toBe(false);
expect(toBool(‘no’)).toBe(false);
expect(toBool(‘nO’)).toBe(false);
expect(toBool(‘n’)).toBe(false);
expect(toBool(‘N’)).toBe(false);
});

test(‘various forms of Non (French) should return false’, function () {
expect(toBool(‘Non’)).toBe(false);
expect(toBool(‘non’)).toBe(false);
expect(toBool(‘nON’)).toBe(false);
expect(toBool(‘NoN’)).toBe(false);
});

test(‘various forms of True should return true’, function () {
expect(toBool(‘True’)).toBe(true);
expect(toBool(‘true’)).toBe(true);
expect(toBool(‘TRUE’)).toBe(true);
expect(toBool(‘TruE’)).toBe(true);
expect(toBool(‘TRuE’)).toBe(true);
expect(toBool(‘TRue’)).toBe(true);
expect(toBool(‘trUE’)).toBe(true);
expect(toBool(‘truE’)).toBe(true);
expect(toBool(‘t’)).toBe(true);
expect(toBool(‘T’)).toBe(true);
});

test(‘various forms of Vrai (French) should return true’, function () {
expect(toBool(‘Vrai’)).toBe(true);
expect(toBool(‘vrai’)).toBe(true);
expect(toBool(‘VRAI’)).toBe(true);
expect(toBool(‘VraI’)).toBe(true);
expect(toBool(‘VRaI’)).toBe(true);
expect(toBool(‘vRAI’)).toBe(true);
expect(toBool(‘vrAI’)).toBe(true);
expect(toBool(‘vraI’)).toBe(true);
expect(toBool(‘v’)).toBe(true);
expect(toBool(‘V’)).toBe(true);
});

test(‘various forms of False should return false’, function () {
expect(toBool(‘False’)).toBe(false);
expect(toBool(‘false’)).toBe(false);
expect(toBool(‘FALSE’)).toBe(false);
expect(toBool(‘FALSe’)).toBe(false);
expect(toBool(‘FALSe’)).toBe(false);
expect(toBool(‘FALse’)).toBe(false);
expect(toBool(‘FAlse’)).toBe(false);
expect(toBool(‘falsE’)).toBe(false);
expect(toBool(‘falSE’)).toBe(false);
expect(toBool(‘faLSE’)).toBe(false);
expect(toBool(‘fALSE’)).toBe(false);
expect(toBool(‘f’)).toBe(false);
expect(toBool(‘F’)).toBe(false);
});

test(‘various forms of Faux (French) should return false’, function () {
expect(toBool(‘Faux’)).toBe(false);
expect(toBool(‘faux’)).toBe(false);
expect(toBool(‘FAUX’)).toBe(false);
expect(toBool(‘FAUx’)).toBe(false);
expect(toBool(‘FAux’)).toBe(false);
expect(toBool(‘Faux’)).toBe(false);
expect(toBool(‘fAUX’)).toBe(false);
expect(toBool(‘faUX’)).toBe(false);
expect(toBool(‘fauX’)).toBe(false);
});

test(‘positive numbers should be true’, function () {
for (let i = 1; i < 1000; i++) { expect(toBool(i)).toBe(true); } }); test('zero should be false', function () { expect(toBool(0)).toBe(false); }); test('negative numbers should be false', function () { for (let i = 1; i < 1000; i++) { expect(toBool(i * -1)).toBe(false); } }); test('various non-values should throw', function () { expect(() => toBool(undefined)).toThrow(new Error(‘invalid value’));
expect(() => toBool()).toThrow(new Error(‘invalid value’));
expect(() => toBool(null)).toThrow(new Error(‘invalid value’));
});
});

/**
* * Yes, yes, YES, Y, Oui, oui, OUI, O, t, TRUE, true, True, vrai, V, VRAI, 1, 2, …some positive number
* No, no, NO, Non, non, NON, N, n, f, FALSE, false, False, FAUX, faux, Faux, 0, -1, -2, …some negative number

*/

describe(‘all()’, function () {
test(‘single “true” value works the same as toBool()’, function () {
expect(all(true)).toBe(true);
expect(all(‘true’)).toBe(true);
expect(all(10000)).toBe(true);
});

test(‘single “false” value works the same as toBool()’, function () {
expect(all(false)).toBe(false);
expect(all(‘false’)).toBe(false);
expect(all(-10000)).toBe(false);
});

test(‘lists of normalized true values results in true’, function () {
expect(
all(
‘Yes’,
‘yes’,
‘YES’,
‘Y’,
‘Oui’,
‘oui’,
‘OUI’,
‘O’,
‘t’,
‘TRUE’,
true,
‘True’,
‘VRAI’,
‘vrai’,
‘V’,
1,
2,
3,
100000
)
).toBe(true);
});

test(‘lists of mostly normalized true values results in false’, function () {
// Last value switched to false
expect(
all(
‘Yes’,
‘yes’,
‘YES’,
‘Y’,
‘Oui’,
‘oui’,
‘OUI’,
‘O’,
‘t’,
‘TRUE’,
true,
‘True’,
‘VRAI’,
‘vrai’,
‘V’,
1,
2,
3,
-100000
)
).toBe(false);
});
});

describe(‘some()’, function () {
test(‘single “true” value works the same as toBool()’, function () {
expect(some(true)).toBe(true);
expect(some(‘true’)).toBe(true);
expect(some(10000)).toBe(true);
});

test(‘single “false” value works the same as toBool()’, function () {
expect(some(false)).toBe(false);
expect(some(‘false’)).toBe(false);
expect(some(-10000)).toBe(false);
});

test(‘lists of normalized true values results in true’, function () {
expect(
some(
‘Yes’,
‘yes’,
‘YES’,
‘Y’,
‘Oui’,
‘oui’,
‘OUI’,
‘O’,
‘t’,
‘TRUE’,
true,
‘True’,
‘VRAI’,
‘vrai’,
‘V’,
1,
2,
3,
100000
)
).toBe(true);
});

test(‘lists of mostly normalized true values results in true’, function () {
// Last value switched to false
expect(
some(
‘Yes’,
‘yes’,
‘YES’,
‘Y’,
‘Oui’,
‘oui’,
‘OUI’,
‘O’,
‘t’,
‘TRUE’,
true,
‘True’,
‘VRAI’,
‘vrai’,
‘V’,
1,
2,
3,
0
)
).toBe(true);
});
});

describe(‘none()’, function () {
test(‘lists of normalized false values results in true’, function () {
expect(
none(
‘No’,
‘Non’,
‘no’,
‘non’,
‘NO’,
‘NON’,
‘N’,
‘n’,
‘f’,
‘FAUX’,
‘faux’,
‘FALSE’,
false,
‘False’,
0,
-1,
-100000
)
).toBe(true);
});

test(‘lists of mostly normalized false values results in false’, function () {
// Last value switched to true
expect(
none(
‘No’,
‘Non’,
‘no’,
‘non’,
‘NO’,
‘NON’,
‘N’,
‘n’,
‘f’,
‘FAUX’,
‘faux’,
‘FALSE’,
false,
‘False’,
0,
-1,
100000
)
) .toBe(false);
});
});
});

/*******************************************************************************
* Problem 10 – build a URL
*
* An imaginary web API is available at https://api.web222-example.org/v3/query
* It allows users to query data by creating a URL in a particular way. As we
* know from week 1, a URL can contain optional name=value pairs, see:
* https://web222.ca/weeks/week01/#urls
*
* For example:
*
* https://www.store.com/search?q=dog includes q=dog
*
* https://www.store.com?_encoding=UTF8&node=18521080011 includes
* both _encoding=UTF8 and also node=18521080011, separated by &
*
* We will write a buildQuery() function to build a query URL for the API
* based on arguments passed by the caller.
*
* The buildQuery() function accepts the following arguments:
*
* – query: a URI encoded search string, for example “butterfly” or “Horse-chestnut”
* – per_page: a number indicating how many results to return per page (min=1, max=100)
* – page: a number, indicating the starting page to return (min=1)
* – format: a string, indicating the type of data to return. The following formats are
* valid: html, json, xml.
*
* Write an implementation of buildUrl() that accepts arguments for all of the above
* parameters, validates them (e.g., per_page must be between 1 and 100, etc), and returns
* a properly formatted URL. If any of the values passed to buildQuery() are invalid,
* an Error should be thrown.
*
* For example:
*
* buildQuery(‘Monarch Butterfly’, 25, 1, ‘json’) would return the following URL:
*
* https://api.web222-example.org/v3/query?query=’Monarch%20Butterfly’&per_page=25&page=1&format=json
*
* NOTE:
*
* NOTE: make sure you properly encode the query value, since URLs can’t contain
* spaces or other special characters. HINT: use the encodeURIComponent() function
* to do this, see:
*
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
*
* @param {string} query the query to use. Must be properly URI encoded
* @param {number} perPage the number of items per page (1-100)
* @param {number} page the page of results to start at (must be 1 or greater)
* @param {string} format the format of results (one of: html, json, xml)
* @returns {string} the properly formatted URL
******************************************************************************/

function buildQuery(query, perPage, page, format) {
// Replace this comment with your code…
}

problem test 10

const { buildQuery } = require(‘./solutions’);

// function buildQuery(query, order, count, license) {

describe(‘Problem 10 – buildQuery() function’, function () {
function assertUrl(urlString, query, perPage, page, format) {
let url = new URL(urlString);
expect(url.origin).toBe(‘https://api.web222-example.org’);
expect(url.pathname).toBe(‘/v3/query’);
expect(url.searchParams.get(‘query’)).toEqual(query);
expect(url.searchParams.get(‘per_page’)).toEqual(String(perPage));
expect(url.searchParams.get(‘page’)).toEqual(String(page));
expect(url.searchParams.get(‘format’)).toEqual(format);
}

test(‘correct values produce an expected url’, function () {
let url = buildQuery(‘butterfly’, 10, 1, ‘json’);
expect(typeof url).toEqual(‘string’);
assertUrl(url, ‘butterfly’, 10, 1, ‘json’);
});

test(‘query values are properly encoded on url’, function () {
let url = buildQuery(‘butterfly with special characters:/\\{}<>[]}`”‘, 25, 3, ‘xml’);
expect(typeof url).toEqual(‘string’);
assertUrl(url, ‘butterfly with special characters:/\\{}<>[]}`”‘, 25, 3, ‘xml’);
});

test(‘perPage below 1 throws but 1 works’, function () {
expect(() => buildQuery(‘butterfly’, 0, 1, ‘xml’)).toThrow();

let url = buildQuery(‘butterfly’, 1, 1, ‘xml’);
expect(typeof url).toEqual(‘string’);
assertUrl(url, ‘butterfly’, 1, 1, ‘xml’);
});

test(‘perPage above 100 throws but 100 works’, function () {
expect(() => buildQuery(‘butterfly’, 101, 1, ‘xml’)).toThrow();

let url = buildQuery(‘butterfly’, 100, 1, ‘xml’);
expect(typeof url).toEqual(‘string’);
assertUrl(url, ‘butterfly’, 100, 1, ‘xml’);
});

test(‘page below 1 throws but 1 works’, function () {
expect(() => buildQuery(‘butterfly’, 1, 0, ‘xml’)).toThrow();

let url = buildQuery(‘butterfly’, 1, 1, ‘xml’);
expect(typeof url).toEqual(‘string’);
assertUrl(url, ‘butterfly’, 1, 1, ‘xml’);
});

test(‘license must be one of the expected values’, function () {
[‘xml’, ‘html’, ‘json’].forEach((format) => {
let url = buildQuery(‘monarch butterfly’, 50, 2, format);
expect(typeof url).toEqual(‘string’);
assertUrl(url, ‘monarch butterfly’, 50, 2, format);
});
});

test(‘formats other than the expected values will throw’, function () {
expect(() => buildQuery(‘butterfly’, 1, 1, ”)).toThrow();
expect(() => buildQuery(‘butterfly’, 1, 1)).toThrow();
expect(() => buildQuery(‘butterfly’, 1, 1, null)).toThrow();
expect(() => buildQuery(‘butterfly’, 1, 1, ‘text’)).toThrow();
});
});

Complete Answer:

Get Instant Help in Homework Asap
Get Instant Help in Homework Asap
Calculate your paper price
Pages (550 words)
Approximate price: -
Open chat
1
Hello 👋
Thank you for choosing our assignment help service!
How can I help you?