@innet/utils
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-alpha.1 • Public • Published
InnetJs logo by Mikhail Lysikov

  @innet/utils

 

NPM downloads changelog license

Abstract

Here you can find some utils to easily create innet plugins.

stars watchers

Install

npm

npm i @innet/utils

yarn

yarn add @innet/utils

logger

This function helps to log your application.

import innet, { createHandler, useApp } from 'innet'
import { logger } from '@innet/utils'

const handler = createHandler([
  logger(() => {
    console.log(useApp())
  }),
])

innet(1, handler)
// > 1

You can pass some function to the logger. You can use hooks inside the function.

createSubPlugin

This function helps to create a plugin which contains other plugins.

For example let's create exist plugin that uses sub-plugins if app is not undefined or null.

import innet, { NEXT, useApp, runPlugins } from 'innet'
import { createSubPlugin } from '@innet/utils'

const exist = createSubPlugin(
  plugins => {
    const app = useApp()

    if (app === null || app === undefined) return NEXT

    runPlugins(app, useHandler(), plugins)
  },
)

Now we can use it.

import innet, { createHandler, useApp } from 'innet'
import { logger } from '@innet/utils'

const handler = createHandler([
  exist([
    logger(() => console.log(useApp())),
  ]),
])

innet(null, handler)
innet(undefined, handler)
// nothing happens

innet('test', handler)
// > 'test'

createConditionPlugin

This function based on createSubPlugin, but has simplified interface. With it, you can easily eject some types of applications.

For example let's create a plugin to eject numbers.

import { useApp } from "innet"
import { createConditionPlugin } from '@innet/utils'

const number = createConditionPlugin(() => typeof useApp() === 'number')

number

This plugin is right from the last example. Let's take a look at usage.

import innet, { createHandler, useApp } from 'innet'
import { number, logger } from '@innet/utils'

const handler = createHandler([
  number([
    logger(() => console.log(useApp())),
  ]),
])

innet(null, handler)
innet('test', handler)
innet('1', handler)
// nothing happens

innet(1, handler)
// > 1

string

This plugin handles only string values.

import innet, { createHandler, useApp } from 'innet'
import { string, logger } from '@innet/utils'

const handler = createHandler([
  string([
    logger(() => console.log(useApp())),
  ]),
])

innet(null, handler)
innet(1, handler)
// nothing happens

innet('1', handler)
// > '1'

fn

This plugin handles only function values.

import innet, { createHandler, useApp } from 'innet'
import { fn, logger } from '@innet/utils'

const handler = createHandler([
  fn([
    logger(() => console.log(useApp())),
  ]),
])

innet(null, handler)
innet(1, handler)
innet('1', handler)
// nothing happens

innet(() => {}, handler)
// > () => {}

node

This plugin handles only Node values.

import innet, { createHandler, useApp } from 'innet'
import { node, logger } from '@innet/utils'

const handler = createHandler([
  node([
    logger(() => console.log(useApp())),
  ]),
])

innet(null, handler)
innet(1, handler)
innet('1', handler)
// nothing happens

innet(document.createElement('div'), handler)
// > div

promise

This plugin handles only promise values.

import innet, { createHandler, useApp } from 'innet'
import { promise, logger } from '@innet/utils'

const handler = createHandler([
  promise([
    logger(() => console.log(useApp())),
  ]),
])

innet(null, handler)
innet(1, handler)
innet('1', handler)
// nothing happens

innet(
  new Promise(resolve => resolve()),
  handler,
)
// > promise

async

This plugin helps to work with async, just an example:

import innet, { createHandler, useApp } from 'innet'
import { async, logger } from '@innet/utils'

const handler = createHandler([
  async,
  logger(() => console.log(useApp())),
])

innet(1, handler)
// > 1

const app = new Promise(resolve => resolve('test'))

innet(app, handler)
// nothing happens

await app
// > 'test'

nullish

You can use it to eject null

import innet, { createHandler, useApp } from 'innet'
import { logger, nullish } from '@innet/utils'

const handler = createHandler([
  nullish([
    logger(() => console.log(useApp())),
  ]),
])

innet(undefined, handler)
innet('test', handler)
// nothing happens

innet(null, handler)
// > null

object

You can use it to eject an object

import innet, { createHandler, useApp } from 'innet'
import { object, logger } from '@innet/utils'

const handler = createHandler([
  object([
    logger(() => console.log(useApp())),
  ]),
])

innet(undefined, handler)
innet('test', handler)
// nothing happens

innet({}, handler)
// > {}

innet(null, handler)
// > null

Because of null is an object, you get the last console log, to prevent it, you can combine the plugin with nullish.

import innet, { createHandler, useApp } from 'innet'
import { object, logger, nullish } from '@innet/utils'

const handler = createHandler([
  nullish([]),
  object([
    logger(() => console.log(useApp())),
  ]),
])

innet(undefined, handler)
innet('test', handler)
innet(null, handler)
// nothing happens

innet({}, handler)
// > {}

innet([], handler)
// > []

array

Because of array is an object you get the last console log. To handle an array use plugin of array.

import innet, { createHandler, useApp } from 'innet'
import { array, logger } from '@innet/utils'

const handler = createHandler([
  array([
    logger(() => console.log(useApp())),
  ]),
])

innet('test', handler)
innet({}, handler)
// nothing happens

innet([], handler)
// > []

arraySync

This plugin helps to handle an array synchronously. It runs the handler for each item of the array.

import innet, { createHandler, useApp } from 'innet'
import { arraySync, logger } from '@innet/utils'

const handler = createHandler([
  arraySync,
  logger(() => console.log(useApp())),
])

innet('test', handler)
// > 'test'.

innet(['test'], handler)
// > 'test'

innet(['test1', 'test2'], handler)
// > 'test1'
// > 'test2'

innet(['test1', ['test2', ['test3', 'test4']]], handler)
// > 'test1'
// > 'test2'
// > 'test3'
// > 'test4'

iterable

You can use it to eject iterable objects.

import innet, { createHandler, useApp } from 'innet'
import { iterable, logger } from '@innet/utils'

const handler = createHandler([
  iterable([
    logger(() => console.log(useApp())),
  ]),
])

innet([1, 2, 3], handler)
// [1, 2, 3]
innet(new Set([1, 2, 3]), handler)
// Set([1, 2, 3])

innet({}, handler)
// nothing happens

asyncIterable

You can use it to eject async iterable objects.

import innet, { createHandler, useApp } from 'innet'
import { asyncIterable, logger } from '@innet/utils'

const handler = createHandler([
  asyncIterable([
    logger(() => console.log(useApp())),
  ]),
])

innet([1, 2, 3], handler)
innet(new Set([1, 2, 3]), handler)
// nothing happens

async function * test () {}

innet(test(), handler)
// Promise

Issues

If you find a bug or have a suggestion, please file an issue on GitHub.

issues

Readme

Keywords

Package Sidebar

Install

npm i @innet/utils

Weekly Downloads

5

Version

2.0.0-alpha.1

License

MIT

Unpacked Size

33.1 kB

Total Files

123

Last publish

Collaborators

  • deight