open-index/open-npm huggingface dataset:
npm package: @qvvg/mario
Description: pipes and stuff
Author: qvvg
Latest version: 1.3.0

# MARIO

Because pipes...

### Install

```bash
npm install --save @qvvg/mario
```

### use pipe as composition

```js
import {pipe} from "@qvvg/mario"

const double = x => x * 2
const result = await pipe(4, [double, double, double, double])

assert(result.value, 64)
```

### use pipe as validation

```js
import {pipe, Ok, Nope, isOk, isNope} from "@qvvg/mario"

const isString = value => {
  return typeof value === "string"
    ? Ok(value)
    : Nope(value, "Needs to be a string!")
}

const hasAt = value => {
  return /@/.test(value) ? Ok(value) : Nope(value, "Must have an @...")
}

const validateEmail = email => pipe(email, [isString, hasAt])

const v1 = await validateEmail("bob@bob.bob")
assert(v1.value, "bob@bob.bob")
assert(isOk(v1), true)
assert(isNope(v1), false)

const v2 = await validateEmail(1234)
assert(v2.value, 1234)
assert(isOk(v2), false)
assert(isNope(v2), true)
assert(v2.reason, "Needs to be a string!")

const v3 = await validateEmail("bobbob.bob")
assert(v3.value, "bobbob.bob")
assert(isOk(v3), false)
assert(isNope(v3), true)
assert(v3.reason, "Must have an @...")
```

### pipes can be async

```js
import {pipe, Ok, Nope, log} from "@qvvg/mario"

await pipe(null, [
  _ => fetch(url),
  r => (r.ok ? Ok(r.json()) : Nope(r.json(), r.statusText)),
  log("DATA"),
])
```

### pipes can handle errors

```js
import {pipe, log} from "@qvvg/mario"

const double = x => x * 2
const bang = x => {
  throw new Error("EXPLOSION!!!")
}

const result = await pipe(4, [
  double,
  double,
  bang,
  log("I will never happen because bang"),
  double,
])

assert(result.value, 16)
assert(isOk(result), false)
assert(isNope(result), true)
assert(result.reason, "pipe::throw")
assert(result.error.message, "EXPLOSION!!!")
```

### pipes can be composed

```js
import {pipe, Ok} from "@qvvg/mario"
const add = x => y => Ok(x + y)

const result = await pipe(10, [
  pipe([add(3), add(-2)]), // +1
  pipe([add(-9), add(15)]), // +6
  pipe([add(-5)]), // -5
])

assert(result.value, 12)
```
npm package: @qvvg/templar
Description: tagged template literals
Author: James Hunter
Latest version: 0.0.2

# templar

> use them backticks, because they are a thing now

## Usage

```javascript
import {templar} from "@qvvg/templar"

const template = templar`
From: ${o => o.from}
To: ${o => o.to}
Message: ${o => o.msg}
`

const letter = template({
  from: "Bob the Builder",
  to: "Postman Pat",
  msg: "Please pick up your black and white cat.",
})

// prettier-ignore
assert( letter, `
From: Bob the Builder
To: Postman Pat
Message: Please pick up your black and white cat.
`)
```

```javascript
import {templar} from "@qvvg/templar"

const template1 = templar`
a: ${o => o.a}
b: ${o => o.b}
`

const template2 = templar`
x: ${o => o.x}
y: ${o => o.y}
`

const template3 = templar`
${template1}
${template2}
oh hello!
${template1}
${template2}
`

const rawr = template3({a: "A", b: "B", x: "X", y: "Y"})

// prettier-ignore
assert(rawr, `
a: A
b: B
x: X
y: Y
oh hello!
a: A
b: B
x: X
y: Y
`)
```

## Examples

**Role your own graphql libary**

```javascript
// gql.js
import {templar} from "@qvvg/templar"

export const gql = templar

export const gqlr = (...args) => (opts = {}) => {
  const params = opts.params || {}
  const headers = opts.headers || {}
  const endpoint = opts.endpoint || ""

  return fetch(endpoint, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      ...headers,
    },
    body: JSON.stringify({
      query: gql(...args)(params),
    }),
  }).then(d => d.json())
}

// somewhere-else.js
import React, {useEffect, useState} from "react"
import {gqlr, gql} from "./gql"

const USER = gql`
  fragment USER on User {
    username
  }
`

const query = gqlr`
  query {
    me { ...USER }
  }
  ${USER}
`

export const Username = () => {
  const [username, setUsername] = useState(null)

  useEffect(() => {
    query({
      endpoint: "http://...",
      headers: {authorization: "bearer ..."},
    }).then(response => setUsername(response.data.me.username))
  }, [])

  return username == null ? null : username
}
```

**Role your own pipeline thing?**

```javascript
const pipe = (fns = []) => token => {
  return fns.reduce((token, fn) => fn(token), token)
}

const param = (key, value) => token => {
  token.params = token.parms || {}
  token.params[key] = value
  return token
}

const script = (...args) => token => {
  token.type = "script"
  token.value = templar(...args)(token.params)
  return token
}

const query = (...args) => token => {
  token.type = "query"
  token.value = templar(...args)(token.parms)
  return token
}

const desc = (...args) => token => {
  token.description = templar(...args)(token.params)
  return token
}

const rawr = pipe([
  param("msg", "woot woot im a boot"),
  script`
    pub fun main() {
      log("msg: ${o => o.msg}")
    }
  `,
  desc`
    the message: ${o => o.msg}
  `,
])

assert(rawr, {
  params: {
    msg: "woot woot im a boot",
  },
  type: "script",
  code: `
    pub fun main() {
      log("msg: woot woot im a boot")
    }
  `,
  descrition: `
    the message: woot woot im a boot
  `,
})
```