Это сложно, но так как вы делаете это на Node.js, вам не нужно беспокоиться о капризах различных движков JavaScript, что хорошо.
Вам нужно будет использовать недавно-стандартизированный Function.prototype.toString
.Ваш baz
является методом , поэтому toString
возвращает определение метода для него, но другие функции могут проходить как объявления функций, выражения функций, функции стрелок и т. Д.
Этоследует начать:
const strs = [];
for (const [name, value] of Object.entries(foo)) {
if (typeof value === "function") {
const fstr = value.toString().trim();
if (fstr.startsWith("function") || fstr[0] === "(") {
strs.push(`${name}: ${fstr}`);
} else {
strs.push(fstr); // probably a method
}
} else {
strs.push(`${name}: ${JSON.stringify(value)}`);
}
}
const sep = "\n ";
const str = `exports.config = {${sep}${strs.join(`,${sep}`)}\n};`;
Live Example (если вы не используете браузер с V8 - например, Chrome, Chromium, Brave - тогда это может не сработать):
const foo = {
bar: "bar value",
biz: function() {
// This is a function assigned to a property
return this.bar;
},
buz: function() {
// This is an arrow function assigned to a property
// VERY surprising that this comes out as a traditional function
return this.bar.toUpperCase();
},
baz() {
// This is a method
console.log(this.bar);
},
};
const strs = [];
for (const [name, value] of Object.entries(foo)) {
if (typeof value === "function") {
const fstr = value.toString().trim();
if (fstr.startsWith("function") || fstr[0] === "(") {
strs.push(`${name}: ${fstr}`);
} else {
strs.push(fstr); // probably a method
}
} else {
strs.push(`${name}: ${JSON.stringify(value)}`);
}
}
const sep = "\n ";
const str = `exports.config = {${sep}${strs.join(`,${sep}`)}\n};`;
console.log(str);
.as-console-wrapper {
max-height: 100% !important;
}
Очевидно, что здесь есть много возможностей для совершенствования (например, что если в объекте есть функция, назначенная одному из свойств foo
)?), он просто предназначен для того, чтобы дать вам отправную точку.