example-calculator
This package provides a simple calculator class and several "safe" mathematical operations, in which all undefined or infinite results are replaced with 0. It is a simple educational project using a range of fully-configured and integrated external tools.
Why
This is a simple educational project to set up and work with a variety of libraries and external tools. Among others, it uses:
- ts-pkg-scripts build tools for compiling, bundling and packaging TypeScript libraries (uses gulp and rollup internally)
- TypeScript for all source, test, and gulp task files
- the mocha test framework, chai assertion library and istanbul code coverage tool with its nyc command line interface
- Travis.CI for continuous integration (running tests and deploying new package versions)
- CodeCov and Code Climate for monitoring code coverage and watching the codebase for code smells
- semantic-release to release new versions of the package, when code gets pushed to the master branch and significant changes have occurred, and for generating the changelog
- husky to run commit hooks
- prettier for auto-formatting the source code (run as a pre-commit hook on all staged changes)
- commitlint for ensuring that all commit messages follow the same format (run as a commit message hook)
- commitizen for writing commit messages adhering to the required format. Instead of using
git commit
to push commits, you useyarn commit
, which invokes commitizen. - Shields from shields.io for displaying a range of status info about the repository and package
- David for monitoring if all dependencies are up to date
- Snyk for monitoring the project for security vulnerabilities
- Gitter as community chat
- typedoc for generating documentation
- gh-pages for pushing the documentation to Github pages. It is accessible at https://gfmio.github.io/example-calculator.
- All Contributors for managing contributors
The repository also features Github issue templates, a pull request template, a code of conduct and a contributing guideline.
Explore some of the tools above and look at the configuration for each to see how they are used and start using them in your own projects, if they fit your needs.
If you want to suggest changes or additions, find an error or that anything does not work or no longer works, please file an issue or submit a pull request with your suggested changes.
Install
Install the package from NPM:
# If you use yarn yarn add example-calculator # If you use NPM npm install example-calculator
You can also import the package bundle from unpkg by inserting the following script tag in your HTML code.
The module content will be available as the UMD global exampleCalculator
.
Usage
The package provides a calculator class.
;;
You can supply an initial value to the calculator as an argument to its constructor (by default 0).
;
The current value of the calculator can be accessed using the value()
method.
console.logcalculator.value;// Prints 0console.logcalculator2.value;// Prints 2
clear()
resets the value in the memory of the calculator to 0. All operations of the calculator, apart from value()
return the instance, so operations can be chained.
console.logcalculator2.clear.value;// Prints 0
The calculator provides a range of simple mathematical operations, which perform an operation on the value in memory.
// Adds 2 to the current value in memorycalculator.add2;// Subtracts 1 from the current value in memorycalculator.sub1;// Multiplies the current value in memory with 2calculator.mul2;// Divides the current value in memory by 2calculator.div2;// Sets the value in memory to the modulus (remainder) of the current value and 3calculator.mod3;// Sets the value in memory to the exponentiated current valuecalculator.exp;// Sets the value in memory to the natural logarithm of the current valuecalculator.ln;// Sets the value in memory to sine of the current valuecalculator.sin;// Sets the value in memory to cosine of the current valuecalculator.cos;// Sets the value in memory to tangent of the current valuecalculator.tan;// Sets the value to the inverse (1/x) of the current valuecalculator.inverse;
Some mathematical operations are not defined for all values, such as division by 0 or the logarithm of a negative number or 0. In all of these circumstances, the return values are 0.
calculator.clear.add1.div0.value;// Returns 0calculator.clear.add1.mod0.value;// Returns 0calculator.clear.sub1.ln.value;// Returns 0calculator.clear.inverse.value;// Returns 0
You can also import some operations as standalone functions from their submodules:
;;;;;; console.logadd1, 1;// Prints 2console.logsub1, 1;// Prints 0console.logmul2, 2;// Prints 4console.logdiv6, 3;// Prints 2console.logmod6, 4;// Prints 2console.logln1;// Prints 0
Contributing
Please read CODE_OF_CODUCT.md for details on our code of conduct and CONTRIBUTING.md for all details on how to contribute to this project (with code or otherwise) and the process for submitting pull requests to us.
Versioning
We use SemVer for versioning. Releases are generated using semantic-release. For the versions available, see the tags on this repository.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- This project has taken some inspiration from typescript-library-starter by @alexjoverm
- The README and CONTRIBUTING files have taken inspiration from these gists by @PurpleBooth and the contributing guidelines from phly and akka.
Authors
- Frédérique Mittelstaedt (@gfmio) - Initial work
See also the list of contributors who participated in this project.
Contributors
Thanks goes to these wonderful people (emoji key):
Frédérique Mittelstaedt 📖 ⚠️ 💻 |
---|
This project follows the all-contributors specification. Contributions of any kind welcome!