npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@innet/utils

v2.0.0-alpha.1

Published

Utils to create innet plugins

Downloads

16

Readme

  @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