Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date bug #225

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-gifts-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-autocast': patch
---

Fix for date casting
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions plugins/autocast/src/autocast.plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
castNumber,
FALSY_VALUES,
TRUTHY_VALUES,
castBoolean,
castDate,
TRUTHY_VALUES,
FALSY_VALUES,
castNumber,
} from './autocast.plugin'

describe('autocast plugin', () => {
Expand Down Expand Up @@ -34,11 +34,14 @@ describe('autocast plugin', () => {
])('should return a date', (date) => {
expect(castDate(date)).toBe('Wed, 16 Aug 2023 00:00:00 GMT')
})
describe.each([castNumber('foo'), castBoolean('foo'), castDate('foo')])(
'is uncastable; should return the original value',
(castFn) => {
expect(castFn).toBe('foo')
}
)
describe.each([
[castNumber, 'number'],
[castBoolean, 'boolean'],
[castDate, 'date'],
])('is uncastable; should throw error', (castFn, type) => {
expect(() => castFn('foo')).toThrowError(
`Failed to cast 'foo' to '${type}'`
)
})
})
})
26 changes: 15 additions & 11 deletions plugins/autocast/src/autocast.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import api from '@flatfile/api'
import { FlatfileListener, FlatfileEvent } from '@flatfile/listener'
import { BulkRecordHook } from '@flatfile/plugin-record-hook'
import { FlatfileRecord, TPrimitive } from '@flatfile/hooks'
import { FlatfileEvent, FlatfileListener } from '@flatfile/listener'
import { BulkRecordHook } from '@flatfile/plugin-record-hook'
import { logInfo } from '@flatfile/util-common'

export function autocast(
Expand Down Expand Up @@ -42,12 +42,12 @@ export function autocast(
caster &&
typeof originalValue !== field.type
) {
record.computeIfPresent(field.key, caster)

if (originalValue === record.get(field.key)) {
try {
record.computeIfPresent(field.key, caster)
} catch (e) {
record.addError(
field.key,
`Failed to cast '${originalValue}' to '${field.type}'`
e.message || 'Failed to cast value'
)
}
}
Expand All @@ -70,7 +70,9 @@ const CASTING_FUNCTIONS: {
}

export function castNumber(value: TPrimitive): TPrimitive {
if (typeof value === 'string') {
if (typeof value === 'number') {
return value
} else if (typeof value === 'string') {
const strippedValue = value.replace(/,/g, '')
if (!isNaN(Number(strippedValue))) {
const num = Number(strippedValue)
Expand All @@ -79,13 +81,15 @@ export function castNumber(value: TPrimitive): TPrimitive {
}
}
}
return value
throw new Error(`Failed to cast '${value}' to 'number'`)
}

export const TRUTHY_VALUES = ['1', 'yes', 'true', 'on', 't', 'y', 1]
export const FALSY_VALUES = ['-1', '0', 'no', 'false', 'off', 'f', 'n', 0, -1]
export function castBoolean(value: TPrimitive): TPrimitive {
if (typeof value === 'string' || typeof value === 'number') {
if (typeof value === 'boolean') {
return value
} else if (typeof value === 'string' || typeof value === 'number') {
if (value === '') {
return null
}
Expand All @@ -97,7 +101,7 @@ export function castBoolean(value: TPrimitive): TPrimitive {
return false
}
}
return value
throw new Error(`Failed to cast '${value}' to 'boolean'`)
}

export function castDate(value: TPrimitive): TPrimitive {
Expand All @@ -107,5 +111,5 @@ export function castDate(value: TPrimitive): TPrimitive {
return date.toUTCString()
}
}
return value
throw new Error(`Failed to cast '${value}' to 'date'`)
}