@talismn/util 0.0.10 → 0.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/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @talismn/util
2
+
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fixed publish config
8
+
9
+ ## 0.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 43c1a3a: Initial release
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
1
  # @talismn/util
2
2
 
3
- **A set of util functions for working with polkadot & talisman data.**
3
+ This library hosts non-UI code meant to be shared between Talisman projects
4
+
5
+ It doesn't need to be published as npm package, therefore it is simply transpiled with tsc.
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Javascript's `Math` library for `BigInt`.
3
+ * Taken from https://stackoverflow.com/questions/51867270/is-there-a-library-similar-to-math-that-supports-javascript-bigint/64953280#64953280
4
+ */
5
+ export declare const BigMath: {
6
+ abs(x: bigint): bigint;
7
+ sign(x: bigint): bigint;
8
+ min(value: bigint, ...values: bigint[]): bigint;
9
+ max(value: bigint, ...values: bigint[]): bigint;
10
+ };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Javascript's `Math` library for `BigInt`.
3
+ * Taken from https://stackoverflow.com/questions/51867270/is-there-a-library-similar-to-math-that-supports-javascript-bigint/64953280#64953280
4
+ */
5
+ export const BigMath = {
6
+ abs(x) {
7
+ return x < BigInt("0") ? -x : x;
8
+ },
9
+ sign(x) {
10
+ if (x === BigInt("0"))
11
+ return BigInt("0");
12
+ return x < BigInt("0") ? BigInt("-1") : BigInt("1");
13
+ },
14
+ // TODO: Improve our babel/tsc config to let us use the `**` operator on bigint values.
15
+ // Error thrown: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. ts(2791)
16
+ // pow(base: bigint, exponent: bigint) {
17
+ // return base ** exponent
18
+ // },
19
+ min(value, ...values) {
20
+ for (const v of values)
21
+ if (v < value)
22
+ value = v;
23
+ return value;
24
+ },
25
+ max(value, ...values) {
26
+ for (const v of values)
27
+ if (v > value)
28
+ value = v;
29
+ return value;
30
+ },
31
+ };
@@ -0,0 +1,8 @@
1
+ export declare type FunctionPropertyNames<T> = {
2
+ [K in keyof T]: T[K] extends Function ? K : never;
3
+ }[keyof T];
4
+ export declare type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
5
+ export declare type NonFunctionPropertyNames<T> = {
6
+ [K in keyof T]: T[K] extends Function ? never : K;
7
+ }[keyof T];
8
+ export declare type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
@@ -0,0 +1,3 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
2
+ // Some handy types from https://www.typescriptlang.org/docs/handbook/advanced-types.html#distributive-conditional-types
3
+ export {};
@@ -0,0 +1,11 @@
1
+ export declare const MAX_DECIMALS_FORMAT = 12;
2
+ /**
3
+ * Custom decimal number formatting for Talisman
4
+ * note that the NumberFormat().format() call is the ressource heavy part, it's not worth trying to optimize other parts
5
+ * @param num input number
6
+ * @param digits number of significant digits to display
7
+ * @param locale locale used to format the number
8
+ * @param options formatting options
9
+ * @returns the formatted value
10
+ */
11
+ export declare const formatDecimals: (num?: string | number | null, digits?: number, options?: Partial<Intl.NumberFormatOptions>, locale?: string) => string;
@@ -0,0 +1,44 @@
1
+ import BigNumber from "bignumber.js";
2
+ export const MAX_DECIMALS_FORMAT = 12;
3
+ /**
4
+ * Custom decimal number formatting for Talisman
5
+ * note that the NumberFormat().format() call is the ressource heavy part, it's not worth trying to optimize other parts
6
+ * @param num input number
7
+ * @param digits number of significant digits to display
8
+ * @param locale locale used to format the number
9
+ * @param options formatting options
10
+ * @returns the formatted value
11
+ */
12
+ export const formatDecimals = (num, digits = 4, options = {}, locale = "en-US") => {
13
+ if (num === null || num === undefined)
14
+ return "";
15
+ const value = new BigNumber(num);
16
+ // very small numbers should display "< 0.0001"
17
+ const minDisplayVal = 1 / Math.pow(10, digits);
18
+ if (value.gt(0) && value.lt(minDisplayVal))
19
+ return `< ${formatDecimals(minDisplayVal)}`;
20
+ // count digits
21
+ const flooredValue = value.integerValue();
22
+ const intDigits = flooredValue.isEqualTo(0) ? 0 : flooredValue.toString().length;
23
+ // we never want to display a rounded up value
24
+ // to prevent JS default rounding, we will remove/truncate insignificant digits ourselves before formatting
25
+ let truncatedValue = value;
26
+ //remove insignificant fraction digits
27
+ const excessFractionDigitsPow10 = new BigNumber(10).pow(digits > intDigits ? digits - intDigits : 0);
28
+ truncatedValue = truncatedValue
29
+ .multipliedBy(excessFractionDigitsPow10)
30
+ .integerValue()
31
+ .dividedBy(excessFractionDigitsPow10);
32
+ //remove insignificant integer digits
33
+ const excessIntegerDigits = new BigNumber(intDigits > digits ? intDigits - digits : 0);
34
+ const excessIntegerDigitsPow10 = new BigNumber(10).pow(excessIntegerDigits);
35
+ if (excessIntegerDigits.gt(0))
36
+ truncatedValue = truncatedValue
37
+ .dividedBy(excessIntegerDigitsPow10)
38
+ .integerValue()
39
+ .multipliedBy(excessIntegerDigitsPow10);
40
+ // format
41
+ return Intl.NumberFormat(locale, Object.assign({
42
+ //compact notation (K, M, B) if above 9999
43
+ notation: truncatedValue.gt(9999) ? "compact" : "standard", maximumSignificantDigits: digits + (truncatedValue.lt(1) ? 1 : 0) }, options)).format(truncatedValue.toNumber());
44
+ };
@@ -0,0 +1 @@
1
+ export declare const getBase64ImageUrl: (base64: string) => string | null;
@@ -0,0 +1,8 @@
1
+ export const getBase64ImageUrl = (base64) => {
2
+ const match = /^data:([^;]*?);base64,(.*?)$/.exec(base64);
3
+ if (!match)
4
+ return null;
5
+ const buffer = Buffer.from(match[2], "base64");
6
+ const blob = new Blob([new Uint8Array(buffer, 0, buffer.length)], { type: match[1] });
7
+ return URL.createObjectURL(blob);
8
+ };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
- export * from './bigNumbers';
2
- export * from './chains';
3
- export * from './decodeAnyAddress';
4
- export * from './deferred';
5
- export * from './encodeAnyAddress';
6
- export * from './hexToBytes';
7
- export * from './planckToTokens';
8
- export * from './tokensToPlanck';
9
- export * from './useFuncMemo';
1
+ export * from "./BigMath";
2
+ export * from "./FunctionPropertyNames";
3
+ export * from "./formatDecimals";
4
+ export * from "./getBase64ImageUrl";
5
+ export * from "./isArrayOf";
6
+ export * from "./planckToTokens";
7
+ export * from "./tokensToPlanck";
package/dist/index.js CHANGED
@@ -1,28 +1,7 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8
- var __reExport = (target, module2, desc) => {
9
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
10
- for (let key of __getOwnPropNames(module2))
11
- if (!__hasOwnProp.call(target, key) && key !== "default")
12
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
13
- }
14
- return target;
15
- };
16
- var __toModule = (module2) => {
17
- return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
18
- };
19
- __markAsModule(exports);
20
- __reExport(exports, __toModule(require("./bigNumbers")));
21
- __reExport(exports, __toModule(require("./chains")));
22
- __reExport(exports, __toModule(require("./decodeAnyAddress")));
23
- __reExport(exports, __toModule(require("./deferred")));
24
- __reExport(exports, __toModule(require("./encodeAnyAddress")));
25
- __reExport(exports, __toModule(require("./hexToBytes")));
26
- __reExport(exports, __toModule(require("./planckToTokens")));
27
- __reExport(exports, __toModule(require("./tokensToPlanck")));
28
- __reExport(exports, __toModule(require("./useFuncMemo")));
1
+ export * from "./BigMath";
2
+ export * from "./FunctionPropertyNames";
3
+ export * from "./formatDecimals";
4
+ export * from "./getBase64ImageUrl";
5
+ export * from "./isArrayOf";
6
+ export * from "./planckToTokens";
7
+ export * from "./tokensToPlanck";
@@ -0,0 +1 @@
1
+ export declare function isArrayOf<T, P extends Array<unknown>>(array: unknown[], func: new (...args: P) => T): array is T[];
@@ -0,0 +1,5 @@
1
+ export function isArrayOf(array, func) {
2
+ if (array.length > 0 && array[0] instanceof func)
3
+ return true;
4
+ return false;
5
+ }
@@ -1 +1,3 @@
1
+ export declare function planckToTokens(planck: string, tokenDecimals: number): string;
1
2
  export declare function planckToTokens(planck: string, tokenDecimals?: number): string | undefined;
3
+ export declare function planckToTokens(planck?: string, tokenDecimals?: number): string | undefined;
@@ -1,39 +1,9 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8
- var __export = (target, all) => {
9
- __markAsModule(target);
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __reExport = (target, module2, desc) => {
14
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
15
- for (let key of __getOwnPropNames(module2))
16
- if (!__hasOwnProp.call(target, key) && key !== "default")
17
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
18
- }
19
- return target;
20
- };
21
- var __toModule = (module2) => {
22
- return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
23
- };
24
- __export(exports, {
25
- planckToTokens: () => planckToTokens
26
- });
27
- var import_bignumber = __toModule(require("bignumber.js"));
28
- function planckToTokens(planck, tokenDecimals) {
29
- if (!planck || typeof tokenDecimals !== "number")
30
- return;
31
- const base = new import_bignumber.default(10);
32
- const exponent = new import_bignumber.default(tokenDecimals).negated();
33
- const multiplier = base.pow(exponent);
34
- return new import_bignumber.default(planck).multipliedBy(multiplier).toString();
1
+ import BigNumber from "bignumber.js";
2
+ export function planckToTokens(planck, tokenDecimals) {
3
+ if (typeof planck !== "string" || typeof tokenDecimals !== "number")
4
+ return;
5
+ const base = new BigNumber(10);
6
+ const exponent = new BigNumber(tokenDecimals).negated();
7
+ const multiplier = base.pow(exponent);
8
+ return new BigNumber(planck).multipliedBy(multiplier).toString(10);
35
9
  }
36
- // Annotate the CommonJS export names for ESM import in node:
37
- 0 && (module.exports = {
38
- planckToTokens
39
- });
@@ -1 +1,3 @@
1
+ export declare function tokensToPlanck(tokens: string, tokenDecimals: number): string;
1
2
  export declare function tokensToPlanck(tokens: string, tokenDecimals?: number): string | undefined;
3
+ export declare function tokensToPlanck(tokens?: string, tokenDecimals?: number): string | undefined;
@@ -1,39 +1,9 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
8
- var __export = (target, all) => {
9
- __markAsModule(target);
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __reExport = (target, module2, desc) => {
14
- if (module2 && typeof module2 === "object" || typeof module2 === "function") {
15
- for (let key of __getOwnPropNames(module2))
16
- if (!__hasOwnProp.call(target, key) && key !== "default")
17
- __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
18
- }
19
- return target;
20
- };
21
- var __toModule = (module2) => {
22
- return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
23
- };
24
- __export(exports, {
25
- tokensToPlanck: () => tokensToPlanck
26
- });
27
- var import_bignumber = __toModule(require("bignumber.js"));
28
- function tokensToPlanck(tokens, tokenDecimals) {
29
- if (!tokens || typeof tokenDecimals !== "number")
30
- return;
31
- const base = new import_bignumber.default(10);
32
- const exponent = new import_bignumber.default(tokenDecimals);
33
- const multiplier = base.pow(exponent);
34
- return new import_bignumber.default(tokens).multipliedBy(multiplier).toString();
1
+ import BigNumber from "bignumber.js";
2
+ export function tokensToPlanck(tokens, tokenDecimals) {
3
+ if (typeof tokens !== "string" || typeof tokenDecimals !== "number")
4
+ return;
5
+ const base = new BigNumber(10);
6
+ const exponent = new BigNumber(tokenDecimals);
7
+ const multiplier = base.pow(exponent);
8
+ return new BigNumber(tokens).multipliedBy(multiplier).toString(10);
35
9
  }
36
- // Annotate the CommonJS export names for ESM import in node:
37
- 0 && (module.exports = {
38
- tokensToPlanck
39
- });
package/package.json CHANGED
@@ -1,73 +1,50 @@
1
1
  {
2
2
  "name": "@talismn/util",
3
- "version": "0.0.10",
4
- "description": "A set of util functions for working with polkadot & talisman data.",
5
- "main": "./src/index.ts",
6
- "types": "./dist/index.d.ts",
7
- "browser": "./dist/index.js",
8
- "exports": "./dist/index.js",
9
- "scripts": {
10
- "prepare": "husky install",
11
- "pre-commit": "lint-staged",
12
- "test": "echo \"Error: no test specified\" && exit 1",
13
- "watch": "nodemon --watch src --ext js,ts --exec yarn prepack",
14
- "build": "esbuild-node-tsc",
15
- "build:types": "tsc --emitDeclarationOnly",
16
- "prepack": "yarn build && yarn build:types"
17
- },
3
+ "version": "0.1.1",
18
4
  "author": "Talisman",
19
- "license": "GPL-3.0-only",
20
- "peerDependencies": {
21
- "@polkadot/api": "*",
22
- "@polkadot/keyring": "*",
23
- "@polkadot/util-crypto": "*",
24
- "react": ">=17"
25
- },
26
- "dependencies": {
27
- "@polkadot/extension-inject": "^0.39.3",
28
- "@polkadot/networks": "^7.3.1",
29
- "bignumber.js": "^9.0.1"
5
+ "homepage": "https://talisman.xyz",
6
+ "license": "UNLICENSED",
7
+ "publishConfig": {
8
+ "access": "public"
30
9
  },
31
- "devDependencies": {
32
- "@polkadot/api": "^5.9.1",
33
- "@polkadot/keyring": "^7.4.1",
34
- "@polkadot/util-crypto": "^7.4.1",
35
- "@types/react": "^17.0.2",
36
- "esbuild-node-tsc": "^1.6.1",
37
- "husky": "^6.0.0",
38
- "import-sort-style-module": "^6.0.0",
39
- "lint-staged": "^11.1.2",
40
- "prettier": "^2.3.2",
41
- "prettier-plugin-import-sort": "^0.0.7",
42
- "react": "^17.0.2",
43
- "typescript": "^4.3.5"
44
- },
45
- "files": [
46
- "dist/**/*"
47
- ],
48
10
  "repository": {
11
+ "directory": "packages/util",
49
12
  "type": "git",
50
- "url": "git+https://github.com/TalismanSociety/util.git"
13
+ "url": "https://github.com/talismansociety/talisman.git"
51
14
  },
52
- "bugs": {
53
- "url": "https://github.com/TalismanSociety/util/issues"
15
+ "main": "dist/index.js",
16
+ "types": "dist/index.d.ts",
17
+ "files": [
18
+ "/dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">=14"
54
22
  },
55
- "homepage": "https://github.com/TalismanSociety/util#readme",
56
- "packageManager": "yarn@3.0.1",
57
- "lint-staged": {
58
- "*.{ts,tsx,js,jsx,html,css,scss}": "prettier --write"
23
+ "scripts": {
24
+ "dev": "tsc --watch --declarationMap",
25
+ "build": "tsc --declarationMap",
26
+ "build:packages:prod": "rm -rf dist && tsc",
27
+ "prepack": "yarn build:packages:prod",
28
+ "test": "jest",
29
+ "lint": "eslint . --max-warnings 0",
30
+ "clean": "rm -rf dist && rm -rf .turbo rm -rf node_modules"
59
31
  },
60
- "prettier": {
61
- "arrowParens": "avoid",
62
- "printWidth": 120,
63
- "quoteProps": "consistent",
64
- "semi": false,
65
- "singleQuote": true
32
+ "dependencies": {
33
+ "bignumber.js": "^9.1.0"
66
34
  },
67
- "importSort": {
68
- ".js, .jsx, .ts, .tsx": {
69
- "style": "module",
70
- "parser": "typescript"
71
- }
35
+ "devDependencies": {
36
+ "@talismn/eslint-config": "^0.0.0",
37
+ "@talismn/tsconfig": "^0.0.0",
38
+ "@types/jest": "^27.5.1",
39
+ "eslint": "^8.4.0",
40
+ "jest": "^28.1.0",
41
+ "ts-jest": "^28.0.2",
42
+ "typescript": "^4.6.4"
43
+ },
44
+ "eslintConfig": {
45
+ "root": true,
46
+ "extends": [
47
+ "@talismn/eslint-config"
48
+ ]
72
49
  }
73
50
  }