UNPKG

fp-ts-std

Version:

The missing pseudo-standard library for fp-ts.

23 lines (22 loc) 1.38 kB
import * as IO from "fp-ts/IO"; import * as NEA from "fp-ts/NonEmptyArray"; import * as O from "fp-ts/Option"; import { constVoid, flow, pipe } from "fp-ts/function"; import { invoke } from "./Function"; export const fromNodeList = Array.from; export const querySelector = (q) => (x) => () => pipe(x, invoke("querySelector")([q]), O.fromNullable); export const querySelectorAll = (q) => (x) => () => pipe(x, invoke("querySelectorAll")([q]), fromNodeList, NEA.fromArray); export const childNodes = (x) => () => pipe(x.childNodes, fromNodeList, NEA.fromArray); export const remove = (x) => () => pipe(x, invoke("remove")([])); export const appendChild = (child) => (parent) => () => pipe(parent, invoke("appendChild")([child])); export const emptyChildren = flow(childNodes, IO.chain(O.traverse(IO.Applicative)(IO.traverseArray(remove)))); export const getTextContent = (x) => () => pipe(x.textContent, O.fromNullable); export const setTextContent = (x) => (y) => () => { y.textContent = x; }; export const addEventListener = (type) => (listener) => (el) => () => { const _listener = (e) => listener(e)(); pipe(el, invoke("addEventListener")([type, _listener])); return () => pipe(el, invoke("removeEventListener")([type, _listener])); }; export const addEventListener_ = (type) => (listener) => (el) => pipe(addEventListener(type)(listener)(el), IO.map(constVoid));