@oxide/turnstile.ts 0.7.0-rc.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util.ts CHANGED
@@ -28,7 +28,7 @@ export const isObjectOrArray = (o: unknown) =>
28
28
  */
29
29
  export const mapObj = (
30
30
  kf: (k: string) => string,
31
- vf: (k: string | undefined, v: unknown) => unknown = (k, v) => v,
31
+ vf: (k: string | undefined, v: unknown) => unknown = (_, v) => v,
32
32
  ) =>
33
33
  (o: unknown): unknown => {
34
34
  if (!isObjectOrArray(o)) { return o }
@@ -42,8 +42,18 @@ export const mapObj = (
42
42
  return newObj
43
43
  }
44
44
 
45
+ const isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/
46
+
45
47
  export const parseIfDate = (k: string | undefined, v: unknown) => {
46
- if (typeof v === 'string' && (k?.startsWith('time_') || k === 'timestamp' || k?.endsWith('_at') || k?.endsWith('_time'))) {
48
+ if (
49
+ typeof v === 'string'
50
+ && isoDateRegex.test(v)
51
+ && (k?.startsWith('time_')
52
+ || k?.endsWith('_time')
53
+ || k?.endsWith('_expiration')
54
+ || k?.endsWith('_at')
55
+ || k === 'timestamp')
56
+ ) {
47
57
  const d = new Date(v)
48
58
  if (isNaN(d.getTime())) { return v }
49
59
  return d