diff --git a/README.md b/README.md index e55fb69..38f3b05 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,19 @@ import { Time } from "@itallonet/unit-converter"; const minuteToYearResult = Time(1000, { from: "min", to: "year" }); console.log(minuteToYearResult) + +``` + +- Frequency (Hz, mHz, kHz, MHz, GHz, THz, rpm, deg/s, rad/s) + +``` js + +import { Frequency } from "@itallonet/unit-converter"; + +const hertzToMegahertzResult = Frequency(1000, { from: "Hz", to: "MHz" }); +console.log(hertzToMegahertzResult) + + ``` ## Author @@ -79,7 +92,6 @@ console.log(minuteToYearResult) ## Roadmap -- Frequency - Speed - Pressure - Digital \ No newline at end of file diff --git a/RESULTS.md b/RESULTS.md index c4d95b7..115c7e8 100644 --- a/RESULTS.md +++ b/RESULTS.md @@ -96,4 +96,18 @@ Time(1000, { from: "ms", to: "h" }) execution time: 0.014ms Time(1000, { from: "ms", to: "d" }) execution time: 0.031ms Time(1000, { from: "ms", to: "year" }) execution time: 0.017ms +``` + +# Frequency Test + +```js + +Frequency(1000, { from: "Hz", to: "kHz" }) execution time: 0.106ms +Frequency(1000, { from: "Hz", to: "MHz" }) execution time: 0.018ms +Frequency(1000, { from: "Hz", to: "GHz" }) execution time: 0.017ms +Frequency(1000, { from: "Hz", to: "THz" }) execution time: 0.017ms +Frequency(1000, { from: "Hz", to: "rpm" }) execution time: 0.018ms +Frequency(1000, { from: "Hz", to: "deg/s" }) execution time: 0.015ms +Frequency(1000, { from: "Hz", to: "rad/s" }) execution time: 0.018ms + ``` \ No newline at end of file diff --git a/__test.js b/__test.js index 6372623..55a6142 100644 --- a/__test.js +++ b/__test.js @@ -1,4 +1,4 @@ -const { Length, Area, Mass, Volume, Temperature, Time } = require("./module") +const { Length, Area, Mass, Volume, Temperature, Time, Frequency } = require("./module") const __test = () => { console.info("Init tests!") @@ -139,8 +139,25 @@ const __test = () => { console.log(`Time(1000, { from: "${test.from}", to: "${test.to}" }) = ${result}`); }); - //todo - // test time + console.info("Frequency") + + tests = [ + { from: "Hz", to: "kHz" }, + { from: "Hz", to: "MHz" }, + { from: "Hz", to: "GHz" }, + { from: "Hz", to: "THz" }, + { from: "Hz", to: "rpm" }, + { from: "Hz", to: "deg/s" }, + { from: "Hz", to: "rad/s" }, + ] + + tests.forEach((test) => { + const label = `Frequency(1000, { from: "${test.from}", to: "${test.to}" }) execution time`; + console.time(label); + const result = Frequency(1000, test); + console.timeEnd(label); + console.log(`Frequency(1000, { from: "${test.from}", to: "${test.to}" }) = ${result}`); + }); } __test(); \ No newline at end of file diff --git a/module.js b/module.js index 1991090..ea52acd 100644 --- a/module.js +++ b/module.js @@ -1,5 +1,9 @@ -const Length = require("./source/length"), Area = require("./source/area"), -Mass = require("./source/mass"), Volume = require("./source/volume"), -Temperature = require("./source/temperature"), Time = require("./source/time"); - -module.exports = { Length, Area, Mass, Volume, Temperature, Time } \ No newline at end of file +module.exports = { + Length: require("./source/length"), + Area: require("./source/area"), + Mass: require("./source/mass"), + Volume: require("./source/volume"), + Temperature: require("./source/temperature"), + Time: require("./source/time"), + Frequency: require("./source/frequency") +} \ No newline at end of file diff --git a/package.json b/package.json index c2d2fca..275bf81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@itallonet/unit-converter", - "version": "0.6.0", + "version": "0.7.0", "description": "Simple and easy to use unit converter for javascript in general", "main": "module.js", "scripts": { diff --git a/source/frequency.js b/source/frequency.js new file mode 100644 index 0000000..f6218be --- /dev/null +++ b/source/frequency.js @@ -0,0 +1,19 @@ +const Converter = require("./converter"); + +function Frequency(input, options) { + const conversionFactors = { + Hz: 1, + mHz: 1e-3, + kHz: 1e3, + MHz: 1e6, + GHz: 1e9, + THz: 1e12, + rpm: 1 / 60, + "deg/s": 1 / 360, + "rad/s": 1 / (2 * Math.PI) + }; + + return Converter(input, options, conversionFactors); +} + +module.exports = Frequency;