push.js 867 B

1234567891011121314151617181920212223242526272829303132333435
  1. const push = (name, action, customisation = null) => {
  2. if (typeof(action) !== "function") {
  3. throw new TypeError("Action must be an function.");
  4. }
  5. if (typeof(name) !== "string") {
  6. throw new TypeError("Name of the push must be string.");
  7. }
  8. if (customisation !== null && typeof(customisation) !== "function") {
  9. throw new TypeError("Customisation must be null or function");
  10. }
  11. const target = document.createElement("button");
  12. target.type = "button";
  13. target.name = name;
  14. target.id = "push-" + name;
  15. target.innerText = name
  16. .toUpperCase()
  17. .replaceAll("-", " ")
  18. .replaceAll("_", " ");
  19. target.classList.add("push");
  20. target.addEventListener("click", action);
  21. if (customisation) {
  22. customisation(target);
  23. }
  24. return target;
  25. };
  26. export { push };