Skip to content

Commit

Permalink
Fix code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent 87f81cb commit 0a5bb38
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 51 deletions.
10 changes: 2 additions & 8 deletions src/config/normalize/lib/keywords/list/condition.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
const normalize = function (definition) {
if (typeof definition !== 'boolean') {
throw new TypeError('Definition must be a boolean.')
}

return definition
}
import { normalizeBoolean } from '../normalize/common.js'

const main = function (definition) {
return definition ? undefined : { skip: true }
Expand All @@ -17,6 +11,6 @@ export default {
name: 'condition',
hasInput: true,
undefinedInput: true,
normalize,
normalize: normalizeBoolean,
main,
}
11 changes: 2 additions & 9 deletions src/config/normalize/lib/keywords/list/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import fastGlob from 'fast-glob'
import { isNotJunk } from 'junk'

import { validateDefinedString } from '../../type.js'

const normalize = function (definition) {
if (typeof definition !== 'boolean') {
throw new TypeError('Definition must be a boolean.')
}

return definition
}
import { normalizeBoolean } from '../normalize/common.js'

const main = async function (definition, input, { cwd }) {
if (!definition) {
Expand Down Expand Up @@ -43,6 +36,6 @@ const main = async function (definition, input, { cwd }) {
export default {
name: 'glob',
hasInput: true,
normalize,
normalize: normalizeBoolean,
main,
}
2 changes: 1 addition & 1 deletion src/config/normalize/lib/keywords/list/path/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { resolve } from 'path'
import { validateDefinedString } from '../../../type.js'

import { validateAccess } from './access.js'
import { normalize } from './normalize.js'
import { fileExists, validateExists } from './exist.js'
import { normalize } from './normalize.js'
import { validateType } from './type.js'

const main = async function (keywords, input, { cwd }) {
Expand Down
10 changes: 2 additions & 8 deletions src/config/normalize/lib/keywords/list/pick.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
const normalize = function (definition) {
if (typeof definition !== 'boolean') {
throw new TypeError('Definition must be a boolean.')
}

return definition
}
import { normalizeBoolean } from '../normalize/common.js'

const main = function (definition) {
return definition ? undefined : { skip: true, input: undefined }
Expand All @@ -19,6 +13,6 @@ export default {
name: 'pick',
hasInput: true,
undefinedInput: true,
normalize,
normalize: normalizeBoolean,
main,
}
10 changes: 2 additions & 8 deletions src/config/normalize/lib/keywords/list/prefix.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
const normalize = function (definition) {
if (typeof definition !== 'string') {
throw new TypeError('Definition must be a string.')
}

return definition.trim()
}
import { normalizeString } from '../normalize/common.js'

const main = function (definition) {
return { info: { prefix: definition } }
Expand All @@ -14,6 +8,6 @@ const main = function (definition) {
export default {
name: 'prefix',
undefinedInput: true,
normalize,
normalize: normalizeString,
main,
}
12 changes: 3 additions & 9 deletions src/config/normalize/lib/keywords/list/required.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { normalizeBoolean } from '../normalize/common.js'

const test = function (input) {
return input === undefined
}

const normalize = function (definition) {
if (typeof definition !== 'boolean') {
throw new TypeError('Definition must be a boolean.')
}

return definition
}

const main = function (definition) {
if (definition) {
throw new Error('must be defined.')
Expand All @@ -23,6 +17,6 @@ export default {
name: 'required',
undefinedInput: true,
test,
normalize,
normalize: normalizeBoolean,
main,
}
10 changes: 2 additions & 8 deletions src/config/normalize/lib/keywords/list/warn.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
const normalize = function (definition) {
if (typeof definition !== 'string') {
throw new TypeError('Definition must be a string.')
}

return definition.trim()
}
import { normalizeString } from '../normalize/common.js'

const main = function (definition) {
return { warning: definition }
Expand All @@ -15,6 +9,6 @@ const main = function (definition) {
export default {
name: 'warn',
hasInput: true,
normalize,
normalize: normalizeString,
main,
}
17 changes: 17 additions & 0 deletions src/config/normalize/lib/keywords/normalize/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Normalize string definition
export const normalizeString = function (definition) {
if (typeof definition !== 'string') {
throw new TypeError('Definition must be a string.')
}

return definition.trim()
}

// Normalize boolean definition
export const normalizeBoolean = function (definition) {
if (typeof definition !== 'boolean') {
throw new TypeError('Definition must be a boolean.')
}

return definition
}

0 comments on commit 0a5bb38

Please sign in to comment.