Skip to content

Commit

Permalink
Split file
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 26, 2021
1 parent 5df086d commit c4afee7
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 117 deletions.
117 changes: 117 additions & 0 deletions src/system/footer/shared/redundant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import omit from 'omit.js'

import { setArray } from '../../../utils/set.js'

// Some dimensions are redundant, i.e. removing them does change which systems
// are matched because either:
// - The prop is matched by several dimensions in `dimensionsArray`
// - The systems are missing some dimensions
// For example, if a prop applies to all systems, its `dimensionsArray` will
// list each possible `dimensions`. This logic would reduce it to an empty
// `dimensionArray`.
// We do this by iterating through each dimension, checking if it can be
// removed, then removing it.
export const skipRedundantInfo = function (dimensionsArray, systems) {
return dimensionsArray.reduce(
skipRedundantDimensions.bind(undefined, systems),
dimensionsArray,
)
}

// Sometimes, removing dimensions can result in several equivalent but different
// result depending on the order in which dimensions are iterated.
// - We iterate from the last to the first dimensions, so that the last
// dimensions are removed instead of the first ones, since the first ones
// are more likely to be more significant for users.
// eslint-disable-next-line max-params
const skipRedundantDimensions = function (
systems,
dimensionsArray,
dimensions,
index,
) {
return Object.keys(dimensions).reduceRight(
skipRedundantDimension.bind(undefined, { systems, index }),
dimensionsArray,
)
}

const skipRedundantDimension = function (
{ systems, index },
dimensionsArray,
dimensionName,
) {
if (
!isRedundantDimension({ systems, dimensionsArray, dimensionName, index })
) {
return dimensionsArray
}

const dimensions = dimensionsArray[index]
const dimensionsA = omit.default(dimensions, [dimensionName])
return setArray(dimensionsArray, index, dimensionsA)
}

// Check if removing the dimension would result in a different count of matching
// systems
const isRedundantDimension = function ({
systems,
dimensionsArray,
dimensionName,
index,
}) {
const { length } = systems.filter((system) =>
dimensionsArrayMatches({ system, dimensionsArray, index, dimensionName }),
)
return length === dimensionsArray.length
}

const dimensionsArrayMatches = function ({
system,
dimensionsArray,
index,
dimensionName,
}) {
return dimensionsArray.some((dimensions, indexB) =>
dimensionsMatches({
system,
dimensions,
index,
indexB,
dimensionName,
}),
)
}

const dimensionsMatches = function ({
system,
dimensions,
index,
indexB,
dimensionName,
}) {
return Object.entries(dimensions).every(([dimensionNameB, dimensionValueB]) =>
dimensionMatches({
system,
index,
indexB,
dimensionName,
dimensionNameB,
dimensionValueB,
}),
)
}

const dimensionMatches = function ({
system,
index,
indexB,
dimensionName,
dimensionNameB,
dimensionValueB,
}) {
return (
system.dimensions[dimensionNameB] === dimensionValueB ||
(indexB === index && dimensionNameB === dimensionName)
)
}
118 changes: 1 addition & 117 deletions src/system/footer/shared/simplify.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import omit from 'omit.js'

import { setArray } from '../../../utils/set.js'
import { uniqueDeep } from '../../../utils/unique.js'

import { concatDimensionsValues } from './concat.js'
import { skipRedundantInfo } from './redundant.js'

// Reduce the amount of system dimensions and properties so the system footer
// looks simpler
Expand All @@ -19,120 +17,6 @@ const simplifyPropGroup = function ({ propEntries, dimensionsArray }, systems) {
return { propEntries, dimensionsArray: dimensionsArrayD }
}

// Some dimensions are redundant, i.e. removing them does change which systems
// are matched because either:
// - The prop is matched by several dimensions in `dimensionsArray`
// - The systems are missing some dimensions
// For example, if a prop applies to all systems, its `dimensionsArray` will
// list each possible `dimensions`. This logic would reduce it to an empty
// `dimensionArray`.
// We do this by iterating through each dimension, checking if it can be
// removed, then removing it.
const skipRedundantInfo = function (dimensionsArray, systems) {
return dimensionsArray.reduce(
skipRedundantDimensions.bind(undefined, systems),
dimensionsArray,
)
}

// Sometimes, removing dimensions can result in several equivalent but different
// result depending on the order in which dimensions are iterated.
// - We iterate from the last to the first dimensions, so that the last
// dimensions are removed instead of the first ones, since the first ones
// are more likely to be more significant for users.
// eslint-disable-next-line max-params
const skipRedundantDimensions = function (
systems,
dimensionsArray,
dimensions,
index,
) {
return Object.keys(dimensions).reduceRight(
skipRedundantDimension.bind(undefined, { systems, index }),
dimensionsArray,
)
}

const skipRedundantDimension = function (
{ systems, index },
dimensionsArray,
dimensionName,
) {
if (
!isRedundantDimension({ systems, dimensionsArray, dimensionName, index })
) {
return dimensionsArray
}

const dimensions = dimensionsArray[index]
const dimensionsA = omit.default(dimensions, [dimensionName])
return setArray(dimensionsArray, index, dimensionsA)
}

// Check if removing the dimension would result in a different count of matching
// systems
const isRedundantDimension = function ({
systems,
dimensionsArray,
dimensionName,
index,
}) {
const { length } = systems.filter((system) =>
dimensionsArrayMatches({ system, dimensionsArray, index, dimensionName }),
)
return length === dimensionsArray.length
}

const dimensionsArrayMatches = function ({
system,
dimensionsArray,
index,
dimensionName,
}) {
return dimensionsArray.some((dimensions, indexB) =>
dimensionsMatches({
system,
dimensions,
index,
indexB,
dimensionName,
}),
)
}

const dimensionsMatches = function ({
system,
dimensions,
index,
indexB,
dimensionName,
}) {
return Object.entries(dimensions).every(([dimensionNameB, dimensionValueB]) =>
dimensionMatches({
system,
index,
indexB,
dimensionName,
dimensionNameB,
dimensionValueB,
}),
)
}

const dimensionMatches = function ({
system,
index,
indexB,
dimensionName,
dimensionNameB,
dimensionValueB,
}) {
return (
system.dimensions[dimensionNameB] === dimensionValueB ||
(indexB === index && dimensionNameB === dimensionName)
)
}

// Properties which apply to all systems result in a `dimensionsArray` with a
// single empty object. We normalize it to an empty array.
const normalizeTopSystem = function (dimensionsArray) {
Expand Down

0 comments on commit c4afee7

Please sign in to comment.