Skip to content

Instantly share code, notes, and snippets.

View bingzer's full-sized avatar

Ricky Tobing bingzer

View GitHub Profile
@bingzer
bingzer / stringify.js
Created February 21, 2021 01:27 — forked from cowboy/stringify.js
JavaScript: like JSON.stringify but handles functions, good for creating arbitrary .js objects?
var stringify = function(obj, prop) {
var placeholder = '____PLACEHOLDER____';
var fns = [];
var json = JSON.stringify(obj, function(key, value) {
if (typeof value === 'function') {
fns.push(value);
return placeholder;
}
return value;
}, 2);
@bingzer
bingzer / evaluate.js
Created February 15, 2021 23:47 — forked from softwarespot/evaluate.js
Pass context to eval()
function evaluate(code, args = {}) {
// Call is used to define where "this" within the evaluated code should reference.
// eval does not accept the likes of eval.call(...) or eval.apply(...) and cannot
// be an arrow function
return function evaluateEval() {
// Create an args definition list e.g. "arg1 = this.arg1, arg2 = this.arg2"
const argsStr = Object.keys(args)
.map(key => `${key} = this.${key}`)
.join(',');
const argsDef = argsStr ? `let ${argsStr};` : '';