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 🙏

© 2026 – Pkg Stats / Ryan Hefner

kotlin-scope-functions-js

v1.0.0

Published

A JavaScript library implementing Kotlin-like scope functions: let and also

Readme

kotlin-scope-functions-js

一个实现了类似 Kotlin 中的 scope functions( let/also/takeIf/takeUnless ) 的 JavaScript 库。

这些函数主要用在可选链 ?. 中,用于简化代码。

注意: 不要在开发通用库时使用该库,因为它依赖于全局对象的扩展。开发多人项目时也需慎重选用。

功能

  • let 函数:用于转换值并返回结果
  • also 函数:用于执行操作并返回对象本身
  • takeIf 函数:如果条件满足则返回对象本身,否则返回 null
  • takeUnless 函数:如果条件不满足则返回对象本身,否则返回 null

特色

  • [x] 完整的 TypeScript 类型定义
  • [x] 函数定义使用 Object.defineProperty 添加,设置为不可枚举,不会出现在 for...in 循环中。个人项目放心使用。
  • [x] 对基本类型支持度最高,在这些类型上使用时,会正确保留原始类型,而不会转换为对象。 (以下类型的 instanceof object 均为 false)
    • [x] string/字符串
    • [x] number/数字
    • [x] bigint/大整数
    • [x] boolean/布尔值
    • [x] symbol/符号

对于不支持 with/run/apply 的说明

  • 个人不喜欢使用 this
  • 这些函数的参数不能使用箭头函数,因为箭头函数的 this 指向是固定的

安装

npm install kotlin-scope-functions-js

使用方法

// 引入库
// esm 方式
import 'kotlin-scope-functions-js';
// 或者 cjs 方式
// require('kotlin-scope-functions-js');

// 使用 let 函数 - 转换值
const url = maybeNull();
const result = await url?.let(it => fetch(it));

// 等价于:
const result = await (url ? fetch(url) : null);

// 使用 also 函数 - 操作对象
const person = (new Persion).also(it => {
  it.name = 'John';
  it.age = 30;
});

// 使用 takeIf 函数 - 条件判断
const user = getUserOrNull()?.takeIf(it => it.age >= 18);