UNPKG

contexture-mongo

Version:
42 lines (41 loc) 1.07 kB
import F from "futil"; import _ from "lodash/fp.js"; let joinmap = { all: "$and", any: "$or", none: "$nor" }; let hasValue = _.flow( // NOTE: Don't change the below, otherwise things will explode! // Cascade can take a third arg which causes this to behave weird (since hasValue is actually called with extra args like the schema, etc) // See https://github.com/smartprocure/futil-js/issues/218 (x) => F.cascade(["value", "values"], x), _.castArray, _.compact, _.negate(_.isEmpty) ); var text_default = { hasValue, filter: (node) => ({ [joinmap[node.join || "all"]]: _.map( (val) => ({ [node.field]: { $regex: { containsWord: val, startsWith: `^${val}`, wordStartsWith: `\\b${val}`, endsWith: `${val}$`, wordEndsWith: `${val}\\b`, is: `^${val}$`, containsExact: `\\b${val}\\b` }[node.operator], $options: "i" } }), node.values || [node.value] ) }) }; export { text_default as default };