Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);Promise.resolve([1, 2, 3]) .then(call('map', x => 2 * x)) .then(console.log); //[ 2, 4, 6 ] @@ -1136,6 +1136,48 @@ console.log<hexToRGB('#27ae60ff'); // 'rgba(39, 174, 96, 255)' hexToRGB('27ae60'); // 'rgb(39, 174, 96)' hexToRGB('#fff'); // 'rgb(255, 255, 255)' +httpGet
Makes a
GETrequest to the passed URL.Use
XMLHttpRequestweb api to make agetrequest to the givenurl. Handle theonloadevent, by calling the givencallbacktheresponseText. Handle theonerrorevent, by running the providederrfunction. Omit the third argument,err, to log errors to the console'serrorstream by default.const httpGet = (url, callback, err = console.error) => { + const request = new XMLHttpRequest(); + request.open('GET', url, true); + request.onload = () => callback(request.responseText); + request.onerror = () => err(request); + request.send(); +}; +httpGet( + 'https://jsonplaceholder.typicode.com/posts/1', + console.log +); /* +Logs: { + "userId": 1, + "id": 1, + "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", + "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" +} +*/ +httpPost
Makes a
POSTrequest to the passed URL.Use
XMLHttpRequestweb api to make apostrequest to the givenurl. Set the value of anHTTPrequest header withsetRequestHeadermethod. Handle theonloadevent, by calling the givencallbacktheresponseText. Handle theonerrorevent, by running the providederrfunction. Omit the third argument,data, to send no data to the providedurl. Omit the fourth argument,err, to log errors to the console'serrorstream by default.const httpPost = (url, callback, data = null, err = console.error) => { + const request = new XMLHttpRequest(); + request.open('POST', url, true); + request.setRequestHeader('Content-type', 'application/json; charset=utf-8'); + request.onload = () => callback(request.responseText); + request.onerror = () => err(request); + request.send(data); +}; ++const newPost = { + "userId": 1, + "id": 1337, + "title": "Foo", + "body": "bar bar bar" +}; +const data = JSON.stringify(newPost); +httpPost('https://jsonplaceholder.typicode.com/posts', console.log, data; /* +Logs: { + "userId": 1, + "id": 1337, + "title": "Foo", + "body": "bar bar bar" +} +*/prettyBytes
Converts a number in bytes to a human-readable string.
Use an array dictionary of units to be accessed based on the exponent. Use
Number.toPrecision()to truncate the number to a certain number of digits. Return the prettified string by building it up, taking into account the supplied options and whether it is negative or not. Omit the second argument,precision, to use a default precision of3digits. Omit the third argument,addSpace, to add space between the number and unit by default.const prettyBytes = (num, precision = 3, addSpace = true) => { const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; if (Math.abs(num) < 1) return num + (addSpace ? ' ' : '') + UNITS[0]; diff --git a/snippets/httpGet.md b/snippets/httpGet.md index abd31ac15..33f6d72d8 100644 --- a/snippets/httpGet.md +++ b/snippets/httpGet.md @@ -10,7 +10,7 @@ Omit the third argument, `err`, to log errors to the console's `error` stream by ```js const httpGet = (url, callback, err = console.error) => { const request = new XMLHttpRequest(); - request.open("GET", url, true); + request.open('GET', url, true); request.onload = () => callback(request.responseText); request.onerror = () => err(request); request.send(); @@ -18,7 +18,10 @@ const httpGet = (url, callback, err = console.error) => { ``` ```js -httpGet('https://jsonplaceholder.typicode.com/posts/1', console.log); /* +httpGet( + 'https://jsonplaceholder.typicode.com/posts/1', + console.log +); /* Logs: { "userId": 1, "id": 1, diff --git a/snippets/httpPost.md b/snippets/httpPost.md index ae5c43469..27d4b4135 100644 --- a/snippets/httpPost.md +++ b/snippets/httpPost.md @@ -11,16 +11,17 @@ Omit the fourth argument, `err`, to log errors to the console's `error` stream b ```js const httpPost = (url, callback, data = null, err = console.error) => { - const request = new XMLHttpRequest(); - request.open("POST", url, true); - request.setRequestHeader('Content-type','application/json; charset=utf-8'); - request.onload = () => callback(request.responseText); - request.onerror = () => err(request); - request.send(data); + const request = new XMLHttpRequest(); + request.open('POST', url, true); + request.setRequestHeader('Content-type', 'application/json; charset=utf-8'); + request.onload = () => callback(request.responseText); + request.onerror = () => err(request); + request.send(data); }; ``` ```js + const newPost = { "userId": 1, "id": 1337,