Skip to content

Instantly share code, notes, and snippets.

View williamcspace's full-sized avatar

William Chan williamcspace

  • Shanghai Xinyue Information Technology Ltd.
  • Shanghai
View GitHub Profile
@williamcspace
williamcspace / merge-arrays-functional.js
Created August 22, 2016 07:05
imperative vs functional in js
var merge2dArrayIntoOne = function(arrays){
return arrays.reduce(function(p, n){
return p.concat(n);
});
};
> db.products.findOne()
{
name : 'left-handed smoke shifter',
manufacturer : 'Acme Corp',
catalog_number: 1234,
parts : [ // array of references to Part documents
ObjectID('AAAA'), // reference to the #4 grommet above
ObjectID('F17C'), // reference to a different Part
ObjectID('D2AA'),
// etc
function reflow(elt){
console.log(elt.offsetHeight);
}
// default exports
export default 42;
export default {};
export default [];
export default foo;
export default function () {}
export default class {}
export default function foo () {}
export default class foo {}
@williamcspace
williamcspace / express.js
Created August 13, 2016 14:02
Barebone express app
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
@williamcspace
williamcspace / jquery.component.js
Created August 12, 2016 20:25
use jquery within ng2
import {Component, ElementRef, OnInit} from 'angular2/core';
declare var jQuery: any;
@Component({
selector: 'my-jquery',
template: `
<button>Click Me</button>
`
})
@williamcspace
williamcspace / combineReducer.js
Last active August 26, 2016 05:47
simple redux concept
const combineReducers = (reducers) => {
return (state = {}, action) => {
return Object.keys(reducers).reduce(
(nectState key) => {
nextState[key] = reducers[key](
state[key],
action
);
return nextState;
}
var fs = require('fs')
var Q = require('q')
var fs_stat = Q.denodeify(fs.stat)
var fs_readdir = Q.denodeify(fs.readdir)
var files = [
'./fixtures/file1',
'./fixtures/file2',
'./fixtures/file3',
'./fixtures/file4'
__d("Chromedome", ["fbt"], function(a, b, c, d, e, f, g) {
f.start = function(h) {
if (h.off || top !== window ||!/(^|\.)facebook\.com$/.test(document.domain))
return;
var i = h.stop || "Stop!", j = h.text || "This is a browser feature intended for developers. If someone told you to copy-paste something here to enable a Facebook feature or \"hack\" someone's account, it is a scam and will give them access to your Facebook account.", k = h.more || g._("For more information, see {url}.", [g.param("url", 'https://www.facebook.com/selfxss')]);
if ((window.chrome || window.safari)&&!h.textonly) {
var l = 'font-family:helvetica; font-size:20px; ';
[[i, h.c1 || l + 'font-size:50px; font-weight:bold; ' + 'color:red; -webkit-text-stroke:1px black;'], [j, h.c2 || l], [k, h.c3 || l], ['', '']].map(function(r) {
setTimeout(console.log.bind(console, '\n%c' + r[0], r[1]));
});
@williamcspace
williamcspace / sticky-socket.js
Created May 20, 2016 11:22
sticky session socket with nodejs cluster
var express = require('express'),
cluster = require('cluster'),
net = require('net'),
sio = require('socket.io'),
sio_redis = require('socket.io-redis');
var port = 3000,
num_processes = require('os').cpus().length;
if (cluster.isMaster) {