getUrl.js

  1. import { concat } from "ramda";
  2. export default concat(
  3. "https://en.wikipedia.org/w/api.php?origin=*&action=opensearch&search="
  4. );

concat

  1. R.concat('ABC', 'DEF'); // 'ABCDEF'
  2. R.concat([4, 5, 6], [1, 2, 3]); //=> [4, 5, 6, 1, 2, 3]
  3. R.concat([], []); //=> []

getInputValue.js

  1. import { pathOr, trim, pipe } from "ramda";
  2. export default pipe(pathOr("", ["target", "value"]), trim);

pathOr

  1. R.pathOr('N/A', ['a', 'b'], {a: {b: 2}}); //=> 2
  2. R.pathOr('N/A', ['a', 'b'], {c: {b: 2}}); //=> "N/A"

trim

  1. R.trim(' xyz '); //=> 'xyz'
  2. R.map(R.trim, R.split(',', 'x, y, z')); //=> ['x', 'y', 'z']

Results.js

  1. export default ([query, names, summaries, links]) => `
  2. <h2>Searching for "${query}"</h2>
  3. <ul class="list-group">
  4. ${names.map(
  5. (name, index) => `
  6. <li class="list-group-item">
  7. <a href=${links[index]} target="_blank">
  8. <h4>${name}</h4>
  9. </a>
  10. <p>${summaries[index]}</p>
  11. </li>
  12. `
  13. ).join('')}}
  14. </ul>
  15. `;

index.js

  1. import "bootstrap/dist/css/bootstrap.min.css";
  2. import { pipe, tap,ifElse,isEmpty } from "ramda";
  3. import getInputValue from "./getInputValue";
  4. import getUrl from "./getUrl";
  5. import data from "./data";
  6. import Results from "./Results";
  7. import { debounce } from "lodash";
  8. const doNothing = () => {};
  9. const render = (markup) => {
  10. const resultsElement = document.getElementById("results");
  11. resultsElement.innerHTML = markup;
  12. };
  13. const searchAndRenderResults = pipe(getUrl, tap(console.warn), (url) =>
  14. fetch(url)
  15. .then(
  16. (res) => res.json(),
  17. (err) => {
  18. console.warn(err);
  19. return data;
  20. }
  21. )
  22. .then(Results)
  23. .then(render)
  24. );
  25. const makeSearchRequestIfValid = pipe(
  26. getInputValue,
  27. ifElse(isEmpty, doNothing, debounce(searchAndRenderResults, 1000))
  28. );
  29. const inputElement = document.querySelector("input");
  30. inputElement.addEventListener("keyup", makeSearchRequestIfValid);

tap

  1. const sayX = x => console.log('x is ' + x);
  2. R.tap(sayX, 100); //=> 100
  3. // logs 'x is 100'

ifElse

  1. const incCount = R.ifElse(
  2. R.has('count'),
  3. R.over(R.lensProp('count'), R.inc),
  4. R.assoc('count', 1)
  5. );
  6. incCount({}); //=> { count: 1 }
  7. incCount({ count: 1 }); //=> { count: 2 }

isEmpty

  1. R.isEmpty([1, 2, 3]); //=> false
  2. R.isEmpty([]); //=> true
  3. R.isEmpty(''); //=> true
  4. R.isEmpty(null); //=> false
  5. R.isEmpty({}); //=> true
  6. R.isEmpty({length: 0}); //=> false