generated from Exabyte-io/template-definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
Chore/sof 6463 #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Chore/sof 6463 #45
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
85dd591
feat: add basic filter function for entities
azech-hqs 4da934b
chore: add fn to index
azech-hqs 1d4df18
chore: add isContained array utility fn
azech-hqs 56175bc
chore: extend filter fn to support multiPaths
azech-hqs f2e7ca4
revert: isContained array utility fn
azech-hqs ae2b542
chore: adjust filter fn to support regex multipath
azech-hqs e6f0c15
chore: add file utility fn
azech-hqs d5c093c
chore: clean up utility index
azech-hqs b0b3a6e
chore: add utility fn for dirs
azech-hqs 5ba232e
chore: use pure JS where possible
azech-hqs 53d1c83
Merge branch 'main' into chore/SOF-6463
azech-hqs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import lodash from "lodash"; | ||
|
|
||
| /** | ||
| * Check if one path matches regular expression or exact string. | ||
| * @param {{path: string}} pathObject - Entity or object with path property | ||
| * @param {Array<{path: string}|{regex: RegExp}>} filterObjects - Filter conditions | ||
| * @return {boolean} | ||
| */ | ||
| function isPathSupported(pathObject, filterObjects) { | ||
| return filterObjects.some((filterObj) => { | ||
| if (filterObj.path) { | ||
| return filterObj.path === pathObject.path; | ||
| } | ||
| if (filterObj.regex) { | ||
| return filterObj.regex.test(pathObject.path); | ||
| } | ||
| return false; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Check if _all_ paths in concatenated path match filtering conditions. | ||
| * @param {{path: string}} pathObject - Path object with concatenated path (multipath) | ||
| * @param {string} multiPathSeparator - String sequence used for concatenation of paths | ||
| * @param {Array<{path: string}|{regex: RegExp}>} filterObjects - Filter conditions | ||
| * @return {boolean} | ||
| */ | ||
| function isMultiPathSupported(pathObject, multiPathSeparator, filterObjects) { | ||
| const expandedPaths = pathObject.path.split(multiPathSeparator).map((p) => ({ path: p })); | ||
| return expandedPaths.every((expandedPath) => isPathSupported(expandedPath, filterObjects)); | ||
| } | ||
|
|
||
| /** | ||
| * Filter list of entity paths or entities by paths and regular expressions. | ||
| * @param {Object[]} entitiesOrPaths - Array of objects defining entity path | ||
| * @param {Array<{ path: string }|{ regex: string }|{ regex: RegExp }>} filterObjects - Array of path or regular expression objects | ||
| * @param {string} multiPathSeparator - string by which paths should be split | ||
| * @return {Object[]} - filtered entity path objects or entities | ||
| */ | ||
| export function filterEntityList({ entitiesOrPaths, filterObjects = [], multiPathSeparator = "" }) { | ||
| const filterObjects_ = filterObjects.map((o) => (o.regex ? { regex: new RegExp(o.regex) } : o)); | ||
|
|
||
| let filtered; | ||
| if (multiPathSeparator) { | ||
| filtered = entitiesOrPaths.filter((e) => | ||
| isMultiPathSupported(e, multiPathSeparator, filterObjects_), | ||
| ); | ||
| } else { | ||
| filtered = entitiesOrPaths.filter((e) => isPathSupported(e, filterObjects_)); | ||
| } | ||
|
|
||
| return lodash.uniqBy(filtered, "path"); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { expect } from "chai"; | ||
|
|
||
| import { filterEntityList } from "../../src/utils/filter"; | ||
|
|
||
| describe("entity filter", () => { | ||
| const entities = [ | ||
| { name: "A", path: "/root/entity/a" }, | ||
| { name: "B", path: "/root/entity/b" }, | ||
| { name: "C", path: "/root/entity/c" }, | ||
| { name: "D", path: "/root/entity/d" }, | ||
| ]; | ||
|
|
||
| it("should filter an entity list with paths", () => { | ||
| const filterObjects = [{ path: "/root/entity/b" }, { path: "/root/entity/c" }]; | ||
| const filtered = filterEntityList({ filterObjects, entitiesOrPaths: entities }); | ||
| const expected = [ | ||
| { name: "B", path: "/root/entity/b" }, | ||
| { name: "C", path: "/root/entity/c" }, | ||
| ]; | ||
| expect(filtered).to.have.deep.members(expected); | ||
| }); | ||
|
|
||
| it("should filter an entity list with regular expressions", () => { | ||
| const filterObjects = [{ regex: /\/root\/entity\/[bc]/ }]; | ||
| const filtered = filterEntityList({ filterObjects, entitiesOrPaths: entities }); | ||
| const expected = [ | ||
| { name: "B", path: "/root/entity/b" }, | ||
| { name: "C", path: "/root/entity/c" }, | ||
| ]; | ||
| expect(filtered).to.have.deep.members(expected); | ||
| }); | ||
|
|
||
| it("should filter an entity list with both paths and regular expressions", () => { | ||
| const filterObjects = [{ path: "/root/entity/b" }, { regex: /\/root\/entity\/[c]/ }]; | ||
| const filtered = filterEntityList({ filterObjects, entitiesOrPaths: entities }); | ||
| const expected = [ | ||
| { name: "B", path: "/root/entity/b" }, | ||
| { name: "C", path: "/root/entity/c" }, | ||
| ]; | ||
| expect(filtered).to.have.deep.members(expected); | ||
| }); | ||
|
|
||
| it("should filter an entity list containing concatenated paths", () => { | ||
| const filterObjects = [{ path: "/root/entity/b" }, { path: "/root/entity/c" }]; | ||
| const multiPathEntities = [ | ||
| { name: "AB", path: "/root/entity/a::/root/entity/b" }, | ||
| { name: "BC", path: "/root/entity/b::/root/entity/c" }, | ||
| ]; | ||
| const multiPathSeparator = "::"; | ||
| const filtered = filterEntityList({ | ||
| filterObjects, | ||
| entitiesOrPaths: multiPathEntities, | ||
| multiPathSeparator, | ||
| }); | ||
| const expected = [{ name: "BC", path: "/root/entity/b::/root/entity/c" }]; | ||
| expect(filtered).to.have.deep.members(expected); | ||
| }); | ||
|
|
||
| it("should filter an entity list containing concatenated paths using regex", () => { | ||
| const filterObjects = [{ path: "/root/entity/b" }, { regex: /\/root\/entity\/[c]/ }]; | ||
| const multiPathEntities = [ | ||
| { name: "AB", path: "/root/entity/a::/root/entity/b" }, | ||
| { name: "BC", path: "/root/entity/b::/root/entity/c" }, | ||
| ]; | ||
| const multiPathSeparator = "::"; | ||
| const filtered = filterEntityList({ | ||
| filterObjects, | ||
| entitiesOrPaths: multiPathEntities, | ||
| multiPathSeparator, | ||
| }); | ||
| const expected = [{ name: "BC", path: "/root/entity/b::/root/entity/c" }]; | ||
| expect(filtered).to.have.deep.members(expected); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use npm https://www.npmjs.com/package/path