Skip to content

Commit

Permalink
feat:add all value methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AnsonSIDAN committed Jul 28, 2024
1 parent 9755610 commit 20c7ced
Show file tree
Hide file tree
Showing 2 changed files with 457 additions and 59 deletions.
158 changes: 102 additions & 56 deletions src/types/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
/* eslint-disable class-methods-use-this */
import { Asset } from '@meshsdk/core';

// interface Asset {
// unit: string;
// quantity: bigint;
// }

// Record Samples:
// const utxoAssets = [
// {
Expand Down Expand Up @@ -40,7 +45,7 @@ export class Value {
* 3. If the unit does not exist, add the unti to the object.
* 4. Return the Value class instance.
* @param asset
* @returns
* @returns this
*/
addAsset = (asset: Asset): this => {
// Defines a method named 'addAsset' on the class, which takes an 'asset' parameter of type 'Asset' and returns an instance of the class itself ('this').
Expand All @@ -67,7 +72,7 @@ export class Value {
* 4. If the unit does not exist, add the unti to the object.
* 5. Return the Value class instance.
* @param assets
* @returns
* @returns this
*/
addAssets = (assets: Asset[]): this => {
// Defines a method named 'addAssets' on the class, which takes an 'assets' parameter of type 'Asset[]' (an array of assets) and returns an instance of the class itself ('this').
Expand All @@ -94,118 +99,159 @@ export class Value {
* 2. If the unit exists, subtract the new quantity from the existing quantity.
* 3. If the unit does not exist, print an error message.
* @param asset
* @returns
* @returns this
*/
negateAsset = (asset: Asset): this => {
// Defines a method named 'negateAsset' on the class, which takes an 'asset' parameter of type 'Asset' and returns an instance of the class itself ('this').
if (this.value[asset.unit]) {
// Checks if the 'unit' property of the 'asset' object already exists as a key in the 'value' object of the class. If it does, the block inside will execute.
this.value[asset.unit] -= BigInt(asset.quantity);
const newQuantity = this.value[asset.unit] - BigInt(asset.quantity);
// Subtracts the 'quantity' of the 'asset' from the existing quantity for that unit in the 'value' object. The 'quantity' is converted to a BigInt to handle large numbers or ensure consistency in number type.
if (this.value[asset.unit] === 0n) {
// Checks if the resulting quantity is equal to 0.
if (newQuantity > 0n) {
this.value[asset.unit] = newQuantity;
// If the resulting quantity is greater than 0, update the quantity for that unit in the 'value' object.
} else if (newQuantity === 0n) {
delete this.value[asset.unit];
// If the resulting quantity is equal to 0, it deletes the unit from the 'value' object, effectively removing the asset.
// If the resulting quantity is equal to 0, delete the unit from the 'value' object, effectively removing the asset unit.
} else {
this.value[asset.unit] = newQuantity;
}
} else {
console.error(`Unit ${asset.unit} does not exist.`);
// If the 'unit' does not exist in the 'value' object, it prints an error message to the console indicating the unit does not exist.
this.value[asset.unit] = -BigInt(asset.quantity);
}
return this;
// Returns the instance of the class, allowing for method chaining.
};

/**
* Subtract an array of assets from the Value class's value record. If an asset with the same unit already exists in the value record, the quantity of the
* existing asset will be decreased by the quantity of the new asset. If no such asset exists, an error message should be printed.
* @param assets
* @returns
* @returns this
*/
negateAssets = (assets: Asset[]): this => {
assets.forEach((asset) => {
if (this.value[asset.unit]) {
// Checks if the 'unit' property of the 'asset' object already exists as a key in the 'value' object of the class. If it does, the block inside will execute.
this.value[asset.unit] -= BigInt(asset.quantity);
// If the unit exists, subtract the quantity from the existing quantity
if (this.value[asset.unit] === 0n) {
// Check if the resulting quantity is equal to 0
const newQuantity = this.value[asset.unit] - BigInt(asset.quantity);
if (newQuantity > 0n) {
this.value[asset.unit] = newQuantity;
} else if (newQuantity === 0n) {
delete this.value[asset.unit];
// If yes, delete the unit from the value object
} else {
this.value[asset.unit] = newQuantity;
}
} else {
console.error(`Unit ${asset.unit} does not exist.`);
// If the unit does not exist, print an error message
this.value[asset.unit] = -BigInt(asset.quantity);
}
});
return this;
// Allows for method chaining by returning the instance of the class
};

/**
* Get the quantity of asset object per unit
* @param unit
* @returns
*/
// get = (unit: string): Asset => {
// if (this.value[unit]) {
// // Check if the unit exists in the value object
// return { unit: unit, quantity: BigInt(this.value[unit]) };
// // If the unit exists, return an Asset object with the unit and its quantity
// }
// console.error(`Unit ${unit} does not exist.`);
// return { unit: unit, quantity: '0' };
// // If the unit does not exist, return an Asset object with the unit and a quantity of '0'
// }
// };
get = (unit: string): bigint => {
if (this.value[unit]) {
// Check if the unit exists in the value object
return BigInt(this.value[unit]);
// If the unit exists, return an Asset object with the unit and its quantity
}
console.error(`Unit ${unit} does not exist.`);
return BigInt(0);
// If the unit does not exist, return an Asset object with the unit and a quantity of '0'
};

/**
* Get all assets (return Record of Asset[])
* @returns
* @param
* @returns Record<string, Asset[]>
*/
// units = (): Record<string, Asset[]> => {
// const result: Record<string, Asset[]> = {};
// Object.keys(this.value).forEach((unit) => {
// if (!result[unit]) {
// result[unit] = [];
// }
// result[unit].push({ unit: unit, quantity: BigInt(this.value[unit]) });
// });
// return result;
// };
units = (): Record<string, { unit: string; quantity: bigint }[]> => {
// Defines a method named 'units' on the class, which returns a Record object where the keys are the units and the values are an array of objects containing the unit and quantity.
const result: Record<string, { unit: string; quantity: bigint }[]> = {};
// Initializes an empty Record object named result to store the result
Object.keys(this.value).forEach((unit) => {
// Iterates over each key in the 'value' object of the class
if (!result[unit]) {
result[unit] = [];
}
// If the unit does not exist in the result object, initialize it as an empty array
result[unit].push({ unit, quantity: BigInt(this.value[unit]) });
//
});
return result;
// Returns the result object containing the units and their quantities
};

/**
* Check if the value is greater than or equal to an inputted value
* @param value
* @returns
* @param unit - The unit to compare (e.g., "ADA")
* @param other - The value to compare against
* @returns boolean
*/
geq = (value: Value): boolean => {
return true;
geq = (unit: string, other: Value): boolean => {
// Defines a method named 'geq' on the class, which takes a 'unit' parameter of type 'string' and an 'other' parameter of type 'Value' and returns a boolean value.
if (this.value[unit] === undefined || other.value[unit] === undefined) {
// Checks if the unit does not exist in either the current value object or the value object of the 'other' parameter.
return false;
// If the unit does not exist in either object, return false.
}
return BigInt(this.value[unit]) >= BigInt(other.value[unit]);
// Compares the quantity of the unit in the current value object with the quantity of the same unit in the 'other' value object. The comparison is done using BigInt to handle large numbers or ensure consistency in number type.
};

/**
* Check if the value is less than the given value
* @param value
* @returns
* Check if the value is less than the inputted value
* @param unit - The unit to compare (e.g., "ADA")
* @param other - The value to compare against
* @returns boolean
*/
leq = (value: Value): boolean => {
return true;
leq = (unit: string, other: Value): boolean => {
// Defines a method named 'leq' on the class, which takes a 'unit' parameter of type 'string' and an 'other' parameter of type 'Value' and returns a boolean value.
if (this.value[unit] === undefined || other.value[unit] === undefined) {
// Checks if the unit does not exist in either the current value object or the value object of the 'other' parameter.
return false;
// If the unit does not exist in either object, return false.
}
return BigInt(this.value[unit]) <= BigInt(other.value[unit]);
// Compares the quantity of the unit in the current value object with the quantity of the same unit in the 'other' value object. The comparison is done using BigInt to handle large numbers or ensure consistency in number type.
};

/**
* Check if the value is empty
* @param value
* @returns
* @param
* @returns boolean
*/
isEmpty = (): boolean => {
return true;
return Object.keys(this.value).length === 0;
// Returns a boolean value indicating whether the 'value' object of the class is empty (i.e., has no keys).
};

/**
* Merge the given values
* @param values
* @returns
* @returns this
*/
merge = (values: Value[]): this => {
merge = (values: Value | Value[]): this => {
// Ensure values is always an array
const valuesArray = Array.isArray(values) ? values : [values];
// If the 'values' parameter is an array, assign it to 'valuesArray'; otherwise, create an array with 'values' as the only element.

valuesArray.forEach((other) => {
// Iterate over each 'other' value in the 'valuesArray'
Object.entries(other.value).forEach(([key, value]) => {
// Iterate over each key-value pair in the 'value' object of the 'other' value
if (this.value[key] === undefined) {
this.value[key] = BigInt(value);
// If the key does not exist in the 'value' object of the class, add the key and its value to the 'value' object.
} else {
this.value[key] = BigInt(this.value[key]) + BigInt(value);
// If the key already exists in the 'value' object, add the value to the existing value for that key.
}
});
});
return this;
// Return the instance of the class to allow for method chaining.
};
}
Loading

0 comments on commit 20c7ced

Please sign in to comment.