-
Notifications
You must be signed in to change notification settings - Fork 2
Description
After 2 longs days of searching and dispair, your library seems to save me. Thank you.
I am a bit worried about the viability, though, seeing your previous responses. Is this a dead project for you, or would you still consider taking PRs, and releasing new versions?
Context
I am in need to determine whether a first pattern raa is covered by another ram.
Interesting references I found are:
- Determining whether a regex is a subset of another
- The Inclusion Problem for Regular Expressions
- Does an algorithm exist which can determine whether one regular language matches any input another regular language matches?
- Testing the Equivalence of Regular Languages
- Is one regular language subset of another? (seems to be an approach that is not as good as the others)
- Algorithm to find out whether the matches for two Glob patterns (or Regular Expressions) intersect
The approach that I found referenced was
- create
ram|raa, the pattern that expresses that a string must matchramorraa - derive DFA(
ram|raa) and DFA(ram), and minimize them - if the 2 resulting minimized DFAs are isomorph,
raais covered byram
Indeed, ram|raa (ram or raa) allows more strings to match than ram alone. If however ram|raa turns out to match exactly the same set of strings as ram alone, it means that all the strings that are matched by raa are already in the set of strings matched by ram, i.e., raa is covered by ram.
Alternatively, we can determine the intersection of raa and ram:
- create
ram&raa, the pattern that expresses that a string must match bothramandraa - derive DFA(
ram&raa) and DFA(raa), and minimize them - if the 2 resulting minimized [DFA]s are isomorph,
raais covered byram
Indeed, ram&raa (ram and raa) allows less strings to match than raa alone. If however ram&raa turns out to match exactly the same set of strings as raa alone, it means that there are no strings that are matched by raa that are not matched by ram, i.e., raa is covered by ram.
This now seems to be realised trivially by your library:
const intersect = require('glob-intersection')
function minimize (pattern) {
return intersect(pattern, pattern)
}
function covered (raaPattern, ramPattern) {
let intersection = intersect(raaPattern, ramPattern)
console.log(`${raaPattern} ∩ ${ramPattern} = ${intersection}`)
const result = typeof intersection === 'string' && intersection === minimize(raaPattern)
console.log(`${raaPattern} ⊆ ${ramPattern} ? ${result}`)
return result
}
covered('/I/*/p523564.faa.bar.baz/**', '/I/**')
covered('/I/**', '/I/*/p523564.faa.bar.baz/**')
covered('/I/*/p523564.faa.bar.baz/**', '/I/*')
covered('/I/*/p523564.faa.bar.baz/service/instance', '/I/host/p523564.faa.bar.baz/**')
covered('{GET,PUT,DELETE}', '*')
covered('*', '{GET,PUT,DELETE}')
covered('PUT', 'DELETE')
covered('PUT', '{GET,DELETE}')
covered('{GET,PUT}', '{GET,PUT,DELETE}') // requires minimisation of raaPattern