Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 6, 2022
1 parent e9cce84 commit 0b3bac9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/config/normalize/lib/star_dot_path/entries.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { parse } from './parsing/parse.js'
import { serialize } from './parsing/serialize.js'
import { getObjectTokenType } from './tokens/main.js'

// List all values (and their associated path) matching a specific query for
// on specific target value.
export const listEntries = function (target, path) {
export const listEntries = function (target, queryOrPath) {
const path = parse(queryOrPath)
return path.reduce(listTokenEntries, [
{ value: target, path: [], defined: true },
])
Expand Down
4 changes: 1 addition & 3 deletions src/config/normalize/lib/star_dot_path/get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { listEntries, normalizeEntry } from './entries.js'
import { parse } from './parsing/parse.js'

// Retrieve all properties in `target` matching a query string.
export const list = function (target, queryOrPath) {
Expand All @@ -21,8 +20,7 @@ export const has = function (target, queryOrPath) {
}

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

const isDefined = function ({ defined }) {
Expand Down
4 changes: 1 addition & 3 deletions src/config/normalize/lib/star_dot_path/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import omit from 'omit.js'
import { setArray } from '../../../../utils/set.js'

import { listEntries, handleMissingValue } from './entries.js'
import { parse } from './parsing/parse.js'
import { setValue } from './set.js'

// Same as `set()` but removing a value
export const remove = function (target, queryOrPath) {
const path = parse(queryOrPath)
const entries = listEntries(target, path)
const entries = listEntries(target, queryOrPath)
return entries.some(hasRootPath)
? undefined
: entries.reduce(
Expand Down
4 changes: 1 addition & 3 deletions src/config/normalize/lib/star_dot_path/set.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { setArray } from '../../../../utils/set.js'

import { listEntries, handleMissingValue } from './entries.js'
import { parse } from './parsing/parse.js'

// Set a value to one or multiple properties in `target` using a query string
export const set = function (target, queryOrPath, value) {
const path = parse(queryOrPath)
const entries = listEntries(target, path)
const entries = listEntries(target, queryOrPath)
return entries.reduce(
(targetA, entry) => setEntry(targetA, entry.path, value, 0),
target,
Expand Down

0 comments on commit 0b3bac9

Please sign in to comment.