{"id":49538516,"url":"https://github.com/flycran/smart-unit","last_synced_at":"2026-05-02T13:05:15.457Z","repository":{"id":344332267,"uuid":"1181452460","full_name":"flycran/smart-unit","owner":"flycran","description":"Elegant unit conversion utility for JavaScript \u0026 TypeScript","archived":false,"fork":false,"pushed_at":"2026-04-12T00:44:16.000Z","size":324,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-04-12T02:25:59.267Z","etag":null,"topics":["browser","formatting","javascript","lightweight","nodejs","npm","typescript","unit-conversion","utilities"],"latest_commit_sha":null,"homepage":"","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":"2026-03-14T06:40:33.000Z","updated_at":"2026-04-12T00:44:14.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/flycran/smart-unit","commit_stats":null,"previous_names":["flycran/smart-unit"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/flycran/smart-unit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fsmart-unit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fsmart-unit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fsmart-unit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fsmart-unit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flycran","download_url":"https://codeload.github.com/flycran/smart-unit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flycran%2Fsmart-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":["browser","formatting","javascript","lightweight","nodejs","npm","typescript","unit-conversion","utilities"],"created_at":"2026-05-02T13:05:06.897Z","updated_at":"2026-05-02T13:05:15.452Z","avatar_url":"https://github.com/flycran.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# smart-unit\n\n\u003e Elegant unit conversion utility for JavaScript \u0026 TypeScript\n\n[![npm version](https://img.shields.io/npm/v/smart-unit)](https://www.npmjs.com/package/smart-unit)\n[![npm downloads](https://img.shields.io/npm/dm/smart-unit)](https://www.npmjs.com/package/smart-unit)\n[![bundle size](https://img.shields.io/bundlephobia/minzip/smart-unit)](https://bundlephobia.com/package/smart-unit)\n[![test](https://github.com/flycran/smart-unit/workflows/Test/badge.svg)](https://github.com/flycran/smart-unit/actions)\n[![license](https://img.shields.io/npm/l/smart-unit)](./LICENSE)\n\n\u003cp align=\"center\"\u003e\n  English | \u003ca href=\"./README.zh-CN.md\"\u003e中文\u003c/a\u003e | \u003ca href=\"https://flycran.github.io/smart-unit/en\"\u003eDocumentation\u003c/a\u003e\n\u003c/p\u003e\n\n---\n\n**smart-unit** is a lightweight automatic unit conversion tool with intelligent formatting.\n\n```ts\nimport SmartUnit from 'smart-unit';\n\nconst size = new SmartUnit(['B', 'KB', 'MB', 'GB'], { baseDigit: 1024 });\n\nsize.format(1024 * 1024 * 100);  // \"100MB\"\nsize.format(1536);               // \"1.5KB\"\nsize.parse('2.5GB');             // 2684354560\n```\n\n## Features\n\n- 🎯 **Smart Formatting** — Automatically selects the optimal unit for display\n- 🔢 **Bidirectional Conversion** — Supports unit formatting (`format`) and reverse parsing (`parse`)\n- ⚡ **High Performance** — Minimal overhead, supports Node.js and browsers\n- 🧮 **High Precision** — Optional `decimal.js` integration for arbitrary precision calculations\n- 📦 **TypeScript First** — Complete type safety\n- 🪶 **Lightweight** — Core functionality with small bundle size\n- ✅ **Well Tested** — 100+ test cases covering various edge cases\n\n## Installation\n\n```bash\nnpm install smart-unit\n```\n\n## Try It Online\n\n[![Edit in CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/flycran/smart-unit/tree/master/examples/basic)\n\n## Quick Start\n\n### Fixed Ratio Units\n\n```ts\nimport SmartUnit from 'smart-unit';\n\nconst fileSize = new SmartUnit(['B', 'KB', 'MB', 'GB', 'TB'], {\n  baseDigit: 1024,\n});\n\nfileSize.format(1024 * 1024 * 100);  // =\u003e \"100MB\"\nfileSize.format(1536);               // =\u003e \"1.5KB\"\n```\n\n### Variable Ratio Units\n\n```ts\nconst length = new SmartUnit(['mm', 10, 'cm', 100, 'm', 1000, 'km']);\n\nlength.format(1500);     // =\u003e \"1.5m\"\nlength.format(1500000);  // =\u003e \"1.5km\"\n```\n\n### High Precision \u0026 BigInt Support\n\n```ts\nimport { SmartUnitPrecision } from 'smart-unit/precision'\n\nconst bigLength = new SmartUnitPrecision(\n  ['mm', 10, 'cm', 100, 'm', 1000, 'km', 1000, 'Mm', 1000, 'Gm', 1000, 'Tm']\n);\n\n// Supports BigInt and values beyond JS safe integer range\nbigLength.format(10n ** 18n);  // =\u003e \"1000Tm\"\n```\n\n### Parsing Unit Strings\n\n```ts\nconst time = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h']);\n\ntime.parse('90s');   // =\u003e 90000 (ms)\ntime.parse('2.5h');  // =\u003e 9000000 (ms)\n```\n\n### Chain Formatting\n\n```ts\nconst time = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h']);\n\ntime.formatChain(63000) // =\u003e 1m3s\ntime.formatChain(3663000);  // =\u003e 1h1m3s\n```\n\n### Internationalization\n\n```ts\nconst i18nMap = {\n  ms: 'ms',\n  s: 'seconds',\n  m: 'minutes',\n  h: 'hours',\n}\nconst t = (unit: keyof typeof i18nMap) =\u003e i18nMap[unit]\n\nconst timeI18n = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h'], {\n  separator: ' ',\n}).withConvert(t)\n\ntimeI18n.formatChain(90000) // 1minutes 30seconds\ntimeI18n.formatChain(9000000) // 2hours 30minutes\n```\n\n## Using TypeScript\n\nSmartUnit provides complete type safety support for TypeScript projects.\n\n```ts\nimport SmartUnit, { type GetUnitNames } from 'smart-unit'\n\nconst time = new SmartUnit(['ms', 1000, 's', 60, 'm', 60, 'h'])\n// Use utility function to export types\ntype TimeUnits = GetUnitNames\u003ctypeof time\u003e // =\u003e type TimeUnits = \"m\" | \"ms\" | \"s\" | \"h\"\n\n// The t function must accept all TimeUnits units, otherwise a type error will occur\nconst timeI18n = time.withConvert(t)\n// The unit parameter of toBase receives TimeUnits type constraint\ntime.toBase(60, 'h')\n```\n\n## Contributing\n\nContributions are welcome! Feel free to submit issues or pull requests.\n\n## License\n\nMIT © [flycran](https://github.com/flycran)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflycran%2Fsmart-unit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflycran%2Fsmart-unit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflycran%2Fsmart-unit/lists"}