# for-own.macro

> A babel-macro that makes for-in only visit own properties

Latest version **1.0.0** (published 2019-02-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install for-own.macro
pnpm add for-own.macro
yarn add for-own.macro
bun add for-own.macro
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-02-15 |
| First published | 2019-02-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Nicolò Ribaudo |
| Maintainers | nicolo-ribaudo |
| Keywords | babel-plugin-macros |

## Links

- npm: https://www.npmjs.com/package/for-own.macro
- Repository: https://github.com/nicolo-ribaudo/for-own.macro
- Homepage: https://github.com/nicolo-ribaudo/for-own.macro#readme
- Issues: https://github.com/nicolo-ribaudo/for-own.macro/issues
- npm.io page: https://npm.io/package/for-own.macro

## Dependencies (1)

- [babel-plugin-macros](https://npm.io/package/babel-plugin-macros.md) ^2.5.0

## 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.0 (latest) — 2019-02-15

## README

<div align="center">
  <h1>for-own.macro</h1>

  A babel-macro that makes for-in only visit own properties
</div>

<hr />

## The problem

`for ... in` statements get enumerable keys from the whole prototype chain.
To prevent bugs, it is reccomented to write loops like this:
```js
for (const key in obj) {
  if (obj.hasOwnProperty(key)) {
    // "key" is a property of "obj"
  }
}
```
Pretty verbose, right?

## This solution

This is a [babel-plugin-macro](https://github.com/kentcdodds/babel-plugin-macros) which allows you to iterate only over _own_ keys of an object when using `for ... in`.

## Installation

This module can be installed either with `npm` or with `yarn`, and should be installed as one of your project's `devDependencies`:
```
npm install --save-dev for-own.macro

# or

yarn add --dev for-own.macro
```

## Usage

Once you have [configured `babel-plugin-macros`](https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md#adding-the-plugin-to-your-config) you can import/requie `import-all.macro`.

Here is an example:

```js
import own from "for-own.macro";

for (const key in own(obj)) {
  const value = obj[key];
}

//     ↓ ↓ ↓ ↓ ↓ ↓

for (const key in obj) if (Object.hasOwnProperty.call(obj, key)) {
  const value = obj[key];
}
```

## Caveats

The `own` macro only works inside `for ... in` loops. This code will throw an error at compile time:

```js
const ownProps = own(obj);
```

## LICENSE

MIT

---
_Source: https://npm.io/package/for-own.macro · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
