-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (69 loc) · 1.87 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
import { isPositiveNumber } from "./isPositiveNumber";
/**
* Add two numbers.
*
* @param {number} first
* @param {number} second
* @returns {number}
* @throws {Error}
*/
export function add(first, second) {
if (typeof first !== 'number' || typeof second !== 'number') {
throw new Error('Parameter was not a number.');
}
return first + second;
}
/**
* Add a new item to a list.
*
* @param {HTMLOListElement|HTMLUListElement} $el Unordered or order list element
* @param {string} text Text to add to list item
*/
export function addListItem($el, text) {
const li = document.createElement('li');
li.appendChild(document.createTextNode(text));
$el.appendChild(li);
}
/**
* Comma-separated string converts to array.
*
* @param {string} text comma-separated string
* @returns {array}
* @throws {Error}
*/
export function commaSeparatedStringToArray(text) {
if (typeof text !== 'string') {
throw new Error('Parameter was not a string.');
}
return text.split(',');
}
/**
* Add two positive numbers together.
*
* @param {number} first
* @param {number} second
* @returns {number}
* @throws {Error}
*/
export function addPositiveNumbers(first, second) {
if (!isPositiveNumber(first) || !isPositiveNumber(second)) {
throw new Error('Parameter was not a number.');
}
return first + second;
}
/**
* Get the title of the latest article on Rolling Stone.
*
* @returns {string}
*/
export function latestRollingStoneArticleTitle() {
return fetch('https://www.rollingstone.com/wp-json/wp/v2/posts?per_page=1')
.then((response) => {
if (response.status !== 200) {
throw new Error('Status code is not 200, it is ' + response.status);
}
return response;
})
.then((response) => response.json())
.then((data) => data[0].title.rendered);
}