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

danieleloscozzese/rely-on

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"));

About

A DOM helper function to refine the types of selected elements that should not be missing.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published