bpk

babel-plugin-kotlish-also

a babel plugin for kotlin-like extension function (also, let and etc) support

Showing:

Popularity

Downloads/wk

0

GitHub Stars

3

Maintenance

Last Commit

3yrs ago

Contributors

2

Package

Dependencies

0

Size (min+gzip)

0.6KB

License

MIT

Type Definitions

Built-In

Tree-Shakeable

No?

Categories

Readme

使用方式

一:安装

npm install --save-dev babel-plugin-kotlish-also

二:在.babelrc添加如下plugin

{
  "plugins": [
    "kotlish-also"
  ]
}

使用场景

来看下面这段代码

const foo = (o) => o.a().b.c

如果我们调试时想要输出 oo.a() 等, 通常情况下,我们不得不做出如下改变

// if we want to print o or o.a etc
const foo = (o) => {
  console.log(o)
  const t = o.a()
  console.log(t)
  return t.b.c
}

当调试完毕,又要改回原有代码, 也算是相当麻烦, 当使用了 kotlish-also 后,可以写成这样

const foo = (o) => 
  o.also(i => console.log(i))
    .a().also(i => console.log(i))
    .b.c

或者使用'it'省略匿名函数也可以

const foo = (o) =>
  o.also(console.log(it))
    .a().also(console.log(it))
    .b.c

插件最终会将代码转换成如下形式

const foo = (o) => {
  return function (_o) {
    (function (it) {
      return console.log(it);
    })(_o);

    return _o;
  }(function (_o) {
    (function (it) {
      return console.log(it);
    })(_o);

    return _o;
  }(o).a()).b.c;
}

甚至,插件为了方便输出,增加了alsoPrint方法

const a = obj.alsoPrint().a.alsoPrint('变量a的值为%o')

等价于

const a = obj.also(function (_o) {
  console.log(_o)
}).a.also(function (_o) {
  console.log('变量a的值为%o', _o)
})

除了also, kotlish-also 同时提供的方法还有let,takeIf,takeUnless等, 它们都是仿照kotlin编写的,其具体语法可参考 kotlin stdlib functions

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100