Skip to content

Instantly share code, notes, and snippets.

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

Captain Emo craftgear

🏠
Working from home
  • self-employed
  • Japan
View GitHub Profile
@craftgear
craftgear / permutate.js
Last active May 14, 2018 12:30
Permutation with recursion
const insertItem = (xs, index, item) => [...xs.slice(0, index), item, ...xs.slice(index)];
const range = number => [...Array(number).keys()];
const permutate = (xs, accum = []) => {
const [head, ...tail] = xs;
if (!head) {
return accum;
}
return accum.length === 0
@craftgear
craftgear / pad.js
Last active November 25, 2017 02:52
leftPad, rightPad, oh whatever.
const pad = (leftOrRight = 'left') => maxLength => char => value => {
const strValue = typeof value === 'number' ? value.toString() : value;
if (strValue.length >= maxLength) {
return strValue;
}
if (typeof maxLength !== 'number') {
throw new Error('maxLength should be a number');
}
const padding = Array(maxLength - strValue.length)
@craftgear
craftgear / splitByRegexp.go
Last active November 20, 2017 03:23
split a string into a slice of strings by regexp (goで正規表現を使って文字列を分割する)
func splitByRegexp(str string, separator string) []string {
normalRe := "[^%s]*[%s]"
rawRe := `[^%s]*[%s]`
var re *regexp.Regexp
if strings.Index(separator, `\\`) > -1 {
re = regexp.MustCompile(fmt.Sprintf(normalRe, separator, separator))
} else {
re = regexp.MustCompile(fmt.Sprintf(rawRe, separator, separator))
}
async (callback) => {
const { err, ...result } = await asyncCode().catch(e => ({ err: e }));
if (err) {
callback(err);
return;
}
callback(null, result);
};
async (callback) => {
try {
const result = await asyncCode();
callback(null, result)
}
catch (e) {
callback(e)
}
}
async (callback) => {
const result = await asyncCode();
callback(null, result)
}
@craftgear
craftgear / gist:5669101
Last active December 17, 2015 20:39
expressでミドルウェアもしくはイベントハンドラでセッションの値を参照する
//ミドルウェアでやる場合
app.configure(function(){
//・・・(略)・・・
app.use(function (req, res, next) {
app.locals.message = req.session.message;
next();
});
//・・・(略)・・・
});
@craftgear
craftgear / jadewatch.coffee
Created February 23, 2012 22:35
jade watch
# run with 'coffee jadewatch.coffee dirname'
fs = require 'fs'
if process.argv.length < 3
console.log 'Aborted: arguments are too short.'
return
else
child_process = require('child_process')
child_process.exec "ps", (err,stdout,stderr)->
if stdout.indexOf("coffee jadewatch.coffee") < 0
@craftgear
craftgear / mongoose_paginate.coffee
Created December 28, 2011 02:44
Mongoose Pagination
# This snippet is inspired by edwardhotchkiss's mongoose-paginate (https://github.com/edwardhotchkiss/mongoose-paginate)
# and works with any methods like where, desc, poulate, etc.
#
# paginate method must be called at the end of method chains.
#
# paginate = require 'paginate'
# model
# .find()
# .desc("_id")
# .populate("some_field")
@craftgear
craftgear / gist:1324415
Created October 29, 2011 12:53
Express + node-formidable code sample
formidable = require 'formidable'
app.configure ()->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.use (req, res, next)->
contentType = req.headers['content-type']
if contentType? && contentType.match /multipart\/form-data/
form = new formidable.IncomingForm()
form.uploadDir = __dirname + '/temp'