npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

weight-module-alexander-rolf

v1.0.1

Published

A module for converting weight units and handling the weights in a manager, all code is written in Javascript.

Downloads

147

Readme

Weight Module by Alexander Rolf

Introduction to the module

Weight Module - Alexander Rolf is a module developed in 100% Javascript which is used to convert different weight-units and to manage them in a WeightManager. This is the first version of the module and may be developed further in the future to be even more useful for developers when wanting to convert and manage different units of weights.

Installation

Please note that you cannot use an older version of Node than 14. Please check your version of Node.js in your terminal.

node -v

The module uses ES-modules and you will be needing version 14 or newer of Node. To install the module simply open a new Terminal-window and enter:

npm install weight-module-alexander-rolf

Using the module

To use my Weight Module it is required that you import the classes to start start using it:

Import the modules classes, funtions and the object with the following import statement:

import { Weight, WeightManager, convertUnit, unitValidator, weightUnits } from 'weight-module-alexander-rolf

Now you can do different things with my module and I will showcase different use cases for it below. I will start with some practical examples of the Weight-class, followed by the WeightManager-class. These are just some examples so that you can try the module quick and easy, to find more detailed documentation of the classes and the methods you can look at the section below named "Classes and their methods". There is a lot you can do with the weights in this module, for example adding, subtracting, multiplying and dividing but I won't be showing all of this in this section. Therefor I recommend that you read the "Classes and their methods" section and experiment further!

Creating a weight:

const yourCreatedWeight = new Weight(10, 'kg')

This will create a new instance of weight which translates to 10 kilograms.

Here is an example of how you would convert that into grams.

Converting a weight:

const yourConvertedWeight = yourCreatedWeight.convert('g')

This will then convert your 10 kilograms into 10000 grams.

An example of mulitplying a weight Here you can see an example of how you could multiply a weight by any number of times. Let's say we want to multiply our weight by 3:

const yourCreatedWeight = new Weight(3, 'kg')                  // Create a weight.
const yourMultipliedWeight = yourCreatedWeight.multiply(3)     // Multiply the weight using the multiply method.
console.log(yourMultipliedWeight.toString())                   // Will give the output of "9 kg".

An example of comparing weights: You can for example see if a weight is lighter than another weight. To do that you can use the isLighterThan method:

if (yourCreatedWeight.isLighterThan(yourOtherWeight))

In the example above you check if yourCreatedWeight is lighter than yourOtherWeight. The same goes for isHeavierThan-method.

Using WeightManager The WeightManager allows for you to handle multiple weights, you can see it as a collection where you can put the wights and manage/handle them. Here is how it is made:

const yourCreatedManager = new WeightManager()

Here you can use the addWeight method to add it to the manager, and maybe you want to get the lightest weight? See below for a demonstration:

yourCreatedManager.addWeight(weight1)
yourCreatedManager.addWeight(weight2)
yourCreatedManager.addWeight(weight3)
yourCreatedManager.addWeight(weight4)
const yourLightestWeight = yourCreatedManager.getLightestWeight()

You can then simply console.log the 'yourLightestWeight' variable and be presented with the lightest one out of your four weights from the example above.


I hope you found these examples useful and please check out the next section for more detailed classes and methods!

Classes and their Methods

Class #1 - Weight

The first class present in the module is the Weight-class and it is presented below with its methods. With the methods you can do a lot of different things e.g convert, manipulate and create weights.

The Class Name:

Weight

Parameters:

  • weight (number) - Represents the weight and has to be a number.
  • weightUnit (string) - Represents the unit of the weight e.g 'g' or 'lb'
  • Throws error if the parameters are invalid. E.g weight is not a valid number och weightUnit is not a valid unit of weight.

Class #2 - WeightManager

The second class present in the module is the WeightManager-class which works as like a manager for a collection of different instances of Weight. Further down the page you can explore more about what these methods can do!

The Class Name:

WeightManager Returns: A new WeightManager instance.

Methods for Class #1 - Weight

(1.) convert(newWeightUnit) - This method simply converts a weight to a new unit.

Parameters: newWeightUnit (string) - This is the new unit that we want to convert our weight into. What it returns: It resturns the converted weight in a new instance of Weight.

  • Throws error if the parameter newWeightUnit is not a valid unit of weight.

(2.) add(otherWeight) - This method adds a weight to your weight.

Parameters: otherWeight(Weight) - The other weight that you are adding. What it returns: Total weight in a new instance of Weight.

  • Throws error if the parameter otherWeight is not a valid instance of Weight.

(3.) subtract(otherWeight) - This method subtracts another weight from your weight.

Parameters: otherWeight(Weight) - The other weight that you are subtracting. What it returns: A new instance of weight with the subtracted weuight value.

  • Throws error if the parameter otherWeight is not a valid instance of Weight.

(4.) toString() - This method returns a string with the weight and the weightunit.

What it returns: Method returns a string where the weight comes first followed by the unit. E.g "5 kg"


(5.) divide(number) - This method divides the weight.

Parameters: number (number) - The number to divide with. What it returns: A new instance of weight with the divided wuight value.

  • Throws error if the parameter is 0 or invalid.

(6.) multiply(number) - This method multiplies the weight.

Parameters: number (number) - The number to mulitply with. What it returns: A new instance of weight with the multiplied weight value.

  • Throws error if the parameter is invalid.

(7.) isLighterThan(otherWeight) - This method compares if your weight is lighter than the other weight you are comparing it with.

Parameters: otherWeight (Weight) - Weight that will be compared. What it returns: Returns a boolean. True if this weight is lighter, false if not.

  • Throws error if the parameter otherWeight is not a valid instance of Weight.

(8.) hasSameWeightAs(otherWeight) - This method checks if your weight is equal to another weight of your choosing.

Parameters: otherWeight (Weight) - Weight that will be compared. What it returns: Returns a boolean. True if this weight is equal, false if not.

  • Throws error if the parameter otherWeight is not a valid instance of Weight.

(9.) isHeavierThan(otherWeight) - This method compares if your weight is heavier than the other weight you are comparing it with.

Parameters: otherWeight (Weight) - Weight that will be compared. What it returns: Returns a boolean. True if this weight is heavier, false if not.

  • Throws error if the parameter otherWeight is not a valid instance of Weight.

(10.) fromTextInput(userInput) - This method parses a string from e.g a user and creates a new instance of Weight. E.g "5 kg".

Parameters: userInput (string) - The string that will be parsed. What it returns: A new instance of weight.

  • Throws error if either the unit is not valid or the format of the input is invalid.

Methods for Class #2 - WeightManager

(1.) removeWeight(weight) - This method removes a instance of weight from the manager/collection.

Parameters: weight (Weight) - Weight that you want to remove. What it returns: Returns a boolean. True if removed, false if not.


(2.) addWeight(weight) - This method adds a instance of weight to the manager/collection.

Parameters: weight (Weight) - Weight that you want to add.

  • Throws error if the parameter weight is not a valid instance of Weight.

(3.) numberOfWeights() - Gets you the number of weights in the manager/collection.

What it returns: The actual number/count of weights that are in the manager/collection.


(4.) getTotalWeight(weightUnit = 'g') - Sums up and calculate total weight in specified unit, grams by default.

Parameters: weightUnit (string) - The weight unit that you want thew total weight in. What it returns: A new instance of weight with the total weight and specified unit.

  • Throws error if the parameter is a invalid unit.

(5.) getAverageWeight(weightUnit = 'g') - Gets you the average weight in the unit you specifiy, grams by default.

What it returns: A new instance of weight with average weight.

  • Throws error if there are no weights in the manager/collection or if the parameter is a invalid unit.

(6.) getHeaviestWeight() - This method returns the heaviest instance of Weight in the collection.

What it returns: The instance of weight that is the heaviest.

  • Throws error if there are no weights in the manager/collection.

(7.) getLightestWeight() - This method returns the lightest instance of Weight in the collection.

What it returns: The instance of weight that is the lightest.

  • Throws error if there are no weights in the manager/collection.

(8.) mergeManagersData(otherWeightManager) - This method merges the weights in the other instance WeightManager to this instance of WeightManager.

Parameters: otherWeightManager (WeightManager) - Another instance of WeightManager that contains the weights you want to merge.

  • Throws error if otherWeightManager is not a instance of WeightManager.

(9.) eraseAllWeights() - This method erases all the weights in the manager/collection.


Running the tests

NOTE: You will need npm and Node.js, as well as Jest as a devdependency.

Follow the steps below in order to run the tests:

Step 1: Clone the repository

git clone https://github.com/alexanderLNU/weight-module-alexander-rolf.git

Step 2: Correct path

cd weight-module-alexander-rolf

Step 3: Install

npm install

Step 4: Run

npm test

License for the code

This project uses the MIT-license and can be found in the LICENCE file.

Closing words

Thanks for checking out my module! I had a lot of fun making this and I hope it will be useful for someone even though it is not very complex!