all files / lib/collection/filter/ metadata.js

100% Statements 13/13
100% Branches 6/6
100% Functions 3/3
100% Lines 9/9
1 branch Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34                                     74×     74×            
import matches from 'lodash/matches';
 
let matchFn;
 
/**
 * Metadata filter. Checks if a file.data object matches all the configured
 * filter options.
 * @example
 * let filterConfig = {
 *   draft: true
 * };
 * file.data = {
 *   title: 'foo',
 *   draft: true
 * };
 * metadataFilter(filterConfig, file); // true
 * @param {File} file File we're checking.
 * @param {Object} filterConfig Filter config object.
 * @return {boolean} If the File matches the filterConfig object.
 */
function metadataFilter(file, filterConfig) {
  if (!matchFn) {
    matchFn = matches(filterConfig);
  }
 
  return matchFn(file.data);
}
 
metadataFilter.reset = () => {
  matchFn = undefined;
};
 
export default metadataFilter;