Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <label for="search">Find a Canadian City</label>
<input id="search" name="search" placeholder="Windsor"/>
<h4>Results</h4>
<pre class="results-area"></pre>
              
            
!

CSS

              
                body, input {
  font-family: sans-serif;
  font-size:1.5em;
}

fieldset {
  padding:0.5em;
  border-width:0;
}

input[type=checkbox] {
  height:1.5em;
  width:1.5em;
}

ul {
  margin:0;
  padding:0;
}

li {
  margin:0;
  list-style-type:none;
}

.spinner {
  border: 3px solid #444;
  border-right-color: transparent;
  border-radius: 100%;
  margin: 0 10px;
  height: 16px;
  width: 16px;
  animation: rotate 1s infinite linear;
display:inline-block;
}

.hidden {
  display:none;
}

@keyframes rotate {
  0%    { transform: rotate(0deg); }
  100%  { transform: rotate(360deg); }
}
              
            
!

JS

              
                const { fromEvent } = rxjs;
const { debounceTime, tap, switchMap, flatMap } = rxjs.operators;

const searchField = document.querySelector("[name=search]");
const resultsArea = document.querySelector(".results-area");

const changes$ = fromEvent(searchField, "input");

changes$
  .pipe(
    // debouncing is a good idea, but makes it harder demonstrate the point of this post
    // debounceTime(400),

    // what would happen if you used flatMap instead of switchMap here? Answer is at then end of the source code
    switchMap(event => fakeSearch(event.target.value))
  )
  .subscribe(result => {
    resultsArea.innerText =
      JSON.stringify(result, null, 2);
  });

/**
 * End of real frontend code.
 * This stuff provides a fake backend - an async function that returns search results
 */

function roughDelay() {
  return new Promise(resolve =>
    setTimeout(resolve, 250 + Math.random() * 3000)
  );
}

function fakeSearch(keyword) {
  return roughDelay().then(() => {
    return fuse.search(keyword);
  });
}

var options = {
  caseSensitive: false,
  shouldSort: true,
  threshold: 0.6,
  location: 0,
  distance: 100,
  maxPatternLength: 32,
  minMatchCharLength: 1,
  keys: ["name"]
};

const list = [
  "Toronto",
  "Montreal",
  "Calgary",
  "Ottawa",
  "Edmonton",
  "Mississauga",
  "Winnipeg",
  "Vancouver",
  "Brampton",
  "Hamilton",
  "Quebec City",
  "Surrey",
  "Laval",
  "Halifax",
  "London",
  "Markham",
  "Vaughan",
  "Gatineau",
  "Saskatoon",
  "Longueuil",
  "Kitchener",
  "Burnaby",
  "Windsor",
  "Regina",
  "Richmond",
  "Richmond Hill",
  "Oakville",
  "Burlington",
  "Greater Sudbury",
  "Sherbrooke",
  "Oshawa",
  "Saguenay",
  "Lévis",
  "Barrie",
  "Abbotsford",
  "Coquitlam",
  "Trois-Rivières",
  "St. Catharines",
  "Guelph",
  "Cambridge",
  "Whitby",
  "Kelowna",
  "Kingston",
  "Ajax",
  "Langley",
  "Saanich",
  "Terrebonne",
  "Milton",
  "St. John's",
  "Thunder Bay",
  "Waterloo",
  "Delta",
  "Chatham-Kent",
  "Red Deer",
  "Strathcona County",
  "Brantford",
  "Saint-Jean-sur-Richelieu",
  "Cape Breton",
  "Lethbridge",
  "Clarington",
  "Pickering",
  "Nanaimo",
  "Kamloops",
  "Niagara Falls",
  "Victoria",
  "Brossard",
  "Repentigny",
  "Newmarket",
  "Chilliwack",
  "Maple Ridge",
  "Peterborough",
  "Kawartha Lakes",
  "Drummondville",
  "Saint-Jérôme",
  "Prince George",
  "Sault Ste. Marie",
  "Moncton",
  "Sarnia",
  "Wood Buffalo",
  "New Westminster",
  "Saint John",
  "Caledon",
  "Granby",
  "St. Albert",
  "Norfolk County",
  "Medicine Hat",
  "Grande Prairie",
  "Airdrie",
  "Halton Hills",
  "Port Coquitlam",
  "Fredericton",
  "Blainville",
  "Saint-Hyacinthe",
  "Aurora",
  "North Vancouver",
  "Welland",
  "North Bay",
  "Belleville",
  "Mirabel"
].map(name => ({ name }));

const fuse = new Fuse(list, options);

// flatMap results in every response from the server being rendered, even outdated responses (since they don't always come back  to the browser in the order they were requested)
// flatMap(event => fakeSearch(event.target.value))

// switchMap ignores results from outdated search requests, which gives us the desired behaviour
// switchMap(event => fakeSearch(event.target.value))


              
            
!
999px

Console