-
-
Save Tauka/7bed06bdaa9126718c73f2a3fa14ef5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Mutilator(data, name, context) { | |
this.n = name || `mutilation-${+new Date()}`; | |
this.d = data; | |
this.c = context || window; | |
this.isArr = function(p) { | |
return this.d[p].constructor == Array; | |
}; | |
this.dispatch = function(p, v, t) { | |
this.c.dispatchEvent( | |
new CustomEvent(this.n, { | |
detail: { | |
prop: p, | |
val: v, | |
type: t | |
} | |
}) | |
); | |
}; | |
} | |
Mutilator.prototype = { | |
change: function(p, v) { | |
this.d[p] = v; | |
this.dispatch(p, v, "change"); | |
}, | |
add: function(p, v) { | |
this.isArr(p) ? this.d[p].push(v) : (this.d[p] = [this.d[p], v]); | |
this.dispatch(p, v, "add"); | |
}, | |
remove: function(p, v) { | |
!this.isArr(p) ? delete this.d[p] : this.d[p].splice(v, 1); | |
this.dispatch(p, p[v], "remove"); | |
}, | |
recoil: function(f, r) { | |
this.c.addEventListener(this.n, e => { | |
if (!r || r.some(p => e.detail.prop.indexOf(p) > -1)) { | |
return f(e, this.d); | |
} | |
}); | |
} | |
}; | |
export default Mutilator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment