Skip to content

Instantly share code, notes, and snippets.

View xhinking's full-sized avatar

Wang xhinking

  • HangZhou
  • 20:56 (UTC +08:00)
View GitHub Profile
@xhinking
xhinking / style.css
Last active November 13, 2017 15:48
Pinboard style
body {
margin: 0;
font-family: Helvetica,sans-serif;
-webkit-font-smoothing: subpixel-antialiased;
}
#content {
margin: 20px auto;
}
@xhinking
xhinking / cachedFetch.js
Created July 20, 2017 02:24
Cache Fetched AJAX Requests Locally
// source: https://www.sitepoint.com/cache-fetched-ajax-requests/
const cachedFetch = (url, options) => {
let expiry = 5 * 60 // 5 min default
if (typeof options === 'number') {
expiry = options
options = undefined
} else if (typeof options === 'object') {
// I hope you didn't set it to 0 seconds
expiry = options.seconds || expiry
@xhinking
xhinking / hashstr.js
Created July 20, 2017 02:23
Java’s String.hashCode()
const hashstr = s => {
let hash = 0;
if (s.length == 0) return hash;
for (let i = 0; i < s.length; i++) {
let char = s.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
@xhinking
xhinking / db.js
Last active July 12, 2017 02:16
Dexie addons
import Dexie from 'dexie';
class MyDerivedDexie extends Dexie {
anotherProperty;
anotherMethod() {
// Do something here...
}
constructor (name, options) {
git archive --format=zip --remote=git@xxx.git master filepath > zip_name.zip
@xhinking
xhinking / Dockerfile
Created January 20, 2017 09:29
Node Dockerfile example
FROM node:7.4
ENV HEY_HOME /hey-node
RUN mkdir ${HEY_HOME}/
WORKDIR $HEY_HOME
COPY package.json ${HEY_HOME}/
RUN npm install
function asyncify(fn) {
var orig_fn = fn,
intv = setTimeout(function() {
intv = null
if (fn) fn()
}, 0)
fn = null
return function() {
function timeoutify(fn, delay) {
var intv = setTimeout(function() {
intv = null
fn(new Error("Timeout!"))
}, delay)
return function() {
if (intv) {
clearTimeout(intv)
fn.apply(this, arguments)
Array.prototype.where =
function(f)
{
var fn = f ;
// if type of parameter is string
if ( typeof f == "string" )
// try to make it into a function
if ( ( fn = lambda( fn ) ) == null )
// if fail, throw exception
throw "Syntax error in lambda string: " + f ;
function lambda( l ) {
var fn = l.match(/\((.*)\)\s*=>\s*(.*)/) ;
var p = [] ;
var b = "" ;
if ( fn.length > 0 ) fn.shift() ;
if ( fn.length > 0 ) b = fn.pop() ;
if ( fn.length > 0 ) p = fn.pop()
.replace(/^\s*|\s(?=\s)|\s*$|,/g, '').split(' ') ;