Exploring Effect in Codebase: Small Change Showcase

I've just started to mess around with Effect in our codebase.
I did a small change only for now to showcase what we could achieve with it.

This was the original code:
export function createLocalStorageDeserializer<T extends z.ZodTypeAny>(
  schema: T,
  key: string,
  defaultValue: z.infer<T>,
) {
  function deserialize(value: string): z.infer<T> {
    try {
      const parsedValue = JSON.parse(value)

      const validatedValue = schema.safeParse(parsedValue)

      if (!validatedValue.success) {
        // eslint-disable-next-line no-console
        console.error(`Something went wrong during data deserialization for key: ${key}`)
        // eslint-disable-next-line no-console
        console.error(validatedValue.error)

        return typeof defaultValue === 'function' ? defaultValue() : defaultValue
      }

      return validatedValue.data
    } catch (error) {
      // eslint-disable-next-line no-console
      console.error(`Something went wrong during data parsing for key: ${key}`)
      // eslint-disable-next-line no-console
      console.error(error)

      return typeof defaultValue === 'function' ? defaultValue() : defaultValue
    }
  }

  return deserialize
}
Was this page helpful?