{"id":26974260,"url":"https://github.com/iamsquare/convert-units","last_synced_at":"2025-07-31T02:12:27.282Z","repository":{"id":42637048,"uuid":"372089744","full_name":"iamsquare/convert-units","owner":"iamsquare","description":"A handy utility for converting between quantities in different units","archived":false,"fork":false,"pushed_at":"2023-01-07T07:55:37.000Z","size":1254,"stargazers_count":2,"open_issues_count":7,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-03T22:43:27.352Z","etag":null,"topics":["conversion","convert","enums","imperial","metric","metric-units","ratio","utility"],"latest_commit_sha":null,"homepage":"https://iamsquare.it/convert-units","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iamsquare.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-29T23:54:57.000Z","updated_at":"2022-10-04T20:20:46.000Z","dependencies_parsed_at":"2023-02-06T15:15:36.625Z","dependency_job_id":null,"html_url":"https://github.com/iamsquare/convert-units","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/iamsquare/convert-units","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsquare%2Fconvert-units","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsquare%2Fconvert-units/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsquare%2Fconvert-units/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsquare%2Fconvert-units/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamsquare","download_url":"https://codeload.github.com/iamsquare/convert-units/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamsquare%2Fconvert-units/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267977443,"owners_count":24175158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conversion","convert","enums","imperial","metric","metric-units","ratio","utility"],"created_at":"2025-04-03T10:32:49.544Z","updated_at":"2025-07-31T02:12:27.263Z","avatar_url":"https://github.com/iamsquare.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# iamsquare/convert-units\n\n[![NPM](https://img.shields.io/npm/v/@iamsquare/convert-units.svg?style=flat-square)](https://www.npmjs.com/package/@iamsquare/convert-units) [![Coverage](https://img.shields.io/coveralls/github/iamsquare/convert-units/master?style=flat-square)](https://coveralls.io/github/iamsquare/convert-units) [![GitHub issues](https://img.shields.io/github/issues-raw/iamsquare/convert-units.svg?style=flat-square)](https://github.com/iamsquare/complex.js/issues) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@iamsquare/convert-units?style=flat-square) [![GitHub](https://img.shields.io/github/license/iamsquare/convert-units?style=flat-square)](https://github.com/iamsquare/convert-units/blob/master/LICENSE)\n\n\u003e A handy utility for converting between quantities in different units.\n\n## Installation\n\n```shell\nnpm i @iamsquare/convert-units\n```\n\n## Usage\n\n`@iamsquare/convert-units` is a functional port of [`convert-units`](https://www.npmjs.com/package/convert-units) with extended type support.\n\nYou start by instancing a `Converter`:\n\n```js\nimport { Converter } from '@iamsquare/convert-units';\n\nconst converter = new Converter({ measuresData: { ... }});\n```\n\nIf you want to use all measure types, just instance `Converter` like so:\n\n```js\nimport { allMeasures } from '@iamsquare/convert-units';\n\nconst converter = new Converter({ measuresData: allMeasures }); // TMeasures =\u003e AllMeasures, TSystems =\u003e AllSystems, TUnitTypes =\u003e AllUnitType\n```\n\nIf you want to use a subset of all measure type you must declare `TMeasures`, `TSystems` and `TUnitTypes` explicitly (typescript can't infer types on its own if they get too complex. **e.g.**: _two measure types with different systems_):\n\n```js\nimport {\n  Converter,\n  area,\n  AreaSystems,\n  AreaUnits,\n  distance,\n  DistanceSystems,\n  DistanceUnits,\n} from '@iamsquare/convert-units';\n\ntype Measures = 'area' | 'distance';\ntype Systems = AreaSystems | DistanceSystems;\ntype Units = AreaUnits | DistanceUnits;\n\nconst converter = new Converter\u003cMeasures, Systems, Units\u003e({\n  area,\n  distance\n});\n```\n\nHere's how you convert metric units for volume:\n\n```js\nimport { convert } from '@iamsquare/convert-units';\n\nconvert(converter, 'l', 'ml', 1);\n// 1000\n```\n\nJump from imperial to metric units the same way:\n\n```js\nconvert(converter, 'lb', 'kg', 1);\n// 0.4536... (tested to 4 significant figures)\n```\n\nJust be careful not to ask for an impossible conversion!\n\n```js\nconvert(converter, 'oz', 'fl-oz', 1);\n// throws exception -- you can't go from mass to volume!\n```\n\nYou can ask to select the best unit for you. Optionally you can explicitly exclude orders of magnitude or specify a cutoff number for selecting the best representation.\n\n```js\nconvertToBest(converter, {}, 'mm', 12000);\n// { value: 12, unitType: 'm' } (the smallest unit with a value above 1)\n\nconvertToBest(converter, { exclude: ['m'] }, 'mm', 12000);\n// { value: 1200, unitType: 'cm' } (the smallest unit excluding meters)\n\nconvertToBest(converter, { cutOffNumber: 10 }, 'mm', 900);\n// { value: 900, unitType: 'cm' } (the smallest unit with a value equal to or above 10)\n\nconvertToBest(converter, { cutOffNumber: 10 }, 'mm', 1000);\n// { value: 10, unitType: 'm' } (the smallest unit with a value equal to or above 10)\n```\n\nYou can get a list of the measurement types supported with `measures`\n\n```js\nmeasures(converter);\n// [ 'distance', 'mass', 'volume', ... ]\n```\n\nIf you ever want to know the possible conversions for a _unit_, just use `possibilities`\n\n```js\nimport { possibilities } = from '@iamsquare/convert-units';\n\npossibilities(converter, 'l');\n// [ 'ml', 'l', 'tsp', 'Tbs', 'fl-oz', 'cup', 'pnt', 'qt', 'gal' ]\n\npossibilities(converter, 'kg');\n// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb' ]\n```\n\nYou can also get the possible conversions for a _measure_:\n\n```js\npossibilities(converter, 'mass');\n// [ 'mcg', 'mg', 'g', 'kg', 'oz', 'lb', 'mt', 't' ]\n```\n\nYou can also get the all the available units:\n\n```js\npossibilities(converter);\n// [ 'mm', 'cm', 'm', 'in', 'ft-us', 'ft', 'mi', ... ];\n```\n\nTo get a detailed description of a unit, use `describe`\n\n```js\ndescribe(converter, 'kg');\n```\n\n```js\n{\n  unitType: 'kg',\n  measure: 'mass',\n  system: 'metric',\n  name: {\n    singular: 'SINGULAR_KILOGRAM',\n    plural: 'PLURAL_KILOGRAM'\n  }\n}\n```\n\nTo get detailed descriptions of all units, use `list`.\n\n```js\nlist(converter);\n```\n\n```js\n[\n  {\n    unitType: 'kg',\n    measure: 'mass',\n    system: 'metric',\n    name: {\n      singular: 'SINGULAR_KILOGRAM',\n      plural: 'PLURAL_KILOGRAM'\n    }\n  },\n  ...\n]\n```\n\nYou can also get detailed description of all units for a measure:\n\n```js\nlist(converter, 'mass');\n```\n\n```js\n[\n  {\n    unitType: 'kg',\n    measure: 'mass',\n    system: 'metric',\n    name: {\n      singular: 'SINGULAR_KILOGRAM',\n      plural: 'PLURAL_KILOGRAM'\n    }\n  },\n  ...\n]\n```\n\n**NOTE**: By default the i18n module is _not_ active so until a translation dictionary is provided `list` and `describe` will return the raw dictionary key instead.\n\n### Documentation\n\nFor a more in-depth documentation take a look [`here`](http://iamsquare.it/convert-units)\n\n## Supported Units\n\n| **_Measure_** | **_Metric_** | **_Imperial_** | **_Other_** |\n|:-:|-|-|-|\n| _Area_ | mm2, cm2, m2, ha, km2 | in2, ft2, ac, mi2 | - |\n| _Acceleration_ | g-force, m/s2 | - | - |\n| _Angle_ | - | - | deg, rad, grad, arcmin, arcsec |\n| _Apparent Power_ | - | - | VA, mVA, kVA, MVA, GVA |\n| _Charge_ | - | - | c, mC, μC, nC, pC |\n| _Current_ | - | - | A, mA, kA |\n| _Digital_ | - | - | b, Kb, Mb, Gb, Tb, B, KB, MB, GB, TB |\n| _Energy_ | - | - | Wh, mWh, kWh, MWh, GWh, J, kJ |\n| _Force_ | N, kN | lbf | - |\n| _Frequency_ | - | - | Hz, mHz, kHz, MHz, GHz, THz, rpm, deg/s, rad/s |\n| _Illuminance_ | lx | ft-cd | - |\n| _Length_ | nm, μm, mm, cm, m, km | in, yd, ft-us, ft, fathom, mi, nMi | - |\n| _Mass_ | mcg, mg, g, kg, mt | oz, lb, t | - |\n| _Pace_ | s/m, min/km | s/ft, min/mi | - |\n| _Parts-Per_ | - | - | ppm, ppb, ppt, ppq |\n| _Pieces_ | - | - | pcs, bk-doz, cp, doz-doz, doz, gr-gr, gros, half-dozen, long-hundred, ream, scores, sm-gr, trio |\n| _Power_ | W, mW, kW, MW, GW, PS | Btu/s, ft-lb/s, hp | - |\n| _Pressure_ | Pa, hPa, kPa, MPa, bar, torr | psi, ksi | - |\n| _Reactive Energy_ | - | - | VARh, mVARh, kVARh, MVARh, GVARh |\n| _Reactive Power_ | - | - | VAR, mVAR, kVAR, MVAR, GVAR |\n| _Speed_ | m/s, km/h | mph, knot, ft/s | - |\n| _Temperature_ | C, K | F, R | - |\n| _Time_ | - | - | ns, mu, ms, s, min, h, d, week, month, year |\n| _Voltage_ | - | - | V, mV, kV |\n| _Volume_ | mm3, cm3, ml, l, kl, m3, km3 | tsp, Tbs, in3, fl-oz, cup, pnt, qt, gal, ft3, yd3 | - |\n| _Volume Flow Rate_ | mm3/s, cm3/s, ml/s, cl/s, dl/s, l/s, l/min, l/h, kl/s, kl/min, kl/h, m3/s, m3/min, m3/h, km3/s | tsp/s, Tbs/s, in3/s, in3/min, in3/h, fl-oz/s, fl-oz/min, fl-oz/h, cup/s, pnt/s, pnt/min, pnt/h, qt/s, gal/s, gal/min, gal/h, ft3/s, ft3/min, ft3/h, yd3/s, yd3/min, yd3/h | - |\n\n**NOTE**: these units are exported as enums so that you don't have to remember which unit is which. Also, if you're using this library in a plain `javascript` environment these are useful to avoid annoying typos.\n\nYou can check the list of these enums in the [`documentation`](https://iamsquare.it/convert-units).\n\n### Want More?\n\nAdding new measurement types is easy. Check one of these [`definitions`](https://github.com/iamsquare/convert-units/tree/master/src/definitions) or [`unit tests`](https://github.com/iamsquare/convert-units/tree/master/src/__tests__/customType.test.ts) to see how they are implemented. When you're done you can import them just like any other measure provided by the library:\n\n```js\nimport {\n  Converter,\n  distance,\n  DistanceSystems,\n  DistanceUnits,\n} from '@iamsquare/convert-units';\n\nimport {\n  customMeasure,\n  CustomMeasureSystems,\n  CustomUnits\n} from './customMeasure';\n\ntype Measures = 'customMeasure' | 'distance';\ntype Systems = CustomMeasureSystems | DistanceSystems;\ntype Units = CustomUnits | DistanceUnits;\n\nconst converter = new Converter\u003cMeasures, Systems, Units\u003e({\n  customMeasure,\n  distance\n});\n```\n\nFeel free to open a PR if you think that your custom definition should be added to the library!\n\n## i18n\n\nEach `Converter` instance can be initialized with an additional `translations` property.\nFor example if you want to use all the measure types and their respective translations, just instance `Converter` like so:\n\n```js\nimport { Converter, allMeasures, allTranslations } from '@iamsquare/convert-units';\n\nconst converter = new Converter({ measureData: allMeasures, translations: allTranslations });\n```\n\nOr if you only need a subset of measures:\n\n```js\nimport {\n  Converter,\n  area,\n  areaTranslations,\n  AreaSystems,\n  AreaUnits,\n  distance,\n  distanceTranslations,\n  DistanceSystems,\n  DistanceUnits\n} from '@iamsquare/convert-units';\n\ntype Measures = 'area' | 'distance';\ntype Systems = AreaSystems | DistanceSystems;\ntype Units = AreaUnits | DistanceUnits;\n\nconst converter = new Converter\u003cMeasures, Systems, Units\u003e({ measureData: { area, distance }, translations: { ...areaTranslations, ...distanceTranslations }});\n```\n\nThe `translationModule` inside the `Converter` class exposes three methods:\n\n```javascript\nconverter.translationModule.setTranslations({ ... })\nconverter.translationModule.mergeTranslations({ ... })\n```\n\nBoth accept a JSON containing key-value pairs, where the key is a valid [`translationKey`](https://www.iamsquare.it/convert-units/modules.html#translationkey) and the value is a `string`. The first one overwrites the current translations dictionary, the second merges the two dictionaries together.\n\n```javascript\nconverter.translationModule.resetTranslations();\n```\n\nResets the translations dictionary to its original state (when the class has been instanced).\n\n```javascript\nconverter.translationModule.getTranslationByKey(key);\n```\n\nReturns the translation for a given key (or the key itself in case it's undefined in the dictionary).\n\n## Dependencies\n\n\u003e This library depends on [`ramda`](https://github.com/ramda/ramda) and [`ramda-extension`](https://github.com/tommmyy/ramda-extension).\n\nThese imports are _**not** tree-shaken_, so if you're bundling this library in a project please refer to your bundler's documentation.\nNote that _Rollup_ and _Webpack_ should handle tree-shaking without extra-effort.\n\n## Roadmap\n\nCheck the kanban board [`here`](https://github.com/orgs/iamsquare/projects/1)\n\n## Development\n\nIf you want to help remember to run the command `npm run setup:dev`.\nIt will install all dependencies and [`husky`](https://www.npmjs.com/package/husky) so that code will get tested and linted before every commit.\n\n## License\n\nCopyright (c) 2021 Marco Cesi, \u003chttps://iamsquare.it\u003e\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamsquare%2Fconvert-units","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamsquare%2Fconvert-units","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamsquare%2Fconvert-units/lists"}