Skip to content

Instantly share code, notes, and snippets.

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

Sergio Fernández xergiodf

🏠
Working from home
View GitHub Profile
import React, { useCallback, useState, useEffect, useContext, createContext } from 'react'
const authContext = createContext()
// Hook for child components to get the auth object and re-render when it changes.
export default () => {
return useContext(authContext)
}
// Provider component that wraps components and makes useAuth() available
const useStorage = (key, initialValue, storage) => {
// Pass initial state function to useState so logic is only executed once
const [storedValue, setStoredValue] = useState(() => {
try {
const item = storage.getItem(key)
return item ? JSON.parse(item) : initialValue
} catch (error) {
console.error(error)
return initialValue
}
import { useEffect, useState, useCallback, useRef } from 'react'
export default (asyncFunction, immediate = true) => {
const [loading, setLoading] = useState(false)
const [result, setResult] = useState(null)
const [error, setError] = useState(null)
// Track a reference to whether the useAsync is actually on a mounted component.
// useEffect below returns a cleanup that sets this to false. Before setting
// any state, we check if the cleanup has run. If it has, don't update the state.
@xergiodf
xergiodf / regexCheatsheet.js
Created January 17, 2019 11:59 — forked from sarthology/regexCheatsheet.js
A regex cheatsheet 👩🏻‍💻 (by Catherine)
let regex;
/* matching a specific string */
regex = /hello/; // looks for the string between the forward slashes (case-sensitive)... matches "hello", "hello123", "123hello123", "123hello"; doesn't match for "hell0", "Hello"
regex = /hello/i; // looks for the string between the forward slashes (case-insensitive)... matches "hello", "HelLo", "123HelLO"
regex = /hello/g; // looks for multiple occurrences of string between the forward slashes...
/* wildcards */
regex = /h.llo/; // the "." matches any one character other than a new line character... matches "hello", "hallo" but not "h\nllo"
regex = /h.*llo/; // the "*" matches any character(s) zero or more times... matches "hello", "heeeeeello", "hllo", "hwarwareallo"
@xergiodf
xergiodf / listing-ports.sh
Created December 18, 2018 14:45
3 simple options to know used ports in linux
# Option 1
netstat -tulpn | grep 9090
# Option 2
sudo lsof -i -P -n | grep 9090
# Option
sudo nmap -sT -O localhost
@xergiodf
xergiodf / findDuplicates.js
Created December 14, 2018 15:57
Mongoose find duplicates by field
const { User } = require("src/db");
/**
* Find all duplicates emails in the User
* mongoose model (mapping users collection)
*
*
* This aggregation function will groups the
* documents comparing emaiils, sanitized
* to lowercase; it adds the "_id" fields to
@xergiodf
xergiodf / loginController.js
Created November 26, 2018 19:49
AngularJS Interceptor
(function () {
'use strict';
angular.module('some.module')
.controller('LoginController', loginController);
/** @ngInject */
function loginController(localStorageService, AuthService) {
...
@xergiodf
xergiodf / README-Template.md
Created September 24, 2018 13:10 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@xergiodf
xergiodf / SimpleStore.js
Last active July 5, 2018 20:02 — forked from ksafranski/SimpleStore.js
Simple localStorage function with Cookie fallback for older browsers.
/**
* Simple localStorage with Cookie Fallback
* v.1.0.0
*
* USAGE:
* ----------------------------------------
* Set New / Modify:
* store('my_key', 'some_value');
*
* Retrieve:
@xergiodf
xergiodf / package.json
Created April 12, 2018 15:21 — forked from sethlopezme/package.json
Complete files for my article, Testing Hapi.js APIs. https://sethlopez.me/article/testing-hapi-js-apis
{
"name": "testing-hapi-js-apis",
"version": "1.0.0",
"license": "MIT",
"scripts": {
"test": "ava --verbose test.js"
},
"dependencies": {
"ava": "^0.16.0",
"hapi": "^15.1.1"