Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 19, 2021
1 parent 9c16d6a commit 23a684d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
14 changes: 0 additions & 14 deletions src/combination/ids/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,3 @@ const getDimensionId = function (combination, dimension) {
const { id } = combination.dimensions[dimension.propName]
return { id, dimension }
}

// Rename the `id` of a dimensionId for a given combination's dimension
export const setDimensionId = function (
combination,
{ id, dimension: { propName } },
) {
return {
...combination,
dimensions: {
...combination.dimensions,
[propName]: { ...combination.dimensions[propName], id },
},
}
}
15 changes: 3 additions & 12 deletions src/combination/ids/namespace.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { setArrayElement } from '../../utils/set.js'

import { getCombDimensionsIds, setDimensionId } from './get.js'
import { getCombDimensionsIds } from './get.js'
import { isSameId, isSameDimension, hasCrossDimensionsIds } from './has.js'
import { setDimensionId, syncDimensionIds } from './set.js'

// When a result is created, we ensure that two dimensions do not have the
// same ids, by throwing errors.
Expand Down Expand Up @@ -115,16 +116,6 @@ const updateRawResult = function (rawResult, combinations) {
}

const rawResultA = { ...rawResult, combinations }
const rawResultB = renameSystemIds(rawResultA)
const rawResultB = syncDimensionIds(rawResultA)
return rawResultB
}

// System ids are persisted in two places: `rawResult.system.id` and
// `rawResult.combinations[*].dimensions.system`.
// We need to update the former.
const renameSystemIds = function (rawResult) {
const { id } = rawResult.combinations[0].dimensions.system
return rawResult.system.id === id
? rawResult
: { ...rawResult, system: { ...rawResult.system, id } }
}
30 changes: 30 additions & 0 deletions src/combination/ids/set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Rename the `id` of a dimensionId for a given combination's dimension
export const setDimensionId = function (
combination,
{ id, dimension: { propName } },
) {
return {
...combination,
dimensions: {
...combination.dimensions,
[propName]: { ...combination.dimensions[propName], id },
},
}
}

// When updating `rawResult.combinations[*].dimensions[*].id`, we need to also
// update some properties in `rawResult` which have the same value,
// such as `rawResult.system.id`.
export const syncDimensionIds = function (rawResult) {
return renameSystemIds(rawResult)
}

// System ids are persisted in two places: `rawResult.system.id` and
// `rawResult.combinations[*].dimensions.system`.
// We need to update the former.
const renameSystemIds = function (rawResult) {
const { id } = rawResult.combinations[0].dimensions.system
return rawResult.system.id === id
? rawResult
: { ...rawResult, system: { ...rawResult.system, id } }
}

0 comments on commit 23a684d

Please sign in to comment.