Skip to content

Instantly share code, notes, and snippets.

View rodrigograca31's full-sized avatar
👨‍💻
Professional 🐛 solver

Rodrigo Graça rodrigograca31

👨‍💻
Professional 🐛 solver
View GitHub Profile
@rodrigograca31
rodrigograca31 / latest-node.sh
Created December 7, 2020 18:57
Latest NodeJS version on Raspberry PI 0
View latest-node.sh
export NODE_VER=14.15.1
if ! node --version | grep -q ${NODE_VER}; then
(cat /proc/cpuinfo | grep -q "Pi Zero") && if [ ! -d node-v${NODE_VER}-linux-armv6l ]; then
echo "Installing nodejs ${NODE_VER} for armv6 from unofficial builds..."
curl -O https://unofficial-builds.nodejs.org/download/release/v${NODE_VER}/node-v${NODE_VER}-linux-armv6l.tar.xz
tar -xf node-v${NODE_VER}-linux-armv6l.tar.xz
fi
echo "Adding node to the PATH"
PATH=$(pwd)/node-v${NODE_VER}-linux-armv6l/bin:${PATH}
fi
@rodrigograca31
rodrigograca31 / events_to_promise.js
Last active October 11, 2020 21:33
Turn Events into Promises in JavaScript
View events_to_promise.js
const events = [download, download2];
const promises = events.map((event) => {
return new Promise((resolve) => {
event.on("end", (paths) => {
resolve(paths);
});
});
});
Promise.all(promises).then((result) => {});
@rodrigograca31
rodrigograca31 / array-shuffle.js
Created August 12, 2020 11:52
Shuffles array in place. ES6 version
View array-shuffle.js
/**
* Shuffles array in place. ES6 version
* @param {Array} a items An array containing the items.
* https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array
*/
shuffle(a: Array<any>) {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
@rodrigograca31
rodrigograca31 / deep-copy.js
Last active August 12, 2020 11:46
Deep copy objects in Javascript
View deep-copy.js
/**
* Makes a copy of values not references and goes down the attributes
*/
deepCopy(o) {
var copy = o,
k;
if (o && typeof o === "object") {
copy =
Object.prototype.toString.call(o) === "[object Array]"
@rodrigograca31
rodrigograca31 / randomInt.ts
Last active September 22, 2023 03:37
Random Integer generator (Typescript)
View randomInt.ts
/**
* Generates a random integer between min and max (inclusive)
* @param {number} min
* @param {number} max
* @returns randomly generated integer
*/
public randomInt = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
@rodrigograca31
rodrigograca31 / CompanyPage.test.js
Created June 7, 2020 18:55
React testing with redux
View CompanyPage.test.js
describe('CompanyPage', () => {
it('renders correctly', () => {
expect(
renderWithProviders(
withRouter(
<CompanyPage companies={testCompanies} avgSalaries={testSalaries} />
)
).baseElement
).toMatchSnapshot();
});
@rodrigograca31
rodrigograca31 / perf.py
Created April 10, 2020 10:12
Test Python performance 🐍⚡
View perf.py
import time
times = 5000000
def func1():
return 5 + 5
def func2():
return 5 + 5 - 5 + 5
@rodrigograca31
rodrigograca31 / 02.py
Created April 7, 2020 16:28
🐍 Python set() algo perf test
View 02.py
#!/usr/bin/env python3
phrase = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non lorem nec ante commodo gravida. Aenean ut ultricies quam. Nullam euismod lorem quis dapibus pharetra. Vestibulum a tincidunt justo. Proin eget faucibus velit. Aenean tincidunt lorem non ex molestie gravida vitae feugiat diam. In venenatis lacus non ipsum convallis porttitor nec vitae erat. Proin auctor, nibh ac hendrerit tempor, arcu lacus interdum erat, sit amet hendrerit mi dolor nec sapien. Maecenas mi dui, viverra ac imperdiet et, auctor id libero. Nunc sit amet felis lacus. Nunc vitae erat sit amet nunc porttitor lacinia vitae nec neque. Praesent tortor diam, molestie volutpat rhoncus et, fermentum a odio. Etiam et felis eget purus semper fermentum. In finibus ex in convallis pellentesque. Praesent fringilla massa ac dictum malesuada. Maecenas nec massa vitae mi porta sodales. Aenean blandit massa vitae leo finibus venenatis. In hac habitasse platea dictumst. Suspendisse eget orci ultrices justo malesua
@rodrigograca31
rodrigograca31 / edit.jsx
Last active December 2, 2019 14:57
Find and edit in array with map
View edit.jsx
props.setMembers([
...props.members.map((el, index) => {
if (el.id == props.editThisOne.id) {
el = member;
}
return el;
})
]);
View function_arrow.js
const person = {
name: 'Rodrigo',
age: 99,
how_old: function() { return this.name + ' is ' + this.age + ' old'}
}
person.how_old()
// Why dont arrow functions work with this?
// How do you solve it?
const person2 = {