@talismn/util 0.1.2 → 0.1.3
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/CHANGELOG.md +6 -0
- package/dist/BigMath.js +4 -1
- package/dist/FunctionPropertyNames.js +2 -1
- package/dist/blake2Concat.js +8 -4
- package/dist/decodeAnyAddress.js +11 -7
- package/dist/encodeAnyAddress.js +10 -6
- package/dist/formatDecimals.js +15 -8
- package/dist/getBase64ImageUrl.js +5 -1
- package/dist/hasOwnProperty.js +5 -1
- package/dist/index.js +28 -12
- package/dist/isArrayOf.js +5 -1
- package/dist/planckToTokens.js +12 -5
- package/dist/tokensToPlanck.js +12 -5
- package/dist/twox64Concat.js +8 -4
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
package/dist/BigMath.js
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.BigMath = void 0;
|
1
4
|
/**
|
2
5
|
* Javascript's `Math` library for `BigInt`.
|
3
6
|
* Taken from https://stackoverflow.com/questions/51867270/is-there-a-library-similar-to-math-that-supports-javascript-bigint/64953280#64953280
|
4
7
|
*/
|
5
|
-
|
8
|
+
exports.BigMath = {
|
6
9
|
abs(x) {
|
7
10
|
return x < BigInt("0") ? -x : x;
|
8
11
|
},
|
package/dist/blake2Concat.js
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.blake2Concat = void 0;
|
4
|
+
const util_1 = require("@polkadot/util");
|
5
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
3
6
|
const bitLength = 128;
|
4
|
-
|
5
|
-
return u8aToHex(u8aConcat(blake2AsU8a(input, bitLength), u8aToU8a(input)));
|
7
|
+
function blake2Concat(input) {
|
8
|
+
return (0, util_1.u8aToHex)((0, util_1.u8aConcat)((0, util_crypto_1.blake2AsU8a)(input, bitLength), (0, util_1.u8aToU8a)(input)));
|
6
9
|
}
|
10
|
+
exports.blake2Concat = blake2Concat;
|
package/dist/decodeAnyAddress.js
CHANGED
@@ -1,15 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.decodeAnyAddress = void 0;
|
4
|
+
const keyring_1 = require("@polkadot/keyring");
|
5
|
+
const util_1 = require("@polkadot/util");
|
6
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
7
|
+
function decodeAnyAddress(encoded, ignoreChecksum, ss58Format) {
|
5
8
|
try {
|
6
|
-
return decodeAddress(encoded, ignoreChecksum, ss58Format);
|
9
|
+
return (0, keyring_1.decodeAddress)(encoded, ignoreChecksum, ss58Format);
|
7
10
|
}
|
8
11
|
catch (error) {
|
9
12
|
if (typeof encoded !== "string")
|
10
13
|
throw error;
|
11
|
-
if (!isEthereumAddress(encoded))
|
14
|
+
if (!(0, util_crypto_1.isEthereumAddress)(encoded))
|
12
15
|
throw error;
|
13
|
-
return hexToU8a(encoded.slice("0x".length));
|
16
|
+
return (0, util_1.hexToU8a)(encoded.slice("0x".length));
|
14
17
|
}
|
15
18
|
}
|
19
|
+
exports.decodeAnyAddress = decodeAnyAddress;
|
package/dist/encodeAnyAddress.js
CHANGED
@@ -1,14 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.encodeAnyAddress = void 0;
|
4
|
+
const keyring_1 = require("@polkadot/keyring");
|
5
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
6
|
+
function encodeAnyAddress(key, ss58Format) {
|
4
7
|
try {
|
5
|
-
return encodeAddress(key, ss58Format);
|
8
|
+
return (0, keyring_1.encodeAddress)(key, ss58Format);
|
6
9
|
}
|
7
10
|
catch (error) {
|
8
11
|
if (typeof key !== "string")
|
9
12
|
throw error;
|
10
|
-
if (!isEthereumAddress(key))
|
13
|
+
if (!(0, util_crypto_1.isEthereumAddress)(key))
|
11
14
|
throw error;
|
12
|
-
return ethereumEncode(key);
|
15
|
+
return (0, util_crypto_1.ethereumEncode)(key);
|
13
16
|
}
|
14
17
|
}
|
18
|
+
exports.encodeAnyAddress = encodeAnyAddress;
|
package/dist/formatDecimals.js
CHANGED
@@ -1,5 +1,11 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.formatDecimals = exports.MAX_DECIMALS_FORMAT = void 0;
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
8
|
+
exports.MAX_DECIMALS_FORMAT = 12;
|
3
9
|
/**
|
4
10
|
* Custom decimal number formatting for Talisman
|
5
11
|
* note that the NumberFormat().format() call is the ressource heavy part, it's not worth trying to optimize other parts
|
@@ -9,14 +15,14 @@ export const MAX_DECIMALS_FORMAT = 12;
|
|
9
15
|
* @param options formatting options
|
10
16
|
* @returns the formatted value
|
11
17
|
*/
|
12
|
-
|
18
|
+
const formatDecimals = (num, digits = 4, options = {}, locale = "en-US") => {
|
13
19
|
if (num === null || num === undefined)
|
14
20
|
return "";
|
15
|
-
const value = new
|
21
|
+
const value = new bignumber_js_1.default(num);
|
16
22
|
// very small numbers should display "< 0.0001"
|
17
23
|
const minDisplayVal = 1 / Math.pow(10, digits);
|
18
24
|
if (value.gt(0) && value.lt(minDisplayVal))
|
19
|
-
return `< ${formatDecimals(minDisplayVal)}`;
|
25
|
+
return `< ${(0, exports.formatDecimals)(minDisplayVal)}`;
|
20
26
|
// count digits
|
21
27
|
const flooredValue = value.integerValue();
|
22
28
|
const intDigits = flooredValue.isEqualTo(0) ? 0 : flooredValue.toString().length;
|
@@ -24,14 +30,14 @@ export const formatDecimals = (num, digits = 4, options = {}, locale = "en-US")
|
|
24
30
|
// to prevent JS default rounding, we will remove/truncate insignificant digits ourselves before formatting
|
25
31
|
let truncatedValue = value;
|
26
32
|
//remove insignificant fraction digits
|
27
|
-
const excessFractionDigitsPow10 = new
|
33
|
+
const excessFractionDigitsPow10 = new bignumber_js_1.default(10).pow(digits > intDigits ? digits - intDigits : 0);
|
28
34
|
truncatedValue = truncatedValue
|
29
35
|
.multipliedBy(excessFractionDigitsPow10)
|
30
36
|
.integerValue()
|
31
37
|
.dividedBy(excessFractionDigitsPow10);
|
32
38
|
//remove insignificant integer digits
|
33
|
-
const excessIntegerDigits = new
|
34
|
-
const excessIntegerDigitsPow10 = new
|
39
|
+
const excessIntegerDigits = new bignumber_js_1.default(intDigits > digits ? intDigits - digits : 0);
|
40
|
+
const excessIntegerDigitsPow10 = new bignumber_js_1.default(10).pow(excessIntegerDigits);
|
35
41
|
if (excessIntegerDigits.gt(0))
|
36
42
|
truncatedValue = truncatedValue
|
37
43
|
.dividedBy(excessIntegerDigitsPow10)
|
@@ -42,3 +48,4 @@ export const formatDecimals = (num, digits = 4, options = {}, locale = "en-US")
|
|
42
48
|
//compact notation (K, M, B) if above 9999
|
43
49
|
notation: truncatedValue.gt(9999) ? "compact" : "standard", maximumSignificantDigits: digits + (truncatedValue.lt(1) ? 1 : 0) }, options)).format(truncatedValue.toNumber());
|
44
50
|
};
|
51
|
+
exports.formatDecimals = formatDecimals;
|
@@ -1,4 +1,7 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.getBase64ImageUrl = void 0;
|
4
|
+
const getBase64ImageUrl = (base64) => {
|
2
5
|
const match = /^data:([^;]*?);base64,(.*?)$/.exec(base64);
|
3
6
|
if (!match)
|
4
7
|
return null;
|
@@ -6,3 +9,4 @@ export const getBase64ImageUrl = (base64) => {
|
|
6
9
|
const blob = new Blob([new Uint8Array(buffer, 0, buffer.length)], { type: match[1] });
|
7
10
|
return URL.createObjectURL(blob);
|
8
11
|
};
|
12
|
+
exports.getBase64ImageUrl = getBase64ImageUrl;
|
package/dist/hasOwnProperty.js
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.hasOwnProperty = void 0;
|
4
|
+
function hasOwnProperty(obj, prop) {
|
2
5
|
return prop in obj;
|
3
6
|
}
|
7
|
+
exports.hasOwnProperty = hasOwnProperty;
|
package/dist/index.js
CHANGED
@@ -1,12 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
"use strict";
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
+
if (k2 === undefined) k2 = k;
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
+
}
|
8
|
+
Object.defineProperty(o, k2, desc);
|
9
|
+
}) : (function(o, m, k, k2) {
|
10
|
+
if (k2 === undefined) k2 = k;
|
11
|
+
o[k2] = m[k];
|
12
|
+
}));
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
+
};
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
+
__exportStar(require("./BigMath"), exports);
|
18
|
+
__exportStar(require("./FunctionPropertyNames"), exports);
|
19
|
+
__exportStar(require("./blake2Concat"), exports);
|
20
|
+
__exportStar(require("./decodeAnyAddress"), exports);
|
21
|
+
__exportStar(require("./encodeAnyAddress"), exports);
|
22
|
+
__exportStar(require("./formatDecimals"), exports);
|
23
|
+
__exportStar(require("./getBase64ImageUrl"), exports);
|
24
|
+
__exportStar(require("./hasOwnProperty"), exports);
|
25
|
+
__exportStar(require("./isArrayOf"), exports);
|
26
|
+
__exportStar(require("./planckToTokens"), exports);
|
27
|
+
__exportStar(require("./tokensToPlanck"), exports);
|
28
|
+
__exportStar(require("./twox64Concat"), exports);
|
package/dist/isArrayOf.js
CHANGED
@@ -1,5 +1,9 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.isArrayOf = void 0;
|
4
|
+
function isArrayOf(array, func) {
|
2
5
|
if (array.length > 0 && array[0] instanceof func)
|
3
6
|
return true;
|
4
7
|
return false;
|
5
8
|
}
|
9
|
+
exports.isArrayOf = isArrayOf;
|
package/dist/planckToTokens.js
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.planckToTokens = void 0;
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
8
|
+
function planckToTokens(planck, tokenDecimals) {
|
3
9
|
if (typeof planck !== "string" || typeof tokenDecimals !== "number")
|
4
10
|
return;
|
5
|
-
const base = new
|
6
|
-
const exponent = new
|
11
|
+
const base = new bignumber_js_1.default(10);
|
12
|
+
const exponent = new bignumber_js_1.default(tokenDecimals).negated();
|
7
13
|
const multiplier = base.pow(exponent);
|
8
|
-
return new
|
14
|
+
return new bignumber_js_1.default(planck).multipliedBy(multiplier).toString(10);
|
9
15
|
}
|
16
|
+
exports.planckToTokens = planckToTokens;
|
package/dist/tokensToPlanck.js
CHANGED
@@ -1,9 +1,16 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.tokensToPlanck = void 0;
|
7
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
8
|
+
function tokensToPlanck(tokens, tokenDecimals) {
|
3
9
|
if (typeof tokens !== "string" || typeof tokenDecimals !== "number")
|
4
10
|
return;
|
5
|
-
const base = new
|
6
|
-
const exponent = new
|
11
|
+
const base = new bignumber_js_1.default(10);
|
12
|
+
const exponent = new bignumber_js_1.default(tokenDecimals);
|
7
13
|
const multiplier = base.pow(exponent);
|
8
|
-
return new
|
14
|
+
return new bignumber_js_1.default(tokens).multipliedBy(multiplier).toString(10);
|
9
15
|
}
|
16
|
+
exports.tokensToPlanck = tokensToPlanck;
|
package/dist/twox64Concat.js
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.twox64Concat = void 0;
|
4
|
+
const util_1 = require("@polkadot/util");
|
5
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
3
6
|
const bitLength = 64;
|
4
|
-
|
5
|
-
return u8aToHex(u8aConcat(xxhashAsU8a(input, bitLength), u8aToU8a(input)));
|
7
|
+
function twox64Concat(input) {
|
8
|
+
return (0, util_1.u8aToHex)((0, util_1.u8aConcat)((0, util_crypto_1.xxhashAsU8a)(input, bitLength), (0, util_1.u8aToU8a)(input)));
|
6
9
|
}
|
10
|
+
exports.twox64Concat = twox64Concat;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@talismn/util",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.3",
|
4
4
|
"author": "Talisman",
|
5
5
|
"homepage": "https://talisman.xyz",
|
6
6
|
"license": "UNLICENSED",
|
@@ -37,7 +37,7 @@
|
|
37
37
|
},
|
38
38
|
"devDependencies": {
|
39
39
|
"@talismn/eslint-config": "^0.0.0",
|
40
|
-
"@talismn/tsconfig": "^0.0.
|
40
|
+
"@talismn/tsconfig": "^0.0.1",
|
41
41
|
"@types/jest": "^27.5.1",
|
42
42
|
"eslint": "^8.4.0",
|
43
43
|
"jest": "^28.1.0",
|