example-calculator

A simple calculator package developed as an exemplary, educational project using a range of libraries and external tools


Keywords
typescript, test, testing, mocha, chai, travis, ci, codecov, code climate, snyk, example, code, coverage
License
MIT
Install
npm install example-calculator@0.7.2

Documentation

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.

Github license Open issues Closed issues Open Pull Requests Closed Pull Requests Commit activity Github contributors Last commit

Travis.CI build status Codecov Coverage Status Known Vulnerabilities Dependencies DevDependencies OptionalDependencies PeerDependencies Code Climate issues Code Climate maintainability Code Climate maintainability (percentage) Code Climate technical debt Code Climate coverage Code Climate coverage (letter)

NPM license Known Vulnerabilities

Actively maintained (2020) Chat on Gitter Blazing Fast Follow gfmio on Twitter code style: prettier All Contributors

Commitizen friendly

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 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
  • 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.

<script src="https://unpkg.com/example-calculator/example-calculator.umd.min.js"></script>

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 0
console.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 memory
calculator.add(2);
// Subtracts 1 from the current value in memory
calculator.sub(1);
// Multiplies the current value in memory with 2
calculator.mul(2);
// Divides the current value in memory by 2
calculator.div(2);
// Sets the value in memory to the modulus (remainder) of the current value and 3
calculator.mod(3);
// Sets the value in memory to the exponentiated current value
calculator.exp();
// Sets the value in memory to the natural logarithm of the current value
calculator.ln();
// Sets the value in memory to sine of the current value
calculator.sin();
// Sets the value in memory to cosine of the current value
calculator.cos();
// Sets the value in memory to tangent of the current value
calculator.tan();
// Sets the value to the inverse (1/x) of the current value
calculator.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().add(1).div(0).value();
// Returns 0
calculator.clear().add(1).mod(0).value();
// Returns 0
calculator.clear().sub(1).ln().value();
// Returns 0
calculator.clear().inverse().value();
// Returns 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 2
console.log(sub(1, 1));
// Prints 0
console.log(mul(2, 2));
// Prints 4
console.log(div(6, 3));
// Prints 2
console.log(mod(6, 4));
// Prints 2
console.log(ln(1));
// 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

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
Frédérique Mittelstaedt

📖 ⚠️ 💻

This project follows the all-contributors specification. Contributions of any kind welcome!