Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 12, 2021
1 parent 248b7ca commit e949242
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/combination/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
export const DIMENSIONS = [
{
// Name used internally or in error messages
dimension: 'task',
mainName: 'task',
// Property name for the identifier
idName: 'taskId',
// Property name for the title
Expand All @@ -20,13 +20,13 @@ export const DIMENSIONS = [
createdByUser: true,
},
{
dimension: 'runner',
mainName: 'runner',
idName: 'runnerId',
titleName: 'runnerTitle',
createdByUser: false,
},
{
dimension: 'system',
mainName: 'system',
idName: 'systemId',
titleName: 'systemTitle',
createdByUser: true,
Expand Down
4 changes: 2 additions & 2 deletions src/combination/ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const isNotDuplicate = function ({ dimension, id }, index, combinationIds) {

// Retrieve each dimension's id of a given combination
export const getCombinationIds = function (combination) {
return DIMENSIONS.map(({ dimension, idName }) => ({
return DIMENSIONS.map((dimension) => ({
dimension,
id: combination[idName],
id: combination[dimension.idName],
}))
}
19 changes: 11 additions & 8 deletions src/combination/list/user_ids.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { DIMENSIONS } from '../dimensions.js'
import { getCombinationsIds } from '../ids.js'
import { getInputIds } from '../inputs.js'

// Retrieve user-defined identifiers
export const getUserIds = function (combinations, inputs) {
const combinationsUserIds = getCombinationsIds(combinations).filter(isUserId)
const combinationsUserIds = getCombinationsIds(combinations)
.filter(isUserId)
.map(getCombinationUserId)
const nonCombinationsIds = getNonCombinationsIds(inputs)
return [...combinationsUserIds, ...nonCombinationsIds]
}

const isUserId = function (combinationId) {
return DIMENSIONS.find(
({ dimension }) => combinationId.dimension === dimension,
).createdByUser
const isUserId = function ({ dimension: { createdByUser } }) {
return createdByUser
}

const getCombinationUserId = function ({ dimension: { mainName }, id }) {
return { mainName, id }
}

// Identifiers that do not relate to dimensions/combinations
Expand All @@ -22,9 +25,9 @@ const getNonCombinationsIds = function (inputs) {
)
}

const listNonCombinationIds = function (dimension, getIds, inputs) {
const listNonCombinationIds = function (mainName, getIds, inputs) {
const ids = getIds(inputs)
return ids.map((id) => ({ dimension, id }))
return ids.map((id) => ({ mainName, id }))
}

const NON_COMBINATION_IDS = {
Expand Down
22 changes: 13 additions & 9 deletions src/combination/list/validate_ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ export const validateCombinationsIds = function (combinations, inputs) {
// Validate that identifiers don't use characters that we are using for parsing
// or could use in the future.
// Only for user-defined identifiers, not plugin-defined.
const validateUserIds = function ({ dimension, id }) {
const validateUserIds = function ({ mainName, id }) {
if (id.trim() === '') {
throw new UserError(
`Invalid ${dimension} "${id}": the identifier must not be empty.`,
`Invalid ${mainName} "${id}": the identifier must not be empty.`,
)
}

if (id.startsWith(USER_ID_INVALID_START)) {
throw new UserError(
`Invalid ${dimension} "${id}": the identifier must not start with a dash.`,
`Invalid ${mainName} "${id}": the identifier must not start with a dash.`,
)
}

if (!USER_ID_REGEXP.test(id)) {
throw new UserError(
`Invalid ${dimension} "${id}": the identifier must contain only letters, digits, - or _`,
`Invalid ${mainName} "${id}": the identifier must contain only letters, digits, - or _`,
)
}

validateReservedIds(id, dimension)
validateReservedIds(id, mainName)
}

// We do not allow starting with dash because of CLI flags parsing.
Expand All @@ -49,12 +49,12 @@ const USER_ID_INVALID_START = '-'
// We forbid other characters for forward compatibility.
const USER_ID_REGEXP = /^\w[\w-]*$/u

const validateReservedIds = function (id, dimension) {
const validateReservedIds = function (id, mainName) {
const reservedIdA = RESERVED_IDS.find((reservedId) => id === reservedId)

if (reservedIdA !== undefined) {
throw new UserError(
`Invalid ${dimension} "${id}": "${id}" is a reserved word`,
`Invalid ${mainName} "${id}": "${id}" is a reserved word`,
)
}
}
Expand All @@ -69,11 +69,15 @@ const RESERVED_IDS = ['not']
// `config.titles`, `config.select`, reporting.
// Non-combination identifiers are not checked for duplicates since they are
// not used for selection, reporting, `config.titles`, etc.
const validateDuplicateId = function ({ dimension, id }, index, allIds) {
const validateDuplicateId = function (
{ dimension: { mainName }, id },
index,
allIds,
) {
const duplicateId = allIds.slice(index + 1).find((nextId) => nextId.id === id)

if (duplicateId !== undefined) {
throw new UserError(`The identifier "${id}" must not be used both as ${dimension} and ${duplicateId.dimension}.
throw new UserError(`The identifier "${id}" must not be used both as ${mainName} and ${duplicateId.dimension}.
Please rename one of them.`)
}
}
9 changes: 6 additions & 3 deletions src/combination/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ export const getCombinationName = function (combination) {
.join(noteColor(', '))
}

const getCombinationNamePart = function ({ dimension, id }, index) {
const dimensionA = index === 0 ? titleize(dimension) : dimension
return `${noteColor(dimensionA)} ${titleColor(id)}`
const getCombinationNamePart = function (
{ dimension: { mainName }, id },
index,
) {
const dimension = index === 0 ? titleize(mainName) : mainName
return `${noteColor(dimension)} ${titleColor(id)}`
}

const titleize = function (string) {
Expand Down

0 comments on commit e949242

Please sign in to comment.