From 9f5127819a865d106bb6773b02784d3577dd5d14 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Sat, 13 Jan 2018 22:59:27 +0000 Subject: [PATCH] Travis build: 1226 --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++ docs/index.html | 13 +++++++++- snippets/parseCookie.md | 8 +++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 77a637b1f..339fb74f2 100644 --- a/README.md +++ b/README.md @@ -344,9 +344,11 @@ average(1, 2, 3); * [`hexToRGB`](#hextorgb-) * [`httpGet`](#httpget) * [`httpPost`](#httppost) +* [`parseCookie`](#parsecookie) * [`prettyBytes`](#prettybytes) * [`randomHexColorCode`](#randomhexcolorcode) * [`RGBToHex`](#rgbtohex) +* [`serializeCookie`](#serializecookie) * [`timeTaken`](#timetaken) * [`toDecimalMark`](#todecimalmark) * [`toOrdinalSuffix`](#toordinalsuffix) @@ -5588,6 +5590,37 @@ Logs: {
[⬆ Back to top](#table-of-contents) +### parseCookie + +Parse an HTTP Cookie header string and return an object of all cookie name-value pairs. + +Use `String.split(';')` to separate key-value pairs from each other. +Use `Array.map()` and `String.split('=')` to separate keys from values in each pair. +Use `Array.reduce()` and `decodeURIComponent()` to create an object with all key-value pairs. + +```js +const parseCookie = str => + str + .split(';') + .map(v => v.split('=')) + .reduce((acc, v) => { + acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim()); + return acc; + }, {}); +``` + +
+Examples + +```js +parseCookie('foo=bar; equation=E%3Dmc%5E2'); // { foo: 'bar', equation: 'E=mc^2' } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### prettyBytes Converts a number in bytes to a human-readable string. @@ -5669,6 +5702,28 @@ RGBToHex(255, 165, 1); // 'ffa501'
[⬆ Back to top](#table-of-contents) +### serializeCookie + +Serialize a cookie name-value pair into a Set-Cookie header string. + +Use template literals and `encodeURIComponent()` to create the appropriate string. + +```js +const serializeCookie = (name, val) => `${encodeURIComponent(name)}=${encodeURIComponent(val)}`; +``` + +
+Examples + +```js +serializeCookie('foo', 'bar'); // 'foo=bar' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### timeTaken Measures the time taken by a function to execute. diff --git a/docs/index.html b/docs/index.html index bce06a724..e10fc6a25 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 ]
@@ -1282,6 +1282,15 @@ Logs: {
   "body": "bar bar bar"
 }
 */
+

parseCookie

Parse an HTTP Cookie header string and return an object of all cookie name-value pairs.

Use String.split(';') to separate key-value pairs from each other. Use Array.map() and String.split('=') to separate keys from values in each pair. Use Array.reduce() and decodeURIComponent() to create an object with all key-value pairs.

const parseCookie = str =>
+  str
+    .split(';')
+    .map(v => v.split('='))
+    .reduce((acc, v) => {
+      acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
+      return acc;
+    }, {});
+
parseCookie('foo=bar; equation=E%3Dmc%5E2'); // { foo: 'bar', equation: 'E=mc^2' }
 

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];
@@ -1299,6 +1308,8 @@ Logs: {
 
randomHexColorCode(); // "#e34155"
 

RGBToHex

Converts the values of RGB components to a color code.

Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (<<) and toString(16), then String.padStart(6,'0') to get a 6-digit hexadecimal value.

const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
 
RGBToHex(255, 165, 1); // 'ffa501'
+

serializeCookie

Serialize a cookie name-value pair into a Set-Cookie header string.

Use template literals and encodeURIComponent() to create the appropriate string.

const serializeCookie = (name, val) => `${encodeURIComponent(name)}=${encodeURIComponent(val)}`;
+
serializeCookie('foo', 'bar'); // 'foo=bar'
 

timeTaken

Measures the time taken by a function to execute.

Use console.time() and console.timeEnd() to measure the difference between the start and end times to determine how long the callback took to execute.

const timeTaken = callback => {
   console.time('timeTaken');
   const r = callback();
diff --git a/snippets/parseCookie.md b/snippets/parseCookie.md
index 88e12860a..293cc7612 100644
--- a/snippets/parseCookie.md
+++ b/snippets/parseCookie.md
@@ -8,7 +8,13 @@ Use `Array.reduce()` and `decodeURIComponent()` to create an object with all key
 
 ```js
 const parseCookie = str =>
-  str.split(';').map(v => v.split('=')).reduce((acc,v) => {acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim()); return acc},{});
+  str
+    .split(';')
+    .map(v => v.split('='))
+    .reduce((acc, v) => {
+      acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
+      return acc;
+    }, {});
 ```
 
 ```js