preserves/implementations/javascript/packages/core/src/text.ts

51 lines
1.2 KiB
TypeScript
Raw Normal View History

import { Value } from './values';
2021-01-09 15:21:25 +00:00
export function stringify(x: any): string {
2021-01-11 15:54:52 +00:00
if (typeof x?.asPreservesText === 'function') {
return x.asPreservesText();
} else {
2021-01-09 15:21:25 +00:00
try {
return JSON.stringify(x);
} catch (_e) {
return ('' + x).asPreservesText();
}
}
}
2021-01-29 11:03:28 +00:00
export function preserves(pieces: TemplateStringsArray, ...values: Value<any>[]): string {
const result = [pieces[0]];
values.forEach((v, i) => {
result.push(stringify(v));
result.push(pieces[i + 1]);
});
return result.join('');
}
2021-03-04 21:42:40 +00:00
declare global {
interface Object { asPreservesText(): string; }
}
Object.defineProperty(Object.prototype, 'asPreservesText', {
enumerable: false,
writable: true,
value: function(): string { return '#!' + JSON.stringify(this); }
2021-03-04 21:42:40 +00:00
});
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 ?? '||';
};