import { isPureObject } from '../../../utils' /** * Stringify values following instantsearch practices. * * @param {any} value - value that needs to be stringified */ function stringifyValue(value: any) { if (typeof value === 'string') { // String return value } else if (value === undefined) { // undefined return JSON.stringify(null) } else { return JSON.stringify(value) } } /** * Recursif function wrap the deepest possible value * the following way: { value: "xx" }. * * For example: * * { * "rootField": { "value": "x" } * "nestedField": { child: { value: "y" } } * } * * recursivity continues until the value is not an array or an object. * * @param {any} value - value of a field * * @returns Record */ function wrapValue(value: any): Record { if (Array.isArray(value)) { // Array return value.map((elem) => wrapValue(elem)) } else if (isPureObject(value)) { // Object return Object.keys(value).reduce>( (nested: Record, key: string) => { nested[key] = wrapValue(value[key]) return nested }, {} ) } else { return { value: stringifyValue(value) } } } /** * Adapt Meilisearch formatted fields to a format compliant to instantsearch.js. * * @param {Record ): Record { if (!hit) return {} const _formattedResult = wrapValue(hit) const highlightedHit = { // We could not determine what the differences are between those two fields. _highlightResult: _formattedResult, _snippetResult: _formattedResult, } return highlightedHit }