Coder Social home page Coder Social logo

grafana-value-formats's Introduction

Grafana Value Formats

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

How to use

Install

$ 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:

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

Samples: (copy from valueForamts.test.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)

1.0.0

Breaking Changes

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

    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

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

    and now

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

grafana-value-formats's People

Contributors

baurine avatar dependabot[bot] avatar shhdgit avatar unbyte avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

grafana-value-formats's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.