# @mercari/extract-primitives

> extract-primitives extracts primitive values from TypeScript's declaration file(.d.ts).

Latest version **1.1.0** (published 2020-07-03) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @mercari/extract-primitives
pnpm add @mercari/extract-primitives
yarn add @mercari/extract-primitives
bun add @mercari/extract-primitives
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2020-07-03 |
| First published | 2020-07-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | mercari_bocelli, tweeeety, whatasoda |
| Keywords | typescript |

## Links

- npm: https://www.npmjs.com/package/@mercari/extract-primitives
- Repository: https://github.com/mercari/extract-primitives
- Homepage: https://github.com/mercari/extract-primitives#readme
- Issues: https://github.com/mercari/extract-primitives/issues
- npm.io page: https://npm.io/package/@mercari/extract-primitives

## 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.1.0 (latest) — 2020-07-03
- 1.0.0 — 2020-07-02

## README

# extract-primitives
[![npm version](https://badge.fury.io/js/%40mercari%2Fextract-primitives.svg)](https://badge.fury.io/js/%40mercari%2Fextract-primitives)

`extract-primitives` extracts primitive values from TypeScript's declaration file(`.d.ts`).

```ts
// primitives.d.ts
declare namespace Japan {
  namespace Tokyo {
    namespace Minato {
      const isInTokyo: true;
      const isInOsaka: false;
      const area: 20.37;
      namespace Roppongi {
        const Station: 'H04';
      }
    }
  }
  namespace Osaka {
    interface Umeda {
      isInTokyo: false;
      hasYodobashiCamera: true;
    }
  }
}
```

```ts
import extractPrimitives from 'extract-primitives';

const primitives = extractPrimitives(['primitives.d.ts']);

console.log(primitives);
// {
//   Japan: {
//     Tokyo: {
//       Minato: {
//         isInTokyo: true,
//         isInOsaka: false,
//         area: 20.37,
//         Roppongi: {
//           Station: 'H04',
//         },
//       },
//     },
//   },
// }
```

## Installation

```sh
$ npm i -D extract-primitives
$ npm i -D typescript # a peer dependency
```

## Usage

As you see the example above, you can use it by passing file paths. `extract-primitives` accepts multiple files.

Note that a namespace which has no valid primitives are removed from results, like `Osaka` in the example.

### with `webpack.DefinePlugin`

`extract-primitives` makes a great contribution by using with `webpack.DefinePlugin`. You can use values in a declaration file without implementation.

For example, [`proto-to-type`](https://github.com/mercari/proto-to-type) generates an enum declaration like:
```ts
// my-proto.d.ts
type Sample =
  | Sample.UNKNOWN
  | Sample.ACTIVE
  | Sample.INACTIVE;
namespace Sample {
  type UNKNOWN = "UNKNOWN" | 0;
  namespace UNKNOWN {
    const str: "UNKNOWN";
    const num: 0;
  }
  type ACTIVE = "ACTIVE" | 1;
  namespace ACTIVE {
    const str: "ACTIVE";
    const num: 1;
  }
  type INACTIVE = "INACTIVE" | 2;
  namespace INACTIVE {
    const str: "INACTIVE";
    const num: 2;
  }
}
```

The enum `Sample` is able to be used in bundled file by passing extracted primitives to `webpack.DefinePlugin`.

```js
// webpack.config.js
const webpack = require('webpack');
const extractPrimitives = require('extract-primitives');

module.exports = {
  /* ... */
  plugins: [
    new webpack.DefinePlugin(extractPrimitives(['my-proto.d.ts'])),
  ],
  /* ... */
};
```

```ts
console.log(Sample.ACTIVE.str); // 'ACTIVE'
console.log(Sample.ACTIVE.num); // 1
```

### with testing framework

You need to set the primitives as global variables in the testing framework.

The following is an example of `jest`.

```js
// jest.config.js
const extractPrimitives = require('extract-primitives');
module.exports = {
  /* ... */
  globals: {
    ...extractPrimitives(['primitives.d.ts']),
  },
  /* ... */
};
```

## Package Release Flow

We use [semantic-release](https://github.com/semantic-release/semantic-release) to automate package release workflow.

- Note that new version will be published every time when anything is merged into master.
- Merge to `next` branch first if you don't want to set the version as default. Users can install the version by `npm i @mercari/extract-primitives@next`.
- Create branches such as `1.x.x` or `1.1.x` if you want to maintain old major releases.

Take a look [this doc](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/workflow-configuration.md#branch-types) about more detail.

## Committers

 * Shota Hatada ([@whatasoda](https://github.com/whatasoda))

## Contribution

Please read the CLA carefully before submitting your contribution to Mercari.
Under any circumstances, by submitting your contribution, you are deemed to accept and agree to be bound by the terms and conditions of the CLA.

https://www.mercari.com/cla/

## License

Copyright 2020 Mercari, Inc.

Licensed under the MIT License.

---
_Source: https://npm.io/package/@mercari/extract-primitives · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
