diff --git a/implementations/javascript/src/text.ts b/implementations/javascript/src/text.ts index 199b781..12379a6 100644 --- a/implementations/javascript/src/text.ts +++ b/implementations/javascript/src/text.ts @@ -20,3 +20,31 @@ export function preserves(pieces: TemplateStringsArray, ...values: Value[]) }); return result.join(''); } + + +declare global { + interface Object { asPreservesText(): string; } +} + +Object.defineProperty(Object.prototype, 'asPreservesText', { + enumerable: false, + writable: true, + value: function(): string { return '#!' + stringify(this); } +}); + +Boolean.prototype.asPreservesText = function (): string { + return this ? '#t' : '#f'; +}; + +Number.prototype.asPreservesText = function (): string { + return '' + this; +}; + +String.prototype.asPreservesText = function (): string { + return JSON.stringify(this); +}; + +Symbol.prototype.asPreservesText = function (): string { + // TODO: escaping + return this.description ?? '||'; +}; diff --git a/implementations/javascript/src/values.ts b/implementations/javascript/src/values.ts index 6efabbc..a3ee556 100644 --- a/implementations/javascript/src/values.ts +++ b/implementations/javascript/src/values.ts @@ -30,30 +30,3 @@ export type Compound = | Array> | Set | Dictionary, T>; - -declare global { - interface Object { asPreservesText(): string; } -} - -Object.defineProperty(Object.prototype, 'asPreservesText', { - enumerable: false, - writable: true, - value: function(): string { return '#!' + JSON.stringify(this); } -}); - -Boolean.prototype.asPreservesText = function (): string { - return this ? '#t' : '#f'; -}; - -Number.prototype.asPreservesText = function (): string { - return '' + this; -}; - -String.prototype.asPreservesText = function (): string { - return JSON.stringify(this); -}; - -Symbol.prototype.asPreservesText = function (): string { - // TODO: escaping - return this.description ?? '||'; -};