Greasy Fork

Conversion Utility Library

A library for unit conversions.

目前为 2024-11-27 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.cloud/scripts/519002/1492042/Conversion%20Utility%20Library.js

作者
hacker09
版本
1
创建于
2024-11-27
更新于
2024-11-27
大小
2.8 KB
许可证
暂无

To always have the latest version use // @require https://update.greasyfork.org/scripts/519002/Units%20Converter.js

Documentation for the library and its usage:


Unit Conversion Library Documentation

Overview

This library provides a simple framework to convert units of measurement. It supports various unit types (e.g., length, weight, volume, temperature) and handles conversions with single or compound values.

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(); //Change "150 pounds" to the element that contains the string
    
    • Extracts the unit type from user input.
  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
    

const UnitValue = "150 pounds".match(Units)[1].replaceAll(',', '.'); //Change "150 pounds" to the element that contains the string const SecondUnitValue = "150 pounds".match(Units)[2]?.replaceAll(',', '.') || 0; //Change "150 pounds" to the element that contains the string

   - Extracts numeric values (first and second, if available).

3. **Value Conversion**
   ```js
   const convertValue = (value, unitType) => {
       const { factor, convert } = window.UConv[unitType] || {};
       return convert ? convert(value) : value * factor;
   };
  • Converts a value from one unit to another using a predefined conversion map.
  1. New Unit Detection

    var NewUnit = window.UConv[UnitType]?.unit || UnitType;
    
    • Determines the target unit for conversion.
  2. Final Conversion Output

    var ConvertedUnit = `${convertValue(parseFloat(UnitValue), UnitType).toFixed(2)}${SecondUnitValue != 0 ? ` x ${convertValue(parseFloat(SecondUnitValue), UnitType).toFixed(2)}` : ''}`;
    
    • Produces the final converted value(s).
  3. Special Cases

    ConvertedUnit = "5^3".match(/\^(\d+\.?\d*)/) 
       ? (NewUnit = 'power', Math.pow(parseFloat(UnitValue), parseFloat(Text.match(/\^(\d+\.?\d*)/)[1]))) 
       : ConvertedUnit;
    
    • Handles exponential calculations like 5^3.
  4. Display Conversion

    ShowConvertion('Units', NewUnit, ConvertedUnit);
    
    • Outputs the conversion 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);

How It Works

  1. Input Parsing: The script uses regex to extract the unit type and numeric values from text input.
  2. Conversion: It looks up the appropriate conversion factor or custom conversion function from UConv.
  3. Output: Converts the values and returns the result in the desired format, supporting compound and exponential calculations.