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

@mobo-ts/mobo

v0.3.1

Published

A pluggable typescript library / 一个可插件化的 TS 库

Downloads

177

Readme

Mobo

Mobo (Motherboard), A pluggable typescript library.

How to install

pnpm i @mobo-ts/mobo
# or
npm i @mobo-ts/mobo

How to use

The plugin mainly consists of three parts:

  • Context type: declared in MoboPlugin by the extended key, such as: abc.
  • Context initialization options type: declared in MoboPlugin by the extended key merged by InitOptions, such as: abcInitOptions.
  • Initialization function: implemented in the plugin object by init merged by the extended key, such as: initAbc.

There are accurate type hints in Mobo initialization and subcontext initialization. When the context is initializing, the initialization functions in all plugins will also be called, completing the process in a divide-and-conquer manner.

const p: MoboPlugin<{
  mm: {
    /**
     * a 3234
     */
    a: number
  }
  mmInitOptions: {
    /**
     * bbbbbs
     */
    b: number
  }
  abcDd: {
    /**
     * 234
     */
    jjx: number[]
  }
  abcDdInitOptions: {
    /**
     * lkdjf
     */
    sdf: string[]
  }
}> = {
  id: '1',
  initMm(ctx) {
    ctx.a = ctx.initOptions.b + 1
  },
  initAbcDd(ctx) {
    ctx.jjx = ctx.initOptions.sdf.map((s: string) => Number(s))
  },
}

const m = new Mobo([p], ['mm']).init({ b: 4 })
console.log(m.a) // 5
const ctx = m.initSubcontext(['abcDd'], {
  sdf: ['1', '2'],
})
console.log(ctx.jjx) // [1, 2]

const m2 = new Mobo([], ['mm']).use(p).init({ b: 43 })
console.log(m2.a) // 44

Custom plugin type

interface DodoPlugin<
  PropertiesExt extends MoboDefaultPropertiesExt,
  DepPlugin extends AnyMoboPlugin = never,
> extends MoboPlugin<PropertiesExt> {
  /**
   * init Dodo
   */
  initDodo?: MoboPluginInitFnByPlugin<this | DepPlugin, 'dodo'>
}

function createDodo<Plugin extends AnyMoboPlugin>(
  plugins: readonly Plugin[],
): Mobo<Plugin, 'dodo'> {
  return new Mobo(plugins, ['dodo'])
}

const p: DodoPlugin<{
  dodo: {
    a: number
  }
  dodoInitOptions: {
    b: number
  }
}> = {
  id: '1',
  initDodo(ctx) {
    ctx.a = ctx.initOptions.b + 1
  },
}

const dodo = createDodo([p]).init({ b: 4 })
console.log(dodo.a) // 5

Custom plugin higher kinded type

Thanks free-types.

type DodoDefaultPropertiesExt =
  | MoboDefaultPropertiesExtObj<'context' | 'dodo'>
  | MoboDefaultPropertiesExtHkt

interface DodoPlugin<
  PropertiesExt extends DodoDefaultPropertiesExt,
  DepPlugin extends AnyMoboPlugin = never,
> extends MoboPlugin<PropertiesExt> {
  /**
   * init Context
   */
  initContext?: MoboPluginInitFnByPlugin<this | DepPlugin, 'context'>
  /**
   * init Dodo
   */
  initDodo?: MoboPluginInitFnByPlugin<this | DepPlugin, 'context' | 'dodo'>
}

function createDodo<Plugin extends AnyMoboPlugin>(
  plugins: readonly Plugin[],
): Mobo<Plugin, 'context' | 'dodo'> {
  return new Mobo(plugins, ['context', 'dodo'])
}

const p1: DodoPlugin<{
  dodo: {
    a: number
  }
  dodoInitOptions: {
    b: number
  }
}> = {
  id: '1',
  initDodo(ctx) {
    ctx.a = ctx.initOptions.b + 1
  },
}
const p2: DodoPlugin<
  {
    context: {
      co: number[]
    }
    contextInitOptions: {
      ci: number
    }
  },
  typeof p1
> = {
  id: '2',
  initContext(ctx) {
    ctx.co = [ctx.initOptions.ci * 2]
  },
  initDodo(ctx) {
    ctx.co.push(ctx.a++)
  },
}
interface MyPropertiesExtHkt<Plugin extends AnyMoboPlugin> {
  context: {
    /**
     * asdf
     */
    parent?:
      | MoboContext<Plugin, 'context'>
      | MoboContext<Plugin, 'context' | 'dodo'>
  }
  contextInitOptions: {
    /**
     * in333
     */
    parent?:
      | MoboContext<Plugin, 'context'>
      | MoboContext<Plugin, 'context' | 'dodo'>
  }
}
interface $MyPropertiesExtHkt extends MoboDefaultPropertiesExtHkt {
  type: MyPropertiesExtHkt<MoboDefaultPropertiesExtHktPlugin<this>>
}
const p3: DodoPlugin<$MyPropertiesExtHkt> = {
  id: '3',
  initContext(ctx) {
    ctx.parent = ctx.initOptions.parent
  },
}

const m = createDodo([p1]).use(p2, p3).init({ b: 4, ci: 3 })
console.log(m.a) // 6
console.log(m.co) // [6, 5]
console.log(m.parent) // undefined
const ctx = m.initSubcontext(['context'], {
  ci: 7,
  parent: m,
})
console.log(ctx.co) // [14]
console.log(ctx.parent) // m
const ctx2 = ctx.initSubcontext(['context'], {
  ci: 7,
  parent: ctx,
})
console.log(ctx2.co) // [14]
console.log(ctx2.parent) // ctx