Skip to content

Instantly share code, notes, and snippets.

View midnightcodr's full-sized avatar

Rico Chen midnightcodr

View GitHub Profile
// credit: https://hackernoon.com/functional-javascript-resolving-promises-sequentially-7aac18c4431e
const N = 5
const promiseSerial = funcs =>
funcs.reduce(
(promise, func) =>
promise.then(result => func().then(Array.prototype.concat.bind(result))),
Promise.resolve([])
)
// Based on https://stackoverflow.com/questions/48720942/node-js-joi-how-to-display-a-custom-error-messages
const Hapi = require('@hapi/hapi')
const Joi = require('@hapi/joi')
const Boom = require('@hapi/boom')
const server = Hapi.Server({
port: 3000,
routes: {
validate: {
@midnightcodr
midnightcodr / async-await-abuse-example.js
Created May 18, 2019 02:06
Showing a simple example of abusing async/await in javascript
const asyncJob = () => {
console.time('asyncJob')
return new Promise(resolve => {
return setTimeout(() => {
console.timeEnd('asyncJob')
return resolve('result from asycJob')
}, 1000)
})
}
@midnightcodr
midnightcodr / .babelrc
Last active May 2, 2019 19:48
parcel bundler for a simple project with rebass/styled-components
{
"presets": [
"env",
"react"
],
"plugins": [
"transform-object-rest-spread",
"transform-class-properties"
]
}
@midnightcodr
midnightcodr / Input.js
Last active April 10, 2019 13:22
material design text input with styled-components
import React from 'react'
import styled from 'styled-components'
const Wrapper = styled.div`
position: relative;
width: 100%;
padding-bottom: 2rem;
input:focus ~ label,
input:valid ~ label {
@midnightcodr
midnightcodr / server.js
Created April 3, 2019 19:33
hapijs-18-catbox-example.js
serverOptions.cache = [
{
name: 'redisCache',
provider: {
constructor: require('catbox-redis'),
options: {
url: process.env.REDIS_URL
}
}
}
@midnightcodr
midnightcodr / app.js
Created March 29, 2019 02:39
hapi-with-mongoose-example
const Hapi = require('hapi')
const Joi = require('joi')
const Mongoose = require('mongoose')
const Boom = require('boom')
const server = Hapi.Server({
port: 3000,
routes: {
validate: {
failAction: (request, h, err) => {
@midnightcodr
midnightcodr / nsq-consumer.js
Created December 13, 2018 13:36
updated example based on original nsqjs github example
const nsq = require('nsqjs')
const reader = new nsq.Reader('sample_topic', 'test_channel', {
nsqdTCPAddresses: ['127.0.0.1:4150']
})
reader.connect()
reader.on('message', msg => {
console.log('Received message [%s]: %s', msg.id, msg.body.toString())
@midnightcodr
midnightcodr / README.md
Created December 9, 2018 03:42
test-mongo-db-cluster
start_cluster
docker exec -it rc1 mongo
# then run the following in container rc1's mongo shell
rs.initiate(config = { _id: 'test-set', members: [{ _id: 0, host: 'rc1:27017' }, { _id: 1, host: 'rc2:27017' }, { _id: 2, host: 'rc3:27017' }] })
# while in there, populate some test data
use test
db.list.insertOne({ somekey: 'some value' })
db.list.insertOne({ title: 'cool' })
# finally quit
@midnightcodr
midnightcodr / create-parcel-app.sh
Created November 7, 2018 04:27
create-parcel-app
create-parcel-app() {
[ $# -lt 1 ] && echo "$funcstack[1] project-name"
project=$1
if [ -e $project ]; then
echo "object $project already exists"
return
fi
mkdir -p $project/src
cd $project
cat << EOL > src/index.html