@w3ux/utils 0.0.2 → 0.1.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/base.d.ts CHANGED
@@ -62,5 +62,10 @@ declare const appendOrEmpty: (condition: boolean | string | undefined, value: st
62
62
  * @summary Returns ` value` if condition is truthy, or ` fallback` otherwise.
63
63
  */
64
64
  declare const appendOr: (condition: boolean | string | undefined, value: string, fallback: string) => string;
65
+ /**
66
+ * @name appendOr
67
+ * @summary Formats an address with the supplied ss58 prefix, or returns null if invalid.
68
+ */
69
+ declare const formatAccountSs58: (address: string, ss58: number) => string;
65
70
 
66
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout };
71
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, formatAccountSs58, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout };
package/base.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/base.ts
2
2
  import { BigNumber } from "bignumber.js";
3
+ import Keyring from "@polkadot/keyring";
3
4
  var camelize = (str) => {
4
5
  const convertToString = (string) => {
5
6
  if (string) {
@@ -96,11 +97,25 @@ var withTimeout = (ms, promise, options) => {
96
97
  };
97
98
  var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
98
99
  var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
100
+ var formatAccountSs58 = (address, ss58) => {
101
+ try {
102
+ const keyring = new Keyring();
103
+ keyring.setSS58Format(ss58);
104
+ const formatted = keyring.addFromAddress(address).address;
105
+ if (formatted !== address) {
106
+ return formatted;
107
+ }
108
+ return null;
109
+ } catch (e) {
110
+ return null;
111
+ }
112
+ };
99
113
  export {
100
114
  appendOr,
101
115
  appendOrEmpty,
102
116
  camelize,
103
117
  ellipsisFn,
118
+ formatAccountSs58,
104
119
  greaterThanZero,
105
120
  isNotZero,
106
121
  minDecimalPlaces,
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout } from './base.js';
1
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, formatAccountSs58, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout } from './base.js';
2
2
  export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, evalUnits, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.js';
3
3
  import 'bignumber.js';
4
4
  import './types.js';
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/base.ts
2
2
  import { BigNumber } from "bignumber.js";
3
+ import Keyring from "@polkadot/keyring";
3
4
  var camelize = (str) => {
4
5
  const convertToString = (string) => {
5
6
  if (string) {
@@ -96,6 +97,19 @@ var withTimeout = (ms, promise, options) => {
96
97
  };
97
98
  var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
98
99
  var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
100
+ var formatAccountSs58 = (address, ss58) => {
101
+ try {
102
+ const keyring = new Keyring();
103
+ keyring.setSS58Format(ss58);
104
+ const formatted = keyring.addFromAddress(address).address;
105
+ if (formatted !== address) {
106
+ return formatted;
107
+ }
108
+ return null;
109
+ } catch (e) {
110
+ return null;
111
+ }
112
+ };
99
113
 
100
114
  // src/unit.ts
101
115
  import { decodeAddress, encodeAddress } from "@polkadot/keyring";
@@ -369,6 +383,7 @@ export {
369
383
  ellipsisFn,
370
384
  evalUnits,
371
385
  extractUrlValue,
386
+ formatAccountSs58,
372
387
  greaterThanZero,
373
388
  inChrome,
374
389
  isNotZero,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w3ux/utils",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "license": "GPL-3.0-only",
5
5
  "type": "module",
6
6
  "dependencies": {
package/unit.d.ts CHANGED
@@ -43,7 +43,7 @@ declare const setStateWithRef: <T>(value: T, setState: (_state: T) => void, ref:
43
43
  * @summary Retrieve the local stroage value with the key, return defult value if it is not
44
44
  * found.
45
45
  */
46
- declare const localStorageOrDefault: <T>(key: string, _default: T, parse?: boolean) => string | T;
46
+ declare const localStorageOrDefault: <T>(key: string, _default: T, parse?: boolean) => T | string;
47
47
  /**
48
48
  * @name isValidAddress
49
49
  * @summary Return whether an address is valid Substrate address.
package/unit.js CHANGED
@@ -5,6 +5,7 @@ import { BigNumber as BigNumber2 } from "bignumber.js";
5
5
 
6
6
  // src/base.ts
7
7
  import { BigNumber } from "bignumber.js";
8
+ import Keyring from "@polkadot/keyring";
8
9
  var ellipsisFn = (str, amount = 6, position = "center") => {
9
10
  const half = str.length / 2;
10
11
  if (amount <= 4) {