Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@polkadot/util

Package Overview
Dependencies
Maintainers
2
Versions
1410
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@polkadot/util - npm Package Compare versions

Comparing version 10.2.3 to 10.2.4

63

cjs/format/formatBalance.js

@@ -18,17 +18,2 @@ "use strict";

let defaultUnit = DEFAULT_UNIT;
function getUnits(si, withSi, withSiFull, withUnit) {
const unit = (0, _boolean.isBoolean)(withUnit) ? _si.SI[_si.SI_MID].text : withUnit;
return withSi || withSiFull ? si.value === '-' ? withUnit ? ` ${unit}` : '' : ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}` : '';
}
function getPrePost(text, decimals, forceUnit) {
// NOTE We start at midpoint (8) minus 1 - this means that values display as
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we use ceil
// so there are at most 3 decimal before the decimal separator
const si = (0, _si.calcSi)(text, decimals, forceUnit);
const mid = text.length - (decimals + si.power);
const prefix = text.substring(0, mid);
const padding = mid < 0 ? 0 - mid : 0;
const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substring(mid < 0 ? 0 : mid)}0000`.substring(0, 4);
return [si, prefix || '0', postfix];
}

@@ -40,6 +25,10 @@ // Formats a string/number with <prefix>.<postfix><type> notation

forceUnit,
withAll = false,
withSi = true,
withSiFull = false,
withUnit = true
withUnit = true,
withZero = true
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
// we only work with string inputs here - convert anything
// into the string-only value
let text = (0, _toBn.bnToBn)(input).toString();

@@ -57,9 +46,35 @@ if (text.length === 0 || text === '0') {

}
const [si, prefix, postfix] = getPrePost(text, decimals, forceUnit);
const units = getUnits(si, withSi, withSiFull, withUnit);
return `${sign}${(0, _formatDecimal.formatDecimal)(prefix)}.${postfix}${units}`;
// We start at midpoint (8) minus 1 - this means that values display as
// 123.4567 instead of 0.1234 k (so we always have the most relevant).
const si = (0, _si.calcSi)(text, decimals, forceUnit);
const mid = text.length - (decimals + si.power);
const pre = mid <= 0 ? '0' : text.substring(0, mid);
// get the post from the midpoint onward and then first add max decimals
// before trimming to the correct (calculated) amount of decimals again
let post = text.padStart((mid < 0 ? 0 - mid : 0) + 1, '0').substring(mid < 0 ? 0 : mid).padEnd(withAll ? Math.max(decimals, 4) : 4, '0').substring(0, withAll ? Math.max(4, decimals + si.power) : 4);
// remove all trailing 0's (if required via flag)
if (!withZero) {
let end = post.length - 1;
// This looks inefficient, however it is better to do the checks and
// only make one final slice than it is to do it in multiples
do {
if (post[end] === '0') {
end--;
}
} while (post[end] === '0');
post = post.substring(0, end + 1);
}
// the display unit
const unit = (0, _boolean.isBoolean)(withUnit) ? _si.SI[_si.SI_MID].text : withUnit;
// format the units for display based on the flags
const units = withSi || withSiFull ? si.value === '-' ? withUnit ? ` ${unit}` : '' : ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}` : '';
return `${sign}${(0, _formatDecimal.formatDecimal)(pre)}${post && `.${post}`}${units}`;
}
const formatBalance = _formatBalance;
// eslint-disable-next-line @typescript-eslint/unbound-method
exports.formatBalance = formatBalance;

@@ -70,7 +85,3 @@ formatBalance.calcSi = function (text) {

};
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.findSi = _si.findSi;
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.getDefaults = () => {

@@ -84,3 +95,2 @@ return {

// get allowable options to display in a dropdown
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.getOptions = function () {

@@ -97,3 +107,2 @@ let decimals = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultDecimals;

// Sets the default decimals to use for formatting (ui-wide)
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.setDefaults = _ref2 => {

@@ -100,0 +109,0 @@ let {

@@ -16,4 +16,4 @@ "use strict";

type: 'cjs',
version: '10.2.3'
version: '10.2.4'
};
exports.packageInfo = packageInfo;

@@ -22,4 +22,8 @@ /// <reference types="bn.js" />

/**
* @description Format with SI, i.e. m/M/etc.
* @description Returns value using all available decimals
*/
withAll?: boolean;
/**
* @description Format with SI, i.e. m/M/etc. (default = true)
*/
withSi?: boolean;

@@ -34,2 +38,6 @@ /**

withUnit?: boolean | string;
/**
* @description Returns all trailing zeros, otherwise removes (default = true)
*/
withZero?: boolean;
}

@@ -36,0 +44,0 @@ interface BalanceFormatter {

@@ -12,17 +12,2 @@ // Copyright 2017-2023 @polkadot/util authors & contributors

let defaultUnit = DEFAULT_UNIT;
function getUnits(si, withSi, withSiFull, withUnit) {
const unit = isBoolean(withUnit) ? SI[SI_MID].text : withUnit;
return withSi || withSiFull ? si.value === '-' ? withUnit ? ` ${unit}` : '' : ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}` : '';
}
function getPrePost(text, decimals, forceUnit) {
// NOTE We start at midpoint (8) minus 1 - this means that values display as
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we use ceil
// so there are at most 3 decimal before the decimal separator
const si = calcSi(text, decimals, forceUnit);
const mid = text.length - (decimals + si.power);
const prefix = text.substring(0, mid);
const padding = mid < 0 ? 0 - mid : 0;
const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substring(mid < 0 ? 0 : mid)}0000`.substring(0, 4);
return [si, prefix || '0', postfix];
}

@@ -33,6 +18,10 @@ // Formats a string/number with <prefix>.<postfix><type> notation

forceUnit,
withAll = false,
withSi = true,
withSiFull = false,
withUnit = true
withUnit = true,
withZero = true
} = {}) {
// we only work with string inputs here - convert anything
// into the string-only value
let text = bnToBn(input).toString();

@@ -50,15 +39,37 @@ if (text.length === 0 || text === '0') {

}
const [si, prefix, postfix] = getPrePost(text, decimals, forceUnit);
const units = getUnits(si, withSi, withSiFull, withUnit);
return `${sign}${formatDecimal(prefix)}.${postfix}${units}`;
// We start at midpoint (8) minus 1 - this means that values display as
// 123.4567 instead of 0.1234 k (so we always have the most relevant).
const si = calcSi(text, decimals, forceUnit);
const mid = text.length - (decimals + si.power);
const pre = mid <= 0 ? '0' : text.substring(0, mid);
// get the post from the midpoint onward and then first add max decimals
// before trimming to the correct (calculated) amount of decimals again
let post = text.padStart((mid < 0 ? 0 - mid : 0) + 1, '0').substring(mid < 0 ? 0 : mid).padEnd(withAll ? Math.max(decimals, 4) : 4, '0').substring(0, withAll ? Math.max(4, decimals + si.power) : 4);
// remove all trailing 0's (if required via flag)
if (!withZero) {
let end = post.length - 1;
// This looks inefficient, however it is better to do the checks and
// only make one final slice than it is to do it in multiples
do {
if (post[end] === '0') {
end--;
}
} while (post[end] === '0');
post = post.substring(0, end + 1);
}
// the display unit
const unit = isBoolean(withUnit) ? SI[SI_MID].text : withUnit;
// format the units for display based on the flags
const units = withSi || withSiFull ? si.value === '-' ? withUnit ? ` ${unit}` : '' : ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}` : '';
return `${sign}${formatDecimal(pre)}${post && `.${post}`}${units}`;
}
export const formatBalance = _formatBalance;
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.calcSi = (text, decimals = defaultDecimals) => calcSi(text, decimals);
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.findSi = findSi;
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.getDefaults = () => {

@@ -72,3 +83,2 @@ return {

// get allowable options to display in a dropdown
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.getOptions = (decimals = defaultDecimals) => {

@@ -81,3 +91,2 @@ return SI.filter(({

// Sets the default decimals to use for formatting (ui-wide)
// eslint-disable-next-line @typescript-eslint/unbound-method
formatBalance.setDefaults = ({

@@ -84,0 +93,0 @@ decimals,

@@ -23,3 +23,3 @@ {

"type": "module",
"version": "10.2.3",
"version": "10.2.4",
"main": "./cjs/index.js",

@@ -723,6 +723,6 @@ "module": "./index.js",

"@babel/runtime": "^7.20.7",
"@polkadot/x-bigint": "10.2.3",
"@polkadot/x-global": "10.2.3",
"@polkadot/x-textdecoder": "10.2.3",
"@polkadot/x-textencoder": "10.2.3",
"@polkadot/x-bigint": "10.2.4",
"@polkadot/x-global": "10.2.4",
"@polkadot/x-textdecoder": "10.2.4",
"@polkadot/x-textencoder": "10.2.4",
"@types/bn.js": "^5.1.1",

@@ -729,0 +729,0 @@ "bn.js": "^5.2.1"

@@ -10,3 +10,3 @@ // Copyright 2017-2023 @polkadot/util authors & contributors

type: 'esm',
version: '10.2.3'
version: '10.2.4'
};

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc