Skip to content

Instantly share code, notes, and snippets.

Avatar

Hellon Canella helloncanella

View GitHub Profile
View executeSequentialPromises.js
export default function executeSequentialPromises(funcs) {
return funcs.reduce(
(promise, func) =>
promise.then(result =>
(func() || Promise.resolve(undefined)).then(
Array.prototype.concat.bind(result)
)
),
Promise.resolve([])
)
View spreadValuesWithKeySeparatedByComma.js
import _ from "lodash"
/**
* consider the object
*
* const obj = {
* key1: value1
* key2: value2
* "key3,key4,key5": {key3: value3, key4: value4}
* }
*
@helloncanella
helloncanella / index.js
Last active Nov 1, 2019
basic-node-config
View index.js
require = require("esm")(module)
module.exports = require("./server")
View ComposeProviders.js
import React from "react"
import _ from "lodash"
/**
* Provided that a list of providers [P1, P2, P3, P4] is passed as props,
* it renders
*
* <P1>
<P2>
<P3>
View getContextManager.jsx
import React, { useState, useContext, useMemo } from "react"
export default function getContextManager() {
const Context = React.createContext()
function Provider(props) {
const [state, setState] = useState()
const api = useMemo(() => [state, setState], [JSON.stringify(state)])
return <Context.Provider value={api}>{props.children}</Context.Provider>
}
View useNewEntries.js
import _ from "lodash"
import { useState, useEffect, useRef } from "react"
export default function useNewEntries({ entries }) {
const [newEntries, setNewEntries] = useState([])
const oldEntries = useRef(entries)
useEffect(() => {
if (_.isEmpty(entries)) return
const newEntries = entries.filter(entry =>
View useParams.js
import useRouter from "use-react-router"
import { matchPath } from "react-router-dom"
export default function useParams(path) {
const { location } = useRouter()
const { pathname } = location
const pattern = `(.*)?${path}`
const match = matchPath(pathname, { path: pattern }) || {}
View useMatchPath.js
import _ from "lodash"
import useRouter from "use-react-router"
import { join } from "path"
import { matchPath } from "react-router-dom"
export default function useMatchPath(path) {
const { location } = useRouter()
const pathname = _.get(location, "pathname")
const match = matchPath(pathname, { path: join("(.*)?", path) }) || {}
View updateList.js
const LIST = gql`
query List($category: String!) {
list(category: $category) {
id
description
}
}
`
const [list, setList] = useAppState({
View useCount.js
function useCount({ label }) {
const COUNT = gql`
query Count($label: ID) {
count(label: $label) @client
}
`
const [count, setCount] = useAppState({
query: COUNT,
variables: { label }
You can’t perform that action at this time.