Stellar Coin is a flexible parameter-based web calculator system. It can be used to build any type of parameter-based calculators. That is, any type of calculator that takes same parameters from the user and calculates a result. Most prominent example is price calculators.
I created it to use for a simple price calculator. Decided to make it generic and open source. Don't expect it to be complete.
To use Stellar Coin for your own parameter-based calculator webpage, first fork the repository. Then edit to config file.
Here is what those config options do:
appNameis the name of the calculator and is displayed at the top of the webpage.appDescriptionis similar. It is displayed under theappName.valueNameis the name displayed before the overall calculation result. For example, for a price calculator, this can be "Total Price".postValueis the value display after the overall calculation result. For example, for a price calculator, this can be "$" or "dollars".
After setting the config, now you should edit the calc.ts to setup how your calculator should work.
In that file, there are only tree variables. You don't have to change anything else outside that file, but if you want obviously you can do so.
calcParameters are parameters that your calculator uses to compute the final result. All parameters are represented with numbers. There are tree parameter types:
enterselecttoggle
enter is a type of parameter that the user of the calculator enters manually by typing via their keyboards.
select is a type of parameter that user selects out of the values passed to it. Only a single value can be chosen by the user.
toggle is a type of parameter that user toggles. It can only have two values 1 or 0. Representing true and false respectively.
You should now add the parameters of your calculator to the list calcParameters as written in the default version of the file.
There is how you use the parameters (? after the argument means it's optional):
enter(name, description?, min?, max?)
select(name, description?, options)
toggle(name, description?)
nameis the name of the parameter field.descriptionis a information text for that field.minandmaxare limiting values for the fieldoptionsis a list ofselectOptions, which takes two parameters:(name, value).valuehere is the value that will be passed to the function that calculates the final result.
Now the second variable, calcResult. This variable is a lambda function that takes the number values taken from the user via the parameters you set in calcParameters in the same order. So you can do whatever you want here to calculate the final result. To access the parameters simply use args[i] where i is the index of the parameter you declared in calcParameters.
Finally, the last variable, calcDetail. This is again a lambda function but this time it returns a list of strings. Just as calcResult, the parameters are passed to this function. It is used to show the user of the calculator where the result comes from. Every element in this list will be shown in a different line. There are three helper functions for it.
infosimply takes two arguments and turns them into"firstArg (secondArg)"mulis very similar:"firstArg x secondArg"condreturns second argument if the first one is true and null otherwise. (null's are ignored in the display)
You can always look at the default calc.ts file to understand it better. If you know JavaScript/TypeScript, it shouldn't be any hard.
Now the final step. Simply run this to compile the app to the static files with SvelteKit:
npm ci
npm run build
A new directory called build should be created. All the files of the app is there. It's entirely static.
If you want to go deeper, you can write some Playwright tests in the tests directory to make sure your calculations work as intended.
That's it!