Skip to content

Instantly share code, notes, and snippets.

@Lawouach
Last active July 5, 2017 06:00
Show Gist options
  • Save Lawouach/ef86aa82e8952cc41e7263007cd8b591 to your computer and use it in GitHub Desktop.
Save Lawouach/ef86aa82e8952cc41e7263007cd8b591 to your computer and use it in GitHub Desktop.
import { Microgrammar } from "@atomist/microgrammar/Microgrammar";
import { Opt } from "@atomist/microgrammar/Ops";
const csv = `FirstName,LastName,Gender
Patrick, Henry, Male
Grace, 'O''Malley', Female
Fred, "Flintstone, esq.", Male
`;
interface Individual {
firstname: string;
lastname: string;
gender: number;
}
const people = Microgrammar.fromDefinitions<Individual>({
firstname: /^[^,]*/,
_sep1: ",",
lastname: new Alt(/^".*"/, /^[^,]*/),
_sep2: ",",
gender: /^[^\n]*/,
});
people.findMatches(csv).map(r => {
console.log(`${r.firstname} ${r.lastname} is ${r.gender}`);
return true;
});
@Lawouach
Copy link
Author

Lawouach commented Jul 4, 2017

Gives this:

FirstName LastName is Gender
Patrick Henry is Male
Grace 'O''Malley' is Female
Fred "Flintstone, esq." is Male

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment