UNPKG

kotlin-scope-functions-js

Version:

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

34 lines (30 loc) 1.07 kB
// 扩展 Object 接口,添加 let 和 also 方法 declare global { interface Object { /** * 将对象作为参数传递给函数,并返回函数的结果 * @param block 接收对象作为参数的函数 * @returns 函数执行的结果 */ let<T, R>(this: T, block: (it: T) => R): R; /** * 将对象作为参数传递给函数,并返回对象本身 * @param block 接收对象作为参数的函数 * @returns 对象本身 */ also<T>(this: T, block: (it: T) => void): T; /** * 如果 predicate 返回 true,则返回对象本身,否则返回 null * @param predicate 接收对象作为参数的函数 * @returns 对象本身或 null */ takeIf<T>(this: T, predicate: (it: T) => boolean): T | null; /** * 如果 predicate 返回 false,则返回对象本身,否则返回 null * @param predicate 接收对象作为参数的函数 * @returns 对象本身或 null */ takeUnless<T>(this: T, predicate: (it: T) => boolean): T | null; } } export {};