Skip to content

Instantly share code, notes, and snippets.

View midnightcodr's full-sized avatar
💭
A

Rico Chen midnightcodr

💭
A
View GitHub Profile
@midnightcodr
midnightcodr / main.js
Created April 11, 2017 02:20
inquirer with async/await
const inquirer = require('inquirer')
const genList = (list) => {
const choices = list.map((item, index) => {
return {
key: index,
name: `${item.id}: ${item.quantity}@${item.price}`,
value: item.id
}
})
@midnightcodr
midnightcodr / connect.js
Last active April 16, 2017 19:16
Bluebird mongodb disposer
// tested with mongodb 2.2.25
const MongoClient=require('mongodb').MongoClient
const Promise = require('bluebird')
module.exports = (connOpts) => {
return MongoClient
.connect(connOpts, {promiseLibrary: Promise})
// .then(db => db) /* not necessary */
.disposer(db => {
console.log('to disconnect')
@midnightcodr
midnightcodr / main.js
Created March 27, 2017 00:27
light switch with relay and raspberry pi
var gpio = require('rpi-gpio-mod');
function onoff(v) {
gpio.setup(8, gpio.DIR_OUT, write);
function write() {
gpio.write(8, v, function(err) {
if (err) throw err;
console.log('Written to pin');
@midnightcodr
midnightcodr / bulk.js
Last active March 24, 2017 01:20
mongodb bulk insert with bluebird disposer
module.exports = function(col, limit=5000) {
this.bulk = col.initializeUnorderedBulkOp()
let cnt = 0
this.add = record => {
cnt++
this.bulk.insert(record)
if(cnt >= limit) {
this.flush()
}
@midnightcodr
midnightcodr / README.md
Last active August 15, 2023 20:30
websocket performance test using ws
npm install bluebird ws
node server.js
node client.js
@midnightcodr
midnightcodr / sample_contact_us_form_bootstrap_styling.html
Created May 21, 2012 03:36
A sample contact form using twitter bootstrap styling
<form class="well span8">
<div class="row">
<div class="span3">
<label>First Name</label>
<input type="text" class="span3" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="span3" placeholder="Your Last Name">
<label>Email Address</label>
<div class="input-prepend">
<span class="add-on"><i class="icon-envelope"></i></span><input type="text" id="inputIcon" class="span2" style="width:233px" placeholder="Your email address">