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, assertion library and code coverage tool with its command line interface
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 yarnyarn add example-calculator# If you use NPMnpm 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.
import Calculator from "example-calculator";const calculator = new Calculator();
You can supply an initial value to the calculator as an argument to its constructor (by default 0).
const calculator2 = new Calculator(2);
The current value of the calculator can be accessed using the value() method.
console.log(calculator.value());// Prints 0console.log(calculator2.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.log(calculator2.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.add(2);// Subtracts 1 from the current value in memorycalculator.sub(1);// Multiplies the current value in memory with 2calculator.mul(2);// Divides the current value in memory by 2calculator.div(2);// Sets the value in memory to the modulus (remainder) of the current value and 3calculator.mod(3);// 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.
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.
You can also import some operations as standalone functions from their submodules:
import add from "example-calculator/add";import sub from "example-calculator/sub";import mul from "example-calculator/mul";import div from "example-calculator/div";import mod from "example-calculator/mod";import ln from "example-calculator/ln";console.log(add(1, 1));// Prints 2console.log(sub(1, 1));// Prints 0console.log(mul(2, 2));
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.
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
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 use yarn 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