Simple and reliable math utility functions for JavaScript/Node.js projects.
You need to authenticate with GitHub Packages. Create a .npmrc file in your project root:
@petersawm:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=YOUR_GITHUB_TOKENnpm install @petersawm/math-utilsimport { add, subtract, multiply, divide } from '@petersawm/math-utils';
// Addition
console.log(add(5, 3)); // 8
// Subtraction
console.log(subtract(10, 4)); // 6
// Multiplication
console.log(multiply(6, 7)); // 42
// Division
console.log(divide(20, 4)); // 5
// Error handling
try {
divide(10, 0);
} catch (error) {
console.error(error.message); // "Cannot divide by zero"
}Returns the sum of two numbers.
Parameters:
a(number): First numberb(number): Second number
Returns: (number) Sum of a and b
Returns the difference of two numbers.
Parameters:
a(number): First numberb(number): Second number
Returns: (number) Difference of a and b
Returns the product of two numbers.
Parameters:
a(number): First numberb(number): Second number
Returns: (number) Product of a and b
Returns the quotient of two numbers.
Parameters:
a(number): Dividendb(number): Divisor
Returns: (number) Quotient of a and b
Throws: Error if divisor is zero
# Clone the repository
git clone https://github.com/petersawm/math-utils.git
cd math-utils
# Install dependencies
npm installnpm test-
Create a GitHub Personal Access Token:
- Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic)
- Click "Generate new token (classic)"
- Select scopes:
write:packages,read:packages,delete:packages - Copy the generated token
-
Set your token:
export GITHUB_TOKEN=your_token_here- Update
.npmrcwith your token:
@petersawm:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN}- Publish the package:
npm publish- Go to https://github.com/settings/tokens
- Click "Generate new token (classic)"
- Give it a descriptive name (e.g., "npm-packages")
- Select permissions:
- ✅
write:packages - ✅
read:packages - ✅
delete:packages(optional)
- ✅
- Click "Generate token"
- Copy the token immediately (you won't see it again!)
# Set as environment variable
export GITHUB_TOKEN=ghp_your_token_here
# Or add to .npmrc
echo "//npm.pkg.github.com/:_authToken=ghp_your_token_here" >> .npmrcAdd GITHUB_TOKEN as a secret in your repository settings.
MIT
Peter Sawm (@petersawm)
Contributions are welcome! Please feel free to submit a Pull Request.