cursor.tsx 712 B

1234567891011121314151617181920212223242526272829303132333435
  1. export type CursorInfo = {
  2. isPrev: boolean;
  3. offset: number;
  4. value: number;
  5. };
  6. /// Converts a cursor string into a Cursor object.
  7. export function parseCursor(
  8. cursor: string | string[] | undefined | null
  9. ): CursorInfo | undefined {
  10. if (!cursor) {
  11. return undefined;
  12. }
  13. if (Array.isArray(cursor)) {
  14. if (cursor.length > 0) {
  15. cursor = cursor[0];
  16. } else {
  17. return undefined;
  18. }
  19. }
  20. const bits = cursor.split(':');
  21. if (bits.length !== 3) {
  22. return undefined;
  23. }
  24. try {
  25. const value = parseInt(bits[0], 10);
  26. const offset = parseInt(bits[1], 10);
  27. const isPrev = bits[2] === '1';
  28. return {isPrev, offset, value};
  29. } catch (e) {
  30. return undefined;
  31. }
  32. }