diff --git a/README.md b/README.md index e036b03bd..5092802d3 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,13 @@ - Use Ctrl + F or command + F to search for a snippet. - Contributions welcome, please read the [contribution guide](CONTRIBUTING.md). - Snippets are written in ES6, use the [Babel transpiler](https://babeljs.io/) to ensure backwards-compatibility. +- You can import these snippets into your text editor of choice (VSCode, Atom, Sublime) using the files found in [this repo](https://github.com/Rob-Rychs/30-seconds-of-code-texteditorsnippets). - You can import these snippets into Alfred 3, using [this file](https://github.com/lslvxy/30-seconds-of-code-alfredsnippets). - You can find a package with all the snippets on [npm](https://www.npmjs.com/package/tsoc). Bear in mind that most of these snippets are not production-ready. ## Table of Contents -### Adapter +### ๐Ÿ”Œ Adapter
View contents @@ -28,7 +29,7 @@
-### Array +### ๐Ÿ“š Array
View contents @@ -63,6 +64,7 @@ * [`quickSort`](#quicksort) * [`remove`](#remove) * [`sample`](#sample) +* [`sampleSize`](#samplesize) * [`shuffle`](#shuffle) * [`similarity`](#similarity) * [`symmetricDifference`](#symmetricdifference) @@ -76,13 +78,14 @@
-### Browser +### ๐ŸŒ Browser
View contents * [`arrayToHtmlList`](#arraytohtmllist) * [`bottomVisible`](#bottomvisible) +* [`copyToClipboard`](#copytoclipboard) * [`currentURL`](#currenturl) * [`detectDeviceType`](#detectdevicetype) * [`elementIsVisibleInViewport`](#elementisvisibleinviewport) @@ -97,12 +100,13 @@ * [`scrollToTop`](#scrolltotop) * [`setStyle`](#setstyle) * [`show`](#show) +* [`speechSynthesis`](#speechsynthesis) * [`toggleClass`](#toggleclass) * [`UUIDGeneratorBrowser`](#uuidgeneratorbrowser)
-### Date +### โฑ๏ธ Date
View contents @@ -114,7 +118,7 @@
-### Function +### ๐ŸŽ›๏ธ Function
View contents @@ -123,12 +127,13 @@ * [`compose`](#compose) * [`curry`](#curry) * [`functionName`](#functionname) +* [`memoize`](#memoize) * [`runPromisesInSeries`](#runpromisesinseries) * [`sleep`](#sleep)
-### Logic +### ๐Ÿ”ฎ Logic
View contents @@ -137,7 +142,7 @@
-### Math +### โž— Math
View contents @@ -173,16 +178,7 @@
-### Media - -
-View contents - -* [`speechSynthesis`](#speechsynthesis) - -
- -### Node +### ๐Ÿ“ฆ Node
View contents @@ -193,7 +189,7 @@
-### Object +### ๐Ÿ—ƒ๏ธ Object
View contents @@ -205,11 +201,12 @@ * [`orderBy`](#orderby) * [`select`](#select) * [`shallowClone`](#shallowclone) +* [`size`](#size) * [`truthCheckCollection`](#truthcheckcollection)
-### String +### ๐Ÿ“œ String
View contents @@ -222,6 +219,7 @@ * [`escapeHTML`](#escapehtml) * [`escapeRegExp`](#escaperegexp) * [`fromCamelCase`](#fromcamelcase) +* [`isAbsoluteURL`](#isabsoluteurl) * [`palindrome`](#palindrome) * [`repeatString`](#repeatstring) * [`reverseString`](#reversestring) @@ -236,7 +234,7 @@
-### Utility +### ๐Ÿ’Ž Utility
View contents @@ -247,11 +245,15 @@ * [`getType`](#gettype) * [`hexToRGB`](#hextorgb) * [`isArray`](#isarray) +* [`isArrayLike`](#isarraylike) * [`isBoolean`](#isboolean) * [`isFunction`](#isfunction) +* [`isNull`](#isnull) * [`isNumber`](#isnumber) +* [`isPromiseLike`](#ispromiselike) * [`isString`](#isstring) * [`isSymbol`](#issymbol) +* [`isValidJSON`](#isvalidjson) * [`randomHexColorCode`](#randomhexcolorcode) * [`RGBToHex`](#rgbtohex) * [`sdbm`](#sdbm) @@ -259,10 +261,21 @@ * [`toDecimalMark`](#todecimalmark) * [`toOrdinalSuffix`](#toordinalsuffix) * [`validateNumber`](#validatenumber) +* [`yesNo`](#yesno)
-## Adapter +### _Uncategorized_ + +
+View contents + +* [`sortedIndex`](#sortedindex) + +
+ +--- + ## ๐Ÿ”Œ Adapter ### call @@ -424,7 +437,8 @@ arrayMax([1, 2, 4]); // 4
[โฌ† Back to top](#table-of-contents) -## Array +--- + ## ๐Ÿ“š Array ### chunk @@ -1190,6 +1204,38 @@ sample([3, 7, 9, 11]); // 9
[โฌ† Back to top](#table-of-contents) +### sampleSize + +Gets `n` random elements at unique keys from `array` up to the size of `array`. + +Shuffle the array using the [Fisher-Yates algorithm](https://github.com/chalarangelo/30-seconds-of-code#shuffle). +Use `Array.slice()` to get the first `n` elements. +Omit the second argument, `n` to get only one element at random from the array. + +```js +const sampleSize = ([...arr], n = 1) => { + let m = arr.length; + while (m) { + const i = Math.floor(Math.random() * m--); + [arr[m], arr[i]] = [arr[i], arr[m]]; + } + return arr.slice(0, n); +}; +``` + +
+Examples + +```js +sampleSize([1, 2, 3], 2); // [3,1] +sampleSize([1, 2, 3], 4); // [2,3,1] +``` + +
+ +
[โฌ† Back to top](#table-of-contents) + + ### shuffle Randomizes the order of the values of an array, returning a new array. @@ -1437,7 +1483,8 @@ zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
[โฌ† Back to top](#table-of-contents) -## Browser +--- + ## ๐ŸŒ Browser ### arrayToHtmlList @@ -1486,6 +1533,48 @@ bottomVisible(); // true
[โฌ† Back to top](#table-of-contents) +### copyToClipboard + +Copy a string to the clipboard. Only works as a result of user action (i.e. inside a `click` event listener). + +Create a new `