Skip to content

Commit

Permalink
Rename defined to missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 6, 2022
1 parent 0b3bac9 commit 1e3a115
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/config/normalize/lib/star_dot_path/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getObjectTokenType } from './tokens/main.js'
export const listEntries = function (target, queryOrPath) {
const path = parse(queryOrPath)
return path.reduce(listTokenEntries, [
{ value: target, path: [], defined: true },
{ value: target, path: [], missing: false },
])
}

Expand All @@ -16,22 +16,22 @@ const listTokenEntries = function (entries, token) {
}

const getTokenEntries = function ({ value, path }, token) {
const { tokenType, defined, value: valueA } = handleMissingValue(value, token)
return tokenType.getEntries(valueA, path, token, defined)
const { tokenType, missing, value: valueA } = handleMissingValue(value, token)
return tokenType.getEntries(valueA, path, token, missing)
}

// When the value does not exist, we set it deeply with `set()` but not with
// `list|get|has()`.
// We filter out between those two cases using a `defined` property.
// We filter out between those two cases using a `missing` property.
// Tokens like wildcards cannot do this since there is known property to add.
// Array indices that are:
// - Positive are kept
// - Negative are converted to index 0
export const handleMissingValue = function (value, token) {
const tokenType = getObjectTokenType(token)
const defined = tokenType.isDefined(value)
const valueA = defined ? value : tokenType.defaultValue
return { tokenType, defined, value: valueA }
const missing = tokenType.isMissing(value)
const valueA = missing ? tokenType.defaultValue : value
return { tokenType, missing, value: valueA }
}

// Compute all entries properties from the basic ones
Expand Down
8 changes: 4 additions & 4 deletions src/config/normalize/lib/star_dot_path/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export const get = function (target, queryOrPath) {
return entries.length === 0 ? undefined : entries[0].value
}

// Check if a property is defined according to a query
// Check if a property is not missing according to a query
export const has = function (target, queryOrPath) {
const entries = listExistingEntries(target, queryOrPath)
return entries.length !== 0
}

const listExistingEntries = function (target, queryOrPath) {
return listEntries(target, queryOrPath).filter(isDefined)
return listEntries(target, queryOrPath).filter(isExisting)
}

const isDefined = function ({ defined }) {
return defined
const isExisting = function ({ missing }) {
return !missing
}
2 changes: 1 addition & 1 deletion src/config/normalize/lib/star_dot_path/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const hasRootPath = function ({ path }) {
const removeEntry = function (target, path, index) {
const key = path[index]

if (!handleMissingValue(target, key).defined) {
if (handleMissingValue(target, key).missing) {
return target
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/normalize/lib/star_dot_path/tokens/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ const normalize = function ({ type }) {
// Use the token to list entries against a target value.
// We purposely ignore symbol properties by using `Object.keys()`.
// eslint-disable-next-line max-params
const getEntries = function (value, path, token, defined) {
const getEntries = function (value, path, token, missing) {
if (Array.isArray(value)) {
return value.map((childValue, index) => ({
value: childValue,
path: [...path, index],
defined,
missing,
}))
}

return Object.keys(value).map((childKey) => ({
value: value[childKey],
path: [...path, childKey],
defined,
missing,
}))
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/star_dot_path/tokens/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const normalize = function (token) {

// Use the token to list entries against a target value.
// eslint-disable-next-line max-params
const getEntries = function (value, path, token, defined) {
const getEntries = function (value, path, token, missing) {
const index = getArrayIndex(value, token)
return [{ value: value[index], path: [...path, index], defined }]
return [{ value: value[index], path: [...path, index], missing }]
}

// Retrieve an array using a positive or negative index.
Expand Down
8 changes: 4 additions & 4 deletions src/config/normalize/lib/star_dot_path/tokens/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { isRecurseObject } from './recurse.js'

// Properties shared by all token types which apply on objects
export const objectProps = {
isDefined(value) {
return isRecurseObject(value)
isMissing(value) {
return !isRecurseObject(value)
},
defaultValue: {},
}

// Properties shared by all token types which apply on arrays
export const arrayProps = {
isDefined(value) {
return Array.isArray(value)
isMissing(value) {
return !Array.isArray(value)
},
defaultValue: [],
}
4 changes: 2 additions & 2 deletions src/config/normalize/lib/star_dot_path/tokens/prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ const normalize = function (token) {
// - Missing property name: return no entries
// - Property exists but has `undefined` value: return an entry
// eslint-disable-next-line max-params
const getEntries = function (value, path, token, defined) {
const getEntries = function (value, path, token, missing) {
return [
{
value: value[token],
path: [...path, token],
defined: defined && token in value,
missing: missing || !(token in value),
},
]
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/star_dot_path/tokens/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ const equals = function (tokenA, tokenB) {

// Use the token to list entries against a target value.
// eslint-disable-next-line max-params
const getEntries = function (value, path, token, defined) {
const getEntries = function (value, path, token, missing) {
return Object.keys(value)
.filter((childKey) => token.test(childKey))
.map((childKey) => ({
value: value[childKey],
path: [...path, childKey],
defined,
missing,
}))
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/normalize/lib/star_dot_path/tokens/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ const normalize = function ({ type, from = 0, to }) {

// Use the token to list entries against a target value.
// eslint-disable-next-line max-params
const getEntries = function (value, path, { from, to }, defined) {
const getEntries = function (value, path, { from, to }, missing) {
const fromIndex = getArrayIndex(value, from)
const toIndex = Math.max(getArrayIndex(value, to), fromIndex)
return new Array(toIndex - fromIndex).fill().map((_, index) => ({
value: value[index + fromIndex],
path: [...path, index + fromIndex],
defined,
missing,
}))
}

Expand Down

0 comments on commit 1e3a115

Please sign in to comment.