Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | 21x 74x 73x 1x 3x 2x 2x 110x 110x 48x 62x 30x 32x 4x 28x 13x 13x 13x 1x 12x 1x 11x 11x 11x 15x 5x 5x 5x 1x 4x 1x 3x 3x 10x 3x 3x 7x 7x 4x 4x 3x 2x 2x 2x 2x 2x 2x 2x 1x 74x 74x 67x 30x 30x 37x 57x 57x 33x 57x 57x 5x 5x 3x 5x 57x 10x 33x 33x 36x 33x 5x 5x 82x 82x 82x 98x 72x 15x 57x 98x 82x 23x 23x 10x 23x | /*
* Part of Pleiar.no - a collection of tools for nurses
*
* Copyright (C) Fagforbundet 2019
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// @flow
import externalResources from '../data/datasett/external-resources.json';
import dictionary from '../data/datasett/dictionary.json';
import labValues from '../data/datasett/lab.json';
import nutrition from '../data/datasett/nutricalc-data.json';
import external from '../data/externallyIndexed.json';
import searchMetadata from '../data/search-meta.json';
import lunr from './helper.lunr';
import lunrResultSnippet from '../src/helper.lunrSnippet';
import type { DictionaryEntry, ExternalResourcesEntry, LabValueEntry, ToolListEntry, NutritionInfoEntry, ExternallyIndexedItem, SearchMetadataEntry } from './types/data';
import type { lunrResult, lunrMetadata } from './types/libs';
import { HandbookEntry } from './data.handbook';
import { QuickRefEntry } from './data.quickref';
import { removeMarkdown } from './helper.markdown';
import ToolsList from './tools.list';
import { numbers } from './helper.numbers';
import { asyncLoaderRole } from './helper.asyncLoader';
/**
* A single entry in the search index
*/
export type IndexEntry = {|
id: string,
tittel?: string,
body?: string,
keywords?: string,
url?: string
|};
/**
* A single result from getResultFromRef
*/
export type ResolvedLunrRef =
{|
type: "dictionary",
dictionary: DictionaryEntry
|} |
{|
type: "externalResources",
externalResources: ExternalResourcesEntry,
|} |
{|
type: "labValue",
labValues: LabValueEntry
|} |
{|
type: "handbook",
handbook: HandbookEntry
|} |
{|
type: "tool",
tool: ToolListEntry
|} |
{|
type: "nutrition",
nutrition: NutritionInfoEntry,
|} |
{|
type: "external",
external: ExternallyIndexedItem
|} |
{|
type: "quickref",
quickref: QuickRefEntry
|} |
{|
type: "quiz",
quiz: SearchMetadataEntry,
|};
/**
* The signature required from a reducer function provided to
* `PleiarSearcher.search`
*/
type SearchReducer = (accumulator: Array<lunrResult>, result: lunrResult ) => Array<lunrResult>;
type LunrType = typeof lunr ;
/**
* PleiarSearcher is the indexer for the entire site. It wraps `lunr`, performs
* indexing and lets you search this index. It also adds some automatic
* fuzzyness to the searcher, which gets applied if the search has no hits
* as-written.
*/
const PleiarSearcher = {
...asyncLoaderRole,
__index: null,
/**
* Private method that returns our `lunr` instance. If `initialize` has not been called
* this will throw().
*/
_index (): LunrType
{
if(PleiarSearcher.__index !== null)
{
return PleiarSearcher.__index;
}
throw('PleiarSearcher called without first performing initialization');
},
/**
* _performAsyncImport method for {@link asyncLoaderRole}
*/
_performAsyncImport (): Promise<null>
{
return import(/* webpackChunkName: "search-index", webpackPreload: true */ '../data/search-index.json');
},
/**
* _loadedData method for {@link asyncLoaderRole}
*/
_loadedData
// $FlowIssue[signature-verification-failure] - This is data imported for lunr, we don't have a type for it
(data): null
{
PleiarSearcher.__index = lunr.Index.load(data);
return null;
},
/**
* Retrieves the actual data for a single result `ref`. Optionally
* accepts the associated `lunrMetadata` as well, which is used for
* `handbook` results to build an excerpt from the document.
*/
getResultFromRef(ref: string, metadata?: lunrMetadata | null): ResolvedLunrRef
{
const refParts = ref.split('/');
if(refParts[0] === "dictionary")
{
return {
type: refParts[0],
dictionary: (dictionary[ refParts[1] ]: DictionaryEntry),
};
}
else if(refParts[0] === "externalResources")
{
return {
type: refParts[0],
externalResources: (externalResources[ refParts[1] ]: ExternalResourcesEntry),
};
}
else if(refParts[0] === "labValue")
{
return {
type: refParts[0],
labValues: (labValues.values[ refParts[1] ]: LabValueEntry)
};
}
else if(refParts[0] === "handbook")
{
refParts.shift();
const handbook = new HandbookEntry(refParts);
if (!handbook.exists)
{
throw('Wanted to look up handbook entry that does not exist: '+refParts.join('/'));
}
if(metadata === undefined || metadata === null)
{
throw('PleiarSearcher.getResultFromRef: fetching handbook entries requires metadata');
}
const bodyString = removeMarkdown(handbook.content);
handbook.searchSnippet = lunrResultSnippet(bodyString,metadata);
return {
type: "handbook",
handbook: handbook
};
}
else if(refParts[0] === "tool")
{
const category = numbers.parseFloat(refParts[1]);
const component = numbers.parseFloat(refParts[2]);
let tool: ToolListEntry;
if ( ToolsList[ category ] === undefined)
{
throw('Wanted to look up tools entry where the category does not exist: '+refParts.join('/'));
}
if ( ToolsList[ category ].tools[ component ] === undefined)
{
throw('Wanted to look up tools entry where the tools entry does not exist: '+refParts.join('/'));
}
else
{
tool = ToolsList[ category ].tools[ component ];
}
return {
type: "tool",
tool
};
}
else if(refParts[0] === "nutrition")
{
const rest = ref.substr(10);
return {
type: "nutrition",
nutrition: (nutrition.basicGroups[rest]: NutritionInfoEntry)
};
}
else Iif(refParts[0] === "quiz")
{
const rest = ref.substr(5);
return {
type: "quiz",
quiz: (searchMetadata.quiz[rest]: SearchMetadataEntry),
};
}
else if(refParts[0] === "external")
{
const rest = ref.substr(9);
return {
type: "external",
external: (external[rest]: ExternallyIndexedItem)
};
}
else if(refParts[0] === "quickref")
{
refParts.shift();
const quickref = new QuickRefEntry(refParts);
Iif (!quickref.exists)
{
throw('Wanted to look up quickref entry that does not exist: '+refParts.join('/'));
}
Iif(metadata === undefined || metadata === null)
{
throw('PleiarSearcher.getResultFromRef: fetching quickref entries requires metadata');
}
const bodyString = removeMarkdown(quickref.content);
quickref.searchSnippet = lunrResultSnippet(bodyString,metadata);
return {
type: "quickref",
quickref: quickref
};
}
else
{
throw('searcher.getResultFromRef: '+ref+': unrecognized ref type "'+refParts[0]+'"');
}
},
/**
* Perform a search. Searches for string, and optionally applies a reducer
* to each result before returning.
*/
search (expression: string, reducer?: SearchReducer): Array<lunrResult>
{
/*
* lunr has a problem when searching for ÆØÅ-prefixed strings. Therefore
* we apply this hack as a workaround, permitting it to be a bit more
* fuzzy in its matching.
*
* There's a test for this in tests/dictionary.data.test.js that will start
* failing once the problem in lunr is fixed.
*/
Iif (/^[ÆØÅæøå]/.test(expression))
{
expression = '*'+expression;
}
const result = PleiarSearcher._index().search(expression);
if(reducer)
{
reducer = reducer.bind(PleiarSearcher);
return result.reduce(reducer,[]);
}
return result;
},
/**
* A safe variant of searching that handles cases where the user has
* entered an invalid expression, which would usually cause `lunr` to
* `throw`. This will instead remove any special characters from the search
* expression and try again. It can also apply a custom function to modify
* the expression before it gets run.
*/
_safeSearch(expression: string, reducer?: SearchReducer, expressionPreparer?: (string) => string): Array<lunrResult>
{
let searchExp: string = expression;
if(expressionPreparer)
{
searchExp = expressionPreparer( searchExp );
}
let result: Array<lunrResult>;
try
{
result = PleiarSearcher.search(searchExp, reducer);
}
catch(e)
{
searchExp = expression.replace(/[^\w\s]/g,'');
if(expressionPreparer)
{
searchExp = expressionPreparer( searchExp );
}
result = PleiarSearcher.search(searchExp, reducer);
}
return result;
},
/**
* A wrapper for `_safeSearch` that adds increasing levels of fuzzyness to the
* result if it yields no results.
*/
_safeFuzzySearch(expression: string, reducer?: SearchReducer): Array<lunrResult>
{
for(let fuzzyness: number = 0; fuzzyness < 4; fuzzyness++)
{
const searchExp = PleiarSearcher._mkFuzzySearch(expression,fuzzyness);
const result = PleiarSearcher._safeSearch(searchExp,reducer,(str) =>
{
return PleiarSearcher._mkFuzzySearch(str, fuzzyness);
});
if(result.length > 0)
{
return result;
}
}
return [];
},
/**
* Applies fuzzyness of the level supplied to the expression, then returns
* it.
*/
_mkFuzzySearch (expression: string, fuzzyness: number): string
{
const components = expression.split(/\s+/);
const result = [];
for(let component: string of components)
{
if (/^\w/.test(component))
{
if(fuzzyness === 0)
{
component = '*'+component+'*';
}
else
{
component = component+'~'+fuzzyness;
}
}
result.push(component);
}
return result.join(' ');
},
/**
* This is a safe search method (won't `throw`) that also applies fuzzyness
* if needed. Accepts a `reducer` that it will pass on down the chain to
* `search()` if needed.
*/
safePartialSearch(expression: string, reducer?: SearchReducer): Array<lunrResult>
{
let result: Array<lunrResult> = PleiarSearcher._safeSearch( expression, reducer );
if(result.length == 0)
{
result = PleiarSearcher._safeFuzzySearch( expression, reducer );
}
return result;
},
};
export default PleiarSearcher;
|