Skip to content

Commit

Permalink
Add prop.compute()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 23, 2022
1 parent 64c58f4 commit a853865
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/config/normalize/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ const handleGetUserError = function (message) {
const normalizePropValue = async function ({
value,
name,
configProp: { default: defaultValue, transform },
configProp: { default: defaultValue, transform, compute },
configInfos,
get,
}) {
const path = getPath(name)
const opts = { name, path, configInfos, get }

const valueA = await addDefaultValue(value, defaultValue, opts)
return await transformProp(valueA, transform, opts)
const valueB = await computeValue(valueA, compute, opts)
return await transformValue(valueB, transform, opts)
}

const getPath = function (name) {
Expand All @@ -109,12 +110,18 @@ const getPathKey = function ({ key }) {
return key
}

// Apply `default(opts)` which assigns a default value
const addDefaultValue = async function (value, defaultValue, opts) {
return value === undefined ? await maybeFunction(defaultValue, opts) : value
}

// Calls `transform(value)`
const transformProp = async function (value, transform, opts) {
// Apply `compute(opts)` which sets a value from the system, instead of the user
const computeValue = async function (value, compute, opts) {
return compute === undefined ? value : await maybeFunction(compute, opts)
}

// Apply `transform(value)` which transforms the value set by the user
const transformValue = async function (value, transform, opts) {
if (value === undefined || transform === undefined) {
return value
}
Expand Down

0 comments on commit a853865

Please sign in to comment.