Skip to content

Commit

Permalink
Improve serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 2, 2022
1 parent e76163a commit a97ca67
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/history/data/contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,27 @@ export const parseRawResult = function (contents) {
// Serialize the contents of a rawResult
export const serializeRawResult = function (rawResult) {
const contents = JSON.stringify(rawResult, undefined, 2)
return `${contents}\n`
const contentsA = flattenArray(contents)
return `${contentsA}\n`
}

// Some arrays like `histogram` and `quantiles` are big. `JSON.serialize()`
// put each item in a separate line. We put those in a single line instead.
// - This makes it easier to view the file
// - This creates simpler git diffs
// - This creates better git stats when it comes to amount of lines changes
// We only do this for arrays of simple types.
const flattenArray = function (content) {
return content.replace(SIMPLE_ARRAY_REGEXP, flattenArrayItems)
}

// Matches `[...]` but not `[{ ... }]` nor `[[...]]`
const SIMPLE_ARRAY_REGEXP = /(\[)([^[\]{]+)(\])/gmu

// eslint-disable-next-line max-params
const flattenArrayItems = function (_, start, match, end) {
const matchA = match.replace(WHITESPACES_REGEXP, ' ')
return `${start}${matchA}${end}`
}

const WHITESPACES_REGEXP = /\s+/gmu

0 comments on commit a97ca67

Please sign in to comment.