Skip to content

Instantly share code, notes, and snippets.

@nexpr
nexpr / 0_reuse_code.js
Created December 4, 2015 12:37
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@nexpr
nexpr / ILS.js
Last active December 19, 2015 07:40
localStorage風indexedDB
function ILS(store_name, db_name){
var self = this
store_name = store_name || "default"
db_name = db_name || "ILS"
Object.defineProperties(this, {
"store_name": {
value: store_name,
writable: false
},
"db_name": {
@nexpr
nexpr / AFunc.js
Created December 17, 2015 16:53
非同期あり関数を見分けれるようにする
// こんなのを用意しておく
function AFunc(fn){
if(typeof fn !== "function") throw new TypeError();
fn.__proto__ = AFunc.prototype
return fn
}
AFunc.prototype.__proto__ = Function.prototype
// こういうことができるようになる
var actions = [
function(next){
setTimeout(function(){
console.log(10)
next()
}, 1000)
console.log(1)
},
function(next){
setTimeout(function(){
@nexpr
nexpr / template.js
Created December 19, 2015 12:21
`` で使いまわせるテンプレート
Array.prototype.alternate = function(...arrs){
var res = []
arrs.unshift(this)
for(var i=0, precount=-1;precount!==(precount=res.length);i++){
for(var j=0;j<arrs.length;j++){
var arr = arrs[j]
i < arr.length && res.push(arr[i])
}
}
return res
@nexpr
nexpr / EvE.php
Last active February 7, 2016 09:29
phjs >=7.0.0
<?php
require "lib.php";
class EvE {
static $eve;
private $evs = [];
function on(...$args){
overload($args, [
[
"type" => ["string", "function", "int"],
@nexpr
nexpr / promise.js
Last active January 16, 2016 12:32
自作promiseそれなりには動くはず
function promise(fn){
var self = {
status: "pending",
value: undefined,
nexts: [],
__proto__: promise.prototype
}
function common(value, status){
if(self.status !== "pending") return
@nexpr
nexpr / overload.js
Created January 16, 2016 16:57
オーバーロードする
function overload(args, fns){
var result = null
fns.some(fn => {
if(Array.isArray(fn)){
fn = {type: fn[0], function: fn[1]}
}
if(fn.type == null || fn.type.every((reqtype, i) => typeMatch(reqtype, args[i]))){
result = fn.function(...args)
return true
}
@nexpr
nexpr / Map.php
Created February 3, 2016 12:27
配列じゃない方のMap
<?php
class Map{
private $keys = [];
private $values = [];
function set($k, $v){
$index = $this->getIndex($k);
if($index === false){
$this->keys[] = $k;
$this->values[] = $v;
@nexpr
nexpr / overload-typeA-usage.php
Created February 7, 2016 11:44
php overload for method
<?php
class test{
use Overload;
function test__num($a){
return $a * $a;
}
function test__num2($a, $b){
return $a + $b;