# @baurine/grafana-value-formats

> grafana value formats

Latest version **1.0.5** (published 2022-08-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install @baurine/grafana-value-formats
pnpm add @baurine/grafana-value-formats
yarn add @baurine/grafana-value-formats
bun add @baurine/grafana-value-formats
```

## 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.5 |
| Published | 2022-08-31 |
| First published | 2020-03-03 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 56.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | baurine |
| Maintainers | baurine |

## Links

- npm: https://www.npmjs.com/package/@baurine/grafana-value-formats
- npm.io page: https://npm.io/package/@baurine/grafana-value-formats

## Recent versions

- 1.0.5 (latest) — 2022-08-31
- 1.0.4 — 2022-08-02
- 1.0.3 — 2021-01-04
- 1.0.2 — 2020-11-04
- 1.0.0 — 2020-05-11
- 0.1.2 — 2020-03-03

## README

# Grafana Value Formats

Extract code about value formats from grafana repo and did some slight changes.

## How to use

### Install

```sh
$ npm install @baurine/grafana-value-formats
// or
$ yarn add @baurine/grafana-value-formats
```

(I publish this package to npm registry and GitHub Packages both.)

### Use

The main function is `function getValueFormat(unitFormat: string): string {...}`, it will return a function whose signature is below:

```ts
export type ValueFormatter = (
  value: number,
  decimals?: DecimalCount,
  scaledDecimals?: DecimalCount,
  isUtc?: boolean
) => string
```

Samples: (copy from valueForamts.test.ts)

```ts
// if scaledDecimals is null, use decimals
const str = getValueFormat('ms')(10000086.123, 0, null)
expect(str).toBe('3 hour')

const str = getValueFormat('ms')(10000086.123, 1, null)
expect(str).toBe('2.8 hour')

const str = getValueFormat('kbytes')(10000000, 3, null)
expect(str).toBe('9.537 GiB')

const str = getValueFormat('deckbytes')(10000000, 3, null)
expect(str).toBe('10.000 GB')

// if scaledDecimals is not null, use scaledDecimals, ignore decimals
const str = getValueFormat('ms')(1200, 0, 0)
expect(str).toBe('1.200 s')
```

You can find all `unitFormat` we can use in the `categories.ts`, or exported `getValueFormats()` function.

## Release Notes

### 1.0.4

- Add IEC units for Data rate
- Update id of Data rate in SI units

### 1.0.3

- Use dayjs to replace momentjs

### 1.0.2

- Nothing changes, just bump version

### 1.0.1

- Add "qps" unit ([#4](https://github.com/baurine/grafana-value-formats/pull/4))

### 1.0.0

#### Breaking Changes

1. Change a part implementation of `scaledUnits()` method in `src/valueFormats/valueFormats.ts` line 126:

   ```ts
   if (steps > 0 && scaledDecimals !== null && scaledDecimals !== undefined) {
     // old logic
     // decimals = scaledDecimals + 3 * steps; // 为什么是乘以 3，大概是以 factor = 1000 为准，1000 表示 3 个小数点

     // current logic
     // related issue: https://github.com/grafana/grafana/issues/23561
     decimals = scaledDecimals
   }
   ```

   So at before

   ```ts
   expect(scaler(98765, 0, 2)).toBe('98.76500K')
   // because the decimals = 1*3 + 2
   ```

   and now

   ```ts
   expect(scaler(98765, 0, 2)).toBe('98.77K')
   // because the decimals = 2
   ```

---
_Source: https://npm.io/package/@baurine/grafana-value-formats · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
