@w3ux/utils 2.0.7-alpha.2 → 2.0.9

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.cjs CHANGED
@@ -34,10 +34,11 @@ __export(base_exports, {
34
34
  rmCommas: () => rmCommas,
35
35
  rmDecimals: () => rmDecimals,
36
36
  shuffle: () => shuffle,
37
- withTimeout: () => withTimeout
37
+ withTimeout: () => withTimeout,
38
+ withTimeoutThrow: () => withTimeoutThrow
38
39
  });
39
40
  module.exports = __toCommonJS(base_exports);
40
- var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
41
+ var import_utils = require("dedot/utils");
41
42
  var minDecimalPlaces = (val, minDecimals) => {
42
43
  try {
43
44
  const retainCommas = typeof val === "string" && val.includes(",");
@@ -136,12 +137,22 @@ var withTimeout = (ms, promise, options) => {
136
137
  );
137
138
  return Promise.race([promise, timeout]);
138
139
  };
140
+ var withTimeoutThrow = (ms, promise, options) => {
141
+ const timeout = new Promise(
142
+ (reject) => setTimeout(async () => {
143
+ if (typeof options?.onTimeout === "function") {
144
+ options.onTimeout();
145
+ }
146
+ reject("Function timeout");
147
+ }, ms)
148
+ );
149
+ return Promise.race([promise, timeout]);
150
+ };
139
151
  var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
140
152
  var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
141
153
  var formatAccountSs58 = (address, ss58Prefix) => {
142
154
  try {
143
- const codec = (0, import_substrate_bindings.AccountId)(ss58Prefix);
144
- return codec.dec(codec.enc(address));
155
+ return (0, import_utils.encodeAddress)(address, ss58Prefix);
145
156
  } catch (e) {
146
157
  return null;
147
158
  }
@@ -175,7 +186,8 @@ var minBigInt = (...values) => values.reduce((min, current) => current < min ? c
175
186
  rmCommas,
176
187
  rmDecimals,
177
188
  shuffle,
178
- withTimeout
189
+ withTimeout,
190
+ withTimeoutThrow
179
191
  });
180
192
  /* @license Copyright 2024 w3ux authors & contributors
181
193
  SPDX-License-Identifier: GPL-3.0-only */
package/base.d.cts CHANGED
@@ -58,6 +58,13 @@ declare const shuffle: <T>(array: T[]) => T[];
58
58
  declare const withTimeout: (ms: number, promise: Promise<unknown>, options?: {
59
59
  onTimeout?: () => void;
60
60
  }) => Promise<unknown>;
61
+ /**
62
+ * @name withTimeoutThrow
63
+ * @summary Timeout a promise after a specified number of milliseconds by throwing an error
64
+ */
65
+ declare const withTimeoutThrow: <T>(ms: number, promise: Promise<T>, options?: {
66
+ onTimeout?: () => void;
67
+ }) => Promise<unknown>;
61
68
  /**
62
69
  * @name appendOrEmpty
63
70
  * @summary Returns ` value` if a condition is truthy, or an empty string otherwise.
@@ -104,4 +111,4 @@ declare const maxBigInt: (...values: bigint[]) => bigint;
104
111
  */
105
112
  declare const minBigInt: (...values: bigint[]) => bigint;
106
113
 
107
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout };
114
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout, withTimeoutThrow };
package/base.d.ts CHANGED
@@ -58,6 +58,13 @@ declare const shuffle: <T>(array: T[]) => T[];
58
58
  declare const withTimeout: (ms: number, promise: Promise<unknown>, options?: {
59
59
  onTimeout?: () => void;
60
60
  }) => Promise<unknown>;
61
+ /**
62
+ * @name withTimeoutThrow
63
+ * @summary Timeout a promise after a specified number of milliseconds by throwing an error
64
+ */
65
+ declare const withTimeoutThrow: <T>(ms: number, promise: Promise<T>, options?: {
66
+ onTimeout?: () => void;
67
+ }) => Promise<unknown>;
61
68
  /**
62
69
  * @name appendOrEmpty
63
70
  * @summary Returns ` value` if a condition is truthy, or an empty string otherwise.
@@ -104,4 +111,4 @@ declare const maxBigInt: (...values: bigint[]) => bigint;
104
111
  */
105
112
  declare const minBigInt: (...values: bigint[]) => bigint;
106
113
 
107
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout };
114
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout, withTimeoutThrow };
package/base.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/base.ts
2
- import { AccountId } from "@polkadot-api/substrate-bindings";
2
+ import { encodeAddress } from "dedot/utils";
3
3
  var minDecimalPlaces = (val, minDecimals) => {
4
4
  try {
5
5
  const retainCommas = typeof val === "string" && val.includes(",");
@@ -98,12 +98,22 @@ var withTimeout = (ms, promise, options) => {
98
98
  );
99
99
  return Promise.race([promise, timeout]);
100
100
  };
101
+ var withTimeoutThrow = (ms, promise, options) => {
102
+ const timeout = new Promise(
103
+ (reject) => setTimeout(async () => {
104
+ if (typeof options?.onTimeout === "function") {
105
+ options.onTimeout();
106
+ }
107
+ reject("Function timeout");
108
+ }, ms)
109
+ );
110
+ return Promise.race([promise, timeout]);
111
+ };
101
112
  var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
102
113
  var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
103
114
  var formatAccountSs58 = (address, ss58Prefix) => {
104
115
  try {
105
- const codec = AccountId(ss58Prefix);
106
- return codec.dec(codec.enc(address));
116
+ return encodeAddress(address, ss58Prefix);
107
117
  } catch (e) {
108
118
  return null;
109
119
  }
@@ -136,7 +146,8 @@ export {
136
146
  rmCommas,
137
147
  rmDecimals,
138
148
  shuffle,
139
- withTimeout
149
+ withTimeout,
150
+ withTimeoutThrow
140
151
  };
141
152
  /* @license Copyright 2024 w3ux authors & contributors
142
153
  SPDX-License-Identifier: GPL-3.0-only */
package/index.cjs CHANGED
@@ -57,12 +57,13 @@ __export(index_exports, {
57
57
  unimplemented: () => unimplemented,
58
58
  unitToPlanck: () => unitToPlanck,
59
59
  varToUrlHash: () => varToUrlHash,
60
- withTimeout: () => withTimeout
60
+ withTimeout: () => withTimeout,
61
+ withTimeoutThrow: () => withTimeoutThrow
61
62
  });
62
63
  module.exports = __toCommonJS(index_exports);
63
64
 
64
65
  // src/base.ts
65
- var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
66
+ var import_utils = require("dedot/utils");
66
67
  var minDecimalPlaces = (val, minDecimals) => {
67
68
  try {
68
69
  const retainCommas = typeof val === "string" && val.includes(",");
@@ -161,12 +162,22 @@ var withTimeout = (ms, promise, options) => {
161
162
  );
162
163
  return Promise.race([promise, timeout]);
163
164
  };
165
+ var withTimeoutThrow = (ms, promise, options) => {
166
+ const timeout = new Promise(
167
+ (reject) => setTimeout(async () => {
168
+ if (typeof options?.onTimeout === "function") {
169
+ options.onTimeout();
170
+ }
171
+ reject("Function timeout");
172
+ }, ms)
173
+ );
174
+ return Promise.race([promise, timeout]);
175
+ };
164
176
  var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
165
177
  var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
166
178
  var formatAccountSs58 = (address, ss58Prefix) => {
167
179
  try {
168
- const codec = (0, import_substrate_bindings.AccountId)(ss58Prefix);
169
- return codec.dec(codec.enc(address));
180
+ return (0, import_utils.encodeAddress)(address, ss58Prefix);
170
181
  } catch (e) {
171
182
  return null;
172
183
  }
@@ -197,7 +208,7 @@ var u8aConcat = (...u8as) => {
197
208
  };
198
209
 
199
210
  // src/unit.ts
200
- var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
211
+ var import_utils2 = require("dedot/utils");
201
212
  var planckToUnit = (val, units) => {
202
213
  try {
203
214
  units = Math.max(Math.round(units), 0);
@@ -255,8 +266,7 @@ var localStorageOrDefault = (key, _default, parse = false) => {
255
266
  };
256
267
  var isValidAddress = (address) => {
257
268
  try {
258
- const codec = (0, import_substrate_bindings2.AccountId)();
259
- codec.dec(codec.enc(address));
269
+ (0, import_utils2.decodeAddress)(address);
260
270
  return true;
261
271
  } catch (e) {
262
272
  return false;
@@ -434,7 +444,8 @@ var mergeDeep = (target, ...sources) => {
434
444
  unimplemented,
435
445
  unitToPlanck,
436
446
  varToUrlHash,
437
- withTimeout
447
+ withTimeout,
448
+ withTimeoutThrow
438
449
  });
439
450
  /* @license Copyright 2024 w3ux authors & contributors
440
451
  SPDX-License-Identifier: GPL-3.0-only */
package/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout } from './base.cjs';
1
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout, withTimeoutThrow } from './base.cjs';
2
2
  export { u8aConcat } from './convert.cjs';
3
3
  export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.cjs';
4
4
  import 'react';
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout } from './base.js';
1
+ export { appendOr, appendOrEmpty, camelize, ellipsisFn, eqSet, formatAccountSs58, isSuperset, maxBigInt, minBigInt, minDecimalPlaces, pageFromUri, removeHexPrefix, rmCommas, rmDecimals, shuffle, withTimeout, withTimeoutThrow } from './base.js';
2
2
  export { u8aConcat } from './convert.js';
3
3
  export { addedTo, applyWidthAsPadding, capitalizeFirstLetter, extractUrlValue, inChrome, isValidAddress, isValidHttpUrl, localStorageOrDefault, makeCancelable, matchedProperties, mergeDeep, planckToUnit, remToUnit, removeVarFromUrlHash, removedFrom, setStateWithRef, snakeToCamel, sortWithNull, unescape, unimplemented, unitToPlanck, varToUrlHash } from './unit.js';
4
4
  import 'react';
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/base.ts
2
- import { AccountId } from "@polkadot-api/substrate-bindings";
2
+ import { encodeAddress } from "dedot/utils";
3
3
  var minDecimalPlaces = (val, minDecimals) => {
4
4
  try {
5
5
  const retainCommas = typeof val === "string" && val.includes(",");
@@ -98,12 +98,22 @@ var withTimeout = (ms, promise, options) => {
98
98
  );
99
99
  return Promise.race([promise, timeout]);
100
100
  };
101
+ var withTimeoutThrow = (ms, promise, options) => {
102
+ const timeout = new Promise(
103
+ (reject) => setTimeout(async () => {
104
+ if (typeof options?.onTimeout === "function") {
105
+ options.onTimeout();
106
+ }
107
+ reject("Function timeout");
108
+ }, ms)
109
+ );
110
+ return Promise.race([promise, timeout]);
111
+ };
101
112
  var appendOrEmpty = (condition, value) => condition ? ` ${value}` : "";
102
113
  var appendOr = (condition, value, fallback) => condition ? ` ${value}` : ` ${fallback}`;
103
114
  var formatAccountSs58 = (address, ss58Prefix) => {
104
115
  try {
105
- const codec = AccountId(ss58Prefix);
106
- return codec.dec(codec.enc(address));
116
+ return encodeAddress(address, ss58Prefix);
107
117
  } catch (e) {
108
118
  return null;
109
119
  }
@@ -134,7 +144,7 @@ var u8aConcat = (...u8as) => {
134
144
  };
135
145
 
136
146
  // src/unit.ts
137
- import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
147
+ import { decodeAddress } from "dedot/utils";
138
148
  var planckToUnit = (val, units) => {
139
149
  try {
140
150
  units = Math.max(Math.round(units), 0);
@@ -192,8 +202,7 @@ var localStorageOrDefault = (key, _default, parse = false) => {
192
202
  };
193
203
  var isValidAddress = (address) => {
194
204
  try {
195
- const codec = AccountId2();
196
- codec.dec(codec.enc(address));
205
+ decodeAddress(address);
197
206
  return true;
198
207
  } catch (e) {
199
208
  return false;
@@ -370,7 +379,8 @@ export {
370
379
  unimplemented,
371
380
  unitToPlanck,
372
381
  varToUrlHash,
373
- withTimeout
382
+ withTimeout,
383
+ withTimeoutThrow
374
384
  };
375
385
  /* @license Copyright 2024 w3ux authors & contributors
376
386
  SPDX-License-Identifier: GPL-3.0-only */
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@w3ux/utils",
3
- "version": "2.0.7-alpha.2",
3
+ "version": "2.0.9",
4
4
  "license": "GPL-3.0-only",
5
5
  "dependencies": {
6
- "@polkadot-api/substrate-bindings": "^0.11.1"
6
+ "dedot": "^0.8.1"
7
7
  },
8
8
  "main": "index.cjs",
9
9
  "module": "index.js",
package/unit.cjs CHANGED
@@ -43,10 +43,10 @@ __export(unit_exports, {
43
43
  varToUrlHash: () => varToUrlHash
44
44
  });
45
45
  module.exports = __toCommonJS(unit_exports);
46
- var import_substrate_bindings2 = require("@polkadot-api/substrate-bindings");
46
+ var import_utils2 = require("dedot/utils");
47
47
 
48
48
  // src/base.ts
49
- var import_substrate_bindings = require("@polkadot-api/substrate-bindings");
49
+ var import_utils = require("dedot/utils");
50
50
  var rmCommas = (val) => val.replace(/,/g, "");
51
51
  var rmDecimals = (str) => str.split(".")[0];
52
52
 
@@ -108,8 +108,7 @@ var localStorageOrDefault = (key, _default, parse = false) => {
108
108
  };
109
109
  var isValidAddress = (address) => {
110
110
  try {
111
- const codec = (0, import_substrate_bindings2.AccountId)();
112
- codec.dec(codec.enc(address));
111
+ (0, import_utils2.decodeAddress)(address);
113
112
  return true;
114
113
  } catch (e) {
115
114
  return false;
package/unit.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/unit.ts
2
- import { AccountId as AccountId2 } from "@polkadot-api/substrate-bindings";
2
+ import { decodeAddress } from "dedot/utils";
3
3
 
4
4
  // src/base.ts
5
- import { AccountId } from "@polkadot-api/substrate-bindings";
5
+ import { encodeAddress } from "dedot/utils";
6
6
  var rmCommas = (val) => val.replace(/,/g, "");
7
7
  var rmDecimals = (str) => str.split(".")[0];
8
8
 
@@ -64,8 +64,7 @@ var localStorageOrDefault = (key, _default, parse = false) => {
64
64
  };
65
65
  var isValidAddress = (address) => {
66
66
  try {
67
- const codec = AccountId2();
68
- codec.dec(codec.enc(address));
67
+ decodeAddress(address);
69
68
  return true;
70
69
  } catch (e) {
71
70
  return false;