# babel-plugin-kotlish-also

> kotlin-like extension function (also, let and etc) support for javascript.

Latest version **1.0.8** (published 2018-05-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install babel-plugin-kotlish-also
pnpm add babel-plugin-kotlish-also
yarn add babel-plugin-kotlish-also
bun add babel-plugin-kotlish-also
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.8 |
| Published | 2018-05-09 |
| First published | 2018-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | herbluo |
| Maintainers | herbluo |
| Keywords | kotlin, javascript, babel-plugin, translator, also |

## Links

- npm: https://www.npmjs.com/package/babel-plugin-kotlish-also
- Repository: https://github.com/HerbLuo/babel-plugin-kotlish-also
- Homepage: https://github.com/HerbLuo/babel-plugin-kotlish-also#readme
- Issues: https://github.com/HerbLuo/babel-plugin-kotlish-also/issues
- npm.io page: https://npm.io/package/babel-plugin-kotlish-also

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.8 (latest) — 2018-05-09
- 1.0.7 — 2018-05-09
- 1.0.6 — 2018-05-01
- 1.0.5 — 2018-02-28
- 1.0.4 — 2018-02-22
- 1.0.3 — 2018-02-22
- 1.0.2 — 2018-02-22
- 1.0.1 — 2018-02-22
- 1.0.0 — 2018-02-22

## README

### 使用方式
一：安装
```bash
npm install --save-dev babel-plugin-kotlish-also
```
二：在.babelrc添加如下plugin
```json
{
  "plugins": [
    "kotlish-also"
  ]
}
```

### 使用场景
来看下面这段代码
```javascript
const foo = (o) => o.a().b.c
```
如果我们调试时想要输出 `o` 或 `o.a()` 等，
通常情况下，我们不得不做出如下改变
```javascript
// 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` 后，可以写成这样
```javascript
const foo = (o) => 
  o.also(i => console.log(i))
    .a().also(i => console.log(i))
    .b.c
```
或者使用'it'省略匿名函数也可以
```javascript
const foo = (o) =>
  o.also(console.log(it))
    .a().also(console.log(it))
    .b.c
```
插件最终会将代码转换成如下形式
```javascript
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方法
```javascript
const a = obj.alsoPrint().a.alsoPrint('变量a的值为%o')
```
等价于
```javascript
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](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin/index.html#functions)

---
_Source: https://npm.io/package/babel-plugin-kotlish-also · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
