From aea1ce0ce67341b759e4d88723919c33acf38970 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 10 Jan 2018 19:36:26 +0000 Subject: [PATCH] Travis build: 1157 --- README.md | 92 ++++++++++++++++++++++++++++++++++++++++++++ docs/index.html | 44 ++++++++++++++++++++- snippets/httpGet.md | 7 +++- snippets/httpPost.md | 13 ++++--- 4 files changed, 147 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2e86c5224..206666a16 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,8 @@ average(1, 2, 3); * [`extendHex`](#extendhex) * [`getURLParameters`](#geturlparameters) * [`hexToRGB`](#hextorgb-) +* [`httpGet`](#httpget) +* [`httpPost`](#httppost) * [`prettyBytes`](#prettybytes) * [`randomHexColorCode`](#randomhexcolorcode) * [`RGBToHex`](#rgbtohex) @@ -5131,6 +5133,96 @@ hexToRGB('#fff'); // 'rgb(255, 255, 255)'
[⬆ Back to top](#table-of-contents) +### httpGet + +Makes a `GET` request to the passed URL. + +Use [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest) web api to make a `get` request to the given `url`. +Handle the `onload` event, by calling the given `callback` the `responseText`. +Handle the `onerror` event, by running the provided `err` function. +Omit the third argument, `err`, to log errors to the console's `error` stream by default. + +```js +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(); +}; +``` + +
+Examples + +```js +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" +} +*/ +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### httpPost + +Makes a `POST` request to the passed URL. + +Use [`XMLHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest) web api to make a `post` request to the given `url`. +Set the value of an `HTTP` request header with `setRequestHeader` method. +Handle the `onload` event, by calling the given `callback` the `responseText`. +Handle the `onerror` event, by running the provided `err` function. +Omit the third argument, `data`, to send no data to the provided `url`. +Omit the fourth argument, `err`, to log errors to the console's `error` stream by default. + +```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); +}; +``` + +
+Examples + +```js + +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" +} +*/ +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### prettyBytes Converts a number in bytes to a human-readable string. diff --git a/docs/index.html b/docs/index.html index 78f476278..d67cdebec 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 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);
+      }

logo 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 GET request to the passed URL.

Use XMLHttpRequest web api to make a get request to the given url. Handle the onload event, by calling the given callback the responseText. Handle the onerror event, by running the provided err function. Omit the third argument, err, to log errors to the console's error stream 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 POST request to the passed URL.

Use XMLHttpRequest web api to make a post request to the given url. Set the value of an HTTP request header with setRequestHeader method. Handle the onload event, by calling the given callback the responseText. Handle the onerror event, by running the provided err function. Omit the third argument, data, to send no data to the provided url. Omit the fourth argument, err, to log errors to the console's error stream 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 of 3 digits. 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,