Skip to content

Instantly share code, notes, and snippets.

@joshburgess
Created February 11, 2018 21:22
Show Gist options
  • Save joshburgess/a840a35056d9b1e0cf4e07933027ee49 to your computer and use it in GitHub Desktop.
Save joshburgess/a840a35056d9b1e0cf4e07933027ee49 to your computer and use it in GitHub Desktop.
import all from 'ramda/src/all'
import chain from 'ramda/src/chain'
import compose from 'ramda/src/compose'
import contains from 'ramda/src/contains'
import curry from 'ramda/src/curry'
import equals from 'ramda/src/equals'
import filter from 'ramda/src/filter'
import flip from 'ramda/src/flip'
import join from 'ramda/src/join'
import map from 'ramda/src/map'
import omit from 'ramda/src/omit'
import toLower from 'ramda/src/toLower'
import trim from 'ramda/src/trim'
import values from 'ramda/src/values'
import zip from 'ramda/src/zip'
import { OMITTED_UNSEARCHABLE_JSON_PROPS } from '../constants'
import {
// log,
// tapLog,
// tapLogL,
noOp,
isString,
} from './'
const joinLowerCaseStrings = compose(join('|'), map(toLower), filter(isString))
const omitUnsearchable = map(omit(OMITTED_UNSEARCHABLE_JSON_PROPS))
const getSearchableValues = compose(map(values), omitUnsearchable)
const zipOrigAndSearchableData = data => zip(data, getSearchableValues(data))
const toClean = compose(toLower, trim)
const foundWithin = flip(contains)
const allTrue = all(equals(true))
const toResults = ([record, isAllFoundWithin]) => isAllFoundWithin ? [record] : []
const toIsAllFoundWithin = phraseArray => ([record, searchable]) => {
const cleanArray = map(toClean)(phraseArray)
const foundWithinSearchable = foundWithin(searchable)
const allFoundWithin = compose(allTrue, map(foundWithinSearchable))
return [record, allFoundWithin(cleanArray)]
}
const toJoinedLower = ([record, stringArray]) => [record, joinLowerCaseStrings(stringArray)]
const filterByPhraseArray = phraseArray => !phraseArray || !phraseArray.length
? noOp
: compose(
chain(toResults),
map(compose(toIsAllFoundWithin(phraseArray), toJoinedLower)),
zipOrigAndSearchableData,
)
export default curry(filterByPhraseArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment