Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <html>

<body>
  <textarea id='input'>
,West Gardiner,REP Ballot Style 249,undervote,undervote,undervote
,West Gardiner,REP Ballot Style 249,"Crafts, Dale John","Brakey, Eric L.","Bennett, Adrienne"
,West Gardiner,REP Ballot Style 249,undervote,undervote,undervote
,West Gardiner,REP Ballot Style 249,"Bennett, Adrienne","Brakey, Eric L.","Crafts, Dale John"
,West Gardiner,REP Ballot Style 249,"Bennett, Adrienne",undervote,undervote
,West Gardiner,REP Ballot Style 249,"Crafts, Dale John","Bennett, Adrienne","Brakey, Eric L."
,West Gardiner,REP Ballot Style 249,"Crafts, Dale John","Brakey, Eric L.","Bennett, Adrienne"
,West Gardiner,REP Ballot Style 249,"Crafts, Dale John","Bennett, Adrienne","Brakey, Eric L."
,West Gardiner,REP Ballot Style 249,"Crafts, Dale John","Brakey, Eric L.","Bennett, Adrienne"
,West Gardiner,REP Ballot Style 249,"Bennett, Adrienne","Brakey, Eric L.","Crafts, Dale John"
 </textarea>
  <textarea id='candidates'>
Bennet: Bennett, Adrienne
Brakey: Brakey, Eric L.
Crafts: Crafts, Dale John
  </textarea>
  <textarea id='results'></textarea>
  <div class='dialog'>
    <button onclick='doIt()'>do it!</button>
  </div>
  <script>
    // easy ways to get and set 
    // the values of the textareas
    let getVal = (id) => {
      return document.getElementById(id).value;
    };
    let setVal = (id, value) => {
      document.getElementById(id).value = value;
    };
    let doIt = () => {
      setVal('results',
        csvBallotDataToSimpleString(getVal('input'),
          parseCandidates(getVal('candidates'))
        )
      );
    }
  </script>
</body>

</html>
              
            
!

CSS

              
                input {
  background-color: white;
  color: black;
  width: 60px;
  display: inline-block;
}

.dialog {
  position: absolute;
  bottom: 20px;
  right: 30px;
  vertical-align: top;
  text-align: right;
  width: 100px;
}
textarea {
  width: 25%;
  background-color: black;
  color: #0f0;
  font-size: 15px;
  padding: 3px;
  box-shadow: 2px 2px 4px #000;
}

/*  what? i like shiny. */
button {
	border: 0;
	font-weight: bold;
	width: 80px;
	color: #fff;
	position: relative;
	padding: 2px 12px 2px 8px;
	margin: 4px;
	border-radius: 4px;
	box-shadow: 1px 1px 3px rgb(0,0,0), inset 0px -0px 3px rgba(0,0,0,.5);
	text-shadow: 1px 1px 3px #000;
	background-color: #30a;
        text-align: center;
	background-image:
   -webkit-gradient( linear,
			0% 0%,0% 100%,
			from(rgba(255,255,255,0.5)),
			to(rgba(255,255,255,0.2)),
			color-stop(.5,rgba(255,255,255,0.3)),
			color-stop(.5,rgba(255,255,255,0.0))
			);
 background-image:
   -moz-linear-gradient(top,
			rgba(255,255,255,0.7),
			rgba(255,255,255,0.4) 50%,
			rgba(0,0,0,0.0) 50%,
			rgba(255,255,255,0.3)
			);
 background-image:
   -o-linear-gradient(top,
			rgba(255,255,255,0.7),
			rgba(255,255,255,0.4) 50%,
			rgba(0,0,0,0.0) 50%,
			rgba(255,255,255,0.3)
			);
}

button:hover {
	background-color: #ff0;
	color: #000;
	text-shadow: none;
}

              
            
!

JS

              
                //-------------------------------------------
let csvBallotDataToSimpleString = (csvStr, candidates) => {
  // parse it into an array. numbered
  // fields are the ones we care about
  let list = parseCsv(csvStr, ["a", "b", "c", "0", "1", "2", "3"]);

  // now get it into array of arrays
  // of candidate abbreviations
  list = list.map(function (item) {
    let ballot = [];
    for (var i = 0; i < 4; i++) {
      let fullName = item[i];
      if (fullName !== undefined) {
        let candidateAbbr = candidates[fullName];
        if (candidateAbbr) {
          ballot.push(candidateAbbr);
        }
      }
    }
    return ballot;
  });

  // now make a hash, using strings of
  // candidate abbreviations as the key
  // and number of instances the value
  let hash = {};

  for (var i = 0; i < list.length; i++) {
    let str = list[i].join(", ");
    if (str.length) {
      if (hash[str]) {
        hash[str]++;
      } else {
        hash[str] = 1;
      }
    }
  }
  // now make it an array again, so we
  // can sort by number of instances
  list = [];
  for (var i in hash) {
    list.push({
      num: hash[i],
      str: i
    });
  }
  list.sort(function (a, b) {
    if (b.num == a.num) {
      return b.str.length - a.str.length;
    }
    return b.num - a.num;
  });

  // return it as a single string
  return list
    .map(function (item) {
      return item.num + ": " + item.str;
    })
    .join("\n");
};

//-------------------------------------------
// make a hash of candidate names (keys) to
// candidate abbreviations (values)
let parseCandidates = (str) => {
  let lines = str.split("\n"),
    out = {};
  for (var i = 0; i < lines.length; i++) {
    let line = lines[i];
    let index = line.indexOf(":");
    if (index != -1) {
      out[line.substring(index + 1).trim()] = line.substring(0, index).trim();
    }
  }
  return out;
};

//-------------------------------------------
// generic CSV parser, can handle commas in quotes
// stolen from https://stackoverflow.com/questions/59218548
let parseCsv = (str, headerList, quotechar = '"', delimiter = ",") => {
  const cutlast = (_, i, a) => i < a.length - 1;
  const regex = new RegExp(
    `(?:[\\t ]?)+(${quotechar}+)?(.*?)\\1(?:[\\t ]?)+(?:${delimiter}|$)`,
    "gm"
  );
  const lines = str.split("\n");
  const headers =
    headerList || lines.splice(0, 1)[0].match(regex).filter(cutlast);
  const list = [];
  for (const line of lines) {
    const val = {};
    for (const [i, m] of [...line.matchAll(regex)].filter(cutlast).entries()) {
      val[headers[i]] = m[2].length > 0 ? Number(m[2]) || m[2] : null;
    }
    list.push(val);
  }
  return list;
};

              
            
!
999px

Console