Units Converter

A library for unit conversions.

Stan na 27-11-2024. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://updategf.qytechs.cn/scripts/519002/1492059/Units%20Converter.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

Autor
hacker09
Wersja
1
Utworzono
27-11-2024
Zaktualizowano
27-11-2024
Rozmiar
2,75 KB
Licencja
Brak licencji

Here’s the corrected markdown after ensuring proper formatting and content flow:


To always have the latest version use:

// @require      https://update.greasyfork.org/scripts/519002/Units%20Converter.js

Unit Conversion Library Documentation

Overview

This library provides a simple framework to convert units of measurement for the script https://greasyfork.org/en/scripts/419825. It supports various unit types (e.g., length, weight, volume, temperature) and handles conversions with single or compound values.

To see the latest const Units regex and a real usage scenario check https://greasyfork.org/en/scripts/419825.

Features

  1. Unit Conversion: Converts a numeric value from one unit to another.
  2. Compound Conversions: Handles cases with two values (e.g., "10x20 inches").
  3. Exponential Operations: Supports calculations like powers (e.g., "5^3").
  4. Unit Recognition: Uses a regex pattern to identify units and values from text input.

Usage

Example Input

"150 pounds" → Outputs: "68.03 kg"
"10x20 inches" → Outputs: "25.40 x 50.80 cm"
"5^3" → Outputs: "125 power"

Main Functions

  1. Regex Matching for Units

    const UnitType = "150 pounds".match(Units)[3].toLowerCase(); // Extracts the unit type
    
  2. Value Extraction

    const Units = new RegExp(/^[ \t\xA0]*(-?\d+(?:[., ]\d+)?)(?:[ \t\xA0]*x[ \t\xA0]*(-?\d+(?:[., ]\d+)?))?[ \t\xA0]*(in|inch|inches|"|”|″|cm|cms|centimeters?|m|mt|mts|meters?|ft|kg|lbs?|pounds?|kilograms?|ounces?|g|ozs?|fl oz|fl oz (us)|fluid ounces?|kphs?|km\/h|kilometers per hours?|mhp|mphs?|meters per hours?|(?:°\s?|º\s?|)(?:degrees?\s+)?(?:celsius|fahrenheit|[CF])|km\/hs?|ml|milliliters?|l|liters?|litres?|gal|gallons?|yards?|yd|Millimeter|millimetre|kilometers?|mi|mm|miles?|km|ft|fl|feets?|grams?|kilowatts?|kws?|brake horsepower|mechanical horsepower|hps?|bhps?|miles per gallons?|mpgs?|liters per 100 kilometers?|lt?\/100km|liquid quarts?|lqs?|qt|foot-? ?pounds?|ft-?lbs?|lb fts?|newton-? ?meters?|n·?m|\^\d+)[ \t\xA0]*(?:\(\w+\)[ \t\xA0]*)?$/i); //https://regex101.com/r/ObpIn5/9 Davidebyzero
    
    const UnitValue = "150 pounds".match(Units)[1].replaceAll(',', '.'); // Extracts numeric value
    const SecondUnitValue = "150 pounds".match(Units)[2]?.replaceAll(',', '.') || 0; // Extracts second value if present
    
  3. Value Conversion

    const convertValue = (value, unitType) => {
       const { factor, convert } = window.UConv[unitType] || {};
       return convert ? convert(value) : value * factor; // Converts value using factor or custom function
    };
    
  4. New Unit Detection

    const NewUnit = window.UConv[UnitType]?.unit || UnitType; // Detects target unit
    
  5. Final Conversion Output

    const ConvertedUnit = `${convertValue(parseFloat(UnitValue), UnitType).toFixed(2)}${SecondUnitValue != 0 ? ` x ${convertValue(parseFloat(SecondUnitValue), UnitType).toFixed(2)}` : ''}`; // Formats output
    
  6. Special Cases

    const ConvertedUnit = "5^3".match(/\^(\d+\.?\d*)/) 
       ? (NewUnit = 'power', Math.pow(parseFloat(UnitValue), parseFloat("5^3".match(/\^(\d+\.?\d*)/)[1]))) 
       : ConvertedUnit; // Handles powers
    
  7. Display Conversion

    ShowConvertion('Units', NewUnit, ConvertedUnit); // Displays result
    

Adding New Units

To add new units, modify the addConversion function in the UConv library:

addConversion(['unit1', 'unit2'], 'targetUnit', conversionFactor, optionalConvertFunction);

Example:

addConversion(['pound', 'lb'], 'kg', 0.453592); // Adds "pound" conversion to kilograms

How It Works

  1. Input Parsing: Uses regex to extract unit type and numeric values from text input.
  2. Conversion: Fetches the appropriate conversion factor or custom function from UConv.
  3. Output: Returns the converted value in the desired format, supporting both compound and exponential cases.