aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-10-24 07:59:31 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-10-24 07:59:31 +0800
commit7ccfa8a8afe103e1cc2920724b6f17644fe5629d (patch)
treea0153a9b55f27d8daad436e49d6042fbe18154d5 /packages/instant/src/util
parent751b87af962d5b1f9f65514c10de2f7cb0b13c34 (diff)
downloaddexon-sol-tools-7ccfa8a8afe103e1cc2920724b6f17644fe5629d.tar
dexon-sol-tools-7ccfa8a8afe103e1cc2920724b6f17644fe5629d.tar.gz
dexon-sol-tools-7ccfa8a8afe103e1cc2920724b6f17644fe5629d.tar.bz2
dexon-sol-tools-7ccfa8a8afe103e1cc2920724b6f17644fe5629d.tar.lz
dexon-sol-tools-7ccfa8a8afe103e1cc2920724b6f17644fe5629d.tar.xz
dexon-sol-tools-7ccfa8a8afe103e1cc2920724b6f17644fe5629d.tar.zst
dexon-sol-tools-7ccfa8a8afe103e1cc2920724b6f17644fe5629d.zip
feat: support half-written decimal numbers in BigNumberInput
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r--packages/instant/src/util/big_number.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/instant/src/util/big_number.ts b/packages/instant/src/util/big_number.ts
new file mode 100644
index 000000000..773eb0cb4
--- /dev/null
+++ b/packages/instant/src/util/big_number.ts
@@ -0,0 +1,29 @@
+import { BigNumber } from '@0x/utils';
+import * as _ from 'lodash';
+
+/**
+ * A BigNumber extension that is more flexible about decimal strings.
+ * Such as allowing:
+ * new BigNumberInput(0.) => 0
+ * new BigNumberInput(1.) => 1
+ * new BigNumberInput(1..) => still throws
+ */
+export class BigNumberInput extends BigNumber {
+ private _hasDecimalPeriod: boolean;
+ constructor(bigNumberString: string) {
+ const hasDecimalPeriod = _.endsWith(bigNumberString, '.');
+ let internalString = bigNumberString;
+ if (hasDecimalPeriod) {
+ internalString = bigNumberString.slice(0, bigNumberString.length - 1);
+ }
+ super(internalString);
+ this._hasDecimalPeriod = hasDecimalPeriod;
+ }
+ public toString(): string {
+ const internalString = super.toString();
+ if (this._hasDecimalPeriod) {
+ return `${internalString}.`;
+ }
+ return internalString;
+ }
+}