@w3ux/utils 0.0.2 → 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/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,5 +1,5 @@
1
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout } from './base.js';
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';
1
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, formatAccountSs58, greaterThanZero, isNotZero, minDecimalPlaces, pageFromUri, rmCommas, shuffle, withTimeout } from './base.js';
2
+ export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, evalUnits, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, stringToBigNumber, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.js';
3
3
  import 'bignumber.js';
4
4
  import './types.js';
5
5
  import 'react';
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";
@@ -358,6 +372,7 @@ var mergeDeep = (target, ...sources) => {
358
372
  }
359
373
  return mergeDeep(target, ...sources);
360
374
  };
375
+ var stringToBigNumber = (value) => new BigNumber2(rmCommas(value));
361
376
  export {
362
377
  addedTo,
363
378
  appendOr,
@@ -369,6 +384,7 @@ export {
369
384
  ellipsisFn,
370
385
  evalUnits,
371
386
  extractUrlValue,
387
+ formatAccountSs58,
372
388
  greaterThanZero,
373
389
  inChrome,
374
390
  isNotZero,
@@ -389,6 +405,7 @@ export {
389
405
  shuffle,
390
406
  snakeToCamel,
391
407
  sortWithNull,
408
+ stringToBigNumber,
392
409
  transformToBaseUnit,
393
410
  unescape,
394
411
  unimplemented,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w3ux/utils",
3
- "version": "0.0.2",
3
+ "version": "0.1.1",
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.
@@ -161,5 +161,10 @@ declare const unimplemented: ({ ...props }: {
161
161
  * @param ...sources
162
162
  */
163
163
  declare const mergeDeep: (target: AnyObject, ...sources: AnyObject[]) => AnyObject;
164
+ /**
165
+ * @name stringToBigNumber
166
+ * @summary Converts a balance string into a `BigNumber`.
167
+ */
168
+ declare const stringToBigNumber: (value: string) => BigNumber;
164
169
 
165
- 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 };
170
+ export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, determinePoolDisplay, evalUnits, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, stringToBigNumber, transformToBaseUnit, unescape, unimplemented, unitToPlanck, varToUrlHash };
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) {
@@ -31,6 +32,7 @@ var ellipsisFn = (str, amount = 6, position = "center") => {
31
32
  return "..." + str.slice(amount);
32
33
  }
33
34
  };
35
+ var rmCommas = (val) => val.replace(/,/g, "");
34
36
 
35
37
  // src/unit.ts
36
38
  var remToUnit = (rem) => Number(rem.slice(0, rem.length - 3)) * parseFloat(getComputedStyle(document.documentElement).fontSize);
@@ -290,6 +292,7 @@ var mergeDeep = (target, ...sources) => {
290
292
  }
291
293
  return mergeDeep(target, ...sources);
292
294
  };
295
+ var stringToBigNumber = (value) => new BigNumber2(rmCommas(value));
293
296
  export {
294
297
  addedTo,
295
298
  applyWidthAsPadding,
@@ -311,6 +314,7 @@ export {
311
314
  setStateWithRef,
312
315
  snakeToCamel,
313
316
  sortWithNull,
317
+ stringToBigNumber,
314
318
  transformToBaseUnit,
315
319
  unescape,
316
320
  unimplemented,