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 44d8db8eb..a919b3b51 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);}
@@ -64,8 +65,8 @@
nthElement
pick
pull
-pullAll
pullAtIndex
+pullAtValue
remove
sample
shuffle
@@ -359,24 +360,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' ]
-
-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' ]
Mutates the original array to filter out the values at the specified indexes.
@@ -398,6 +392,26 @@ UseArray.push() to keep track of pulled values
// console.log(myArray); -> [ 'a', 'c' ]
// console.log(pulled); -> [ 'b', 'd' ]
+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' ]
+*/
+
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().
@@ -1040,16 +1054,23 @@ Return the string truncated to the desired length, with ... appende
// getType(new Set([1,2,3])) -> "set"
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)'
+
Checks if the given argument is an array.
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)' + ``` 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' ] -``` 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/static-parts/index-start.html b/static-parts/index-start.html index b988f7928..bcf5f8d17 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);} 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