Created
April 24, 2018 11:29
-
-
Save innerdaze/49307659245104bff94a0f5a36af7fbf to your computer and use it in GitHub Desktop.
Hot Ramda XXX Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint no-unused-vars: 0 */ | |
import { | |
prop, | |
values, | |
filter, | |
startsWith, | |
contains, | |
compose, | |
converge, | |
concat, | |
toLower, | |
either, | |
curry, | |
flatten, | |
groupBy, | |
ifElse, | |
propSatisfies, | |
isNil, | |
F | |
} from 'ramda' | |
const getProductName = prop('ProductName') | |
const getProductId = prop('ProductID') | |
const barcodeToLowerCase = compose(toLower, prop('Barcode')) | |
const productNameToLowerCase = compose(toLower, getProductName) | |
const productIdToLowerCase = compose(toLower, getProductId) | |
const stringContainsCaseInsensitive = compose(contains, toLower) | |
const stringStartsWithCaseInsensitive = compose(startsWith, toLower) | |
const matchBarcodeByBarcode = query => | |
compose( | |
stringStartsWithCaseInsensitive(query), | |
barcodeToLowerCase | |
) | |
const matchBarcodeByProductId = query => | |
compose( | |
stringStartsWithCaseInsensitive(query), | |
productIdToLowerCase | |
) | |
const productNameIsEmpty = propSatisfies(isNil, 'ProductName') | |
const matchBarcodeByProductName = query => | |
ifElse( | |
productNameIsEmpty, | |
F, | |
compose( | |
stringStartsWithCaseInsensitive(query), | |
productNameToLowerCase | |
) | |
) | |
const searchBarcodesByProductNameOrBarcode = curry((barcodesById, query) => | |
values( | |
filter( | |
either( | |
matchBarcodeByBarcode(query), | |
matchBarcodeByProductName(query) | |
), | |
barcodesById | |
) | |
) | |
) | |
const searchBarcodesByProductIdOrBarcode = curry((barcodesById, query) => | |
values( | |
filter( | |
either( | |
matchBarcodeByBarcode(query), | |
matchBarcodeByProductId(query) | |
), | |
barcodesById | |
) | |
) | |
) | |
const searchBarcodesByBarcode = curry((barcodesById, query) => | |
values( | |
filter( | |
matchBarcodeByBarcode(query), | |
barcodesById | |
) | |
) | |
) | |
const searchBarcodesByProductId = curry((barcodesById, query) => | |
values( | |
filter( | |
matchBarcodeByProductId(query), | |
barcodesById | |
) | |
) | |
) | |
const matchProductByProductName = query => | |
ifElse( | |
productNameIsEmpty, | |
F, | |
compose( | |
stringContainsCaseInsensitive(query), | |
productNameToLowerCase | |
) | |
) | |
const matchProductByProductId = query => | |
compose( | |
stringStartsWithCaseInsensitive(query), | |
productIdToLowerCase | |
) | |
const searchProductsByIdOrName = curry((productsById, query) => | |
values( | |
filter( | |
either( | |
matchProductByProductName(query), | |
matchProductByProductId(query) | |
), | |
productsById | |
) | |
) | |
) | |
const searchProductsByProductName = curry((productsById, query) => | |
values( | |
filter( | |
matchProductByProductName(query), | |
productsById | |
) | |
) | |
) | |
const searchProductsByProductId = curry((productsById, query) => | |
values( | |
filter( | |
matchProductByProductId(query), | |
productsById | |
) | |
) | |
) | |
const searchProductsAndBarcodes = curry((productsById, barcodesById) => converge( | |
concat, | |
[ | |
searchProductsByProductName(productsById), | |
searchBarcodesByProductNameOrBarcode(barcodesById) | |
] | |
)) | |
const searchAndGroupByProductId = curry((productsById, barcodesById) => compose( | |
groupBy( | |
getProductId | |
), | |
searchProductsAndBarcodes(productsById, barcodesById) | |
)) | |
const flattenedSearchAndGroupByProductId = curry((productsById, barcodesById) => compose( | |
flatten, | |
values, | |
searchAndGroupByProductId(productsById, barcodesById) | |
)) | |
export default flattenedSearchAndGroupByProductId |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment