From 556b543f55a829ddba6124da1f117e7a8af75c98 Mon Sep 17 00:00:00 2001 From: King Date: Tue, 19 Dec 2017 08:19:04 -0500 Subject: [PATCH 1/4] update pull.md -> merge pull.md & pullAll.md & remove pullAll.md --- snippets/pull.md | 13 +++++++++---- snippets/pullAll.md | 16 ---------------- 2 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 snippets/pullAll.md diff --git a/snippets/pull.md b/snippets/pull.md index 42efe6c1b..16f0e25c4 100644 --- a/snippets/pull.md +++ b/snippets/pull.md @@ -9,10 +9,15 @@ _(For a snippet that does not mutate the original array see [`without`](#without ```js const pull = (arr, ...args) => { - let pulled = arr.filter((v, i) => !args.includes(v)); + let pulled = arr.filter((v, i) => !args.toString().split(',').includes(v)); arr.length = 0; pulled.forEach(v => arr.push(v)); }; -// let myArray = ['a', 'b', 'c', 'a', 'b', 'c']; -// pull(myArray, 'a', 'c'); -// console.log(myArray) -> [ 'b', 'b' ] + +// let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c']; +// pull(myArray1, 'a', 'c'); +// console.log(myArray1) -> [ 'b', 'b' ] + +// let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c']; +// pull(myArray2, ['a', 'c']); +// console.log(myArray2) -> [ 'b', 'b' ] ``` diff --git a/snippets/pullAll.md b/snippets/pullAll.md deleted file mode 100644 index ab8bf2285..000000000 --- a/snippets/pullAll.md +++ /dev/null @@ -1,16 +0,0 @@ -### pullAll - -Mutates the original array to filter out the values specified (accepts an array of values). - -Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. -Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. - -```js -const pullAll = (arr, pullArr) => { - let pulled = arr.filter((v, i) => !pullArr.includes(v)); - arr.length = 0; pulled.forEach(v => arr.push(v)); -} -// let myArray = ['a', 'b', 'c', 'a', 'b', 'c']; -// pullAll(myArray, ['a', 'c']); -// console.log(myArray) -> [ 'b', 'b' ] -``` From 1f011c0abcab062064585a7a812f98a48558e373 Mon Sep 17 00:00:00 2001 From: Kutsan Kaplan Date: Tue, 19 Dec 2017 17:10:06 +0300 Subject: [PATCH 2/4] Update `hexToRGB()` with additional supports and fixes --- snippets/hexToRGB.md | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/snippets/hexToRGB.md b/snippets/hexToRGB.md index 273aafd21..2d08b4321 100644 --- a/snippets/hexToRGB.md +++ b/snippets/hexToRGB.md @@ -1,16 +1,23 @@ ### hexToRGB -Converts a colorcode to a `rgb()` string. +Converts a color code to a `rgb()` or `rgba()` string if alpha value is provided. -Use bitwise right-shift operator and mask bits with `&` (and) operator to convert a hexadecimal color code (prefixed with `#`) to a string with the RGB values. In case it's a 3-digit-colorcode, do the same with the 6-digit-colorcode extended by the extendHex() function (ref. `extendHex` snippet) +Use bitwise right-shift operator and mask bits with `&` (and) operator to convert a hexadecimal color code (with or without prefixed with `#`) to a string with the RGB values. If it's 3-digit color code, first convert to 6-digit version. If any alpha value is provided alongside 6-digit hex, give `rgba()` string in return. ```js -const hexToRgb = hex => { - const extendHex = shortHex => - '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join(''); - const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex; - return `rgb(${parseInt(extendedHex.slice(1), 16) >> 16}, ${(parseInt(extendedHex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendedHex.slice(1), 16) & 0x0000ff})`; -} -// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)' -// hexToRgb('#acd') -> 'rgb(170, 204, 221)' +const hexToRGB = hex => { + let alpha = false, h = hex.slice(hex.startsWith('#') ? 1 : 0); + if (h.length === 3) h = [...h].map(x => x + x).join(''); + else if (h.length === 8) alpha = true; + h = parseInt(h, 16); + return 'rgb' + (alpha ? 'a' : '') + '(' + + (h >>> (alpha ? 24 : 16)) + ', ' + + ((h & (alpha ? 0x00ff0000 : 0x00ff00)) >>> (alpha ? 16 : 8)) + ', ' + + ((h & (alpha ? 0x0000ff00 : 0x0000ff)) >>> (alpha ? 8 : 0)) + + (alpha ? `, ${(h & 0x000000ff)}` : '') + ')'; +}; +// hexToRGB('#27ae60ff') -> 'rgba(39, 174, 96, 255)' +// hexToRGB('27ae60') -> 'rgb(39, 174, 96)' +// hexToRGB('#fff') -> 'rgb(255, 255, 255)' + ``` From e214f3f4e32e6f8b5cda2b971435b0a0ce544e71 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 19 Dec 2017 16:45:24 +0200 Subject: [PATCH 3/4] Build, resolve #235 --- README.md | 85 ++++++++++++++++++++++-------------- docs/index.html | 74 +++++++++++++++++++------------ static-parts/README-start.md | 1 + tag_database | 3 +- 4 files changed, 102 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 2a7f9e566..bce4069d1 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ - 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 Alfred 3, using [this file](https://github.com/lslvxy/30-seconds-of-code-alfredsnippets). ## Table of Contents @@ -36,8 +37,8 @@ * [`nthElement`](#nthelement) * [`pick`](#pick) * [`pull`](#pull) -* [`pullAll`](#pullall) * [`pullAtIndex`](#pullatindex) +* [`pullAtValue`](#pullatvalue) * [`remove`](#remove) * [`sample`](#sample) * [`shuffle`](#shuffle) @@ -512,31 +513,17 @@ _(For a snippet that does not mutate the original array see [`without`](#without ```js const pull = (arr, ...args) => { - let pulled = arr.filter((v, i) => !args.includes(v)); + let pulled = arr.filter((v, i) => !args.toString().split(',').includes(v)); arr.length = 0; pulled.forEach(v => arr.push(v)); }; -// let myArray = ['a', 'b', 'c', 'a', 'b', 'c']; -// pull(myArray, 'a', 'c'); -// console.log(myArray) -> [ 'b', 'b' ] -``` -[⬆ back to top](#table-of-contents) +// let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c']; +// pull(myArray1, 'a', 'c'); +// console.log(myArray1) -> [ 'b', 'b' ] -### pullAll - -Mutates the original array to filter out the values specified (accepts an array of values). - -Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. -Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. - -```js -const pullAll = (arr, pullArr) => { - let pulled = arr.filter((v, i) => !pullArr.includes(v)); - arr.length = 0; pulled.forEach(v => arr.push(v)); -} -// let myArray = ['a', 'b', 'c', 'a', 'b', 'c']; -// pullAll(myArray, ['a', 'c']); -// console.log(myArray) -> [ 'b', 'b' ] +// let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c']; +// pull(myArray2, ['a', 'c']); +// console.log(myArray2) -> [ 'b', 'b' ] ``` [⬆ back to top](#table-of-contents) @@ -568,6 +555,33 @@ const pullAtIndex = (arr, pullArr) => { [⬆ back to top](#table-of-contents) +### pullAtValue + +Mutates the original array to filter out the values specified. Returns the removed elements. + +Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. +Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. +Use `Array.push()` to keep track of pulled values + +```js +const pullAtValue = (arr, pullArr) => { + let removed = [], + pushToRemove = arr.forEach((v, i) => pullArr.includes(v) ? removed.push(v) : v), + mutateTo = arr.filter((v, i) => !pullArr.includes(v)); + arr.length = 0; + mutateTo.forEach(v => arr.push(v)); + return removed; +} +/* +let myArray = ['a', 'b', 'c', 'd']; +let pulled = pullAtValue(myArray, ['b', 'd']); +console.log(myArray); -> [ 'a', 'c' ] +console.log(pulled); -> [ 'b', 'd' ] +*/ +``` + +[⬆ back to top](#table-of-contents) + ### remove Removes elements from an array for which the given function returns `false`. @@ -1717,19 +1731,26 @@ const getType = v => ### hexToRGB -Converts a colorcode to a `rgb()` string. +Converts a color code to a `rgb()` or `rgba()` string if alpha value is provided. -Use bitwise right-shift operator and mask bits with `&` (and) operator to convert a hexadecimal color code (prefixed with `#`) to a string with the RGB values. In case it's a 3-digit-colorcode, do the same with the 6-digit-colorcode extended by the extendHex() function (ref. `extendHex` snippet) +Use bitwise right-shift operator and mask bits with `&` (and) operator to convert a hexadecimal color code (with or without prefixed with `#`) to a string with the RGB values. If it's 3-digit color code, first convert to 6-digit version. If any alpha value is provided alongside 6-digit hex, give `rgba()` string in return. ```js -const hexToRgb = hex => { - const extendHex = shortHex => - '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join(''); - const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex; - return `rgb(${parseInt(extendedHex.slice(1), 16) >> 16}, ${(parseInt(extendedHex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendedHex.slice(1), 16) & 0x0000ff})`; -} -// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)' -// hexToRgb('#acd') -> 'rgb(170, 204, 221)' +const hexToRGB = hex => { + let alpha = false, h = hex.slice(hex.startsWith('#') ? 1 : 0); + if (h.length === 3) h = [...h].map(x => x + x).join(''); + else if (h.length === 8) alpha = true; + h = parseInt(h, 16); + return 'rgb' + (alpha ? 'a' : '') + '(' + + (h >>> (alpha ? 24 : 16)) + ', ' + + ((h & (alpha ? 0x00ff0000 : 0x00ff00)) >>> (alpha ? 16 : 8)) + ', ' + + ((h & (alpha ? 0x0000ff00 : 0x0000ff)) >>> (alpha ? 8 : 0)) + + (alpha ? `, ${(h & 0x000000ff)}` : '') + ')'; +}; +// hexToRGB('#27ae60ff') -> 'rgba(39, 174, 96, 255)' +// hexToRGB('27ae60') -> 'rgb(39, 174, 96)' +// hexToRGB('#fff') -> 'rgb(255, 255, 255)' + ``` [⬆ back to top](#table-of-contents) diff --git a/docs/index.html b/docs/index.html index b57d6a3df..6fef6d1bd 100644 --- a/docs/index.html +++ b/docs/index.html @@ -61,8 +61,8 @@ nthElement pick pull -pullAll pullAtIndex +pullAtValue remove sample shuffle @@ -356,24 +356,17 @@ Omit the second argument, n, to get the first element of the array. Use Array.length = 0 to mutate the passed in array by resetting it's length to zero and Array.push() to re-populate it with only the pulled values.

(For a snippet that does not mutate the original array see without)

const pull = (arr, ...args) => {
-  let pulled = arr.filter((v, i) => !args.includes(v));
+  let pulled = arr.filter((v, i) => !args.toString().split(',').includes(v));
   arr.length = 0; pulled.forEach(v => arr.push(v));
 };
-// let myArray = ['a', 'b', 'c', 'a', 'b', 'c'];
-// pull(myArray, 'a', 'c');
-// console.log(myArray) -> [ 'b', 'b' ]
-
-

pullAll

-

Mutates the original array to filter out the values specified (accepts an array of values).

-

Use Array.filter() and Array.includes() to pull out the values that are not needed. -Use Array.length = 0 to mutate the passed in array by resetting it's length to zero and Array.push() to re-populate it with only the pulled values.

-
const pullAll = (arr, pullArr) => {
-  let pulled = arr.filter((v, i) => !pullArr.includes(v));
-  arr.length = 0; pulled.forEach(v => arr.push(v));
-}
-// let myArray = ['a', 'b', 'c', 'a', 'b', 'c'];
-// pullAll(myArray, ['a', 'c']);
-// console.log(myArray) -> [ 'b', 'b' ]
+
+// let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c'];
+// pull(myArray1, 'a', 'c');
+// console.log(myArray1) -> [ 'b', 'b' ]
+
+// let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c'];
+// pull(myArray2, ['a', 'c']);
+// console.log(myArray2) -> [ 'b', 'b' ]
 

pullAtIndex

Mutates the original array to filter out the values at the specified indexes.

@@ -395,6 +388,26 @@ Use Array.push() to keep track of pulled values

// console.log(myArray); -> [ 'a', 'c' ] // console.log(pulled); -> [ 'b', 'd' ] +

pullAtValue

+

Mutates the original array to filter out the values specified. Returns the removed elements.

+

Use Array.filter() and Array.includes() to pull out the values that are not needed. +Use Array.length = 0 to mutate the passed in array by resetting it's length to zero and Array.push() to re-populate it with only the pulled values. +Use Array.push() to keep track of pulled values

+
const pullAtValue = (arr, pullArr) => {
+  let removed = [], 
+    pushToRemove = arr.forEach((v, i) => pullArr.includes(v) ? removed.push(v) : v),
+    mutateTo = arr.filter((v, i) => !pullArr.includes(v));
+  arr.length = 0;
+  mutateTo.forEach(v => arr.push(v));
+  return removed;
+}
+/*
+let myArray = ['a', 'b', 'c', 'd'];
+let pulled = pullAtValue(myArray, ['b', 'd']);
+console.log(myArray); -> [ 'a', 'c' ]
+console.log(pulled); -> [ 'b', 'd' ]
+*/
+

remove

Removes elements from an array for which the given function returns false.

Use Array.filter() to find array elements that return truthy values and Array.reduce() to remove elements using Array.splice(). @@ -1037,16 +1050,23 @@ Return the string truncated to the desired length, with ... appende // getType(new Set([1,2,3])) -> "set"


hexToRGB

-

Converts a colorcode to a rgb() string.

-

Use bitwise right-shift operator and mask bits with & (and) operator to convert a hexadecimal color code (prefixed with #) to a string with the RGB values. In case it's a 3-digit-colorcode, do the same with the 6-digit-colorcode extended by the extendHex() function (ref. extendHex snippet)

-
const hexToRgb = hex => {
-  const extendHex = shortHex =>
-    '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('');
-  const extendedHex = hex.slice(hex.startsWith('#') ? 1 : 0).length === 3 ? extendHex(hex) : hex;
-  return `rgb(${parseInt(extendedHex.slice(1), 16) >> 16}, ${(parseInt(extendedHex.slice(1), 16) & 0x00ff00) >> 8}, ${parseInt(extendedHex.slice(1), 16) & 0x0000ff})`;
-}
-// hexToRgb('#27ae60') -> 'rgb(39, 174, 96)'
-// hexToRgb('#acd') -> 'rgb(170, 204, 221)'
+

Converts a color code to a rgb() or rgba() string if alpha value is provided.

+

Use bitwise right-shift operator and mask bits with & (and) operator to convert a hexadecimal color code (with or without prefixed with #) to a string with the RGB values. If it's 3-digit color code, first convert to 6-digit version. If any alpha value is provided alongside 6-digit hex, give rgba() string in return.

+
const hexToRGB = hex => {
+  let alpha = false, h = hex.slice(hex.startsWith('#') ? 1 : 0);
+  if (h.length === 3) h = [...h].map(x => x + x).join('');
+  else if (h.length === 8) alpha = true;
+  h = parseInt(h, 16);
+  return 'rgb' + (alpha ? 'a' : '') + '('
+    + (h >>> (alpha ? 24 : 16)) + ', '
+    + ((h & (alpha ? 0x00ff0000 : 0x00ff00)) >>> (alpha ? 16 : 8)) + ', '
+    + ((h & (alpha ? 0x0000ff00 : 0x0000ff)) >>> (alpha ? 8 : 0))
+    + (alpha ? `, ${(h & 0x000000ff)}` : '') + ')';
+};
+// hexToRGB('#27ae60ff') -> 'rgba(39, 174, 96, 255)'
+// hexToRGB('27ae60') -> 'rgb(39, 174, 96)'
+// hexToRGB('#fff') -> 'rgb(255, 255, 255)'
+
 

isArray

Checks if the given argument is an array.

diff --git a/static-parts/README-start.md b/static-parts/README-start.md index 6a6504483..92eb9fe8a 100644 --- a/static-parts/README-start.md +++ b/static-parts/README-start.md @@ -6,5 +6,6 @@ - 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 Alfred 3, using [this file](https://github.com/lslvxy/30-seconds-of-code-alfredsnippets). ## Table of Contents diff --git a/tag_database b/tag_database index c189c4a9e..45af1e960 100644 --- a/tag_database +++ b/tag_database @@ -74,9 +74,8 @@ pipe:function powerset:math promisify:function pull:array -pullAtValue:array -pullAll:array pullAtIndex:array +pullAtValue:array randomIntegerInRange:math randomNumberInRange:math readFileLines:node From 4a2dcc5d6a4c5abb09c105b74338f4e5aa957a20 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 19 Dec 2017 17:11:16 +0200 Subject: [PATCH 4/4] Resolves #243 --- docs/index.html | 1 + static-parts/index-start.html | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/index.html b/docs/index.html index 6fef6d1bd..38b5a08ac 100644 --- a/docs/index.html +++ b/docs/index.html @@ -17,6 +17,7 @@ html, * { font-family: 'Poppins', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, sans-serif; } code, pre, kbd, code *, pre *, kbd * { font-family: 'Inconsolata', Menlo, Consolas, monospace; } code, kbd { font-size: 1em; } + code { transform: scale(1); } /* Deals with the issue described in #243 */ pre { font-size: 1rem; border: 0.0625rem solid var(--secondary-border-color); border-radius: var(--universal-border-radius);} diff --git a/static-parts/index-start.html b/static-parts/index-start.html index fd9fdc1f8..7b4228dbd 100644 --- a/static-parts/index-start.html +++ b/static-parts/index-start.html @@ -17,6 +17,7 @@ html, * { font-family: 'Poppins', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, "Helvetica Neue", Helvetica, sans-serif; } code, pre, kbd, code *, pre *, kbd * { font-family: 'Inconsolata', Menlo, Consolas, monospace; } code, kbd { font-size: 1em; } + code { transform: scale(1); } /* Deals with the issue described in #243 */ pre { font-size: 1rem; border: 0.0625rem solid var(--secondary-border-color); border-radius: var(--universal-border-radius);}