Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
'use strict';
const lunr = require('lunr');
const {Builder: lunrBuilder, Index: lunrIndex} = lunr;
const path = require('path');
const fs = require('fs');
const async = require('async');
// customize Lunr so it normalizes punctuation
function fixToken(token) {
const tokenString = token.toString();
const rewrittenToken = tokenString.replace(/[\(\)\!\?\'\"#&’\.\[\]\{\}…\,”]/g, '');
return rewrittenToken.length !== tokenString.length ? token.update(() => rewrittenToken) : token;
}
lunr.Pipeline.registerFunction(fixToken, 'normalizeTokens');
function normalizeTokens(builder) {
builder.pipeline.add(fixToken);
builder.searchPipeline.add(fixToken);
}
/**
* An in-memory search engine implementation with file storage suitable for small sites.
*/
class LunrIndex {
/**
* Constructs a Lunr index from the provided id filter and map
* @param {object} scope The scope.
* @param {RegExp} [idFilter] A regular expression that validates content item ids before they are read and indexed.
* @param {Function} map The map function takes a content item and returns an index entry, or null.
* An index entry should have an id property. The index will automatically add an `id` property set to be the id of the item
buildIndex() {
log.info('Splicing gym metadata and indexing gym data...');
const gymsBase = require('PgP-Data/data/gyms'),
gymsMetadata = require('PgP-Data/data/gyms-metadata'),
mergedGyms = gymsBase
.map(gym => Object.assign({}, gym, gymsMetadata[gym.gymId])),
stopwordFilter = this.stopWordFilter,
blacklistWordFilter = lunr.generateStopWordFilter(settings.blacklistWords);
this.blacklistWordFilter = blacklistWordFilter;
lunr.Pipeline.registerFunction(blacklistWordFilter, 'blacklistWords');
lunr.Pipeline.registerFunction(stopwordFilter, 'customStopwords');
this.gyms = new Map(mergedGyms
.map(gym => [gym.gymId, gym]));
this.regionMap = require('PgP-Data/data/region-map');
this.regionGraph = require('PgP-Data/data/region-graph');
this.index = lunr(function () {
// reference will be the entire gym object so we can grab whatever we need from it (GPS coordinates, name, etc.)
this.ref('object');
// static fields for gym name, nickname, and description
this.field('name');
this.field('nickname');
this.field('description');
buildIndex() {
log.info('Splicing gym metadata and indexing gym data...');
const gymsBase = require('PgP-Data/data/gyms'),
gymsMetadata = require('PgP-Data/data/gyms-metadata'),
mergedGyms = gymsBase
.map(gym => Object.assign({}, gym, gymsMetadata[gym.gymId])),
stopwordFilter = this.stopWordFilter,
blacklistWordFilter = lunr.generateStopWordFilter(settings.blacklistWords);
this.blacklistWordFilter = blacklistWordFilter;
lunr.Pipeline.registerFunction(blacklistWordFilter, 'blacklistWords');
lunr.Pipeline.registerFunction(stopwordFilter, 'customStopwords');
this.gyms = new Map(mergedGyms
.map(gym => [gym.gymId, gym]));
this.regionMap = require('PgP-Data/data/region-map');
this.regionGraph = require('PgP-Data/data/region-graph');
this.index = lunr(function () {
// reference will be the entire gym object so we can grab whatever we need from it (GPS coordinates, name, etc.)
this.ref('object');
// static fields for gym name, nickname, and description
this.field('name');
this.field('nickname');
this.field('description');
constructor() {
lunr.Pipeline.registerFunction(Gym.blacklistWordFilter, 'blacklistWords');
lunr.Pipeline.registerFunction(Search.stopWordFilter, 'customStopwords');
}
constructor() {
lunr.Pipeline.registerFunction(Gym.blacklistWordFilter, 'blacklistWords');
lunr.Pipeline.registerFunction(Search.stopWordFilter, 'customStopwords');
}