Skip to content

Instantly share code, notes, and snippets.

@dan-laskowski
dan-laskowski / getSelectedText.js
Created May 5, 2021 12:09
[Get the text that the user has selected] #javascript #js #selected #text
/ Get the text that the user has selected
const getSelectedText = () => window.getSelection().toString();
getSelectedText();
@dan-laskowski
dan-laskowski / swapVariables.js
Created May 5, 2021 12:09
[Swap the values of 2 variables] #javascript #js #swap #variables
// Swap the values of 2 variables
let a = 1;
let b = 2;
[a, b] = [b, a];
// Result:
// a = 2
// b = 1
@dan-laskowski
dan-laskowski / getCookieValue.js
Created May 5, 2021 12:08
[Get the value of a specified cookie] #javascript #js #cookie #value
// Get the value of a specified cookie
cookie = name => `; ${document.cookie}`.split(`; ${name}=`).pop().split(';').shift();
cookie('_ga');
// Result: "GA1.2.1929736587.1601974046"
@dan-laskowski
dan-laskowski / generateHex.js
Created May 5, 2021 12:07
[Generate a random HEX color] #javascript #js #generate #hex
// Generate a random HEX color
randomColor = () => `#${Math.random().toString(16).slice(2, 8).padStart(6, '0')}`;
// Or
const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;
@dan-laskowski
dan-laskowski / sortNumbers.js
Created May 5, 2021 12:06
[Sort an array containing numbers] #javascript #js #array #sort #number
// Sort an array containing numbers
const sort = arr => arr.sort((a, b) => a - b);
sort([1, 5, 2, 4, 3]);
// Result: [1, 2, 3, 4, 5]
@dan-laskowski
dan-laskowski / capitalize.js
Created May 5, 2021 12:03
[Capitalize the first letter of a string] #javascript #js
// Capitalize the first letter of a string
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
capitalize("hello, you are a cool person!");
// Result: "Hello, you are a cool person!"
@dan-laskowski
dan-laskowski / sortUsersDsc
Created January 4, 2021 19:59
[Sort users by descending follow count] #javascript #js #array #sort
let sortUsersByDescendingFollowCount = users.sort((a, b) => a.followers < b.followers ? 1 : -1)
@dan-laskowski
dan-laskowski / unique.js
Created November 7, 2020 11:38
[Array with unique categories] #js #javascript
const allCategories = ['all', ...new Set(items.map((item) => item.category))];
@dan-laskowski
dan-laskowski / reset.css
Created November 6, 2020 13:25
[RESET CSS] #css #reset
/* http://meyerweb.com/eric/tools/css/reset/
v2.0-modified | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
@dan-laskowski
dan-laskowski / strikethrough.css
Created October 2, 2020 16:45
[Animated line-through text] #css #animation
.todo-item__text--completed {
display: inline-block;
position: relative;
transition: all .5 cubic-bezier(.55,0,.1,1);
/* text-decoration: line-through; */
}
.todo-item__text--completed:after {
content: '';
position: absolute;