Commit 14e4534b authored by Robin Bron's avatar Robin Bron

<implicit> operator now supported; main returns factory instead now

parent a35e977e
Pipeline #65655010 passed with stages
in 2 minutes and 15 seconds
module.exports = function hasModule( name ) {
if ( require.resolve ) {
try {
return !!require.resolve(name);
} catch(e) {
return false;
}
}
try {
require(name);
return true;
} catch(e) {
return false;
}
};
import expect from 'expect';
// Setup environment
if ( 'object' !== typeof process ) process = {};
if (!process.env) process.env = {};
process.env.TEST = true;
// Load extends
expect.extend(require('jest-isa'));
// Load our module
let hasModule = require('./has-module');
test('Ensure hasModule is a function', async () => {
expect(hasModule).toBeDefined(); // Basics
expect(hasModule).isA(Function);
});
test('Verify some packages', async () => {
expect(hasModule('./has-module')).toBe(true);
expect(hasModule('./index')).toBe(true);
expect(hasModule('jest-isa')).toBe(true);
expect(hasModule('non-existent-module')).toBe(false);
expect(hasModule('pizza-courier')).toBe(false);
});
// TODO: fix this one
// test('Verify some packages without require.resolve', async () => {
//
// // Reload module
// process.env.NO_RESOLVE = true;
// console.log(process.env);
// delete require.cache[require.resolve('./has-module')];
// hasModule = require('./has-module');
//
// expect(hasModule('./has-module')).toBe(true);
// expect(hasModule('./index')).toBe(true);
// expect(hasModule('jest-isa')).toBe(true);
// expect(hasModule('non-existent-module')).toBe(false);
// expect(hasModule('pizza-courier')).toBe(false);
// });
const hasModule = require('./has-module'),
parser = (function() {
if (hasModule('lucene')) return require('lucene');
if (hasModule('lucene-queryparser')) return require('lucene-queryparser');
if (hasModule('lucene-query-parser')) return require('lucene-query-parser');
throw new Error("None of 'lucene', 'lucene-queryparser' or 'lucene-query-parser' are installed");
})();
// Factory
const lucene = module.exports = function factory( parser ) {
const lucene = module.exports = function compiler(query) {
// Parser must be given
if (!parser) {
throw new Error('No parser given. Must be one of \'lucene\',\'lucene-queryparser\',\'lucene-query-parser\'');
}
// The given parser must be compatible
if (('object' !== typeof parser) || ('function' !== typeof parser.parse)) {
throw new Error('Incompatible parser given. Must be one of \'lucene\',\'lucene-queryparser\',\'lucene-query-parser\'');
}
// Returns Function(Object):Number
function compile(query) {
if ('string' !== typeof query) return ()=>0;
if (!query) return ()=>0;
// Parse string query
if (!query) return () => 0;
if ('string' === typeof query) {
// Actually compile what was given
try {
query = parser.parse(query);
} catch (e) {
return () => 0;
} catch(e) {
return ()=>0;
}
}
// Compile combined
if (query.operator) {
return lucene.operators[query.operator](
lucene(query.left),
lucene(query.right),
);
}
// Compile multi-query
if (query.operator) {
return lucene.operators[query.operator](
compile(query.left),
compile(query.right),
);
}
// Wrapped
if (query.left) {
return lucene(query.left);
}
// Wrapped
if (query.left) {
return compile(query.left);
}
// Ensure default boost
query.boost = query.boost || 1;
// Ensure default boost
query.boost = query.boost || 1;
// Return the first detected filter
for (const filter of lucene.filters) {
if (filter.detect(query)) {
return filter.compile(query);
// Return the first detected filter
for (const filter of lucene.filters) {
if (filter.detect(query)) {
return filter.compile(query);
}
}
}
// Return no match
return () => 0;
};
// Return no match
return ()=>0;
}
// Return the data when matching
// TODO: lucene() = { f() passthrough: f() }
lucene.passthrough = function(query) {
let match = lucene(query);
return function(data) {
if(match(data)) return data;
return undefined;
// Return the data when matching
compile.passthrough = function(parser, query) {
let match = compile(query);
return function(data) {
if(match(data)) return data;
return undefined;
};
};
};
return compile;
}
// Add filters & operators
lucene.filters = require('./filters');
......
const operators = module.exports = {};
operators['OR'] = require('./or');
operators['AND'] = require('./and');
operators['OR NOT'] = require('./or-not')(operators);
operators['AND NOT'] = require('./and-not')(operators);
const operators = module.exports = {};
operators['OR'] = require('./or');
operators['AND'] = require('./and');
operators['OR NOT'] = require('./or-not')(operators);
operators['AND NOT'] = require('./and-not')(operators);
operators['<implicit>'] = require('./and')(operators);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment