{"id":20529537,"url":"https://github.com/haimkastner/unitsnet-js","last_synced_at":"2025-04-07T07:09:11.346Z","repository":{"id":36766872,"uuid":"229628504","full_name":"haimkastner/unitsnet-js","owner":"haimkastner","description":"A better way to hold unit variables and easily convert to the destination unit","archived":false,"fork":false,"pushed_at":"2025-03-24T20:44:17.000Z","size":2940,"stargazers_count":26,"open_issues_count":3,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-31T06:08:42.868Z","etag":null,"topics":["conversion","converter","measure","measurement","measurements","measures","unit","unit-converter","units","units-of-measure"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/unitsnet-js","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/haimkastner.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-22T20:43:53.000Z","updated_at":"2025-03-24T20:43:59.000Z","dependencies_parsed_at":"2023-12-19T08:48:25.542Z","dependency_job_id":"f4866841-a724-40ed-b4f4-91dffc86044a","html_url":"https://github.com/haimkastner/unitsnet-js","commit_stats":{"total_commits":269,"total_committers":6,"mean_commits":"44.833333333333336","dds":"0.32713754646840154","last_synced_commit":"6be0dbde75ef3abb2cd3a83d0bd88aa3c13090a9"},"previous_names":[],"tags_count":152,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/haimkastner%2Funitsnet-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/haimkastner","download_url":"https://codeload.github.com/haimkastner/unitsnet-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247608151,"owners_count":20965952,"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","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","converter","measure","measurement","measurements","measures","unit","unit-converter","units","units-of-measure"],"created_at":"2024-11-15T23:32:41.686Z","updated_at":"2025-04-07T07:09:11.325Z","avatar_url":"https://github.com/haimkastner.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# unitsnet-js\n\n[![unitsnet-js](https://github.com/haimkastner/unitsnet-js/actions/workflows/nodejs.yml/badge.svg?branch=master)](https://github.com/haimkastner/unitsnet-js/actions/workflows/nodejs.yml)\n[![Coverage Status](https://coveralls.io/repos/github/haimkastner/unitsnet-js/badge.svg?branch=master\u0026style=plastic)](https://coveralls.io/github/haimkastner/unitsnet-js?branch=master)\n\n [![Latest Release](https://img.shields.io/github/v/release/haimkastner/unitsnet-js?style=plastic)](https://github.com/haimkastner/unitsnet-js/releases) \n [![npm version](https://img.shields.io/npm/v/unitsnet-js.svg?style=plastic\u0026label=npm)](https://www.npmjs.com/package/unitsnet-js)\n\n[![GitHub stars](https://img.shields.io/github/stars/haimkastner/unitsnet-js.svg?style=social\u0026label=Stars)](https://github.com/haimkastner/unitsnet-js/stargazers) \n[![npm downloads](https://img.shields.io/npm/dt/unitsnet-js.svg?style=social)](https://www.npmjs.com/package/unitsnet-js)\n[![License](https://img.shields.io/github/license/haimkastner/unitsnet-js.svg?style=social)](https://github.com/haimkastner/unitsnet-js/blob/master/LICENSE) \n\nThe unitsnet-js package provides an efficient way to store unit variables and perform easy conversions to different units when it required. \n\nIt offers support for more than 100 unit types across various unit categories, including pretty-printing, comparison, and arithmetic methods. \n\nThe API is designed to be user-friendly and straightforward to use.\n\nThe library is built on top of the [Units.NET](https://github.com/angularsen/UnitsNet) project and leverages their [definitions sources](https://github.com/angularsen/UnitsNet/tree/master/Common/UnitDefinitions) to generate the JavaScript unit classes.\n\n\n###### The unitsnet-js package does not require any external dependencies or packages to function.\n\nPackage is available on NPM at https://www.npmjs.com/package/unitsnet-js\n\n[Units.NET on other platforms](#unitsnet-on-other-platforms)\n\n## Install via NPM:\n\n```bash \n\nnpm install unitsnet-js\n\n```\n\n## Example Usage in TypeScript\n\n```typescript\nimport { Angle, AngleUnits, Length, LengthUnits } from 'unitsnet-js';\n\n\nlet angle = Angle.FromDegrees(180);\n// equals to\nangle =  new Angle(180, AngleUnits.Degrees);\n\nconsole.info(angle.Radians); // 3.141592653589793\nconsole.info(angle.Microradians); // 3141592.65358979\nconsole.info(angle.Gradians); // 200\nconsole.info(angle.Microdegrees); // 180000000\n\n// As an alternative, a convert style method are also available\nconsole.info(angle.convert(AngleUnits.Radians)); // 3.141592653589793\nconsole.info(angle.convert(AngleUnits.Microradians)); // 3141592.65358979\nconsole.info(angle.convert(AngleUnits.Gradians)); // 200\nconsole.info(angle.convert(AngleUnits.Microdegrees)); // 180000000\n\n// Print the default unit toString (The default for angle is degrees)\nconsole.info(angle.toString()); // 180 °\n\nconsole.info(angle.toString(AngleUnits.Degrees)); // 180 °\nconsole.info(angle.toString(AngleUnits.Radians)); // 3.141592653589793 rad\n\n// Specify fraction digits max length\nconsole.info(angle.toString(AngleUnits.Radians, { fractionalDigits: 2 })); // 3.14 rad\n```\n\n## Additional methods\n\nCheck, compare, calculate etc. with unitsnet:\n\n```typescript\nconst length1 = Length.FromMeters(10);\nconst length2 = Length.FromDecimeters(100);\nconst length3 = Length.FromMeters(3);\n\n// 'equals' method\nconsole.log(length1.equals(length2)) // true;\nconsole.log(length1.equals(length3)) // false;\n\n// 'compareTo' method\nconsole.log(length1.compareTo(length3)) // 1;\nconsole.log(length3.compareTo(length1)) // -1;\nconsole.log(length2.compareTo(length1)) // 0;\n\n// Arithmetics methods\nconst results1 = length1.add(length3);\nconst results2 = length1.subtract(length3);\nconst results3 = length1.multiply(length3);\nconst results4 = length1.divide(length3);\nconst results5 = length1.modulo(length3);\nconst results6 = length1.pow(length3);\nconsole.log(results1.toString(LengthUnits.Meters)) // 13 m\nconsole.log(results2.toString(LengthUnits.Meters)) // 7 m\nconsole.log(results3.toString(LengthUnits.Meters)) // 30 m\nconsole.log(results4.toString(LengthUnits.Meters)) // 3.3333333333333335 m\nconsole.log(results5.toString(LengthUnits.Meters)) // 1 m\nconsole.log(results6.toString(LengthUnits.Meters)) // 1000 m\n```\n\n## DTO - Data Transfer Object\n\nAs UnitsNet provides a convenient way to work within a running service, there are occasions where the data needs to be exposed outside of the service, typically through an API containing the unit value or consumed from an API.\n\nTo support this with a clear API schema and make it easy to convert to and from this schema to the specific format, it's recommended to use DTOs and the UnitsNet flavor converters.\n\n```typescript\nimport { Length, LengthDto, LengthUnits } from 'unitsnet-js';\n\n// Create a Length unit object\nconst length = Length.FromMeters(100.01);\n           \n// Obtain the DTO object, represented by the default unit - meter\nconst lengthDto: LengthDto = length.toDto(); // {\"value\":100.01,\"unit\":\"Meter\"}\n\n// Obtain the same value but represent DTO in KM \nconst lengthDtoRepresentsInKM: LengthDto = length.toDto(LengthUnits.Kilometers); // {\"value\":0.10001,\"unit\":\"Kilometer\"}\n\n// Obtain Length object from lengthDto\n\nconst lengthFromMetersDto = Length.FromDto(lengthDto);\n// The exact same value as\nconst lengthFromKMDto = Length.FromDto(lengthDtoRepresentsInKM);\n```\n\n\nCheck out the OpenAPI [unitsnet-openapi-spec](https://haimkastner.github.io/unitsnet-openapi-spec-example/) example schema.\n\nAlso, refer to the detailed discussions on GitHub: [haimkastner/unitsnet-js#31](https://github.com/haimkastner/unitsnet-js/issues/31) \u0026 [angularsen/UnitsNet#1378](https://github.com/angularsen/UnitsNet/issues/1378).\n\n\n## External Arithmetics Methods\n\nAs a default, the arithmetic formula uses JavaScript default operations (e.g., +, -, etc.)\nHowever, JavaScript operations use floating point arithmetic with precision surpassing JavaScript's default [IEEE 754](https://standards.ieee.org/ieee/754/6210/) based implementation.\n\nFor example, you may want to perform arithmetics like the classic `0.1 + 0.2`.\nWith the default implementation, this operation yields `0.30000000000000004`. \nRead more about this issue at https://stackoverflow.com/q/1458633/8281649 \n\nTo cater to such cases, the library exposes an operator override mechanism, allowing for usage of dedicated, high precision mathematic libraries.\n\n```typescript\nimport numeral from 'numeral';\nimport { Length, setOperatorOverride, ArithmeticOperation } from 'unitsnet-js';\n\nconst lengthA = Length.FromMeters(0.1);\nconst lengthB = Length.FromMeters(0.2);\n\nconsole.log(lengthA.add(lengthB).Meters); // 0.30000000000000004\n\nsetOperatorOverride(ArithmeticOperation.Add, (valueA: number,valueB: number) =\u003e {\n\treturn numeral(valueA).add(valueB).value() as number;\n});\n\nconsole.log(lengthA.add(lengthB).Meters); // 0.3\n```\n\nThe override mechanism is designed with high performance in mind.\nWhile running in a test context, the overhead was observed to be roughly 1%.\n\nIf your application requires real-time class performance, however, we recommend you verify this conforms to your specification.\n\nNote that override functions are global and exported directly from the package index.\n\n\u003c/br\u003e\n\n| Function                   | Purpose                                                  | Notes  |\n|----------------------------|----------------------------------------------------------|--------\n| `setOperatorOverride`      | Override a given operator                                |\n| `unsetOperatorOverride`    | Remove an operator override                              | Safe to call even when operator is not overridden\n| `unsetAllOperatorOverrides`| Removes all operator overrides                           | Safe to call even when there are no overridden operators\n| `areAnyOperatorsOverridden`| Determine whether any operators are currently overridden |\n\n\n## Migrating from 2.x.x to 3.x.x\n\nVersion 3.0.0 consolidates some external interfaces and includes a brand new infrastructure for operator\noverriding (see [External arithmetics methods](#external-arithmetics-methods) section).\n\nAs such, a few minor breaking changes have been introduced, specified below.\n\nIf your code does not use any of these types/functions, no change is required.\n\n\n| Type                       | Name                 | Status   | Migration Strategy                                                       | Notes |\n|----------------------------|----------------------|----------|--------------------|-----------------------------------------------------|\n| `type`                     | `ArithmeticFormula`  | Removed  | Use `OperatorOverrides` directly or alias the type in your code | Consolidated into new interface `OperatorOverrides`\n| `type`                     | `CompareToFormula`   | Removed  | Use `OperatorOverrides` directly or alias the type in your code | Consolidated into new interface `OperatorOverrides`\n| `type`                     | `EqualsFormula`      | Removed  | Use `OperatorOverrides` directly or alias the type in your code | Consolidated into new interface `OperatorOverrides`\n| `function`                 | `setEqualsFormula`   | Removed  | Use `OperatorOverrides` with `OperatorType.Equals`              | Consolidated into new function `setOperatorOverride`\n| `function`                 | `setCompareToFormula`| Removed  | Use `OperatorOverrides` with `OperatorType.CompareTo`           | Consolidated into new function `setOperatorOverride`\n\n\n## Supported units\n\n[UnitsNet supported Units](Units.md)\n\n## Units.NET on other platforms\n\nGet the same strongly typed units on other platforms, based on the same [unit definitions](/Common/UnitDefinitions).\n\n| Language                   | Name        | Package                                           \t\t\t\t\t | Repository                                           | Maintainers  |\n|----------------------------|-------------|---------------------------------------------------------------------|------------------------------------------------------|--------------|\n| C#                         | UnitsNet    | [nuget](https://www.nuget.org/packages/UnitsNet/) \t\t\t\t\t | [github](https://github.com/angularsen/UnitsNet)     | @angularsen  |\n| JavaScript /\u003cbr\u003eTypeScript | unitsnet-js | [npm](https://www.npmjs.com/package/unitsnet-js)  \t\t\t\t\t | [github](https://github.com/haimkastner/unitsnet-js) | @haimkastner |\n| Python                     | unitsnet-py | [pypi](https://pypi.org/project/unitsnet-py)      \t\t\t\t\t | [github](https://github.com/haimkastner/unitsnet-py) | @haimkastner |\n| Golang                     | unitsnet-go | [pkg.go.dev](https://pkg.go.dev/github.com/haimkastner/unitsnet-go) | [github](https://github.com/haimkastner/unitsnet-go) | @haimkastner |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaimkastner%2Funitsnet-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhaimkastner%2Funitsnet-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhaimkastner%2Funitsnet-js/lists"}