Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danestves/browser-monads-ts
Provide interfaces for the window, document and navigator variables of a webpage to be used on SSR and Client.
https://github.com/danestves/browser-monads-ts
browser-monads document gatsby javascript navigator nextjs typescript window
Last synced: 3 months ago
JSON representation
Provide interfaces for the window, document and navigator variables of a webpage to be used on SSR and Client.
- Host: GitHub
- URL: https://github.com/danestves/browser-monads-ts
- Owner: danestves
- License: mit
- Created: 2021-04-29T23:21:39.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-11T18:19:37.000Z (over 2 years ago)
- Last Synced: 2024-09-15T22:52:36.193Z (4 months ago)
- Topics: browser-monads, document, gatsby, javascript, navigator, nextjs, typescript, window
- Language: TypeScript
- Homepage:
- Size: 255 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Browser Monads TS
> This package is inspired by [browser-monads](https://github.com/Jense5/browser-monads) currently without support from the author.
Provide interfaces for the `window`, `document` and `navigator` variables of a webpage.
## What is a monad?
> In functional programming, a monad is an abstraction that allows structuring programs generically. Supporting languages may use monads to abstract away boilerplate code needed by the program logic.
>
> -Wikipedia - [Monad (functional programming)]()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) and [NextJS](https://nextjs.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).
## Example
```js
import { window, document, exists } from 'browser-monads-ts';// inside browser ? window.location.href : ''
window.location.href;// inside browser ? document.getElementById("myId") : ''
document.getElementById('myId');// inside browser ? true : false
exists(window);
```