Skip to content
This repository has been archived by the owner on Aug 26, 2019. It is now read-only.

Latest commit

 

History

History
24 lines (16 loc) · 965 Bytes

README.md

File metadata and controls

24 lines (16 loc) · 965 Bytes

Summary

HTML applications often require an identified element to be present and select the element with the assumption that it is present.

Type-annotation tools like TypeScript and Flow will let you know that this isn't strictly the case: if an element isn't present, then null will be returned from document.getElementById.

Refining the type of the result can be done in an if (element !== null)...else but this must be done every time in order to eliminate the possibility of null.

This package allows code to rely on a Node being present:

  • If given a string, it selects the element with that ID and verifies it exists before returning it.
  • If given a Node | null, it checks the value is a Node and:
    • if it is, returns it.
    • if it is null, throws an Error.

Examples

const element = relyOn("key-field", "The field must exist!");
const element = relyOn(document.querySelector(".identifier"));