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.

30 lines
643 B

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