@stellar-expert/formatter 2.5.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/formatter.js +34 -20
- package/lib/formatter.js.map +1 -1
- package/package.json +7 -7
- package/src/numeric-format.js +3 -3
- package/src/stroops.js +31 -11
package/lib/formatter.js
CHANGED
|
@@ -39,7 +39,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
39
39
|
"default": () => (/* binding */ commonjs)
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
;//
|
|
42
|
+
;// ./src/truncation.js
|
|
43
43
|
/**
|
|
44
44
|
* Remove trailing zero symbols from a formatted numeric string
|
|
45
45
|
* @param {String} value
|
|
@@ -65,27 +65,30 @@ function shortenString(value, symbols = 8) {
|
|
|
65
65
|
const affixLength = Math.max(2, Math.floor(symbols / 2));
|
|
66
66
|
return value.substring(0, affixLength) + '…' + value.substring(value.length - affixLength);
|
|
67
67
|
}
|
|
68
|
-
;//
|
|
68
|
+
;// ./src/stroops.js
|
|
69
69
|
|
|
70
70
|
|
|
71
71
|
/**
|
|
72
|
-
* Convert value in stroops (
|
|
72
|
+
* Convert value in stroops (raw amount) to the normal string representation
|
|
73
73
|
* @param {String|Number|BigInt} valueInStroops
|
|
74
|
+
* @param {Number} [decimals]
|
|
74
75
|
* @return {String}
|
|
75
76
|
*/
|
|
76
|
-
function fromStroops(valueInStroops) {
|
|
77
|
+
function fromStroops(valueInStroops, decimals = 7) {
|
|
77
78
|
try {
|
|
79
|
+
decimals = normalizeDecimals(decimals);
|
|
78
80
|
let parsed = typeof valueInStroops === 'bigint' ? valueInStroops : BigInt(valueInStroops.toString().replace(/\.\d*/, ''));
|
|
79
81
|
let negative = false;
|
|
80
82
|
if (parsed < 0n) {
|
|
81
83
|
negative = true;
|
|
82
84
|
parsed *= -1n;
|
|
83
85
|
}
|
|
84
|
-
const
|
|
85
|
-
const
|
|
86
|
+
const divider = 10n ** BigInt(decimals);
|
|
87
|
+
const int = parsed / divider;
|
|
88
|
+
const fract = parsed % divider;
|
|
86
89
|
let res = int.toString();
|
|
87
90
|
if (fract) {
|
|
88
|
-
res += '.' + fract.toString().padStart(
|
|
91
|
+
res += '.' + fract.toString().padStart(decimals, '0');
|
|
89
92
|
}
|
|
90
93
|
if (negative) {
|
|
91
94
|
res = '-' + res;
|
|
@@ -97,30 +100,33 @@ function fromStroops(valueInStroops) {
|
|
|
97
100
|
}
|
|
98
101
|
|
|
99
102
|
/**
|
|
100
|
-
* Convert arbitrary stringified amount to
|
|
103
|
+
* Convert arbitrary stringified amount to raw representation
|
|
101
104
|
* @param {String|Number} value
|
|
105
|
+
* @param {Number} [decimals]
|
|
102
106
|
* @return {BigInt}
|
|
103
107
|
*/
|
|
104
|
-
function toStroops(value) {
|
|
108
|
+
function toStroops(value, decimals = 7) {
|
|
105
109
|
if (!value) return 0n;
|
|
106
110
|
if (typeof value === 'number') {
|
|
107
111
|
value = value.toFixed(7);
|
|
108
112
|
}
|
|
109
113
|
if (typeof value !== 'string' || !/^-?[\d.,]+$/.test(value)) return 0n; //invalid format
|
|
110
114
|
try {
|
|
115
|
+
decimals = normalizeDecimals(decimals);
|
|
111
116
|
let [int, decimal = '0'] = value.split('.', 2);
|
|
112
117
|
let negative = false;
|
|
113
118
|
if (int.startsWith('-')) {
|
|
114
119
|
negative = true;
|
|
115
120
|
int = int.slice(1);
|
|
116
121
|
}
|
|
117
|
-
|
|
122
|
+
const divider = 10n ** BigInt(decimals);
|
|
123
|
+
let res = BigInt(int) * divider + BigInt(decimal.slice(0, decimals).padEnd(decimals, '0'));
|
|
118
124
|
if (negative) {
|
|
119
125
|
res *= -1n;
|
|
120
|
-
if (res < -
|
|
126
|
+
if (res < -0x80000000000000000000000000000000n)
|
|
121
127
|
//overflow
|
|
122
128
|
return 0n;
|
|
123
|
-
} else if (res >
|
|
129
|
+
} else if (res > 0x80000000000000000000000000000000n)
|
|
124
130
|
//overflow
|
|
125
131
|
return 0n;
|
|
126
132
|
return res;
|
|
@@ -128,11 +134,19 @@ function toStroops(value) {
|
|
|
128
134
|
return 0n;
|
|
129
135
|
}
|
|
130
136
|
}
|
|
131
|
-
|
|
137
|
+
function normalizeDecimals(decimals) {
|
|
138
|
+
if (decimals === undefined) return 7;
|
|
139
|
+
if (typeof decimals !== 'number') {
|
|
140
|
+
decimals = parseInt(decimals.toString(), 0);
|
|
141
|
+
}
|
|
142
|
+
if (decimals < 0) return 0;
|
|
143
|
+
if (decimals > 38) return 38;
|
|
144
|
+
return decimals;
|
|
145
|
+
}
|
|
146
|
+
;// ./src/numeric-format.js
|
|
132
147
|
|
|
133
148
|
|
|
134
149
|
function addDecimalsSeparators(value, separator = ',', trimTrailingZeros = true) {
|
|
135
|
-
//TODO: use Bignumber.toFormat() method instead
|
|
136
150
|
//split numeric to parts
|
|
137
151
|
let [int, reminder] = value.split('.');
|
|
138
152
|
let res = '';
|
|
@@ -202,7 +216,7 @@ function formatWithAutoPrecision(value, separator = ',') {
|
|
|
202
216
|
}
|
|
203
217
|
return formatWithPrecision(value, reminderPrecision, separator);
|
|
204
218
|
}
|
|
205
|
-
|
|
219
|
+
const abbreviationPrefixes = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q'];
|
|
206
220
|
/**
|
|
207
221
|
* Convert a number to a human-readable format using abbreviation
|
|
208
222
|
* @param {Number} value - Value to format
|
|
@@ -213,7 +227,7 @@ function formatWithAbbreviation(value, decimals = 2) {
|
|
|
213
227
|
let abs = Math.abs(value);
|
|
214
228
|
const tier = Math.log10(abs) / 3 | 0;
|
|
215
229
|
if (tier <= 0) return formatWithAutoPrecision(value);
|
|
216
|
-
const suffix = [
|
|
230
|
+
const suffix = abbreviationPrefixes[tier];
|
|
217
231
|
abs = stripTrailingZeros((abs / Math.pow(10, tier * 3)).toFixed(decimals));
|
|
218
232
|
return `${value < 0 ? '-' : ''}${abs || '0'}${suffix}`;
|
|
219
233
|
}
|
|
@@ -235,7 +249,7 @@ function formatWithGrouping(value, group) {
|
|
|
235
249
|
|
|
236
250
|
/**
|
|
237
251
|
* Format a number as price with specified significant digits precision
|
|
238
|
-
* @param {String|Number|
|
|
252
|
+
* @param {String|Number|BigInt} value - Value to format
|
|
239
253
|
* @param {Number} significantDigits - Significant digits for automatic formatting
|
|
240
254
|
* @return {String}
|
|
241
255
|
*/
|
|
@@ -274,7 +288,7 @@ function setPrecision(amount, precision) {
|
|
|
274
288
|
}
|
|
275
289
|
return amount;
|
|
276
290
|
}
|
|
277
|
-
;//
|
|
291
|
+
;// ./src/timestamp-format.js
|
|
278
292
|
/**
|
|
279
293
|
* Convert any timestamp representation to a valid date format
|
|
280
294
|
* @param {Date|String|Number} date - Date to parse
|
|
@@ -324,7 +338,7 @@ function formatDateUTC(date) {
|
|
|
324
338
|
return normalizeDate(date).toISOString().replace(/(T|\.\d+Z)/g, ' ') // make it more human friendly
|
|
325
339
|
.trim();
|
|
326
340
|
}
|
|
327
|
-
;//
|
|
341
|
+
;// ./src/approximation.js
|
|
328
342
|
/**
|
|
329
343
|
* Convert rational price representation to Number
|
|
330
344
|
* @param {{n: Number, d: Number}|Number|String} price
|
|
@@ -336,7 +350,7 @@ function approximatePrice(price) {
|
|
|
336
350
|
if (typeof price === 'string') return parseFloat(price);
|
|
337
351
|
return price;
|
|
338
352
|
}
|
|
339
|
-
;//
|
|
353
|
+
;// ./src/commonjs.js
|
|
340
354
|
|
|
341
355
|
|
|
342
356
|
|
package/lib/formatter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter.js","mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;;UCVA;UACA;;;;;WCDA;WACA;WACA;WACA;WACA,yCAAyC,wCAAwC;WACjF;WACA;WACA;;;;;WCPA;;;;;;;;;;;;ACAA;AACA;AACA;AACA;AACA;AACO,SAASA,kBAAkBA,CAACC,KAAK,EAAE;EACtC,IA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AAa,CAACC,IAAI,CAAC,CACrBW,WAAW,CAAC,CAAC,CACbpE,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC;EAAA,CAC5BqE,IAAI,CAAC,CAAC;AACf;;ACnDA;AACA;AACA;AACA;AACA;AACO,SAASC,gBAAgBA,CAACC,KAAK,EAAE;EACpC,IAAI,CAACA,KAAK,EACN,OAAO,CAAC;EACZ,IAAIA,KAAK,CAACC,CAAC,EACP,OAAOD,KAAK,CAACC,CAAC,GAAGD,KAAK,CAACE,CAAC;EAC5B,IAAI,OAAOF,KAAK,KAAK,QAAQ,EACzB,OAAOhC,UAAU,CAACgC,KAAK,CAAC;EAC5B,OAAOA,KAAK;AAChB;;ACL4B;AACuD;AAChC;AACc;AACd;AAEnD,MAAMG,SAAS,GAAG;EACdtB,eAAe;EACfjB,uBAAuB;EACvBO,sBAAsB;EACtBK,kBAAkB;EAClBf,mBAAmB;EACnBiB,WAAW;EACXpB,kBAAkB;EAClBsC,aAAa;EACbX,aAAa;EACbS,eAAe;EACfK,gBAAgB;EAChBpE,aAAa;EACbP,kBAAkB;EAClBwB,SAAS;EACTV,WAAWA,EAAAA,WAAAA;AACf,CAAC;AAED,+CAAeiE,SAAS,E","sources":["webpack://formatter/webpack/universalModuleDefinition","webpack://formatter/webpack/bootstrap","webpack://formatter/webpack/runtime/define property getters","webpack://formatter/webpack/runtime/hasOwnProperty shorthand","webpack://formatter/./src/truncation.js","webpack://formatter/./src/stroops.js","webpack://formatter/./src/numeric-format.js","webpack://formatter/./src/timestamp-format.js","webpack://formatter/./src/approximation.js","webpack://formatter/./src/commonjs.js"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"formatter\"] = factory();\n\telse\n\t\troot[\"formatter\"] = factory();\n})(this, () => {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","/**\n * Remove trailing zero symbols from a formatted numeric string\n * @param {String} value\n * @return {String}\n */\nexport function stripTrailingZeros(value) {\n if (typeof value !== 'string')\n return value\n let [int, reminder] = value.split('.')\n if (!reminder)\n return int\n reminder = reminder.replace(/0+$/, '')\n if (!reminder.length)\n return int\n return int + '.' + reminder\n}\n\n/**\n * Truncate strings longer than N symbols replacing characters in the middle with ellipsis\n * @param {String} value - Original string\n * @param {Number} [symbols] - Maximum string length\n * @return {String}\n */\nexport function shortenString(value, symbols = 8) {\n if (!value || value.length <= symbols)\n return value\n const affixLength = Math.max(2, Math.floor(symbols / 2))\n return value.substring(0, affixLength) + '…' + value.substring(value.length - affixLength)\n}","import {stripTrailingZeros} from './truncation.js'\n\n/**\n * Convert value in stroops (Int64 amount) to the normal string representation\n * @param {String|Number|BigInt} valueInStroops\n * @return {String}\n */\nexport function fromStroops(valueInStroops) {\n try {\n let parsed = typeof valueInStroops === 'bigint' ?\n valueInStroops :\n BigInt(valueInStroops.toString().replace(/\\.\\d*/,''))\n let negative = false\n if (parsed < 0n) {\n negative = true\n parsed *= -1n\n }\n const int = parsed / 10000000n\n const fract = parsed % 10000000n\n let res = int.toString()\n if (fract) {\n res += '.' + fract.toString().padStart(7, '0')\n }\n if (negative) {\n res = '-' + res\n }\n return stripTrailingZeros(res)\n } catch (e) {\n return '0'\n }\n}\n\n\n/**\n * Convert arbitrary stringified amount to int64 representation\n * @param {String|Number} value\n * @return {BigInt}\n */\nexport function toStroops(value) {\n if (!value)\n return 0n\n if (typeof value === 'number') {\n value = value.toFixed(7)\n }\n if (typeof value !== 'string' || !/^-?[\\d.,]+$/.test(value))\n return 0n //invalid format\n try {\n let [int, decimal = '0'] = value.split('.', 2)\n let negative = false\n if (int.startsWith('-')) {\n negative = true\n int = int.slice(1)\n }\n let res = BigInt(int) * 10000000n + BigInt(decimal.slice(0, 7).padEnd(7, '0'))\n if (negative) {\n res *= -1n\n if (res < -0x8000000000000000n) //overflow\n return 0n\n } else if (res > 0xFFFFFFFFFFFFFFFFn) //overflow\n return 0n\n return res\n } catch (e) {\n return 0n\n }\n}","import {stripTrailingZeros} from './truncation.js'\nimport {toStroops} from './stroops.js'\n\nfunction addDecimalsSeparators(value, separator = ',', trimTrailingZeros = true) {\n //TODO: use Bignumber.toFormat() method instead\n //split numeric to parts\n let [int, reminder] = value.split('.')\n let res = ''\n //split digit groups\n while (int.length > 3) {\n res = separator + int.substring(int.length - 3) + res\n int = int.substring(0, int.length - 3)\n }\n //strip negative sign\n if (int === '-') {\n res = res.substring(1)\n }\n res = int + res\n if (reminder) {\n res += '.' + reminder\n }\n if (trimTrailingZeros) {\n res = stripTrailingZeros(res)\n }\n return res\n}\n\n/**\n * Check if a provided value can be safely used as token amount in Stellar operations\n * @param {String} value\n * @param {Boolean} denominate\n * @param {Boolean} nonZero\n * @return {Boolean}\n */\nexport function isValidInt64Amount(value, denominate = true, nonZero = false) {\n try {\n const parsed = denominate ?\n toStroops(value) :\n BigInt(value)\n if (nonZero) {\n if (parsed <= 0n)\n return false\n } else {\n if (parsed < 0n)\n return false\n }\n return parsed <= 9223372036854775807n\n } catch (e) {\n return false\n }\n}\n\n/**\n * Format a number with specified precision and decimals separator\n * @param {String|Number|BigInt} value - Numeric value to format\n * @param {Number} [precision] - Desired precision (7 digits by default)\n * @param {String} [separator] - Decimals separator\n * @return {String}\n */\nexport function formatWithPrecision(value, precision = 7, separator = ',') {\n return addDecimalsSeparators(setPrecision(value, precision), separator, true)\n}\n\n/**\n * Format a number using automatically determined precision\n * @param {String|Number} value - Numeric value to format\n * @param {String} [separator] - Decimals separator\n * @return {String}\n */\nexport function formatWithAutoPrecision(value, separator = ',') {\n if (!value)\n return '0'\n const p = Math.ceil(Math.log10(parseFloat(value)))\n let reminderPrecision = p > 1 ?\n (3 - p) :\n (Math.abs(p) + 2)\n if (reminderPrecision < 0) {\n reminderPrecision = 0\n }\n return formatWithPrecision(value, reminderPrecision, separator)\n}\n\n/**\n * Convert a number to a human-readable format using abbreviation\n * @param {Number} value - Value to format\n * @param {Number} [decimals] - Precision of the abbreviated string to retain\n * @return {String}\n */\nexport function formatWithAbbreviation(value, decimals = 2) {\n let abs = Math.abs(value)\n const tier = Math.log10(abs) / 3 | 0\n\n if (tier <= 0)\n return formatWithAutoPrecision(value)\n\n const suffix = ['', 'K', 'M', 'G', 'T', 'P'][tier]\n abs = stripTrailingZeros((abs / Math.pow(10, tier * 3)).toFixed(decimals))\n return `${value < 0 ? '-' : ''}${abs || '0'}${suffix}`\n}\n\n/**\n * Format a number with rounding to specific precision group\n * @param {String|Number|BigInt} value - Value to format\n * @param {Number} group - Logarithmic group rate for rounding\n * @return {String}\n */\nexport function formatWithGrouping(value, group) {\n if (!value)\n return '0'\n const precision = (group >= 1 || group === 0) ?\n 0 :\n Math.abs(Math.log10(group))\n if (group >= 1) {\n value = Math.ceil(value / group) * group\n }\n return formatWithPrecision(value, precision)\n}\n\n/**\n * Format a number as price with specified significant digits precision\n * @param {String|Number|Bignumber} value - Value to format\n * @param {Number} significantDigits - Significant digits for automatic formatting\n * @return {String}\n */\nexport function formatPrice(value, significantDigits = 4) {\n const primitive = (typeof value === 'number') ?\n value.toFixed(7) :\n value.toString()\n const [int, fract] = primitive.split('.')\n if (int.replace('-', '') !== '0') {\n significantDigits -= int.length\n if (significantDigits < 0) {\n significantDigits = 0\n }\n } else {\n if (!(fract > 0))\n return '0'\n significantDigits = Math.max(Math.ceil(Math.abs(Math.log10(parseFloat('.' + fract)))), significantDigits)\n }\n return formatWithPrecision(value, significantDigits, '')\n}\n\n/**\n * Format amount according to default Stellar precision\n * @param {Number|String} amount - Value to format\n * @return {String}\n */\nexport function adjustPrecision(amount = '0') {\n return stripTrailingZeros(setPrecision(amount, 7))\n}\n\nfunction setPrecision(amount, precision) {\n if (typeof amount === 'number') {\n amount = amount.toFixed(precision)\n } else {\n amount = amount.toString()\n const sidx = amount.indexOf('.')\n if (sidx >= 0) {\n amount = amount.slice(0, sidx + precision + 1)\n }\n }\n return amount\n}","/**\n * Convert any timestamp representation to a valid date format\n * @param {Date|String|Number} date - Date to parse\n * @param {Boolean} [autoUnixFormatDetection] - Intelligent guess for dates already represented as UNIX timestamp (true by default)\n * @return {Date} Parsed date\n */\nexport function normalizeDate(date, autoUnixFormatDetection = true) {\n //try parse string representation\n if (typeof date === 'string') {\n if (!/^\\d+$/.test(date)) {\n if (!date.endsWith('Z')) {\n date += 'Z'\n }\n date = new Date(date)\n } else {\n date = parseInt(date)\n }\n }\n //parse numeric representation\n if (typeof date === 'number') {\n //input resembles a UNIX timestamp\n if (date < 2147483648 && autoUnixFormatDetection) {\n date *= 1000\n }\n date = new Date(date)\n }\n //check validity\n if (!(date instanceof Date) || isNaN(date.valueOf()))\n throw new Error('Invalid timestamp ' + date)\n return date\n}\n\n/**\n * Convert date to a numeric UNIX timestamp representation\n * @param {Date|String|Number} date - Date to convert\n * @return {Number} UNIX timestamp\n */\nexport function toUnixTimestamp(date) {\n return Math.floor(normalizeDate(date).getTime() / 1000)\n}\n\n/**\n * Convert date to ISO-like human-readable format\n * @param {Date|String|Number} date - Date to convert\n * @return {String}\n */\nexport function formatDateUTC(date) {\n return normalizeDate(date)\n .toISOString()\n .replace(/(T|\\.\\d+Z)/g, ' ') // make it more human friendly\n .trim()\n}","/**\n * Convert rational price representation to Number\n * @param {{n: Number, d: Number}|Number|String} price\n * @return {Number}\n */\nexport function approximatePrice(price) {\n if (!price)\n return 0\n if (price.n)\n return price.n / price.d\n if (typeof price === 'string')\n return parseFloat(price)\n return price\n}","import {\r\n adjustPrecision,\r\n formatWithAutoPrecision,\r\n formatWithAbbreviation,\r\n formatWithGrouping,\r\n formatWithPrecision,\r\n formatPrice,\r\n isValidInt64Amount\r\n} from './numeric-format.js'\r\nimport {formatDateUTC, normalizeDate, toUnixTimestamp} from './timestamp-format.js'\r\nimport {approximatePrice} from './approximation.js'\r\nimport {shortenString, stripTrailingZeros} from './truncation.js'\r\nimport {toStroops, fromStroops} from './stroops.js'\r\n\r\nconst formatter = {\r\n adjustPrecision,\r\n formatWithAutoPrecision,\r\n formatWithAbbreviation,\r\n formatWithGrouping,\r\n formatWithPrecision,\r\n formatPrice,\r\n isValidInt64Amount,\r\n formatDateUTC,\r\n normalizeDate,\r\n toUnixTimestamp,\r\n approximatePrice,\r\n shortenString,\r\n stripTrailingZeros,\r\n toStroops,\r\n fromStroops\r\n}\r\n\r\nexport default formatter"],"names":["stripTrailingZeros","value","int","reminder","split","replace","length","shortenString","symbols","affixLength","Math","max","floor","substring","fromStroops","valueInStroops","parsed","BigInt","toString","negative","fract","res","padStart","e","toStroops","toFixed","test","decimal","startsWith","slice","padEnd","addDecimalsSeparators","separator","trimTrailingZeros","isValidInt64Amount","denominate","nonZero","formatWithPrecision","precision","setPrecision","formatWithAutoPrecision","p","ceil","log10","parseFloat","reminderPrecision","abs","formatWithAbbreviation","decimals","tier","suffix","pow","formatWithGrouping","group","formatPrice","significantDigits","primitive","adjustPrecision","amount","sidx","indexOf","normalizeDate","date","autoUnixFormatDetection","endsWith","Date","parseInt","isNaN","valueOf","Error","toUnixTimestamp","getTime","formatDateUTC","toISOString","trim","approximatePrice","price","n","d","formatter"],"sourceRoot":""}
|
|
1
|
+
{"version":3,"file":"formatter.js","mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;;UCVA;UACA;;;;;WCDA;WACA;WACA;WACA;WACA,yCAAyC,wCAAwC;WACjF;WACA;WACA,E;;;;;WCPA,wF;;;;;;;;;;;;ACAA;AACA;AACA;AACA;AACA;AACO,SAASA,kBAAkBA,CAACC,KAAK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AChB,C;;ACL4B;AACuD;AAChC;AACc;AACd;AAEnD,MAAMG,SAAS,GAAG;EACdrB,eAAe;EACfjB,uBAAuB;EACvBQ,sBAAsB;EACtBI,kBAAkB;EAClBf,mBAAmB;EACnBiB,WAAW;EACXpB,kBAAkB;EAClBqC,aAAa;EACbV,aAAa;EACbQ,eAAe;EACfK,gBAAgB;EAChBxE,aAAa;EACbP,kBAAkB;EAClB2B,SAAS;EACTb,WAAWA,EAAAA,WAAAA;AACf,CAAC;AAED,+CAAeqE,SAAS,E","sources":["webpack://formatter/webpack/universalModuleDefinition","webpack://formatter/webpack/bootstrap","webpack://formatter/webpack/runtime/define property getters","webpack://formatter/webpack/runtime/hasOwnProperty shorthand","webpack://formatter/./src/truncation.js","webpack://formatter/./src/stroops.js","webpack://formatter/./src/numeric-format.js","webpack://formatter/./src/timestamp-format.js","webpack://formatter/./src/approximation.js","webpack://formatter/./src/commonjs.js"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"formatter\"] = factory();\n\telse\n\t\troot[\"formatter\"] = factory();\n})(this, () => {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","/**\n * Remove trailing zero symbols from a formatted numeric string\n * @param {String} value\n * @return {String}\n */\nexport function stripTrailingZeros(value) {\n if (typeof value !== 'string')\n return value\n let [int, reminder] = value.split('.')\n if (!reminder)\n return int\n reminder = reminder.replace(/0+$/, '')\n if (!reminder.length)\n return int\n return int + '.' + reminder\n}\n\n/**\n * Truncate strings longer than N symbols replacing characters in the middle with ellipsis\n * @param {String} value - Original string\n * @param {Number} [symbols] - Maximum string length\n * @return {String}\n */\nexport function shortenString(value, symbols = 8) {\n if (!value || value.length <= symbols)\n return value\n const affixLength = Math.max(2, Math.floor(symbols / 2))\n return value.substring(0, affixLength) + '…' + value.substring(value.length - affixLength)\n}","import {stripTrailingZeros} from './truncation.js'\n\n/**\n * Convert value in stroops (raw amount) to the normal string representation\n * @param {String|Number|BigInt} valueInStroops\n * @param {Number} [decimals]\n * @return {String}\n */\nexport function fromStroops(valueInStroops, decimals = 7) {\n try {\n decimals = normalizeDecimals(decimals)\n let parsed = typeof valueInStroops === 'bigint' ?\n valueInStroops :\n BigInt(valueInStroops.toString().replace(/\\.\\d*/, ''))\n let negative = false\n if (parsed < 0n) {\n negative = true\n parsed *= -1n\n }\n const divider = 10n ** BigInt(decimals)\n const int = parsed / divider\n const fract = parsed % divider\n let res = int.toString()\n if (fract) {\n res += '.' + fract.toString().padStart(decimals, '0')\n }\n if (negative) {\n res = '-' + res\n }\n return stripTrailingZeros(res)\n } catch (e) {\n return '0'\n }\n}\n\n\n/**\n * Convert arbitrary stringified amount to raw representation\n * @param {String|Number} value\n * @param {Number} [decimals]\n * @return {BigInt}\n */\nexport function toStroops(value, decimals = 7) {\n if (!value)\n return 0n\n if (typeof value === 'number') {\n value = value.toFixed(7)\n }\n if (typeof value !== 'string' || !/^-?[\\d.,]+$/.test(value))\n return 0n //invalid format\n try {\n decimals = normalizeDecimals(decimals)\n let [int, decimal = '0'] = value.split('.', 2)\n let negative = false\n if (int.startsWith('-')) {\n negative = true\n int = int.slice(1)\n }\n\n const divider = 10n ** BigInt(decimals)\n let res = BigInt(int) * divider + BigInt(decimal.slice(0, decimals).padEnd(decimals, '0'))\n if (negative) {\n res *= -1n\n if (res < -0x80000000000000000000000000000000n) //overflow\n return 0n\n } else if (res > 0x80000000000000000000000000000000n) //overflow\n return 0n\n return res\n } catch (e) {\n return 0n\n }\n}\n\nfunction normalizeDecimals(decimals) {\n if (decimals === undefined)\n return 7\n if (typeof decimals !== 'number') {\n decimals = parseInt(decimals.toString(), 0)\n }\n if (decimals < 0)\n return 0\n if (decimals > 38)\n return 38\n return decimals\n}","import {stripTrailingZeros} from './truncation.js'\nimport {toStroops} from './stroops.js'\n\nfunction addDecimalsSeparators(value, separator = ',', trimTrailingZeros = true) {\n //split numeric to parts\n let [int, reminder] = value.split('.')\n let res = ''\n //split digit groups\n while (int.length > 3) {\n res = separator + int.substring(int.length - 3) + res\n int = int.substring(0, int.length - 3)\n }\n //strip negative sign\n if (int === '-') {\n res = res.substring(1)\n }\n res = int + res\n if (reminder) {\n res += '.' + reminder\n }\n if (trimTrailingZeros) {\n res = stripTrailingZeros(res)\n }\n return res\n}\n\n/**\n * Check if a provided value can be safely used as token amount in Stellar operations\n * @param {String} value\n * @param {Boolean} denominate\n * @param {Boolean} nonZero\n * @return {Boolean}\n */\nexport function isValidInt64Amount(value, denominate = true, nonZero = false) {\n try {\n const parsed = denominate ?\n toStroops(value) :\n BigInt(value)\n if (nonZero) {\n if (parsed <= 0n)\n return false\n } else {\n if (parsed < 0n)\n return false\n }\n return parsed <= 9223372036854775807n\n } catch (e) {\n return false\n }\n}\n\n/**\n * Format a number with specified precision and decimals separator\n * @param {String|Number|BigInt} value - Numeric value to format\n * @param {Number} [precision] - Desired precision (7 digits by default)\n * @param {String} [separator] - Decimals separator\n * @return {String}\n */\nexport function formatWithPrecision(value, precision = 7, separator = ',') {\n return addDecimalsSeparators(setPrecision(value, precision), separator, true)\n}\n\n/**\n * Format a number using automatically determined precision\n * @param {String|Number} value - Numeric value to format\n * @param {String} [separator] - Decimals separator\n * @return {String}\n */\nexport function formatWithAutoPrecision(value, separator = ',') {\n if (!value)\n return '0'\n const p = Math.ceil(Math.log10(parseFloat(value)))\n let reminderPrecision = p > 1 ?\n (3 - p) :\n (Math.abs(p) + 2)\n if (reminderPrecision < 0) {\n reminderPrecision = 0\n }\n return formatWithPrecision(value, reminderPrecision, separator)\n}\n\nconst abbreviationPrefixes = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q']\n/**\n * Convert a number to a human-readable format using abbreviation\n * @param {Number} value - Value to format\n * @param {Number} [decimals] - Precision of the abbreviated string to retain\n * @return {String}\n */\nexport function formatWithAbbreviation(value, decimals = 2) {\n let abs = Math.abs(value)\n const tier = Math.log10(abs) / 3 | 0\n\n if (tier <= 0)\n return formatWithAutoPrecision(value)\n\n const suffix = abbreviationPrefixes[tier]\n abs = stripTrailingZeros((abs / Math.pow(10, tier * 3)).toFixed(decimals))\n return `${value < 0 ? '-' : ''}${abs || '0'}${suffix}`\n}\n\n/**\n * Format a number with rounding to specific precision group\n * @param {String|Number|BigInt} value - Value to format\n * @param {Number} group - Logarithmic group rate for rounding\n * @return {String}\n */\nexport function formatWithGrouping(value, group) {\n if (!value)\n return '0'\n const precision = (group >= 1 || group === 0) ?\n 0 :\n Math.abs(Math.log10(group))\n if (group >= 1) {\n value = Math.ceil(value / group) * group\n }\n return formatWithPrecision(value, precision)\n}\n\n/**\n * Format a number as price with specified significant digits precision\n * @param {String|Number|BigInt} value - Value to format\n * @param {Number} significantDigits - Significant digits for automatic formatting\n * @return {String}\n */\nexport function formatPrice(value, significantDigits = 4) {\n const primitive = (typeof value === 'number') ?\n value.toFixed(7) :\n value.toString()\n const [int, fract] = primitive.split('.')\n if (int.replace('-', '') !== '0') {\n significantDigits -= int.length\n if (significantDigits < 0) {\n significantDigits = 0\n }\n } else {\n if (!(fract > 0))\n return '0'\n significantDigits = Math.max(Math.ceil(Math.abs(Math.log10(parseFloat('.' + fract)))), significantDigits)\n }\n return formatWithPrecision(value, significantDigits, '')\n}\n\n/**\n * Format amount according to default Stellar precision\n * @param {Number|String} amount - Value to format\n * @return {String}\n */\nexport function adjustPrecision(amount = '0') {\n return stripTrailingZeros(setPrecision(amount, 7))\n}\n\nfunction setPrecision(amount, precision) {\n if (typeof amount === 'number') {\n amount = amount.toFixed(precision)\n } else {\n amount = amount.toString()\n const sidx = amount.indexOf('.')\n if (sidx >= 0) {\n amount = amount.slice(0, sidx + precision + 1)\n }\n }\n return amount\n}","/**\n * Convert any timestamp representation to a valid date format\n * @param {Date|String|Number} date - Date to parse\n * @param {Boolean} [autoUnixFormatDetection] - Intelligent guess for dates already represented as UNIX timestamp (true by default)\n * @return {Date} Parsed date\n */\nexport function normalizeDate(date, autoUnixFormatDetection = true) {\n //try parse string representation\n if (typeof date === 'string') {\n if (!/^\\d+$/.test(date)) {\n if (!date.endsWith('Z')) {\n date += 'Z'\n }\n date = new Date(date)\n } else {\n date = parseInt(date)\n }\n }\n //parse numeric representation\n if (typeof date === 'number') {\n //input resembles a UNIX timestamp\n if (date < 2147483648 && autoUnixFormatDetection) {\n date *= 1000\n }\n date = new Date(date)\n }\n //check validity\n if (!(date instanceof Date) || isNaN(date.valueOf()))\n throw new Error('Invalid timestamp ' + date)\n return date\n}\n\n/**\n * Convert date to a numeric UNIX timestamp representation\n * @param {Date|String|Number} date - Date to convert\n * @return {Number} UNIX timestamp\n */\nexport function toUnixTimestamp(date) {\n return Math.floor(normalizeDate(date).getTime() / 1000)\n}\n\n/**\n * Convert date to ISO-like human-readable format\n * @param {Date|String|Number} date - Date to convert\n * @return {String}\n */\nexport function formatDateUTC(date) {\n return normalizeDate(date)\n .toISOString()\n .replace(/(T|\\.\\d+Z)/g, ' ') // make it more human friendly\n .trim()\n}","/**\n * Convert rational price representation to Number\n * @param {{n: Number, d: Number}|Number|String} price\n * @return {Number}\n */\nexport function approximatePrice(price) {\n if (!price)\n return 0\n if (price.n)\n return price.n / price.d\n if (typeof price === 'string')\n return parseFloat(price)\n return price\n}","import {\r\n adjustPrecision,\r\n formatWithAutoPrecision,\r\n formatWithAbbreviation,\r\n formatWithGrouping,\r\n formatWithPrecision,\r\n formatPrice,\r\n isValidInt64Amount\r\n} from './numeric-format.js'\r\nimport {formatDateUTC, normalizeDate, toUnixTimestamp} from './timestamp-format.js'\r\nimport {approximatePrice} from './approximation.js'\r\nimport {shortenString, stripTrailingZeros} from './truncation.js'\r\nimport {toStroops, fromStroops} from './stroops.js'\r\n\r\nconst formatter = {\r\n adjustPrecision,\r\n formatWithAutoPrecision,\r\n formatWithAbbreviation,\r\n formatWithGrouping,\r\n formatWithPrecision,\r\n formatPrice,\r\n isValidInt64Amount,\r\n formatDateUTC,\r\n normalizeDate,\r\n toUnixTimestamp,\r\n approximatePrice,\r\n shortenString,\r\n stripTrailingZeros,\r\n toStroops,\r\n fromStroops\r\n}\r\n\r\nexport default formatter"],"names":["stripTrailingZeros","value","int","reminder","split","replace","length","shortenString","symbols","affixLength","Math","max","floor","substring","fromStroops","valueInStroops","decimals","normalizeDecimals","parsed","BigInt","toString","negative","divider","fract","res","padStart","e","toStroops","toFixed","test","decimal","startsWith","slice","padEnd","undefined","parseInt","addDecimalsSeparators","separator","trimTrailingZeros","isValidInt64Amount","denominate","nonZero","formatWithPrecision","precision","setPrecision","formatWithAutoPrecision","p","ceil","log10","parseFloat","reminderPrecision","abs","abbreviationPrefixes","formatWithAbbreviation","tier","suffix","pow","formatWithGrouping","group","formatPrice","significantDigits","primitive","adjustPrecision","amount","sidx","indexOf","normalizeDate","date","autoUnixFormatDetection","endsWith","Date","isNaN","valueOf","Error","toUnixTimestamp","getTime","formatDateUTC","toISOString","trim","approximatePrice","price","n","d","formatter"],"ignoreList":[],"sourceRoot":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stellar-expert/formatter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Formatting utils and common formats for numeric, string, timestamp and binary data",
|
|
5
5
|
"main": "./lib/formatter.js",
|
|
6
6
|
"module": "./src/index.js",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"url": "https://github.com/stellar-expert/formatter/issues"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@babel/core": "^7.
|
|
27
|
-
"@babel/preset-env": "^7.
|
|
28
|
-
"babel-loader": "^
|
|
29
|
-
"jest": "^
|
|
30
|
-
"webpack": "^5.
|
|
31
|
-
"webpack-cli": "^
|
|
26
|
+
"@babel/core": "^7.28.6",
|
|
27
|
+
"@babel/preset-env": "^7.28.6",
|
|
28
|
+
"babel-loader": "^10.0.0",
|
|
29
|
+
"jest": "^30.2.0",
|
|
30
|
+
"webpack": "^5.104.1",
|
|
31
|
+
"webpack-cli": "^6.0.1"
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/numeric-format.js
CHANGED
|
@@ -2,7 +2,6 @@ import {stripTrailingZeros} from './truncation.js'
|
|
|
2
2
|
import {toStroops} from './stroops.js'
|
|
3
3
|
|
|
4
4
|
function addDecimalsSeparators(value, separator = ',', trimTrailingZeros = true) {
|
|
5
|
-
//TODO: use Bignumber.toFormat() method instead
|
|
6
5
|
//split numeric to parts
|
|
7
6
|
let [int, reminder] = value.split('.')
|
|
8
7
|
let res = ''
|
|
@@ -80,6 +79,7 @@ export function formatWithAutoPrecision(value, separator = ',') {
|
|
|
80
79
|
return formatWithPrecision(value, reminderPrecision, separator)
|
|
81
80
|
}
|
|
82
81
|
|
|
82
|
+
const abbreviationPrefixes = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q']
|
|
83
83
|
/**
|
|
84
84
|
* Convert a number to a human-readable format using abbreviation
|
|
85
85
|
* @param {Number} value - Value to format
|
|
@@ -93,7 +93,7 @@ export function formatWithAbbreviation(value, decimals = 2) {
|
|
|
93
93
|
if (tier <= 0)
|
|
94
94
|
return formatWithAutoPrecision(value)
|
|
95
95
|
|
|
96
|
-
const suffix = [
|
|
96
|
+
const suffix = abbreviationPrefixes[tier]
|
|
97
97
|
abs = stripTrailingZeros((abs / Math.pow(10, tier * 3)).toFixed(decimals))
|
|
98
98
|
return `${value < 0 ? '-' : ''}${abs || '0'}${suffix}`
|
|
99
99
|
}
|
|
@@ -118,7 +118,7 @@ export function formatWithGrouping(value, group) {
|
|
|
118
118
|
|
|
119
119
|
/**
|
|
120
120
|
* Format a number as price with specified significant digits precision
|
|
121
|
-
* @param {String|Number|
|
|
121
|
+
* @param {String|Number|BigInt} value - Value to format
|
|
122
122
|
* @param {Number} significantDigits - Significant digits for automatic formatting
|
|
123
123
|
* @return {String}
|
|
124
124
|
*/
|
package/src/stroops.js
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
1
|
import {stripTrailingZeros} from './truncation.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Convert value in stroops (
|
|
4
|
+
* Convert value in stroops (raw amount) to the normal string representation
|
|
5
5
|
* @param {String|Number|BigInt} valueInStroops
|
|
6
|
+
* @param {Number} [decimals]
|
|
6
7
|
* @return {String}
|
|
7
8
|
*/
|
|
8
|
-
export function fromStroops(valueInStroops) {
|
|
9
|
+
export function fromStroops(valueInStroops, decimals = 7) {
|
|
9
10
|
try {
|
|
11
|
+
decimals = normalizeDecimals(decimals)
|
|
10
12
|
let parsed = typeof valueInStroops === 'bigint' ?
|
|
11
13
|
valueInStroops :
|
|
12
|
-
BigInt(valueInStroops.toString().replace(/\.\d*/,''))
|
|
14
|
+
BigInt(valueInStroops.toString().replace(/\.\d*/, ''))
|
|
13
15
|
let negative = false
|
|
14
16
|
if (parsed < 0n) {
|
|
15
17
|
negative = true
|
|
16
18
|
parsed *= -1n
|
|
17
19
|
}
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
+
const divider = 10n ** BigInt(decimals)
|
|
21
|
+
const int = parsed / divider
|
|
22
|
+
const fract = parsed % divider
|
|
20
23
|
let res = int.toString()
|
|
21
24
|
if (fract) {
|
|
22
|
-
res += '.' + fract.toString().padStart(
|
|
25
|
+
res += '.' + fract.toString().padStart(decimals, '0')
|
|
23
26
|
}
|
|
24
27
|
if (negative) {
|
|
25
28
|
res = '-' + res
|
|
@@ -32,11 +35,12 @@ export function fromStroops(valueInStroops) {
|
|
|
32
35
|
|
|
33
36
|
|
|
34
37
|
/**
|
|
35
|
-
* Convert arbitrary stringified amount to
|
|
38
|
+
* Convert arbitrary stringified amount to raw representation
|
|
36
39
|
* @param {String|Number} value
|
|
40
|
+
* @param {Number} [decimals]
|
|
37
41
|
* @return {BigInt}
|
|
38
42
|
*/
|
|
39
|
-
export function toStroops(value) {
|
|
43
|
+
export function toStroops(value, decimals = 7) {
|
|
40
44
|
if (!value)
|
|
41
45
|
return 0n
|
|
42
46
|
if (typeof value === 'number') {
|
|
@@ -45,21 +49,37 @@ export function toStroops(value) {
|
|
|
45
49
|
if (typeof value !== 'string' || !/^-?[\d.,]+$/.test(value))
|
|
46
50
|
return 0n //invalid format
|
|
47
51
|
try {
|
|
52
|
+
decimals = normalizeDecimals(decimals)
|
|
48
53
|
let [int, decimal = '0'] = value.split('.', 2)
|
|
49
54
|
let negative = false
|
|
50
55
|
if (int.startsWith('-')) {
|
|
51
56
|
negative = true
|
|
52
57
|
int = int.slice(1)
|
|
53
58
|
}
|
|
54
|
-
|
|
59
|
+
|
|
60
|
+
const divider = 10n ** BigInt(decimals)
|
|
61
|
+
let res = BigInt(int) * divider + BigInt(decimal.slice(0, decimals).padEnd(decimals, '0'))
|
|
55
62
|
if (negative) {
|
|
56
63
|
res *= -1n
|
|
57
|
-
if (res < -
|
|
64
|
+
if (res < -0x80000000000000000000000000000000n) //overflow
|
|
58
65
|
return 0n
|
|
59
|
-
} else if (res >
|
|
66
|
+
} else if (res > 0x80000000000000000000000000000000n) //overflow
|
|
60
67
|
return 0n
|
|
61
68
|
return res
|
|
62
69
|
} catch (e) {
|
|
63
70
|
return 0n
|
|
64
71
|
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function normalizeDecimals(decimals) {
|
|
75
|
+
if (decimals === undefined)
|
|
76
|
+
return 7
|
|
77
|
+
if (typeof decimals !== 'number') {
|
|
78
|
+
decimals = parseInt(decimals.toString(), 0)
|
|
79
|
+
}
|
|
80
|
+
if (decimals < 0)
|
|
81
|
+
return 0
|
|
82
|
+
if (decimals > 38)
|
|
83
|
+
return 38
|
|
84
|
+
return decimals
|
|
65
85
|
}
|