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 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-compose

v0.0.1

Published

A simple utility for composing JavaScript functions.

Readme

js-compose

一个用于JavaScript/TypeScript的函数装饰与钩子工具库,提供逻辑扩展能力。

安装


npm install js-compose

# 或者

npm install [email protected]:xesam/js-compose.git

核心功能

1. decorate - 基础装饰器

基础函数装饰器,允许自定义函数执行逻辑。

import {decorate} from 'js-compose';

const srcFunc = (a: number, b: number) => a + b;
const decorated = decorate(srcFunc, (src, ...args) => {
    console.log('Before call');
    const result = src?.(...args);
    console.log('After call');
    return result;
});

decorated(1, 2); // 输出: Before call → After call → 3

2. around - 环绕钩子

提供before/afterReturn/afterThrow/after全生命周期钩子。

import {around} from 'js-compose';

const srcFunc = () => {
    throw new Error('Oops');
};
const wrapped = around(srcFunc, {
    before: (...args) => console.log('Before'),
    afterThrow: (err) => console.log('Caught:', err.message),
    after: (res, err) => console.log('Finally')
});

wrapped(); // 输出: Before → Caught: Oops → Finally

3. wrap - 统一包装

自动判断装饰类型(函数装饰或环绕钩子)。

import {wrap} from 'js-compose';

// 使用函数装饰
const wrapped1 = wrap(srcFunc, (src) => src?.() * 2);

// 使用环绕钩子
const wrapped2 = wrap(srcFunc, {
    afterReturn: (res) => res * 2
});

4. hook - 对象属性钩子

通过路径或命名规则钩住对象的多个方法/属性。

import hook from 'js-compose';

const app = {
    config: {
        onLoad: () => {
        }
    }, lifetimes: {
        created: () => {
        }
    }
};
// 钩住config.onLoad和lifetimes.created
const newApp = hook(app, {
    'config.onLoad': {before: () => console.log('Config loading')},
    'lifetimes.created': {before: () => console.log('Component created')}
});

newApp.config.onLoad(); // 输出: Config loading
newApp.lifetimes.created(); // 输出: Component created

API 文档

完整类型定义见src/index.ts,核心类型包括:

  • FunctionDecoration: 基础装饰器类型
  • AroundDecoration: 环绕钩子类型
  • NamedDecoration: 对象属性钩子配置类型

测试

npm test

测试覆盖了调用顺序、this上下文、错误处理等核心场景(见__tests__目录)。

ChangeLog

0.0.1

  • 基础功能实现

许可

MIT