Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Luis Felipe Zaguini zaguiini

🏠
Working from home
View GitHub Profile
View cloudSettings
{"lastUpload":"2020-11-18T11:41:45.987Z","extensionVersion":"v3.4.3"}
@zaguiini
zaguiini / flatten-list.js
Last active Jan 14, 2020
How to flatten a list using reduce and recursion
View flatten-list.js
const flattenList = (array) => {
return array.reduce((flat, next) => {
return flat.concat(Array.isArray(next) ? flattenList(next) : next)
}, [])
}
View easy-peasy-lemon-squeezy.js
import { createStore } from 'easy-peasy'
const store = createStore({
todos: {
items: ['Create store', 'Wrap application', 'Use store'],
add: action((state, payload) => {
state.items.push(payload)
})
}
})
View usereducer+usecontext.js
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 Dec 2, 2019
Brick wall Leetcode solution
View brick-wall.js
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 Nov 2, 2019
Travis CI NPM workflow
View README.md

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 Oct 29, 2019
Selection sort in plain English
View selection-sort.js
/*
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
Return the age given the parameters
View getAge.js
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 Mar 8, 2019
Useful hook for when updating multiple values that must fit 100%
View use-mutual-number-field.js
function useMutualNumberField({
values,
index,
setCurrentValue,
setAllValues,
}) {
return function handlePercentageChange(rawInputtedValue) {
const inputtedValue = parseInt((rawInputtedValue || 0).toString(), 10)
const prevValue = values[index].percentage
@zaguiini
zaguiini / usage.tsx
Created Mar 6, 2019 — forked from sibelius/usage.tsx
useSelectRows is a hook that manage a list of selected items, so you don't have to
View usage.tsx
const MyUserSelectableList = ({ users ) => {
const { onRowCheck, isRowSelected } = useSelectedRows();
return (
<>
{users.map(user => (
<>
<Checkbox value={isRowSelected(user)} onChange={(value) => onRowCheck(user, value)} />
<span>{user.name}</span>
</>
You can’t perform that action at this time.