Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 20, 2022
1 parent de7c7f6 commit a50f8cb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/bin/transtype.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import isPlainObj from 'is-plain-obj'

import { recurseValues } from '../utils/recurse.js'
import { map } from '../config/normalize/lib/wild_wild_utils/main.js'

// Transtype CLI flags after parsing
export const transtypeCliFlags = function (configFlags) {
return recurseValues(configFlags, transtypeCliFlag)
return map(configFlags, '**', transtypeCliFlag, { leaves: true })
}

const transtypeCliFlag = function (value) {
Expand Down
10 changes: 3 additions & 7 deletions src/config/cwd.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import filterObj from 'filter-obj'

import { recurseValues } from '../utils/recurse.js'

import { isRecurseObject } from './merge.js'
import { get } from './normalize/lib/wild_wild_path/main.js'
import { map } from './normalize/lib/wild_wild_utils/main.js'

// When resolving configuration relative file paths:
// - The CLI and programmatic flags always use the current directory.
Expand Down Expand Up @@ -91,12 +90,9 @@ const isNotBaseProp = function (key) {
return !key.endsWith(BASE_KEY_SUFFIX) && !key.endsWith(BASE_ITEMS_SUFFIX)
}

// We ensure this recurse the same way as the merging logic
const recurseBaseProps = function (configObject, mapper) {
return recurseValues(
configObject,
(value) => mapBaseProps(value, mapper),
isRecurseObject,
)
return map(configObject, '**', (value) => mapBaseProps(value, mapper))
}

const mapBaseProps = function (value, mapper) {
Expand Down
30 changes: 0 additions & 30 deletions src/utils/recurse.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
import isPlainObj from 'is-plain-obj'

import { list } from '../config/normalize/lib/wild_wild_path/main.js'

import { mapValues } from './map.js'

// Apply a mapping function over all values of an object or array.
// Only recurse over arrays and plain objects.
// eslint-disable-next-line max-params
export const recurseValues = function (
value,
mapper,
isRecurseObject = isPlainObj,
path = [],
) {
const valueA = mapper(value, path)

if (Array.isArray(valueA)) {
return valueA.map((child, index) =>
recurseValues(child, mapper, isRecurseObject, [...path, index]),
)
}

if (isRecurseObject(valueA)) {
return mapValues(valueA, (child, key) =>
recurseValues(child, mapper, isRecurseObject, [...path, key]),
)
}

return valueA
}

// Find leaf properties matching a specific condition.
// We make sure to use options which match which object gets recursed or not by
// the deep merging logic.
Expand Down

0 comments on commit a50f8cb

Please sign in to comment.