Adapter
ary
Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.prototype.slice(0,n) and the spread operator (...).
const ary = (fn, n) => (...args) => fn(...args.slice(0, n)); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
ary
Creates a function that accepts up to
narguments, ignoring any additional arguments.Call the provided function,
fn, with up tonarguments, usingArray.prototype.slice(0,n)and the spread operator (...).const ary = (fn, n) => (...args) => fn(...args.slice(0, n));const firstTwoMax = ary(Math.max, 2); [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]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); diff --git a/docs/archive.html b/docs/archive.html index b963fe376..796b811de 100644 --- a/docs/archive.html +++ b/docs/archive.html @@ -76,7 +76,7 @@ }, 1700); } }, false); - }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Snippets Archive
These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are.
binarySearch
Use recursion. Similar to
Array.prototype.indexOf()that finds the index of a value within an array. The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search orArray.prototype.indexOf().Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half. Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return
-1.const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Snippets Archive
These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are.
binarySearch
Use recursion. Similar to
Array.prototype.indexOf()that finds the index of a value within an array. The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search orArray.prototype.indexOf().Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half. Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return
-1.const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { if (start > end) return -1; const mid = Math.floor((start + end) / 2); if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1); diff --git a/docs/browser.html b/docs/browser.html index 96815ba51..52d05d463 100644 --- a/docs/browser.html +++ b/docs/browser.html @@ -93,7 +93,7 @@ },1700); } }, false); - }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Browser
arrayToHtmlList
Converts the given array elements into
<li>tags and appends them to the list of the given id.Use
Array.prototype.map(),document.querySelector(), and an anonymous inner closure to create a list of html tags.const arrayToHtmlList = (arr, listID) => + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Browser
arrayToHtmlList
Converts the given array elements into
<li>tags and appends them to the list of the given id.Use
Array.prototype.map(),document.querySelector(), and an anonymous inner closure to create a list of html tags.const arrayToHtmlList = (arr, listID) => (el => ( (el = document.querySelector('#' + listID)), (el.innerHTML += arr.map(item => `<li>${item}</li>`).join('')) diff --git a/docs/contributing.html b/docs/contributing.html index 0332d335d..62e89c93b 100644 --- a/docs/contributing.html +++ b/docs/contributing.html @@ -291,6 +291,7 @@- maxDate
- minDate
- tomorrow
+- yesterday
Function
- attempt
diff --git a/docs/date.html b/docs/date.html index bb54da801..54ec98a58 100644 --- a/docs/date.html +++ b/docs/date.html @@ -93,7 +93,7 @@ },1700); } }, false); - }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Date
dayOfYear
Gets the day of the year from a
Dateobject.Use
new Date()andDate.prototype.getFullYear()to get the first day of the year as aDateobject, subtract it from the provideddateand divide with the milliseconds in each day to get the result. UseMath.floor()to appropriately round the resulting day count to an integer.const dayOfYear = date => + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Date
dayOfYear
Gets the day of the year from a
Dateobject.Use
new Date()andDate.prototype.getFullYear()to get the first day of the year as aDateobject, subtract it from the provideddateand divide with the milliseconds in each day to get the result. UseMath.floor()to appropriately round the resulting day count to an integer.const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);dayOfYear(new Date()); // 272formatDuration
Returns the human readable format of the given number of milliseconds.
Divide
mswith the appropriate values to obtain the appropriate values forday,hour,minute,secondandmillisecond. UseObject.entries()withArray.prototype.filter()to keep only non-zero values. UseArray.prototype.map()to create the string for each value, pluralizing appropriately. UseString.prototype.join(', ')to combine the values into a string.const formatDuration = ms => { @@ -157,4 +157,10 @@ return t.toISOString().split('T')[0]; };tomorrow(); // 2018-10-19 (if current date is 2018-10-18) +yesterday
Results in a string representation of yesterday's date.
Use
new Date()to get the current date, decrement by one usingDate.getDate()and set the value to the result usingDate.setDate(). UseDate.prototype.toISOString()to return a string inyyyy-mm-ddformat.const yesterday = () => { + let t = new Date(); + t.setDate(t.getDate() - 1); + return t.toISOString().split('T')[0]; +}; +yesterday(); // 2018-10-17 (if current date is 2018-10-18)
