@exodus/bigint 1.1.0 → 1.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/bigint",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "bigint wrappers for native BigUnt, bn.js and others",
5
5
  "main": "src/index.js",
6
6
  "author": "Exodus Movement, Inc.",
@@ -16,13 +16,9 @@
16
16
  "lodash": "^4.17.11",
17
17
  "minimalistic-assert": "^1.0.1"
18
18
  },
19
- "devDependencies": {
20
- "eslint": "^8.43.0",
21
- "jest": "^29.3.1"
22
- },
23
19
  "scripts": {
24
- "test": "jest",
25
- "lint": "eslint . --ignore-path ../../.gitignore",
20
+ "test": "run -T jest",
21
+ "lint": "run -T eslint . --ignore-path ../../.gitignore",
26
22
  "lint:fix": "yarn lint --fix"
27
23
  },
28
24
  "repository": {
@@ -32,5 +28,5 @@
32
28
  "bugs": {
33
29
  "url": "https://github.com/ExodusMovement/exodus-core/issues?q=is%3Aissue+is%3Aopen+label%3Abigint"
34
30
  },
35
- "gitHead": "e5bfe616c692bc9f8f28af812777646895306a76"
31
+ "gitHead": "bcdb90e9b23d714d7f8c648ab11bc92832396ae6"
36
32
  }
package/src/bn.js CHANGED
@@ -8,14 +8,26 @@ const FACTORY_SYMBOL = Symbol('bn-wrapper')
8
8
 
9
9
  const unwrap = (value) => (value instanceof Wrapper ? value.unwrap() : value)
10
10
 
11
+ const BI_ZERO = new BN(0)
12
+
11
13
  export default class Wrapper {
12
14
  static name = 'bn.js'
13
15
  static isUnderlyingInstance(a) {
14
16
  return BN.isBN(a)
15
17
  }
16
18
 
17
- static wrap(value, base) {
18
- return new Wrapper(FACTORY_SYMBOL, value, base)
19
+ static wrap(value, base = 10) {
20
+ if (!BN.isBN(value)) {
21
+ if (typeof value !== 'string' && typeof value !== 'number') {
22
+ throw new TypeError(`Unsupported type: ${typeof value}`)
23
+ }
24
+
25
+ if (base !== 10 && base !== 16) throw new TypeError(`Unsupported base: ${base}`)
26
+
27
+ value = new BN(value, base)
28
+ }
29
+
30
+ return new Wrapper(FACTORY_SYMBOL, value)
19
31
  }
20
32
 
21
33
  static get ZERO() {
@@ -28,20 +40,10 @@ export default class Wrapper {
28
40
 
29
41
  __value__
30
42
 
31
- constructor(factorySymbol, value, base = 10) {
43
+ constructor(factorySymbol, value) {
32
44
  if (factorySymbol !== FACTORY_SYMBOL) throw new Error('use wrap() instead')
33
45
 
34
- if (BN.isBN(value)) {
35
- this.__value__ = value
36
- } else {
37
- if (typeof value !== 'string' && typeof value !== 'number') {
38
- throw new TypeError(`Unsupported type: ${typeof value}`)
39
- }
40
-
41
- if (base !== 10 && base !== 16) throw new TypeError(`Unsupported base: ${base}`)
42
-
43
- this.__value__ = new BN(value, base)
44
- }
46
+ this.__value__ = value
45
47
  }
46
48
 
47
49
  unwrap() {
@@ -138,7 +140,7 @@ export default class Wrapper {
138
140
  }
139
141
 
140
142
  isPositive() {
141
- return this.__value__.gt(ZERO.unwrap())
143
+ return this.__value__.gt(BI_ZERO)
142
144
  }
143
145
 
144
146
  toNumber() {
@@ -159,5 +161,5 @@ export default class Wrapper {
159
161
  }
160
162
  }
161
163
 
162
- const ZERO = new Wrapper(FACTORY_SYMBOL, 0)
163
- const TEN = new Wrapper(FACTORY_SYMBOL, 10)
164
+ const ZERO = Wrapper.wrap(0)
165
+ const TEN = Wrapper.wrap(10)
@@ -1,17 +1,13 @@
1
1
  // TODO: calculator delegator should wrap/unwrap all BigInt instances
2
2
  // so that callers never gett a raw BigInt instance back
3
3
 
4
- import { memoize } from 'lodash'
5
4
  import assert from 'minimalistic-assert'
6
5
 
7
6
  const FACTORY_SYMBOL = Symbol('bigint-wrapper')
8
7
 
9
8
  const unwrap = (value) => (value instanceof Wrapper ? value.unwrap() : value)
10
9
 
11
- const memoizedWrap = memoize((value) => Wrapper.wrap(value))
12
-
13
10
  const BI_ZERO = BigInt(0)
14
- const BI_TEN = BigInt(10)
15
11
 
16
12
  export default class Wrapper {
17
13
  static name = 'native-bigint'
@@ -36,11 +32,11 @@ export default class Wrapper {
36
32
  }
37
33
 
38
34
  static get ZERO() {
39
- return memoizedWrap(BI_ZERO)
35
+ return ZERO
40
36
  }
41
37
 
42
38
  static get TEN() {
43
- return memoizedWrap(BI_TEN)
39
+ return TEN
44
40
  }
45
41
 
46
42
  __value__
@@ -174,3 +170,6 @@ export default class Wrapper {
174
170
  return Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex')
175
171
  }
176
172
  }
173
+
174
+ const ZERO = Wrapper.wrap(0)
175
+ const TEN = Wrapper.wrap(10)