HATEOAS-first backend framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728
  1. export const TEXT_SERIALIZER_PAIR = {
  2. serialize(obj: unknown, level = 0): string {
  3. if (Array.isArray(obj)) {
  4. return obj.map((o) => this.serialize(o)).join('\n\n');
  5. }
  6. if (typeof obj === 'object') {
  7. if (obj !== null) {
  8. return Object.entries(obj)
  9. .map(([key, value]) => `${Array(level * 2).fill(' ').join('')}${key}: ${this.serialize(value, level + 1)}`)
  10. .join('\n');
  11. }
  12. return '';
  13. }
  14. if (typeof obj === 'number' && Number.isFinite(obj)) {
  15. return obj.toString();
  16. }
  17. if (typeof obj === 'string') {
  18. return obj;
  19. }
  20. return '';
  21. },
  22. deserialize: <T>(str: string) => str as T
  23. };