@talismn/util 0.3.1 → 0.4.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.
@@ -1 +1 @@
1
- export declare const formatPrice: (price: number, currency: string, compact: boolean) => string;
1
+ export declare const formatPrice: (price: number, currency: string, compact: boolean, forceTickerAfter?: boolean) => string;
@@ -26,3 +26,5 @@ export * from "./validateHexString";
26
26
  export * from "./normalizeAddress";
27
27
  export * from "./isAddressEqual";
28
28
  export * from "./isAscii";
29
+ export * from "./isNotNil";
30
+ export * from "./isTruthy";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * WARNING: This function only checks against null or undefined, it does not coerce the value.
3
+ * ie: false and 0 are considered not nil
4
+ * Use isTruthy instead for a regular coercion check.
5
+ *
6
+ * @param value
7
+ * @returns whether the value is neither null nor undefined
8
+ */
9
+ export declare const isNotNil: <T>(value: T | null | undefined) => value is T;
@@ -0,0 +1 @@
1
+ export declare const isTruthy: <T>(value: T | null | undefined) => value is T;
@@ -183,11 +183,11 @@ const formatDecimals = (num, digits = MIN_DIGITS, options = {}, locale = "en-US"
183
183
  }).format(truncatedValue.toNumber());
184
184
  };
185
185
 
186
- const formatPrice = (price, currency, compact) => {
186
+ const formatPrice = (price, currency, compact, forceTickerAfter) => {
187
187
  return Intl.NumberFormat(undefined, {
188
188
  style: "currency",
189
189
  currency,
190
- currencyDisplay: currency === "usd" ? "narrowSymbol" : "symbol",
190
+ currencyDisplay: forceTickerAfter ? "code" : currency === "usd" ? "narrowSymbol" : "symbol",
191
191
  maximumSignificantDigits: compact ? price < 1 ? 3 : 4 : undefined,
192
192
  roundingPriority: compact ? "auto" : "morePrecision",
193
193
  notation: compact && price >= 10_000 ? "compact" : "standard"
@@ -303,6 +303,18 @@ const isAscii = str => {
303
303
  return [...str].every(char => char.charCodeAt(0) <= 127);
304
304
  };
305
305
 
306
+ /**
307
+ * WARNING: This function only checks against null or undefined, it does not coerce the value.
308
+ * ie: false and 0 are considered not nil
309
+ * Use isTruthy instead for a regular coercion check.
310
+ *
311
+ * @param value
312
+ * @returns whether the value is neither null nor undefined
313
+ */
314
+ const isNotNil = value => value !== null && value !== undefined;
315
+
316
+ const isTruthy = value => Boolean(value);
317
+
306
318
  exports.BigMath = BigMath;
307
319
  exports.Deferred = Deferred;
308
320
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
@@ -323,6 +335,8 @@ exports.isAscii = isAscii;
323
335
  exports.isBigInt = isBigInt;
324
336
  exports.isBooleanTrue = isBooleanTrue;
325
337
  exports.isEthereumAddress = isEthereumAddress;
338
+ exports.isNotNil = isNotNil;
339
+ exports.isTruthy = isTruthy;
326
340
  exports.isValidSubstrateAddress = isValidSubstrateAddress;
327
341
  exports.normalizeAddress = normalizeAddress;
328
342
  exports.planckToTokens = planckToTokens;
@@ -183,11 +183,11 @@ const formatDecimals = (num, digits = MIN_DIGITS, options = {}, locale = "en-US"
183
183
  }).format(truncatedValue.toNumber());
184
184
  };
185
185
 
186
- const formatPrice = (price, currency, compact) => {
186
+ const formatPrice = (price, currency, compact, forceTickerAfter) => {
187
187
  return Intl.NumberFormat(undefined, {
188
188
  style: "currency",
189
189
  currency,
190
- currencyDisplay: currency === "usd" ? "narrowSymbol" : "symbol",
190
+ currencyDisplay: forceTickerAfter ? "code" : currency === "usd" ? "narrowSymbol" : "symbol",
191
191
  maximumSignificantDigits: compact ? price < 1 ? 3 : 4 : undefined,
192
192
  roundingPriority: compact ? "auto" : "morePrecision",
193
193
  notation: compact && price >= 10_000 ? "compact" : "standard"
@@ -303,6 +303,18 @@ const isAscii = str => {
303
303
  return [...str].every(char => char.charCodeAt(0) <= 127);
304
304
  };
305
305
 
306
+ /**
307
+ * WARNING: This function only checks against null or undefined, it does not coerce the value.
308
+ * ie: false and 0 are considered not nil
309
+ * Use isTruthy instead for a regular coercion check.
310
+ *
311
+ * @param value
312
+ * @returns whether the value is neither null nor undefined
313
+ */
314
+ const isNotNil = value => value !== null && value !== undefined;
315
+
316
+ const isTruthy = value => Boolean(value);
317
+
306
318
  exports.BigMath = BigMath;
307
319
  exports.Deferred = Deferred;
308
320
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
@@ -323,6 +335,8 @@ exports.isAscii = isAscii;
323
335
  exports.isBigInt = isBigInt;
324
336
  exports.isBooleanTrue = isBooleanTrue;
325
337
  exports.isEthereumAddress = isEthereumAddress;
338
+ exports.isNotNil = isNotNil;
339
+ exports.isTruthy = isTruthy;
326
340
  exports.isValidSubstrateAddress = isValidSubstrateAddress;
327
341
  exports.normalizeAddress = normalizeAddress;
328
342
  exports.planckToTokens = planckToTokens;
@@ -177,11 +177,11 @@ const formatDecimals = (num, digits = MIN_DIGITS, options = {}, locale = "en-US"
177
177
  }).format(truncatedValue.toNumber());
178
178
  };
179
179
 
180
- const formatPrice = (price, currency, compact) => {
180
+ const formatPrice = (price, currency, compact, forceTickerAfter) => {
181
181
  return Intl.NumberFormat(undefined, {
182
182
  style: "currency",
183
183
  currency,
184
- currencyDisplay: currency === "usd" ? "narrowSymbol" : "symbol",
184
+ currencyDisplay: forceTickerAfter ? "code" : currency === "usd" ? "narrowSymbol" : "symbol",
185
185
  maximumSignificantDigits: compact ? price < 1 ? 3 : 4 : undefined,
186
186
  roundingPriority: compact ? "auto" : "morePrecision",
187
187
  notation: compact && price >= 10_000 ? "compact" : "standard"
@@ -297,4 +297,16 @@ const isAscii = str => {
297
297
  return [...str].every(char => char.charCodeAt(0) <= 127);
298
298
  };
299
299
 
300
- export { BigMath, Deferred, MAX_DECIMALS_FORMAT, addTrailingSlash, blake2Concat, classNames, convertAddress, decodeAnyAddress, decodeSs58Format, encodeAnyAddress, firstThenDebounce, formatDecimals, formatPrice, hasOwnProperty, isAddressEqual, isArrayOf, isAscii, isBigInt, isBooleanTrue, isEthereumAddress, isValidSubstrateAddress, normalizeAddress, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat, validateHexString };
300
+ /**
301
+ * WARNING: This function only checks against null or undefined, it does not coerce the value.
302
+ * ie: false and 0 are considered not nil
303
+ * Use isTruthy instead for a regular coercion check.
304
+ *
305
+ * @param value
306
+ * @returns whether the value is neither null nor undefined
307
+ */
308
+ const isNotNil = value => value !== null && value !== undefined;
309
+
310
+ const isTruthy = value => Boolean(value);
311
+
312
+ export { BigMath, Deferred, MAX_DECIMALS_FORMAT, addTrailingSlash, blake2Concat, classNames, convertAddress, decodeAnyAddress, decodeSs58Format, encodeAnyAddress, firstThenDebounce, formatDecimals, formatPrice, hasOwnProperty, isAddressEqual, isArrayOf, isAscii, isBigInt, isBooleanTrue, isEthereumAddress, isNotNil, isTruthy, isValidSubstrateAddress, normalizeAddress, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat, validateHexString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/util",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",
@@ -34,8 +34,8 @@
34
34
  "jest": "^29.7.0",
35
35
  "ts-jest": "^29.2.5",
36
36
  "typescript": "^5.6.3",
37
- "@talismn/tsconfig": "0.0.2",
38
- "@talismn/eslint-config": "0.0.3"
37
+ "@talismn/eslint-config": "0.0.3",
38
+ "@talismn/tsconfig": "0.0.2"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "@polkadot/keyring": "*",