UNPKG

refined-text-search

Version:

Tokenize a search query with refined options (like Google's) and match against a target

150 lines 7.86 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../src/index"); const TEXT = ` Up am intention on dependent questions oh elsewhere september. No betrayed pleasure possible jointure we in throwing. And can event rapid any shall woman green. `; function oneStepMatch(query) { const tokens = index_1.tokenize(query); return index_1.match(tokens, TEXT); } describe('tokenize', () => { it('single word', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize(' Dependent ')).toEqual([{ term: 'dependent' }]); })); it('multiple words', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize('on dependent')).toEqual([{ term: 'on' }, { term: 'dependent' }]); })); it('multiple words with lots of spaces', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize(' on dependent ')).toEqual([ { term: 'on' }, { term: 'dependent' }, ]); })); it('single phrase', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize('"on dependent"')).toEqual([{ term: 'on dependent' }]); })); it('multiple phrases', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize('"on dependent" "herp di derp" "flerp"')).toEqual([ { term: 'on dependent' }, { term: 'herp di derp' }, { term: 'flerp' }, ]); })); it('exclude', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize('-herp -"herp derp"')).toEqual([ { term: 'herp', exclude: true }, { term: 'herp derp', exclude: true }, ]); })); it('exact', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize('=herp ="herp derp"')).toEqual([ { term: 'herp', exact: true }, { term: 'herp derp', exact: true }, ]); })); it('or', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize('herp OR derp flerp')).toEqual([ { or: true, children: [[{ term: 'herp' }], [{ term: 'derp' }, { term: 'flerp' }]] }, ]); expect(index_1.tokenize('herp OR derp -flerp')).toEqual([ { or: true, children: [[{ term: 'herp' }], [{ term: 'flerp', exclude: true }, { term: 'derp' }]], }, ]); expect(index_1.tokenize('herp OR derp OR flerp')).toEqual([ { or: true, children: [[{ term: 'herp' }], [{ term: 'derp' }], [{ term: 'flerp' }]], }, ]); expect(index_1.tokenize('herp OR =derp -flerp')).toEqual([ { or: true, children: [ [{ term: 'herp' }], [ { term: 'flerp', exclude: true }, { term: 'derp', exact: true }, ], ], }, ]); })); it('kitchen sink', () => __awaiter(void 0, void 0, void 0, function* () { expect(index_1.tokenize('"Hello World" my dear -"how are you" -horrible oh =that ="is sad"')).toEqual([ { term: 'how are you', exclude: true }, { term: 'horrible', exclude: true }, { term: 'hello world' }, { term: 'my' }, { term: 'dear' }, { term: 'oh' }, { term: 'that', exact: true }, { term: 'is sad', exact: true }, ]); })); }); describe('match', () => { it('single word', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('dependent')).toEqual(true); expect(oneStepMatch('up')).toEqual(true); expect(oneStepMatch('herp')).toEqual(false); })); it('multiple words', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('dependent pleasure')).toEqual(true); expect(oneStepMatch('dependent herp')).toEqual(false); expect(oneStepMatch('herp derp')).toEqual(false); })); it('single phrase', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('"dependent questions"')).toEqual(true); expect(oneStepMatch('"dependent pleasure"')).toEqual(false); })); it('multiple phrases', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('"dependent questions" "in throwing"')).toEqual(true); expect(oneStepMatch('"dependent pleasure" "in throwing"')).toEqual(false); expect(oneStepMatch('"dependent pleasure" "in throwing" "shall green"')).toEqual(false); })); it('exclude', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('-herp')).toEqual(true); expect(oneStepMatch('-"possible throwing"')).toEqual(true); expect(oneStepMatch('-pleasure')).toEqual(false); expect(oneStepMatch('-"pleasure possible"')).toEqual(false); })); it('exact', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('="Up am intention on dependent questions oh elsewhere september. No betrayed pleasure possible jointure we in throwing. And can event rapid any shall woman green."')).toEqual(true); expect(oneStepMatch('="Up am intention on dependent questions oh elsewhere september. No betrayed pleasure possible jointure we in throwing. And can event rapid any shall woman green"')).toEqual(false); expect(oneStepMatch('=Up am intention on dependent questions oh elsewhere september. No betrayed pleasure possible jointure we in throwing. And can event rapid any shall woman green.')).toEqual(false); })); it('or', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('possible OR woman')).toEqual(true); expect(oneStepMatch('herp OR derp | intention')).toEqual(true); expect(oneStepMatch('herp OR derp')).toEqual(false); expect(oneStepMatch('herp OR "any shell"')).toEqual(false); expect(oneStepMatch('herp OR "any shall"')).toEqual(true); expect(oneStepMatch('-possible OR -woman')).toEqual(false); expect(oneStepMatch('herp OR derp | -intention')).toEqual(false); expect(oneStepMatch('herp OR -derp')).toEqual(true); expect(oneStepMatch('=herp OR -derp')).toEqual(true); })); it('kitchen sink', () => __awaiter(void 0, void 0, void 0, function* () { expect(oneStepMatch('"possible jointure" -"herp derp" -derp woman')).toEqual(true); expect(oneStepMatch('-"possible jointure" -"herp derp" -derp woman')).toEqual(false); expect(oneStepMatch('"possible jointure" -"herp derp" -derp -woman')).toEqual(false); expect(oneStepMatch('"possible jointure" "herp derp" -derp woman')).toEqual(false); expect(oneStepMatch('"possible jointure" -"herp derp" derp woman')).toEqual(false); expect(oneStepMatch('="possible jointure" -"herp derp" -derp woman')).toEqual(false); })); }); //# sourceMappingURL=index.spec.js.map