Skip to content

Commit

Permalink
Improve parent()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Mar 13, 2022
1 parent f5028e1 commit d76f703
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
61 changes: 22 additions & 39 deletions src/config/normalize/lib/wild_wild_path/parsing/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export const equals = function (queryOrPathsA, queryOrPathsB) {
)
}

const hasSamePath = function (paths, pathA) {
return paths.some((pathB) => isSamePath(pathA, pathB))
}

const isSamePath = function (pathA, pathB) {
return (
pathA.length === pathB.length &&
pathA.every((tokenA, index) => isSameToken(tokenA, pathB[index]))
)
}

// Check if two simple paths are equal
export const equalsSimple = function (simplePathA, simplePathB) {
validateSimplePath(simplePathA)
Expand All @@ -29,56 +40,28 @@ export const equalsSimple = function (simplePathA, simplePathB) {
export const fastEqualsSimple = function (simplePathA, simplePathB) {
return (
simplePathA.length === simplePathB.length &&
simplePathA.every((prop, index) => simplePathB[index] === prop)
simplePathA.every((prop, index) => isSameProp(simplePathB[index], prop))
)
}

// Check if a query is a parent of another.
// With unions, it checks if any query is a parent of any of the other one.
// The comparison is currently token type-wise.
// Also it does not check whether a token is a subset of another, i.e.:
// - * does not match other token types
// - RegExps does not match prop tokens
// - Unions are not resolved
// Also, this is only for the query without any target, i.e.:
// - Negative indices do not match positive indices
// This makes it more useful for queries with only prop and positive indices
// tokens:
// - E.g. on the entries returned by `list()`
export const parent = function (parentQueryOrPaths, childQueryOrPaths) {
const parentPaths = parse(parentQueryOrPaths)
const childPaths = parse(childQueryOrPaths)
return parentPaths.some((parentPath) => hasParentPath(parentPath, childPaths))
}

const hasParentPath = function (parentPath, childPaths) {
return childPaths.some((childPath) => isParentPath(parentPath, childPath))
}

const isParentPath = function (parentPath, childPath) {
// Check if a simple path is a parent to another
export const parent = function (parentSimplePath, childSimplePath) {
return (
childPath.length > parentPath.length &&
childPath.every(
childSimplePath.length > parentSimplePath.length &&
childSimplePath.every(
(childToken, index) =>
index >= parentPath.length ||
isSameToken(childToken, parentPath[index]),
index >= parentSimplePath.length ||
isSameProp(childToken, parentSimplePath[index]),
)
)
}

const hasSamePath = function (paths, pathA) {
return paths.some((pathB) => isSamePath(pathA, pathB))
}

const isSamePath = function (pathA, pathB) {
return (
pathA.length === pathB.length &&
pathA.every((tokenA, index) => isSameToken(tokenA, pathB[index]))
)
}

export const isSameToken = function (tokenA, tokenB) {
const tokenTypeA = getObjectTokenType(tokenA)
const tokenTypeB = getObjectTokenType(tokenB)
return tokenTypeA === tokenTypeB && tokenTypeA.equals(tokenA, tokenB)
}

const isSameProp = function (propA, propB) {
return propA === propB
}
5 changes: 3 additions & 2 deletions src/config/normalize/lib/wild_wild_path/parsing/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const isQueryString = function (queryOrPaths) {
return typeof queryOrPaths === 'string'
}

// Simple paths are a subset of paths which uses no unions and only array|prop
// tokens.
// Simple paths are a subset of paths which uses:
// - No unions
// - Only prop tokens, and array tokens (positive only)
// Those are the ones exposed in output, as opposed to normal paths which are
// exposed in input.
export const validateSimplePath = function (simplePath) {
Expand Down

0 comments on commit d76f703

Please sign in to comment.