Skip to content

Instantly share code, notes, and snippets.

View zaguiini's full-sized avatar
🏠
Working from home

Luis Felipe Zaguini zaguiini

🏠
Working from home
View GitHub Profile
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function performanceURL(url, sampleSize, sleepMs = 1000) {
const performanceList = [];
for (let i = 0; i < sampleSize; i++) {
const t0 = performance.now();
await window.wpcom.request({ method: 'GET', path: url });
const t1 = performance.now();
performanceList.push(t1 - t0);
{"lastUpload":"2021-09-13T13:13:59.269Z","extensionVersion":"v3.4.3"}
@zaguiini
zaguiini / flatten-list.js
Last active January 14, 2020 14:34
How to flatten a list using reduce and recursion
const flattenList = (array) => {
return array.reduce((flat, next) => {
return flat.concat(Array.isArray(next) ? flattenList(next) : next)
}, [])
}
import { createStore } from 'easy-peasy'
const store = createStore({
todos: {
items: ['Create store', 'Wrap application', 'Use store'],
add: action((state, payload) => {
state.items.push(payload)
})
}
})
import React from 'react'
const Store = React.createContext()
const useStore = () => React.useContext(Store)
const reducer = (state, action) => {
switch(action.type) {
case 'ADD_TODO':
return { ...state, todos: [...state.todos, action.payload] }
@zaguiini
zaguiini / brick-wall.js
Last active December 2, 2019 03:22
Brick wall Leetcode solution
const wall = [
[1, 2, 2, 1],
[3, 1, 2],
[1, 2, 2],
[2, 4],
[3, 1, 2],
[1, 3, 1, 1]
]
const leastBricks = (wall) => {
@zaguiini
zaguiini / README.md
Last active November 2, 2019 22:45
Travis CI NPM workflow

Travis CI - NPM workflow

This workflow does, in the following order:

  • install your packages
  • run the lint command
  • run the test command
  • publishes the package with the version defined in the release tag
  • publishes the new version code on github (updates the package.json to match the latest tag)

It will only trigger the deploy phase on the master branch when there's the release tag present. Otherwise it will just build and test your files.

@zaguiini
zaguiini / selection-sort.js
Last active October 29, 2019 04:05
Selection sort in plain English
/*
Time complexity: O(nˆ2) since it has two nested loops
Space complexity: O(1) since it uses a reference to something that already exists and does not create new arrays
*/
const selectionSort = array => {
for (let arrayIndex = 0; arrayIndex < array.length - 1; arrayIndex++) {
let minIndex = arrayIndex
for (let subArrayIndex = arrayIndex + 1; subArrayIndex < array.length; subArrayIndex++) {
@zaguiini
zaguiini / getAge.js
Last active May 27, 2019 13:07
Return the age given the parameters
function getAge(month, day, year) {
const birthDate = new Date(year, month - 1, day)
const fromNow = new Date() - birthDate
const absoluteAge = new Date(fromNow).getFullYear()
// that's because timestamps starts from 1970, so
// we're getting relatively to that year
return Math.abs(absoluteAge - 1970)
}
@zaguiini
zaguiini / use-mutual-number-field.js
Last active March 8, 2019 13:27
Useful hook for when updating multiple values that must fit 100%
function useMutualNumberField({
values,
index,
setCurrentValue,
setAllValues,
}) {
return function handlePercentageChange(rawInputtedValue) {
const inputtedValue = parseInt((rawInputtedValue || 0).toString(), 10)
const prevValue = values[index].percentage