Skip to content

Commit

Permalink
Add hack to support getters and setters in objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Tarvas committed Oct 8, 2019
1 parent 707e6ba commit f1c0c7b
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/object.ts
Expand Up @@ -2,6 +2,22 @@ import { Next } from "./types";
import { quoteKey } from "./quote";
import { FunctionParser } from "./function";

function callableToString(
callable: Function,
indent: string,
next: Next,
key?: string
) {
const parser = new FunctionParser(callable, indent, next, key);
let result = parser.stringify();
if (!key) {
// Remove 'function ' prefix from getters and setters
// TODO: Handle in FunctionParser instead?
result = result.replace(/^function /, "");
}
return indent + result.split("\n").join(`\n${indent}`);
}

/**
* Stringify an object of keys and values.
*/
Expand All @@ -13,9 +29,19 @@ export function objectToString(obj: any, indent: string, next: Next) {
.reduce(
function(values, key) {
if (typeof obj[key] === "function") {
const parser = new FunctionParser(obj[key], indent, next, key);
const result = parser.stringify();
values.push(indent + result.split("\n").join(`\n${indent}`));
values.push(callableToString(obj[key], indent, next, key));
return values;
}

// Handle getters & setters
const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor && (descriptor.get || descriptor.set)) {
if (descriptor.get) {
values.push(callableToString(descriptor.get, indent, next));
}
if (descriptor.set) {
values.push(callableToString(descriptor.set, indent, next));
}
return values;
}

Expand Down

0 comments on commit f1c0c7b

Please sign in to comment.