Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@6ewis
Created April 29, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6ewis/a51f9f38b3cbd9d5116fbeff931b7d14 to your computer and use it in GitHub Desktop.
Save 6ewis/a51f9f38b3cbd9d5116fbeff931b7d14 to your computer and use it in GitHub Desktop.
const data = [
{
"client_number": "00001",
"client_name": "voila Communications Ltd.",
"address1": "Ben votrant",
"address2": "voila Communications",
"address3": "P.O. Box 2086",
"address4": "55 Victoria Street",
"address5": "toronto",
"address6": "BERMUDA"
},
{
"client_number": "00002",
"client_name": "sinc Limited-in Liquidation",
"address1": "vertravt Bank Trust",
"address2": "Corporate (v) Limited",
"address3": "P.O. Box 3",
"address4": "28/35 John Street,",
"address5": "St. channel, Jersey",
"address6": "USA"
},
];
//Lewis solution
const checkIfInputEqBillingInfo = R.curry((value, acc, next) =>
acc || next.toLowerCase().slice(0, value.length) === value);
function getSuggestions(value) {
const inputValue = value.trim().toLowerCase();
const inputLength = inputValue.length;
return inputLength === 0 ?
[] :
R.filter(billing =>
R.reduce(checkIfInputEqBillingInfo(inputValue), false, [billing.client_number, billing.client_name])
, data);
}
const value = "voila";
getSuggestions(value);
//bradcomp solution
/*
const isTheSame = curry((prop, rval, obj) =>
obj[prop].toLowerCase().slice(0, rval.length) === rval
)
const getSuggestions = curry((data, value) => {
const inputValue = value ? value.trim().toLowerCase() : null;
const sameName = isTheSame('client_name', inputValue);
const sameNumber = isTheSame('client_number', inputValue);
return inputValue ?
R.filter(R.either(sameName, sameNumber), data) :
[];
});
getSuggestions(data, "voila");
getSuggestions(data, "00002");
*/
//Lewis plain js version
/*
const value = "voila";
function getSuggestions(value) {
const inputValue = value.trim().toLowerCase();
const inputLength = inputValue.length;
return inputLength === 0 ? [] : data.filter(billingClient =>
(billingClient.client_number.toLowerCase().slice(0, inputLength) === inputValue) ||
(billingClient.client_name.toLowerCase().slice(0, inputLength) === inputValue)
);
}
getSuggestions(value);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment