@lucid-evolution/utils 0.1.15 → 0.1.17

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/dist/index.cjs CHANGED
@@ -65,6 +65,7 @@ __export(src_exports, {
65
65
  slotToUnixTime: () => slotToUnixTime,
66
66
  sortUTxOs: () => sortUTxOs,
67
67
  stakeCredentialOf: () => stakeCredentialOf,
68
+ stringify: () => stringify,
68
69
  toLabel: () => toLabel,
69
70
  toNativeScript: () => toNativeScript,
70
71
  toPublicKey: () => toPublicKey,
@@ -1114,18 +1115,38 @@ var selectUTxOs = (utxos, totalAssets) => {
1114
1115
  return [];
1115
1116
  return selectedUtxos;
1116
1117
  };
1117
- var sortUTxOs = (utxos, order = "descending") => {
1118
- return [...utxos].sort((a, b) => {
1119
- if (a.assets["lovelace"] > b.assets["lovelace"]) {
1120
- return order === "ascending" ? 1 : -1;
1121
- }
1122
- if (a.assets["lovelace"] < b.assets["lovelace"]) {
1123
- return order === "ascending" ? -1 : 1;
1124
- }
1125
- return 0;
1126
- });
1118
+ var sortUTxOs = (utxos, order = "LargestFirst") => {
1119
+ switch (order) {
1120
+ case "LargestFirst":
1121
+ return [...utxos].sort(largestFirst);
1122
+ case "SmallestFirst":
1123
+ return [...utxos].sort(smallestFirst);
1124
+ }
1125
+ };
1126
+ var largestFirst = (a, b) => {
1127
+ const lovelaceA = Number(a.assets["lovelace"]);
1128
+ const lovelaceB = Number(b.assets["lovelace"]);
1129
+ if (lovelaceA === lovelaceB) {
1130
+ return Object.keys(a.assets).length - Object.keys(b.assets).length;
1131
+ }
1132
+ return -1 * (lovelaceA - lovelaceB);
1133
+ };
1134
+ var smallestFirst = (a, b) => {
1135
+ const lovelaceA = Number(a.assets["lovelace"]);
1136
+ const lovelaceB = Number(b.assets["lovelace"]);
1137
+ if (lovelaceA == lovelaceB) {
1138
+ return Object.keys(a.assets).length - Object.keys(b.assets).length;
1139
+ }
1140
+ return lovelaceA - lovelaceB;
1127
1141
  };
1128
1142
  var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
1143
+
1144
+ // src/objects.ts
1145
+ var stringify = (data) => JSON.stringify(
1146
+ data,
1147
+ (key, value) => typeof value === "bigint" ? value.toString() + "n" : value,
1148
+ 2
1149
+ );
1129
1150
  // Annotate the CommonJS export names for ESM import in node:
1130
1151
  0 && (module.exports = {
1131
1152
  PROTOCOL_PARAMETERS_DEFAULT,
@@ -1163,6 +1184,7 @@ var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputInde
1163
1184
  slotToUnixTime,
1164
1185
  sortUTxOs,
1165
1186
  stakeCredentialOf,
1187
+ stringify,
1166
1188
  toLabel,
1167
1189
  toNativeScript,
1168
1190
  toPublicKey,
package/dist/index.d.cts CHANGED
@@ -61,15 +61,26 @@ declare function coreToTxOutput(output: CML.TransactionOutput): TxOutput;
61
61
  declare function coresToTxOutputs(outputs: CML.TransactionOutput[]): TxOutput[];
62
62
  declare const selectUTxOs: (utxos: UTxO[], totalAssets: Assets) => UTxO[];
63
63
  /**
64
- * Sorts an array of UTXOs by the amount of "lovelace" in ascending or descending order.
64
+ * Union type for specifying sorting order in function "sortUTxOs"
65
+ */
66
+ type SortOrder =
67
+ /**
68
+ * Largest amount of "lovelace" with least number of unique assets first
69
+ */
70
+ "LargestFirst"
71
+ /**
72
+ * Smallest amount of "lovelace" with least number of unique assets first
73
+ */
74
+ | "SmallestFirst";
75
+ /**
76
+ * Sorts an array of UTXOs according to specified sort order ("LargestFirst" by default).
65
77
  *
66
78
  * @param {UTxO[]} utxos - The array of UTXO objects to be sorted.
67
- * @param {"ascending" | "descending"} [order="descending"] - The order in which to sort the UTXOs.
68
- * Use "ascending" for ascending order and "descending" for descending order.
79
+ * @param {SortOrder} [order="LargestFirst"] - The order in which to sort the UTXOs.
69
80
  * @returns {UTxO[]} - The sorted array of UTXOs.
70
81
  *
71
82
  */
72
- declare const sortUTxOs: (utxos: UTxO[], order?: "ascending" | "descending") => UTxO[];
83
+ declare const sortUTxOs: (utxos: UTxO[], order?: SortOrder) => UTxO[];
73
84
  declare const isEqualUTxO: (self: UTxO, that: UTxO) => boolean;
74
85
 
75
86
  declare function valueToAssets(value: CML.Value): Assets;
@@ -90,4 +101,6 @@ declare function fromUnit(unit: Unit): {
90
101
  declare function toUnit(policyId: PolicyId, name?: string | null, label?: number | null): Unit;
91
102
  declare function addAssets(...assets: Assets[]): Assets;
92
103
 
93
- export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
104
+ declare const stringify: (data: any) => string;
105
+
106
+ export { PROTOCOL_PARAMETERS_DEFAULT, type SortOrder, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, stringify, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
package/dist/index.d.ts CHANGED
@@ -61,15 +61,26 @@ declare function coreToTxOutput(output: CML.TransactionOutput): TxOutput;
61
61
  declare function coresToTxOutputs(outputs: CML.TransactionOutput[]): TxOutput[];
62
62
  declare const selectUTxOs: (utxos: UTxO[], totalAssets: Assets) => UTxO[];
63
63
  /**
64
- * Sorts an array of UTXOs by the amount of "lovelace" in ascending or descending order.
64
+ * Union type for specifying sorting order in function "sortUTxOs"
65
+ */
66
+ type SortOrder =
67
+ /**
68
+ * Largest amount of "lovelace" with least number of unique assets first
69
+ */
70
+ "LargestFirst"
71
+ /**
72
+ * Smallest amount of "lovelace" with least number of unique assets first
73
+ */
74
+ | "SmallestFirst";
75
+ /**
76
+ * Sorts an array of UTXOs according to specified sort order ("LargestFirst" by default).
65
77
  *
66
78
  * @param {UTxO[]} utxos - The array of UTXO objects to be sorted.
67
- * @param {"ascending" | "descending"} [order="descending"] - The order in which to sort the UTXOs.
68
- * Use "ascending" for ascending order and "descending" for descending order.
79
+ * @param {SortOrder} [order="LargestFirst"] - The order in which to sort the UTXOs.
69
80
  * @returns {UTxO[]} - The sorted array of UTXOs.
70
81
  *
71
82
  */
72
- declare const sortUTxOs: (utxos: UTxO[], order?: "ascending" | "descending") => UTxO[];
83
+ declare const sortUTxOs: (utxos: UTxO[], order?: SortOrder) => UTxO[];
73
84
  declare const isEqualUTxO: (self: UTxO, that: UTxO) => boolean;
74
85
 
75
86
  declare function valueToAssets(value: CML.Value): Assets;
@@ -90,4 +101,6 @@ declare function fromUnit(unit: Unit): {
90
101
  declare function toUnit(policyId: PolicyId, name?: string | null, label?: number | null): Unit;
91
102
  declare function addAssets(...assets: Assets[]): Assets;
92
103
 
93
- export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
104
+ declare const stringify: (data: any) => string;
105
+
106
+ export { PROTOCOL_PARAMETERS_DEFAULT, type SortOrder, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, stringify, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
package/dist/index.js CHANGED
@@ -1034,18 +1034,38 @@ var selectUTxOs = (utxos, totalAssets) => {
1034
1034
  return [];
1035
1035
  return selectedUtxos;
1036
1036
  };
1037
- var sortUTxOs = (utxos, order = "descending") => {
1038
- return [...utxos].sort((a, b) => {
1039
- if (a.assets["lovelace"] > b.assets["lovelace"]) {
1040
- return order === "ascending" ? 1 : -1;
1041
- }
1042
- if (a.assets["lovelace"] < b.assets["lovelace"]) {
1043
- return order === "ascending" ? -1 : 1;
1044
- }
1045
- return 0;
1046
- });
1037
+ var sortUTxOs = (utxos, order = "LargestFirst") => {
1038
+ switch (order) {
1039
+ case "LargestFirst":
1040
+ return [...utxos].sort(largestFirst);
1041
+ case "SmallestFirst":
1042
+ return [...utxos].sort(smallestFirst);
1043
+ }
1044
+ };
1045
+ var largestFirst = (a, b) => {
1046
+ const lovelaceA = Number(a.assets["lovelace"]);
1047
+ const lovelaceB = Number(b.assets["lovelace"]);
1048
+ if (lovelaceA === lovelaceB) {
1049
+ return Object.keys(a.assets).length - Object.keys(b.assets).length;
1050
+ }
1051
+ return -1 * (lovelaceA - lovelaceB);
1052
+ };
1053
+ var smallestFirst = (a, b) => {
1054
+ const lovelaceA = Number(a.assets["lovelace"]);
1055
+ const lovelaceB = Number(b.assets["lovelace"]);
1056
+ if (lovelaceA == lovelaceB) {
1057
+ return Object.keys(a.assets).length - Object.keys(b.assets).length;
1058
+ }
1059
+ return lovelaceA - lovelaceB;
1047
1060
  };
1048
1061
  var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
1062
+
1063
+ // src/objects.ts
1064
+ var stringify = (data) => JSON.stringify(
1065
+ data,
1066
+ (key, value) => typeof value === "bigint" ? value.toString() + "n" : value,
1067
+ 2
1068
+ );
1049
1069
  export {
1050
1070
  PROTOCOL_PARAMETERS_DEFAULT,
1051
1071
  addAssets,
@@ -1082,6 +1102,7 @@ export {
1082
1102
  slotToUnixTime,
1083
1103
  sortUTxOs,
1084
1104
  stakeCredentialOf,
1105
+ stringify,
1085
1106
  toLabel,
1086
1107
  toNativeScript,
1087
1108
  toPublicKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucid-evolution/utils",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",