Skip to content

Commit

Permalink
Use currying
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 16, 2022
1 parent 5102c63 commit 65df111
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/config/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import isPlainObj from 'is-plain-obj'
import mapObj from 'map-obj'

import { UserError } from '../error/main.js'
import { curry } from '../utils/functional.js'

// Configuration validation helper functions
// eslint-disable-next-line max-params
Expand All @@ -14,6 +15,8 @@ export const checkArrayItems = function (checkers, value, name, ...args) {
)
}

export const cCheckArrayItems = curry(checkArrayItems)

// When array has a single item, it is possible that the value was arrified
const getIndexName = function (name, index, value) {
return value.length === 1 ? name : `${name}[${index}]`
Expand All @@ -28,6 +31,8 @@ export const checkObjectProps = function (checkers, value, name, ...args) {
])
}

export const cCheckObjectProps = curry(checkObjectProps)

const applyCheckers = function (checkers, value, ...args) {
return checkers.reduce(
(valueA, checker) => applyChecker(checker, valueA, ...args),
Expand Down
24 changes: 9 additions & 15 deletions src/config/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
checkInteger,
checkString,
normalizeOptionalArray,
checkArrayItems,
cCheckArrayItems,
checkArrayLength,
checkObjectProps,
cCheckObjectProps,
checkDefinedString,
checkJson,
} from './check.js'
Expand Down Expand Up @@ -80,37 +80,31 @@ const NORMALIZERS = {
cwd: [checkDefinedString, normalizeConfigPath],
delta: [normalizeDelta],
force: [checkBoolean],
inputs: [checkObjectProps.bind(undefined, [checkJson])],
inputs: [cCheckObjectProps([checkJson])],
limit: [checkInteger, normalizeLimit],
merge: [checkDefinedString, validateMerge],
output: [checkDefinedString, condition(normalizeConfigPath, isOutputPath)],
outliers: [checkBoolean],
precision: [checkInteger, normalizePrecision],
quiet: [checkBoolean],
reporter: [
normalizeOptionalArray,
checkArrayItems.bind(undefined, [checkDefinedString]),
],
reporter: [normalizeOptionalArray, cCheckArrayItems([checkDefinedString])],
runner: [
normalizeOptionalArray,
checkArrayLength,
checkArrayItems.bind(undefined, [checkDefinedString]),
cCheckArrayItems([checkDefinedString]),
],
save: [checkBoolean],
select: [
normalizeOptionalArray,
checkArrayItems.bind(undefined, [checkString]),
],
select: [normalizeOptionalArray, cCheckArrayItems([checkString])],
showDiff: [checkBoolean],
showMetadata: [checkBoolean],
showPrecision: [checkBoolean],
showSystem: [checkBoolean],
showTitles: [checkBoolean],
since: [normalizeDelta],
system: [checkObjectProps.bind(undefined, [checkDefinedString])],
system: [cCheckObjectProps([checkDefinedString])],
tasks: [
normalizeOptionalArray,
checkArrayItems.bind(undefined, [checkDefinedString, normalizeConfigGlob]),
cCheckArrayItems([checkDefinedString, normalizeConfigGlob]),
],
titles: [checkObjectProps.bind(undefined, [checkDefinedString])],
titles: [cCheckObjectProps([checkDefinedString])],
}
10 changes: 10 additions & 0 deletions src/utils/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ export const condition = function (callFunc, conditionFunc) {
const conditionCall = function (callFunc, conditionFunc, ...args) {
return conditionFunc(...args) ? callFunc(...args) : args[0]
}

// Allow to use `callFunc(...)` instead of `callFunc.bind(undefined, ...)` when
// a function is always meant to be bound.
export const curry = function (callFunc) {
return bindArgs.bind(undefined, callFunc)
}

const bindArgs = function (callFunc, ...boundArgs) {
return callFunc.bind(undefined, ...boundArgs)
}

0 comments on commit 65df111

Please sign in to comment.