json.ts 490 B

1234567891011121314151617
  1. import * as O from "fp-ts/Option"
  2. import { flow } from "fp-ts/function"
  3. /**
  4. * Checks and Parses JSON string
  5. * @param str Raw JSON data to be parsed
  6. * @returns Option type with some(JSON data) or none
  7. */
  8. export const safeParseJSON = (str: string): O.Option<object> =>
  9. O.tryCatch(() => JSON.parse(str))
  10. /**
  11. * Checks if given string is a JSON string
  12. * @param str Raw string to be checked
  13. * @returns If string is a JSON string
  14. */
  15. export const isJSON = flow(safeParseJSON, O.isSome)