Skip to content

Commit

Permalink
fix csv export: escape double quotes inside a field - eg. path_paymen…
Browse files Browse the repository at this point in the history
…t.path contains a JSON object
  • Loading branch information
chatch committed Dec 23, 2018
1 parent 45e071b commit 95311b7
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 80 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
'<rootDir>/src/**/__tests__/**/*.js?(x)',
'<rootDir>/src/**/?(*.)(spec|test).js?(x)',
],
testPathIgnorePatterns: ['/__data__/'],
testEnvironment: 'node',
testURL: 'http://localhost',
transform: {
Expand Down
4 changes: 2 additions & 2 deletions src/components/layout/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class Header extends React.Component {
/>
</Navbar.Form>
<Nav>
<LinkContainer to="/graphs">
{/* <LinkContainer to="/graphs">
<NavItem>Graphs (beta)</NavItem>
</LinkContainer>
</LinkContainer> */}
<LinkContainer to="/operations">
<NavItem>
<FormattedMessage id="operations" />
Expand Down
1 change: 0 additions & 1 deletion src/components/operations/PathPayment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const PathPayment = props => {
type={props.sourceAssetType}
/>
)
console.log(sourceAsset)
return (
<Payment {...props}>
<FormattedMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,67 @@ exports[`decodes ordinary string values 1`] = `
Array [
<span>
<FormattedMessage
id="operation.manage.data.set"
values={
Object {
"name": "lang",
}
id="operation.manage.data.set"
values={
Object {
"name": "lang",
}
}
/>
<FormattedMessage
id="operation.manage.data.set.to"
values={
Object {
"value": "indonesian",
}
id="operation.manage.data.set.to"
values={
Object {
"value": "indonesian",
}
}
/>
</span>,
</span>,
]
`;

exports[`decodes utf8 string values 1`] = `
Array [
<span>
<FormattedMessage
id="operation.manage.data.set"
values={
Object {
"name": "utf8value",
}
id="operation.manage.data.set"
values={
Object {
"name": "utf8value",
}
}
/>
<FormattedMessage
id="operation.manage.data.set.to"
values={
Object {
"value": "马马虎虎",
}
id="operation.manage.data.set.to"
values={
Object {
"value": "马马虎虎",
}
}
/>
</span>,
</span>,
]
`;

exports[`truncates a long key and long value 1`] = `
Array [
<span>
<FormattedMessage
id="operation.manage.data.set"
values={
Object {
"name": "name_some_really_long_12345...",
}
id="operation.manage.data.set"
values={
Object {
"name": "name_some_really_long_12345...",
}
}
/>
<FormattedMessage
id="operation.manage.data.set.to"
values={
Object {
"value": "value_some_really_long_1234...",
}
id="operation.manage.data.set.to"
values={
Object {
"value": "value_some_really_long_1234...",
}
}
/>
</span>,
</span>,
]
`;
31 changes: 31 additions & 0 deletions src/lib/__tests__/__data__/effects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export default [
{
id: '0000774537222303745-0000000002',
account: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
type: 'account_debited',
typeI: 3,
createdAt: '2018-10-15T03:06:14Z',
assetType: 'credit_alphanum4',
assetCode: 'HNY',
assetIssuer: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
amount: '1000000000.0000000',
},
{
id: '0000773746948313089-0000000003',
account: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
type: 'signer_created',
typeI: 10,
createdAt: '2018-10-15T02:50:22Z',
weight: 1,
publicKey: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
key: '',
},
{
id: '0000773746948313089-0000000001',
account: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
type: 'account_created',
typeI: 0,
createdAt: '2018-10-15T02:50:22Z',
startingBalance: '10000.0000000',
},
]
59 changes: 59 additions & 0 deletions src/lib/__tests__/__data__/pathPayments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export default [
{
_links: {
self: {
href: 'https://horizon.stellar.org/operations/92853980303990785',
},
transaction: {
href:
'https://horizon.stellar.org/transactions/35e241e71a4769f94ed51b224c09f2cf9997e48c9b976674e3ccdf756c1a33cd',
},
effects: {
href:
'https://horizon.stellar.org/operations/92853980303990785/effects',
},
succeeds: {
href:
'https://horizon.stellar.org/effects?order=desc\u0026cursor=92853980303990785',
},
precedes: {
href:
'https://horizon.stellar.org/effects?order=asc\u0026cursor=92853980303990785',
},
},
id: '92853980303990785',
paging_token: '92853980303990785',
source_account: 'GALUJVTISLHBRDI3LYYMQFFIAILACUUZKIOLYWNDJEEYWECUQQNYQGOX',
type: 'path_payment',
type_i: 2,
created_at: '2018-12-23T18:34:02Z',
transaction_hash:
'35e241e71a4769f94ed51b224c09f2cf9997e48c9b976674e3ccdf756c1a33cd',
asset_type: 'credit_alphanum4',
asset_code: 'BTC',
asset_issuer: 'GBSTRH4QOTWNSVA6E4HFERETX4ZLSR3CIUBLK7AXYII277PFJC4BBYOG',
from: 'GALUJVTISLHBRDI3LYYMQFFIAILACUUZKIOLYWNDJEEYWECUQQNYQGOX',
to: 'GALUJVTISLHBRDI3LYYMQFFIAILACUUZKIOLYWNDJEEYWECUQQNYQGOX',
amount: '0.0282862',
path: [
{
asset_type: 'credit_alphanum4',
asset_code: 'LTC',
asset_issuer:
'GCSTRLTC73UVXIYPHYTTQUUSDTQU2KQW5VKCE4YCMEHWF44JKDMQAL23',
},
{
asset_type: 'credit_alphanum4',
asset_code: 'USD',
asset_issuer:
'GBSTRUSD7IRX73RQZBL3RQUH6KS3O4NYFY3QCALDLZD77XMZOPWAVTUK',
},
],
source_amount: '0.0282762',
source_max: '0.0282862',
source_asset_type: 'credit_alphanum4',
source_asset_code: 'BTC',
source_asset_issuer:
'GBSTRH4QOTWNSVA6E4HFERETX4ZLSR3CIUBLK7AXYII277PFJC4BBYOG',
},
]
7 changes: 6 additions & 1 deletion src/lib/__tests__/__snapshots__/csv.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`jsonToCSV 1`] = `
exports[`jsonToCSV effects 1`] = `
"\\"id\\",\\"account\\",\\"type\\",\\"typeI\\",\\"createdAt\\",\\"assetType\\",\\"assetCode\\",\\"assetIssuer\\",\\"amount\\",\\"weight\\",\\"publicKey\\",\\"key\\",\\"startingBalance\\"
\\"0000774537222303745-0000000002\\",\\"GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6\\",\\"account_debited\\",\\"3\\",\\"2018-10-15T03:06:14Z\\",\\"credit_alphanum4\\",\\"HNY\\",\\"GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6\\",\\"1000000000.0000000\\",\\"\\",\\"\\",\\"\\",\\"\\"
\\"0000773746948313089-0000000003\\",\\"GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6\\",\\"signer_created\\",\\"10\\",\\"2018-10-15T02:50:22Z\\",\\"\\",\\"\\",\\"\\",\\"\\",\\"1\\",\\"GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6\\",\\"\\",\\"\\"
\\"0000773746948313089-0000000001\\",\\"GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6\\",\\"account_created\\",\\"0\\",\\"2018-10-15T02:50:22Z\\",\\"\\",\\"\\",\\"\\",\\"\\",\\"\\",\\"\\",\\"\\",\\"10000.0000000\\""
`;

exports[`jsonToCSV pathPayments 1`] = `
"\\"_links\\",\\"id\\",\\"paging_token\\",\\"source_account\\",\\"type\\",\\"type_i\\",\\"created_at\\",\\"transaction_hash\\",\\"asset_type\\",\\"asset_code\\",\\"asset_issuer\\",\\"from\\",\\"to\\",\\"amount\\",\\"path\\",\\"source_amount\\",\\"source_max\\",\\"source_asset_type\\",\\"source_asset_code\\",\\"source_asset_issuer\\"
\\"{\\"\\"self\\"\\":{\\"\\"href\\"\\":\\"\\"https://horizon.stellar.org/operations/92853980303990785\\"\\"},\\"\\"transaction\\"\\":{\\"\\"href\\"\\":\\"\\"https://horizon.stellar.org/transactions/35e241e71a4769f94ed51b224c09f2cf9997e48c9b976674e3ccdf756c1a33cd\\"\\"},\\"\\"effects\\"\\":{\\"\\"href\\"\\":\\"\\"https://horizon.stellar.org/operations/92853980303990785/effects\\"\\"},\\"\\"succeeds\\"\\":{\\"\\"href\\"\\":\\"\\"https://horizon.stellar.org/effects?order=desc&cursor=92853980303990785\\"\\"},\\"\\"precedes\\"\\":{\\"\\"href\\"\\":\\"\\"https://horizon.stellar.org/effects?order=asc&cursor=92853980303990785\\"\\"}}\\",\\"92853980303990785\\",\\"92853980303990785\\",\\"GALUJVTISLHBRDI3LYYMQFFIAILACUUZKIOLYWNDJEEYWECUQQNYQGOX\\",\\"path_payment\\",\\"2\\",\\"2018-12-23T18:34:02Z\\",\\"35e241e71a4769f94ed51b224c09f2cf9997e48c9b976674e3ccdf756c1a33cd\\",\\"credit_alphanum4\\",\\"BTC\\",\\"GBSTRH4QOTWNSVA6E4HFERETX4ZLSR3CIUBLK7AXYII277PFJC4BBYOG\\",\\"GALUJVTISLHBRDI3LYYMQFFIAILACUUZKIOLYWNDJEEYWECUQQNYQGOX\\",\\"GALUJVTISLHBRDI3LYYMQFFIAILACUUZKIOLYWNDJEEYWECUQQNYQGOX\\",\\"0.0282862\\",\\"[{\\"\\"asset_type\\"\\":\\"\\"credit_alphanum4\\"\\",\\"\\"asset_code\\"\\":\\"\\"LTC\\"\\",\\"\\"asset_issuer\\"\\":\\"\\"GCSTRLTC73UVXIYPHYTTQUUSDTQU2KQW5VKCE4YCMEHWF44JKDMQAL23\\"\\"},{\\"\\"asset_type\\"\\":\\"\\"credit_alphanum4\\"\\",\\"\\"asset_code\\"\\":\\"\\"USD\\"\\",\\"\\"asset_issuer\\"\\":\\"\\"GBSTRUSD7IRX73RQZBL3RQUH6KS3O4NYFY3QCALDLZD77XMZOPWAVTUK\\"\\"}]\\",\\"0.0282762\\",\\"0.0282862\\",\\"credit_alphanum4\\",\\"BTC\\",\\"GBSTRH4QOTWNSVA6E4HFERETX4ZLSR3CIUBLK7AXYII277PFJC4BBYOG\\""
`;
42 changes: 9 additions & 33 deletions src/lib/__tests__/csv.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
import {jsonToCSV} from '../csv.js'

const effectsRecords = [
{
id: '0000774537222303745-0000000002',
account: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
type: 'account_debited',
typeI: 3,
createdAt: '2018-10-15T03:06:14Z',
assetType: 'credit_alphanum4',
assetCode: 'HNY',
assetIssuer: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
amount: '1000000000.0000000',
},
{
id: '0000773746948313089-0000000003',
account: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
type: 'signer_created',
typeI: 10,
createdAt: '2018-10-15T02:50:22Z',
weight: 1,
publicKey: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
key: '',
},
{
id: '0000773746948313089-0000000001',
account: 'GCOLHJY3DMPDSZW7PNK4RWBQMCYE5Q67W7TT44SD46RKTVDEAUJZTNE6',
type: 'account_created',
typeI: 0,
createdAt: '2018-10-15T02:50:22Z',
startingBalance: '10000.0000000',
},
]
import effectsRecords from './__data__/effects'
import pathPaymentsRecords from './__data__/pathPayments'

test('jsonToCSV', () => {
expect(jsonToCSV(effectsRecords)).toMatchSnapshot()
describe('jsonToCSV', () => {
test('effects', () => {
expect(jsonToCSV(effectsRecords)).toMatchSnapshot()
})
test('pathPayments', () => {
expect(jsonToCSV(pathPaymentsRecords)).toMatchSnapshot()
})
})
20 changes: 10 additions & 10 deletions src/lib/csv.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {saveAs} from './filesaver'

// build and return csv string from array of column values
const toCsvString = stringArr =>
stringArr.reduce(
(accumulated, val, idx) =>
(accumulated += `"${
typeof val === 'object' ? JSON.stringify(val).replace(/"/g, '""') : val
}"${idx < stringArr.length - 1 ? ',' : ''}`),
''
)

const jsonToCSV = records => {
const columns = []
// get list of all columns across all records (as some record lists contain mixed structures)
Expand All @@ -18,16 +28,6 @@ const jsonToCSV = records => {
return accumulated
}, {})

// build and return csv string from array of column values
const toCsvString = stringArr =>
stringArr.reduce(
(accumulated, val, idx) =>
(accumulated += `"${
typeof val === 'object' ? JSON.stringify(val) : val
}"${idx < stringArr.length - 1 ? ',' : ''}`),
''
)

const numCols = columns.length
const headerCsv = toCsvString(columns)

Expand Down

0 comments on commit 95311b7

Please sign in to comment.