Skip to content

Commit

Permalink
[FEATURE] Introduce ReaderFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Dec 2, 2021
1 parent c17e6ef commit 5b5f43e
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/AbstractReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class AbstractReader {
});
}

filter({resourceTagCollection, matchMode, filters}) {
const ReaderFilter = require("./ReaderFilter");
return new ReaderFilter({
reader: this,
resourceTagCollection,
matchMode,
filters
});
}

/**
* Locates resources by one or more glob patterns.
*
Expand Down
104 changes: 104 additions & 0 deletions lib/ReaderFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const AbstractReader = require("./AbstractReader");

/**
* Resource Locator ReaderCollection
*
* @public
* @memberof module:@ui5/fs
* @augments module:@ui5/fs.AbstractReader
*/
class ReaderFilter extends AbstractReader {
/**
* The constructor.
*
* @param {object} parameters Parameters
* @param {module:@ui5/fs.AbstractReader} parameters.reader A resource reader
* @param {module:@ui5/fs.ResourceTagCollection} parameters.resourceTagCollection
* Resource tag collection to apply filters onto
* @param {object[]} parameters.filters Filters
* @param {string} parameters.matchMode Whether to match "any", "all" or "none"
*/
constructor({reader, resourceTagCollection, filters, matchMode}) {
super();
if (!reader) {
throw new Error(`Missing parameter "reader"`);
}
if (!resourceTagCollection) {
throw new Error(`Missing parameter "resourceTagCollection"`);
}
if (!filters || !filters.length) {
throw new Error(`Missing parameter "filters"`);
}
if (!matchMode) {
throw new Error(`Missing parameter "matchMode"`);
}
this._reader = reader;
this._resourceTagCollection = resourceTagCollection;
this._filters = filters;

this._matchMode = matchMode;
}

/**
* Locates resources by glob.
*
* @private
* @param {string|string[]} pattern glob pattern as string or an array of
* glob patterns for virtual directory structure
* @param {object} options glob options
* @param {module:@ui5/fs.tracing.Trace} trace Trace instance
* @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving to list of resources
*/
async _byGlob(pattern, options, trace) {
const result = await this._reader._byGlob(pattern, options, trace);
return result.filter(this._filterResource.bind(this));
}

/**
* Locates resources by path.
*
* @private
* @param {string} virPath Virtual path
* @param {object} options Options
* @param {module:@ui5/fs.tracing.Trace} trace Trace instance
* @returns {Promise<module:@ui5/fs.Resource>} Promise resolving to a single resource
*/
async _byPath(virPath, options, trace) {
const result = await this._reader._byPath(virPath, options, trace);
if (result) {
if (!this._filterResource(result)) {
return null;
}
}
return result;
}

_filterResource(resource) {
let filterFunction;
let testEquality = true;
switch (this._matchMode) {
case "any":
filterFunction = "some";
break;
case "all":
filterFunction = "every";
break;
case "none":
filterFunction = "every";
testEquality = false;
break;
default:
throw Error(`Unknown match mode ${this._matchMode}`);
}
return this._filters[filterFunction](({tag, value: filterValue}) => {
const tagValue = this._resourceTagCollection.getTag(resource, tag);
if (testEquality) {
return tagValue === filterValue;
} else {
return tagValue !== filterValue;
}
});
}
}

module.exports = ReaderFilter;

0 comments on commit 5b5f43e

Please sign in to comment.