Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Jense5/browser-monads
Monads for `window` and `document`
https://github.com/Jense5/browser-monads
browser document gatsby javascript monads window
Last synced: about 2 months ago
JSON representation
Monads for `window` and `document`
- Host: GitHub
- URL: https://github.com/Jense5/browser-monads
- Owner: Jense5
- License: mit
- Created: 2018-04-02T01:08:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T19:52:22.000Z (about 2 years ago)
- Last Synced: 2024-08-04T01:28:16.486Z (5 months ago)
- Topics: browser, document, gatsby, javascript, monads, window
- Language: JavaScript
- Homepage:
- Size: 142 KB
- Stars: 64
- Watchers: 1
- Forks: 3
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-list - browser-monads
README
# Browser Monads
Interface for the `window` and `document` variables of a webpage.
## What is a monad?
You don't really have to know what a monad is since the variables provided by this library aren't *really* monads. Let's take a look at the `window` variable to get a basic understanding. The main purpose of this variable is that it *may be a window* or it *may be nothing*. You don't know what it is and you shouldn't really care. In case it is a window, you can use it as you're used to. In case it is not a window, you can still use the same functions on the instance. The caring about the existence of the variable is already taken care of.
This same concept is applicable to the `document` variable too. You can use the `exists` function in order to check if the variable exists or you are operating on an instance that contains *nothing*.
## Why use monads?
This library makes it possible to use these variables without checks with [Gatsby](https://www.gatsbyjs.org). This way of programming splits the defensive part from your own code, since you don't have to care anymore about the environment in which you are running. This makes it especially useful with server-side rendering. More info about the `nothing`-type can be found [here](https://github.com/slmgc/Nothing).
## Show me the code!
```js
import { window, document, exists } from 'browser-monads';// inside browser ? window.location.href : ''
window.location.href;// inside browser ? true : false
exists(window);
```This library also includes TypeScript type definitions.