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 ] @@ -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. UseArray.map()andString.split('=')to separate keys from values in each pair. UseArray.reduce()anddecodeURIComponent()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 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]; @@ -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 (
<<) andtoString(16), thenString.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()andconsole.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