{"id":49538507,"url":"https://github.com/flycran/auto-unit","last_synced_at":"2026-05-02T13:05:11.281Z","repository":{"id":208578575,"uuid":"721970668","full_name":"flycran/auto-unit","owner":"flycran","description":"Automatically select the appropriate unit of measurement","archived":false,"fork":false,"pushed_at":"2026-03-14T03:28:48.000Z","size":89,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-03-14T14:56:40.033Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/flycran.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-11-22T06:53:08.000Z","updated_at":"2026-03-14T03:28:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"efc52e63-abbc-4acd-a4ea-bd72ad03c7fe","html_url":"https://github.com/flycran/auto-unit","commit_stats":null,"previous_names":["flycran/auto-unit"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/flycran/auto-unit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fauto-unit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fauto-unit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fauto-unit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fauto-unit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flycran","download_url":"https://codeload.github.com/flycran/auto-unit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fauto-unit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32534975,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T12:25:33.646Z","status":"ssl_error","status_checked_at":"2026-05-02T12:24:51.733Z","response_time":132,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":[],"created_at":"2026-05-02T13:05:00.967Z","updated_at":"2026-05-02T13:05:11.276Z","avatar_url":"https://github.com/flycran.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AutoUnit\n\nEnglish documentation is translated by AI. If you have any questions about the content, please refer to the [Chinese document](./README.zh.md). Contributions to improve translations are welcome.\n\n\u003cdiv align=\"center\"\u003e\n\u003ca href=\"./README.zh.md\"\u003e中文文档\u003c/a\u003e\n|\n\u003ca href=\"./README.md\"\u003eEnglish\u003c/a\u003e\n\u003c/div\u003e\n\n`AutoUnit` is a utility class for automatic unit conversion, supporting custom unit systems, thresholds, and decimal place configurations.\n\n- Automatically selects appropriate units to output formatted strings\n- Reverse parses numeric values from unit-formatted strings\n- Quickly build any unit system\n- Supports high-precision calculations\n- Supports calculations beyond JS safe number ranges\n\n## Quick Links\n\n- [Quick Start](#quick-start)\n- [Best Practices](#best-practices)\n- [API](#api)\n- [Contributing](#contributing)\n\n## Quick Start\n\n- **Fixed-base Units**\n\n```ts\nimport AutoUnit from 'auto-unit';\n\nconst autoUnit = new AutoUnit([ 'B', 'KB', 'MB', 'GB', 'TB', 'PB' ], {\n  baseDigit: 1024,\n})\n\nconsole.log(autoUnit.format(1024 * 1024 * 100)) // 100MB\n```\n\n\u003e The `baseDigit` parameter represents the conversion base between units. For example, 1024 means 1KB equals 1024 bytes.\n\n- **Variable-base Units**\n\n```ts\nimport AutoUnit from 'auto-unit';\n\nconst autoUnit = new AutoUnit([ 'mm', 10, 'cm', 100, 'm', 1e3, 'km' ])\n\nconsole.log(autoUnit.format(1000)) // 1m\n```\n\n\u003e You can customize conversion bases between units. For example, [ 'mm', 10, 'cm', 100, 'm', 1e3, 'km' ] means:\n\u003e 1 cm equals 10 mm, 1 m equals 100 cm, and 1 km equals 1000 m.\n\n- **High Precision \u0026 Large Integers**\n\n```ts\nimport AutoUnit from 'auto-unit';\n\nconst units = [ 'mm', 10, 'cm', 100, 'm', 1e3, 'km', 1e3, 'Mm', 1e3, 'Gm', 1e3, 'Tm' ]\nconst autoUnit = new AutoUnit(units, { highPrecision: true })\n\nconsole.log(autoUnit.format(1e18)) // 1000Tm\n```\n\n\u003e The `highPrecision` parameter enables high-precision mode using `decimal.js`. It supports `number`, `string`, `BigInt`, and `Decimal` inputs.\n\n## Best Practices\n\n- For most use cases requiring formatted unit strings, consider wrapping `format`:\n\n```ts\nimport AutoUnit from 'auto-unit';\n\nconst autoUnit = new AutoUnit([ 'B', 'KB', 'MB', 'GB', 'TB', 'PB' ], {\n  baseDigit: 1024,\n})\n\nexport const formatFileSize = (num: number) =\u003e {\n  return autoUnit.format(num)\n}\n```\n\n- In modular projects (e.g., React/Vue), pre-initialize unit systems globally (e.g., in `utils/units.ts`) rather than recreating them per component.\n\n## API\n\n### **Constructor**\n\n```ts\nclass AutoUnit {\n  constructor(\n          readonly units: (string | number)[],\n          option: AutoUnitOptions\u003cDS\u003e = {},\n  ) {}\n}\n```\n\n- **Parameters**:\n  - `units`: Unit array.\n  - `option`: Configuration options.\n    - `baseDigit?`: Base number for auto-generating unit conversions.\n    - `threshold?`: Unit switch threshold (default: `1`).\n    - `decimal?`: Decimal configuration (fixed number or range format).\n    - `highPrecision?`: Enables `decimal.js` for precise calculations.\n\n### **Methods**\n\n###### `getUnit(num: Num\u003cDS\u003e)`\n\nGets the optimal unit and converted value from base units.\n\n- **Parameters**:\n  - `num`: Input number. Supports `number`, `string`, `BigInt`, and `Decimal` in high-precision mode.\n\n- **Returns**:\n  - `{ num: number, unit: string, decimal: Decimal }`: Converted value with unit. Includes `Decimal` in high-precision mode.\n\n###### `format(num: Num\u003cDS\u003e, decimal = this.decimal)`\n\nFormats a number into a unit-string representation.\n\n- **Parameters**:\n  - `num`: Input number (supports extended types in high-precision mode).\n  - `decimal?`: Overrides default decimal configuration.\n\n- **Returns**:\n  - Formatted string with value and unit.\n\n###### `toBase(num: Num\u003cDS\u003e, unit: string)`\n\nConverts a unit-formatted number back to base units.\n\n- **Parameters**:\n  - `num`: Input value in specified unit.\n  - `unit`: Current unit.\n\n- **Returns**:\n  - Converted value in base units (`number` or `Decimal`).\n\n###### `splitUnit(str: string)`\n\nExtracts numeric value and unit from a string.\n\n- **Parameters**:\n  - `str`: Input string containing value and unit.\n\n- **Returns**:\n  - `{ num: number, unit: string, decimal: Decimal }`: Parsed components with `Decimal` in high-precision mode.\n\n###### `parse(str: string)`\n\nConverts a unit-formatted string to base unit value.\n\n- **Parameters**:\n  - `str`: Input string.\n\n- **Returns**:\n  - Converted value in base units (`number` or `Decimal`).\n\n###### `fromUnitFormat(num: number, unit: string, decimal?: DecimalPlace)`\n\nConverts between units and formats with specified decimals.\n\n- **Parameters**:\n  - `num`: Input value.\n  - `unit`: Source unit.\n  - `decimal?`: Overrides default decimal configuration.\n\n- **Returns**:\n  - Formatted string with converted value and unit.\n\n## Contributing\n\nContributions of all forms are welcome, including bug reports, documentation improvements, translation updates, and test enhancements.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflycran%2Fauto-unit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflycran%2Fauto-unit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflycran%2Fauto-unit/lists"}