Skip to content

Instantly share code, notes, and snippets.

@rafagarcia
rafagarcia / nvmCommands.js
Created April 12, 2022 10:47 — forked from chranderson/nvmCommands.js
Useful NVM commands
// check version
node -v || node --version
// list installed versions of node (via nvm)
nvm ls
// install specific version of node
nvm install 6.9.2
// set default version of node
@rafagarcia
rafagarcia / gist:645d94057ff14b14853563d001a7872e
Created March 29, 2022 15:41 — forked from CrookedNumber/gist:8964442
git: Removing the last commit

Removing the last commit

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

If you want to "uncommit" the commits, but keep the changes around for reworking, remove the "--hard": git reset HEAD^ which will evict the commits from the branch and from the index, but leave the working tree around.

If you want to save the commits on a new branch name, then run git branch newbranchname before doing the git reset.

@rafagarcia
rafagarcia / async-await-vs-promises-errors.js
Created December 4, 2018 16:45 — forked from k-vosswinkel/async-await-vs-promises-errors.js
Playing with promises and async/await
const returnsAPromise = (string) => (
new Promise((resolve, reject) => {
if (typeof string !== 'string') reject('Not a string!');
resolve(`String is a resolved promise now: ${string}`);
})
);
const myString = "Kait's string";
let isOurPromiseFinished = false;
// spread operator examples
let a = [3, 4, 5];
let b = [1, 2, ...a, 6];
console.log(b);
function foo(a, b, c) { console.log(`a=${a}, b=${b}, c=${c}`)}
let data = [5, 15, 2];
foo( ...data);
// Array destructuring. Swap values
let param1 = 1;
let param2 = 2;
//swap and assign param1 & param2 each others values
[param1, param2] = [param2, param1];
console.log(param1); // 2
console.log(param2); // 1
// sets
let arr = [1, 1, 2, 2, 3, 3];
let deduped = [...new Set(arr)] // [1, 2, 3]
console.log(deduped);
let mySet = new Set([1,2, 3, 4, 5]);
var filtered = [...mySet].filter((x) => x > 3) // [4, 5]
// Destructuring nested object
var car = {
model: 'bmw 2018',
engine: {
v6: true,
turbo: true,
vin: 12345
}
}
// Destructuring with rest parameter
let {_internal, tooBig, ...cleanObject} = {el1: '1', _internal:"secret", tooBig:{}, el2: '2', el3: '3'};
console.log(cleanObject); // {el1: '1', el2: '2', el3: '3'}
var cars = ['BMW','Benz', 'Benz', 'Tesla', 'BMW', 'Toyota'];
var carsObj = cars.reduce(function (obj, name) {
obj[name] = obj[name] ? ++obj[name] : 1;
return obj;
}, {});
console.log(carsObj);
const numbers = [10, 20, 30, 40];
let updatedNumbers = numbers
.map((num) => num * 2)
.filter((num) => num > 50);
let doubledOver50 = numbers.reduce((finalList, num) => {
num = num * 2; //double each number (i.e. map)
//filter number > 50
if (num > 50) {