20.0.8 • Published 1 month ago

@consolelabs/mochi-formatter v20.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
1 month ago

Mochi UI

This package includes the basic components that you need to render any Mochi data, generally speaking there are 3 main goal:

  • Render Mochi identity
  • Use components such as balance list, activities, transactions list, etc...
  • Utilize helpers such as text/number formatters, etc...

🚀 Usage

import API from "@consolelabs/mochi-rest";
import UI, { utils } from "@consolelabs/mochi-formatter";
import Redis from "ioredis";

// optionally use redis for better perf
UI.redis = new Redis();

const api = new API({});
api.init().then(() => {
  UI.api = api;
});

const [account, otherAccount] = await UI.formatProfile(
  Platform.Web,
  profile_id,
  other_profile_id
);

const markdownText = await UI.components.balance({
  /* props */
});

const formattedUsd = utils.formatUsdDigit(23.12563);
console.log(formattedUsd); // "$23.12"

🤖 API

resolve(on: Platform.Web | Platform.Discord | Platform.Telegram, profile_id_A: string, profile_id_B?: string)

const [account, otherAccount] = await UI.formatProfile(
  Platform.Web, // if you're using UI library on web
  profile_id,
  other_profile_id
);

// or using it on self
const [account] = await UI.formatProfile(Platform.Web, profile_id);

Takes in a platform that you're rendering on, a pair of profile ids to resolve, profile_id_B defaults to profile_id_A if not passed in. The return value is an object with the following properties

type UsernameFmt = {
  // this value is markdown
  value: string;
  // the id of that platform
  id: string;
  // url of this account
  url: string;
  // same as `value` but in plain text format
  plain: string;
  // in case of invalid account, this will be null
  platform?:
    | Platform.App
    | Platform.Mochi
    | Platform.Discord
    | Platform.Telegram
    | Platform.Twitter
    | Platform.Vault
    | null;
};

Utilities

  • formatUsdDigit(input: string | number | object): string: returns a string representation of the usd value with $ prefix
  • formatPercentDigit(input: string | number | object): string: returns a string representation of the percentage value with % suffix
  • formatTokenDigit(input: string | number | object): string: returns a string representation of the token value
  • formatDigit(options: object): string: the base formatter, the shape of options is:
type Options = {
  value: string | number;
  // how many numbers to keep after decimal point
  fractionDigits?: number;
  // decides whether the result should have commas
  withoutCommas?: boolean;
  // use shorten form e.g. 200,000 -> 200K, $1,234,567,890 -> $1B
  // note that by using this option some precision of number will be lost
  shorten?: boolean;
  // use for very small numbers e.g. 1e-8
  scientificFormat?: boolean;
};

Note: All the format functions except formatDigit follows the same decimal point formatting rule, that is:

  • For percentage value, automatically hide decimal point if value >= 10
  • For USD value, automatically hide decimal point if value >= 100
  • For token value, automatically hide decimal point if value >= 1000

  • In case of having to show decimal point, we only take maximum 2 digits that are not 0 starting from the dot, if both are 0 then take the first digit that is not zero, some examples:

0.02345 -> 0.02
0.2103 -> 0.2
0.00003981 -> 0.0003

components (coming soon)

20.0.8

1 month ago

20.0.7

1 month ago

20.0.6

2 months ago

20.0.5

2 months ago

20.0.4

2 months ago

20.0.3

2 months ago

20.0.1

2 months ago

20.0.2

2 months ago

19.0.7

3 months ago

19.0.9

3 months ago

19.0.8

3 months ago

19.0.10

3 months ago

19.0.12

3 months ago

19.0.11

3 months ago

20.0.0

3 months ago

19.0.5

3 months ago

19.0.4

3 months ago

19.0.6

3 months ago

19.0.3

3 months ago

19.0.2

4 months ago

19.0.1

4 months ago

19.0.0

4 months ago

18.2.9

4 months ago

18.2.8

4 months ago

18.2.7

4 months ago

18.2.6

4 months ago

18.2.5

4 months ago

18.2.4

4 months ago

18.2.3

4 months ago

18.2.2

4 months ago

18.2.1

4 months ago

18.2.0

4 months ago

18.1.3

4 months ago

18.1.2

4 months ago

18.1.1

4 months ago

18.1.0

4 months ago

18.0.5

4 months ago

18.0.4

4 months ago

18.0.3

4 months ago

18.0.2

4 months ago

18.0.1

5 months ago

18.0.0

5 months ago

17.0.1

5 months ago

0.0.1-rc.57

9 months ago

0.0.1-rc.56

9 months ago

0.0.1-rc.55

9 months ago

0.0.1-rc.54

9 months ago

0.0.1-rc.53

9 months ago

0.0.1-rc.52

9 months ago

0.0.1-rc.51

9 months ago

0.0.1-rc.50

9 months ago

0.0.1-rc.49

9 months ago

0.0.1-rc.48

9 months ago

0.0.1-rc.47

9 months ago

0.0.1-rc.46

9 months ago

0.0.1-rc.45

9 months ago

0.0.1-rc.44

9 months ago

0.0.1-rc.43

9 months ago

0.0.1-rc.42

9 months ago

0.0.1-rc.41

9 months ago

0.0.1-rc.40

9 months ago

0.0.1-rc.39

9 months ago

0.0.1-rc.38

9 months ago

0.0.1-rc.37

9 months ago

0.0.1-rc.36

9 months ago

0.0.1-rc.34

9 months ago

0.0.1-rc.33

9 months ago

0.0.1-rc.32

9 months ago

0.0.1-rc.31

9 months ago

0.0.1-rc.30

9 months ago

0.0.1-rc.29

9 months ago

0.0.1-rc.28

9 months ago

0.0.1-rc.27

9 months ago

0.0.1-rc.26

9 months ago

0.0.1-rc.25

9 months ago

0.0.1-rc.24

9 months ago

0.0.1-rc.23

9 months ago

0.0.1-rc.22

9 months ago

0.0.1-rc.21

9 months ago

0.0.1-rc.20

9 months ago

0.0.1-rc.19

9 months ago

0.0.1-rc.18

9 months ago

0.0.1-rc.17

9 months ago

0.0.1-rc.16

9 months ago

0.0.1-rc.15

9 months ago

0.0.1-rc.14

9 months ago

0.0.1-rc.13

9 months ago

0.0.1-rc.12

9 months ago

0.0.1-rc.11

9 months ago

0.0.1-rc.10

9 months ago

0.0.1-rc.9

9 months ago

0.0.1-rc.8

9 months ago

0.0.1-rc.7

9 months ago

0.0.1-rc.6

9 months ago

0.0.1-rc.5

9 months ago

0.0.1-rc.4

9 months ago

0.0.1-rc.3

9 months ago

0.0.1-rc.2

9 months ago

0.0.1-rc.1

9 months ago

0.0.1-rc.0

9 months ago