Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davidystephenson/2a14a5a3d7fdd0df25e9a7301fa0bd64 to your computer and use it in GitHub Desktop.

Select an option

Save davidystephenson/2a14a5a3d7fdd0df25e9a7301fa0bd64 to your computer and use it in GitHub Desktop.
export function format(value, formatter) {
const formatted = formatter(value);
return formatted;
}
function formatString (value) {
return value.toUpperCase()
}
function formatNumber(value) {
return value.toFixed(2)
}
const hello = format('hello', formatString);
console.log(hello); // "HELLO"
const pi = format(3.14159, formatNumber);
console.log(pi); // "3.14"
// This version should throw a type error
// const invalid = format(true, formatString);
// console.log(invalid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment