Skip to content

Instantly share code, notes, and snippets.

@nexpr
nexpr / agen-with-promise.js
Last active February 23, 2016 14:09
use generator like async/await
/* library function */
function async(args, gen){
return new Promise((resolve, reject) => {
var ite = gen(...args)
!function recur(pre_value){
var ret = ite.next(pre_value)
ret.done ? resolve(ret.value) : ret.value.then(recur)
}()
})
}
@nexpr
nexpr / uniqueSelector.js
Last active February 14, 2016 02:17
DOMでユニークアクセスできるセレクタ作る
Object.defineProperty(Element.prototype, "index", {
get: function(){
return this.parentElement ?
[].indexOf.call(this.parentElement.children, this)
:
-1
}
})
function createUniqueSelector(elem){
@nexpr
nexpr / sarope.js
Created February 18, 2016 15:43
calc sarogate pair
function sarope(x, y){
if(arguments.length === 1){
var u = x - 0x10000
var upper = ~~(u / 0x400) + 0xd800
var lower = ~~(u % 0x400) + 0xdc00
return {upper, lower}
}else if(arguments.length === 2){
return 0x10000 + (x - 0xd800) * 0x400 + (y - 0xdc00)
}
}
@nexpr
nexpr / monitorOverwriteInnerHTML.js
Created February 23, 2016 13:58
デバッグ用 innerHTML 監視
function monitorOverwriteInnerHTML(elem){
var desc = Object.getOwnPropertyDescriptor(Element.prototype, "innerHTML")
Object.defineProperty(elem, "innerHTML", {
get: function(){
return desc.get.call(this)
},
set: function(x){
console.log(this, x)
desc.set.call(this, x)
}
@nexpr
nexpr / extends-getter-setter-sample.js
Last active February 23, 2016 14:00
class 版の get set の継承
var v = new class B extends class A {
get test(){
console.log("parent getter")
return this.xxx
}
set test(x){
console.log("parent setter")
this.xxx = x
}
} {
@nexpr
nexpr / agen-sample.js
Created March 27, 2016 08:51
async/await by generator
function wait(msec){
return new Promise((resolve, reject) => {
setTimeout(resolve, msec)
})
}
function* async_sample(val){
yield wait(1000)
console.log("after 1second")
console.log(val)
@nexpr
nexpr / StateSave.js
Created April 9, 2016 15:39
木構造でなにか保存する用
function State(data){
this.prev = null
this.next = []
this.data = data
this.active = 0
}
function StateSave(data){
if(data instanceof State){
this.current = data
@nexpr
nexpr / pxpchain.js
Created June 15, 2016 15:34
proxy で undefined でもチェーンさせる
function l(val){
var fn = () => {}
fn.val = val
return new Proxy(fn, {
get: getTrap,
apply: function(target, _this, args){
if(args.length === 0){
return target.val
}
var [apply_fn, ...apply_args] = args
@nexpr
nexpr / fnhelp.js
Created July 2, 2016 13:42
function arguments help
!function(helps){
helps.forEach(e => {
var target = e.target || (typeof e.path === "function" ? e.path() : pathEval(e.path))
if(typeof target !== "function"){
console.error("target not found", e)
}
target.toString = () => e.help.map(arg_str => {
var str = Function.prototype.toString.call(target)
var [_, inner] = str.match(/^[^\{]+(\{.+\})$/) || []
@nexpr
nexpr / fnhelp-v2.js
Last active July 7, 2016 16:31
引数ヘルプ
function attachHelps(helps){
Object.keys(helps).forEach(name => {
var target = window[name]
if(typeof target !== "function"){
throw new Error(`function ${name} not found`)
}
target.toString = () => helps[name].map(arg_str => {
var str = Function.prototype.toString.call(target)