Skip to content

Commit

Permalink
removing triple quotes from csv exports
Browse files Browse the repository at this point in the history
  • Loading branch information
nnecla committed Jun 3, 2024
1 parent 514f530 commit ad88cb5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/browser/modules/Stream/CypherFrame/CypherFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ export class CypherFrame extends Component<CypherFrameProps, CypherFrameState> {

const exportData = stringifyResultArray(
csvFormat,
[keys].concat(records.map(record => recordToStringArray(record)))
[keys].concat(records.map(record => recordToStringArray(record, true)))
)
const data = exportData.slice()
const csv = CSVSerializer(data.shift())
Expand Down
25 changes: 25 additions & 0 deletions src/browser/modules/Stream/CypherFrame/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,31 @@ describe('helpers', () => {
// Then
expect(res).toEqual([['"P1M2DT3.000000004S"']])
})

test('recordToStringArray handles strings correctly when double quotes are disabled', () => {
const records = [
new Record(
['x', 'y', 'n', 'z', '{}'],
[
'xRecord',
neo4j.int(10),
new neo4j.types.Duration(1, 2, 3, 4),
new (neo4j.types.Node as any)('1', ['Person'], { prop1: 'prop1' }),
{}
]
)
]
const res = records.map(record => recordToStringArray(record, true))
expect(res).toEqual([
[
'xRecord',
'10',
'P1M2DT3.000000004S',
'(:Person {prop1: prop1})',
'{}'
]
])
})
})

describe('recordToJSONMapper', () => {
Expand Down
20 changes: 14 additions & 6 deletions src/browser/modules/Stream/CypherFrame/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import neo4j, { Node, Path, Record, Relationship } from 'neo4j-driver'
import bolt from 'services/bolt/bolt'
import { recursivelyExtractGraphItems } from 'services/bolt/boltMappings'
import { stringModifier } from 'services/bolt/cypherTypesFormatting'
import { stringifyMod, unescapeDoubleQuotesForDisplay } from 'services/utils'
import { stringifyMod } from 'services/utils'
import * as viewTypes from 'shared/modules/frames/frameViewTypes'
import { BrowserRequestResult } from 'shared/modules/requests/requestsDuck'

Expand Down Expand Up @@ -231,14 +231,13 @@ export const initialView = (props: any, state: any = {}) => {
*/
export const stringifyResultArray = (
formatter = stringModifier,
arr: any[] = [],
unescapeDoubleQuotes = false
arr: any[] = []
) => {
return arr.map(col => {
if (!col) return col
return col.map((fVal: any) => {
const res = stringifyMod(fVal, formatter)
return unescapeDoubleQuotes ? unescapeDoubleQuotesForDisplay(res) : res
return res
})
})
}
Expand Down Expand Up @@ -423,7 +422,10 @@ function isNeo4jValue(value: any) {
}
}

export const recordToStringArray = (record: Record): string[] => {
export const recordToStringArray = (
record: Record,
discardDoubleQuotes?: boolean
): string[] => {
const recursiveStringify = (value: CypherDataType): string => {
if (Array.isArray(value)) {
if (value.length === 0) return '[]'
Expand All @@ -432,7 +434,13 @@ export const recordToStringArray = (record: Record): string[] => {

if (isCypherPropertyType(value)) {
//Note: later we should use propertyToString here but needs to be updated to show year in durations.
return stringifyMod(value, stringModifier, true)
return stringifyMod(
value,
(v: any) => stringModifier(v, discardDoubleQuotes),
true,
false,
discardDoubleQuotes
)
}

// We have nodes, relationships, paths and cypher maps left.
Expand Down
9 changes: 7 additions & 2 deletions src/shared/services/bolt/cypherTypesFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export const csvFormat = (anything: any) => {
return undefined
}

export const stringModifier = (anything: any) => {
export const stringModifier = (
anything: any,
discardDoubleQuotes?: boolean
) => {
if (typeof anything === 'number') {
return numberFormat(anything)
}
Expand All @@ -30,7 +33,9 @@ export const stringModifier = (anything: any) => {
return spacialFormat(anything)
}
if (isTemporalType(anything)) {
return `"${anything.toString()}"`
return discardDoubleQuotes
? anything.toString()
: `"${anything.toString()}"`
}
return undefined
}
Expand Down
7 changes: 5 additions & 2 deletions src/shared/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ export const stringifyMod = (
value: any,
modFn: any = null,
pretty: boolean | number = false,
skipOpeningIndentation = false
skipOpeningIndentation = false,
discardDoubleQuotes = false
): string => {
const prettyLevel = isNumber(pretty) ? pretty : +pretty
const nextPrettyLevel = prettyLevel ? prettyLevel + 1 : false
Expand Down Expand Up @@ -386,7 +387,9 @@ export const stringifyMod = (
)}${newLine}${endIndentation}}`
}
}
return `${indentation}"${value.toString().replace(escRE, escFunc)}"`
return discardDoubleQuotes
? `${indentation}${value.toString().replace(escRE, escFunc)}`
: `${indentation}"${value.toString().replace(escRE, escFunc)}"`
}

export const unescapeDoubleQuotesForDisplay = (str: any) =>
Expand Down

0 comments on commit ad88cb5

Please sign in to comment.