Skip to content

Commit

Permalink
fix(postgres): fix processing money type (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Feb 8, 2017
1 parent 39cff1d commit 2f25ce8
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/postgres/inventory/type/scalar/pgFloatType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { AdapterType, floatType } from '../../../../interface'
import { sql } from '../../../utils'
import PgType from '../PgType'

// In the absence of /\p{Sc}/ to match all currency symbols, we get the
// following regexp from http://stackoverflow.com/a/27175364/141284
const CURRENY_SYMBOL_REGEXP =
/^[\$\xA2-\xA5\u058F\u060B\u09F2\u09F3\u09FB\u0AF1\u0BF9\u0E3F\u17DB\u20A0-\u20BD\uA838\uFDFC\uFE69\uFF04\uFFE0\uFFE1\uFFE5\uFFE6]/

const pgFloatType: PgType<number> & AdapterType<number> = {
kind: 'ADAPTER',
baseType: floatType,
Expand All @@ -11,7 +16,7 @@ const pgFloatType: PgType<number> & AdapterType<number> = {
if (typeof value === 'string') {
// If this number represents money, it has some extra trimmings that
// need to be fixed.
if (value.startsWith('$'))
if (CURRENY_SYMBOL_REGEXP.test(value))
return parseFloat(value.slice(1).replace(',', ''))

return parseFloat(value)
Expand Down

0 comments on commit 2f25ce8

Please sign in to comment.