All Projects → bulwarkjs → Safedom

bulwarkjs / Safedom

Licence: mit
🔫 safedom is a safe way to you manipulate dom using a purer functional style.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Safedom

Bugz
🐛 Composable User Agent Detection using Ramda
Stars: ✭ 15 (-59.46%)
Mutual labels:  functional-programming
Oqaml
An OCaml based implementation of a Quil QVM
Stars: ✭ 31 (-16.22%)
Mutual labels:  functional-programming
Rexrex
🦖 Composable JavaScript regular expressions
Stars: ✭ 34 (-8.11%)
Mutual labels:  functional-programming
Opal
Simple and powerful programming language with type inference
Stars: ✭ 20 (-45.95%)
Mutual labels:  functional-programming
Purrr
A functional programming toolkit for R
Stars: ✭ 953 (+2475.68%)
Mutual labels:  functional-programming
Flawless
WIP Delightful, purely functional testing no-framework. Don't even try to use it at work!
Stars: ✭ 33 (-10.81%)
Mutual labels:  functional-programming
Revery Playground
Live, interactive playground for Revery examples
Stars: ✭ 14 (-62.16%)
Mutual labels:  functional-programming
Corsair
Corsair using RxJS, Immutable.js and WebGL/three.js
Stars: ✭ 36 (-2.7%)
Mutual labels:  functional-programming
Docker Iocaml Datascience
Dockerfile of Jupyter (IPython notebook) and IOCaml (OCaml kernel) with libraries for data science and machine learning
Stars: ✭ 30 (-18.92%)
Mutual labels:  functional-programming
Shell Functools
Functional programming tools for the shell
Stars: ✭ 971 (+2524.32%)
Mutual labels:  functional-programming
Facsimile
Facsimile Simulation Library
Stars: ✭ 20 (-45.95%)
Mutual labels:  functional-programming
Immutable Tuple
Immutable finite list objects with constant-time equality testing (===) and no memory leaks.
Stars: ✭ 29 (-21.62%)
Mutual labels:  functional-programming
Ulmus
A functional-reactive style programming library for Clojure(script)
Stars: ✭ 33 (-10.81%)
Mutual labels:  functional-programming
Mori Ext
Function bind syntax wrappers for mori
Stars: ✭ 15 (-59.46%)
Mutual labels:  functional-programming
Stm4cats
STM monad for cats-effect
Stars: ✭ 35 (-5.41%)
Mutual labels:  functional-programming
Funktionale
Functional constructs for Kotlin
Stars: ✭ 879 (+2275.68%)
Mutual labels:  functional-programming
Swiftlyext
SwiftlyExt is a collection of useful extensions for Swift 3 standard classes and types 🚀
Stars: ✭ 31 (-16.22%)
Mutual labels:  functional-programming
Fauxgaux
⛳️ Functional Go
Stars: ✭ 36 (-2.7%)
Mutual labels:  functional-programming
Imagene
A General Purpose Image Manipulation Tool
Stars: ✭ 36 (-2.7%)
Mutual labels:  functional-programming
Swift Overture
🎼 A library for function composition.
Stars: ✭ 968 (+2516.22%)
Mutual labels:  functional-programming

safedom is a safe way to you manipulate dom using a purer functional style.

Table of Contents

Installation

safedom is available from npm.

$ npm install safedom -S

Why safedom ?

In many applications most of the errors are still related to DOM manipulations, and as we are dealing with side effects, we need to have null checks, but safedom lets you do this safely. All safe functions are automatically curried and emphasizes a purer functional style.

Overview

//<div id="app" data-value="10">hello world</div>
const safedom = require('safedom')
const value = safedom.select('#app')
  .chain(safedom.getAttr('data-value'))
  .map(eff => eff.value)
  .map(parseInt)
  .map(value => value + 100)
  .getOrElse(0)

console.log(value) // 110

A wonderful error messages

It's good to know where it's not working, for example

  1. Select by Id but is not found in DOM
//<div id="dragon" data-value="10">hello world</div>
const safedom = require('safedom')

safedom.select('#apslkajdp')
  .chain(safedom.getAttr('data-value'))
  .map(eff => eff.value)
  .map(parseInt)
  .map(value => value + 100)
  .matchWith({
    Ok: ({ value }) => value,

    Error: ({ value: error }) => {
      console.log(error)
      /*
        Selector: '#apslkajdp' dont found
        Method: select
      */
    }
  })

  1. Get an attribute from a element but dont have
//<div id="dragon">hello world</div>
const safedom = require('safedom')

const value = safedom.select('#dragon')
  .chain(safedom.getAttr('data-value'))
  .map(eff => eff.value)
  .map(parseInt)
  .matchWith({
    Ok: ({ value }) => value,

    Error: ({ value: error }) => {
      console.log(error)
      /*
        Attribute: 'data-value' don't found
        Node: [object HTMLDivElement]
        Method: getAttr
      */
    }
  })

Documentation

select

Similiar to a document.querySelector but returns a Result monad from folktale.

You can use methods available to manipulate value.

//<div id="dragon">hello world</div>
const safedom = require('safedom')

const value = safedom.select('#dragon')
console.log(value) //InternalConstructor {value: div#app}

To you get value in safe way, you should map value.

//<div id="dragon">hello world</div>
const safedom = require('safedom')

const value = safedom.select('#dragon')
  .map(element => element.textContent)
  .map(console.log)

console.log(value) //hello world

You can have a default value for when you can't find.

//<div id="dragon">hello world</div>
const safedom = require('safedom')

const value = safedom.select('#lion')
  .map(element => element.textContent)
  .getOrElse('Victor')

console.log(value) //Victor

selectAll

Similiar to an document.querySelectorAll but returns a Result monad from folktale. But querySelectorAll returns a NodeList and safedom, an Array.

You can use methods available to manipulate value.

//<div id="dragon">hello world</div>
const safedom = require('safedom')

const value = safedom.selectAll('#dragon')
console.log(value)//InternalConstructor [{value: div#app}]

getAttr

Similiar to a node.getAttribute but returns a Result monad from folktale.

//<div id="app" data-value="10">hello world</div>
const safedom = require('safedom')
const value = safedom.select('#app')
  .chain(safedom.getAttr('data-value')) //should use chain because getAttr returns a Result
  .map(eff => eff.value)
  .map(parseInt)
  .map(value => value + 100)
  .getOrElse(0)

console.log(value) // 110

disable

You can disable specific element

//<div id="app">hello world</div>
const safedom = require('safedom')
const value = safedom.disable('#app')

console.log(value)// InternalConstructor {value: div#app.disabled}
//<div id="app">hello world</div>
const safedom = require('safedom')
const value = safedom.disable('#app')
  .map((el) => {
    console.log(e)
    //<div id="app" class="disabled" readonly="true" aria-disabled="true"> Victor </div>
    return el
  })

enable

Just enable specific element

//<div id="app" class="disabled" readonly="true" aria-disabled="true"> Victor </div>
const safedom = require('safedom')
const value = safedom.enable('#app')

console.log(value)// InternalConstructor {value: div#app}
//<div id="app" class="disabled" readonly="true" aria-disabled="true"> Victor </div>
const safedom = require('safedom')
const value = safedom.enable('#app')
  .map((el) => {
    console.log(e)
    //<div id="app">hello world</div>
    return el
  })

on

Handle DOM-events

const handleClick = () => console.log('clicked')
safedom.on('click', document, handleClick)
const handleClick = () => console.log('clicked')
safedom.select('#app')
 .map(safedom.on('click'))
 .map(clickStream => clickStream(handleClick))

removeAttr

Remove attribute from a node element

// <div data-id="div-with-attribute" random="attribute"></div>

safedom.select('[data-id="div-with-attribute"]')
    .map(safedom.removeAttr('random'))

// <div data-id="div-with-attribute"></div>

or

// <div data-id="multiple-divs" random="attribute"></div>
// <div data-id="multiple-divs" random="attribute"></div>

safedom.selectAll('[data-id="multiple-divs"]')
    .map(nodes => nodes.forEach(safedom.removeAttr('random')))

// <div data-id="multiple-divs"></div>
// <div data-id="multiple-divs"></div>

removeAttrByQuery

Remove attribute from a node element using query. Note: this function does not use selectAll, so it will only remove the attribute from the first element found in DOM

// <div data-id="div-with-attribute" random="attribute"></div>

safedom.removeAttrByQuery('random', '[data-id="div-with-attribute"]')

// <div data-id="div-with-attribute"></div>

setAttr

Similiar to a node.classList.setAttribute()

//<div class="myClass"></div>

const safedom = require('safedom')

safedom.select(`.myClass`)
  .map(safedom.setAttr('id', 'app'))
        
//<div class="myClass" id="app"></div>

addClass

Similiar to a node.classList.add()

//<div class="machine-container"></div>

const safedom = require('safedom')

safedom.select(`.machine-container`)
  .map(safedom.addClass('-with-scale'))

//<div class="machine-container -with-scale"></div>

removeClass

Similiar to a node.classList.remove()

//<div class="machine-container -with-scale"></div>

const safedom = require('safedom')

safedom.select('.machine-container')
  .map(safedom.removeClass('-with-scale'))


//<div class="machine-container"></div>

toggleClass

Similiar to a node.classList.toggle()

//<div class="machine-container -with-scale"></div>

const safedom = require('safedom')

safedom.select('.machine-container')
  .map(safedom.toggleClass('-with-scale'))


//<div class="machine-container"></div>

focus

Focus in specific element

//<div class="machine-container -with-scale"></div>

const safedom = require('safedom')

safedom.select('.machine-container')
  .map(safedom.focus)

See too

License

The code is available under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].