Skip to content

Instantly share code, notes, and snippets.

View futurist's full-sized avatar

James Yang futurist

  • China
View GitHub Profile
@futurist
futurist / tidyrc
Created June 27, 2016 02:10
tidyrc for Emacs HTML tidy.el, put in ~/.tidyrc
coerce-endtags: no
gnu-emacs: yes
@futurist
futurist / github-week-stats.js
Created June 28, 2016 06:48
get total count lines for repo during 1 week
//replace jquery/jquery with the repo you're interested in
fetch('https://api.github.com/repos/jquery/jquery/stats/contributors')
.then(response => response.json())
.then(contributors => contributors
.map(contributor => contributor.weeks
.reduce((lineCount, week) => lineCount + week.a - week.d, 0)))
.then(lineCounts => lineCounts.reduce((lineTotal, lineCount) => lineTotal + lineCount))
.then(lines => window.alert(lines));
@futurist
futurist / policyAllows.js
Last active June 30, 2016 01:00
mithril auth router
function policyAllows(policies = [], item) {
for (var i = 0; i < policies.length; i++) {
if (!policies[i](item)) {
return false;
}
}
return true;
}
@futurist
futurist / functional.js
Last active June 30, 2016 01:37
Functional JS
// Class hack to get a reliable type description in JS
var type = v => ({}).toString.call(v).slice(8,-1)
// is a value a given native JS type (ignores inheritance etc)
var is = t => v => type(v) == t
// use the result of the 2nd function if the 1st fails
var either = (f1,f2) => v => f1(v) || f2(v)
// detect if a value is a POJO or an array
@futurist
futurist / cors.js
Created July 2, 2016 14:14 — forked from balupton/cors.js
Acheiving CORS via a Node HTTP Server
// Create our server
var server;
server = http.createServer(function(req,res){
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
if ( req.method === 'OPTIONS' ) {
res.writeHead(200);
@futurist
futurist / combinePath.js
Created July 5, 2016 05:54
split and combine each array item
// split and combine each array item
var combinePath = function(path, cb) {
return path.reduce(function(pre, cur){
return pre.split(',').map(function(p) {
return !p ? cur : cur.split(',').map(function(v){
return cb(p, v)
}).join(',')
}).join(',')
@futurist
futurist / .gitignore
Created August 28, 2016 06:15
global .gitignore
*.log
# vim files
*.swp
# emacs files
\#*
.#*
.~*
~*
// map key/value into newValue, each array elem holds: [key, value, newValue]
function cssobj_plugin_map (option) {
option = option || {}
var replaceMap = option.maps || [
[
'display',
'flex',
[
@futurist
futurist / cssobj_intro_flexbox.js
Created October 17, 2016 12:46
cssobj_intro_flexbox.js
// cssobj intro flexbox
function displayFlex() {
return {
display: [
'-webkit-box',
'-webkit-flex',
'-moz-box',
'-ms-flexbox',
'flex'
@futurist
futurist / change_case.el
Created October 22, 2016 07:23 — forked from dbrady/change_case.el
emacs lisp defuns to change the case (camelCase <-> snake_case) of the word under cursor, or an entire region if selected. NOTE: the snake-case defuns do not work, they just camelCase. Ooops.
;; Grateful thanks are given to Brian Marick (@marick) for helping me
;; write these. I got the original idea while reading through
;; http://xahlee.org/emacs/elisp_idioms.html, but couldn't work out
;; the elisp regexes. Brian Marick (@marick) stepped in and helped. He
;; took my tortured and broken camelcase-region and turned it into
;; something that actually worked. I then created
;; camelcase-word-or-region. I then wrote the snakecase versions but I
;; see now that all I did was copy the camelcase defuns over and,
;; meaning to go back and finish them Real Soon Now, forgot all about
;; them. I've had a quick look (2011-02-27) but elisp regexes are