{"id":20271669,"url":"https://github.com/ymd-h/format-js","last_synced_at":"2025-09-22T13:30:35.604Z","repository":{"id":229464377,"uuid":"776812577","full_name":"ymd-h/format-js","owner":"ymd-h","description":"format string with Python f-string or date command syntax","archived":false,"fork":false,"pushed_at":"2024-06-30T07:59:13.000Z","size":33,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-14T12:53:27.628Z","etag":null,"topics":["javascript","string-formatter"],"latest_commit_sha":null,"homepage":"https://jsr.io/@ymd-h/format-js","language":"JavaScript","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/ymd-h.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":"2024-03-24T14:18:16.000Z","updated_at":"2024-10-07T01:54:52.000Z","dependencies_parsed_at":"2024-03-24T15:30:52.980Z","dependency_job_id":"831dfbd2-3f53-44a4-8017-852957d862c4","html_url":"https://github.com/ymd-h/format-js","commit_stats":null,"previous_names":["ymd-h/format-js"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymd-h%2Fformat-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymd-h%2Fformat-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymd-h%2Fformat-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymd-h%2Fformat-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ymd-h","download_url":"https://codeload.github.com/ymd-h/format-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233851065,"owners_count":18740154,"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":["javascript","string-formatter"],"created_at":"2024-11-14T12:39:03.720Z","updated_at":"2025-09-22T13:30:35.463Z","avatar_url":"https://github.com/ymd-h.png","language":"JavaScript","readme":"# format.js\n\n![JSR Version](https://img.shields.io/jsr/v/%40ymd-h/format-js)\n\n\n## 1. Usage\nformat.js provides two usage;\n\n1. Python f-string like format\n1. date command like format\n\nBoth usage have default formatter and user customization.\n\n\n### 1.1 f-String like format\nPseudo Syntax: `{index[:[[align]width][.precision]type]}`\n\n#### 1.1.1 Default Usage\n```javascript\nimport { format } from \"https://cdn.jsdelivr.net/gh/ymd-h/format-js@v1/format.js\";\n\nformat(\"{0} + {1} = {2}\", 1, 2, 1+2); // \"1 + 2 = 3\"\nformat(\"Precision: {0:.2f}\", 1.78); // \"Precision: 1.78\"\nformat(\"Width: {0:4d}\", 1); // \"Width:   1\"\nformat(\"Hex: {0:x}\", 15); // \"Hex: f\"\nformat(\"Align: {0:\u003c4d}\", 1); // \"Align: 1   \"\n```\n\n#### 1.1.2 Patch on Default\n```javascript\nimport { patch_default_format } from \"https://cdn.jsdelivr.net/gh/ymd-h/format-js@v1/format.js\";\n\nconst f = patch_default_format(\n    { // key: (value: *, precision: number) =\u003e formatted_string\n        \"w\": (value, precision) =\u003e value.toFixed(precision) + ` (${precision})`,\n    }, // Patch for Format Handlers (optional)\n    { // key: (value: string, width: number) =\u003e aligned_string\n        \"*\": (value, width) =\u003e value.padStart(width, \"*\"),\n    }, // Patch for Align (optional)\n);\n\nf.format(\"{0:.2w}\", 1); // \"1.00 (2)\"\nf.format(\"{0:.6w}\", 1.25); // \"1.000000 (6)\"\nf.format(\"{0:*6.2f}\", 1); // \"**1.00\"\n```\n\n\n#### 1.1.3 Custom Formatter from scratch\n```javascript\nimport { FStringLikeFormatter } from \"https://cdn.jsdelivr.net/gh/ymd-h/format-js@v1/format.js\";\n\nconst f = new FStringLikeFormatter(\n    {\n        ...\n    }, // handlers\n    {\n        ...\n    }, // align\n    { defaultPrecision: 2 }, // options (optional)\n);\n\nf.format(\"My name is {0}.\", \"ymd-h\");\n```\n\n\n### 1.2 date like format\n\n#### 1.2.1 Default Usage\n```javascript\nimport { format_date } from \"https://cdn.jsdelivr.net/gh/ymd-h/format-js@v1/format.js\";\n\nformat_date(\"%Y-%m-%d %H:%M:%S\", new Date(2024, 2, 24, 18, 44, 52)); // \"2024-03-24 18:44:52\"\nformat_date(\"%T\", new Date(2024, 2, 24, 18, 44, 52)); // \"18:44:52\"\n```\n\n\n#### 1.2.2 Patch on Default\n```javascript\nimport { patch_default_formatformat_date } from \"https://cdn.jsdelivr.net/gh/ymd-h/format-js@v1/format.js\";\n\nconst f = patch_default_formatformat_date({\n    \"p\": d =\u003e (d.getHours() \u003c 12) ? \"a.m.\" : \"p.m.\",\n});\n\nf.format(\"%p\", new Date(2024, 2, 24, 18, 44, 52)); // \"p.m.\"\n```\n\n\n#### 1.2.3 Custom Formatter from scratch\n```javascript\nimport { DateLikeFormatter } from \"https://cdn.jsdelivr.net/gh/ymd-h/format-js@v1/format.js\";\n\nconst f = new DateLikeFormatter({\n    \"p\": d =\u003e (d.getHours() \u003c 12) ? \"a.m.\" : \"p.m.\",\n});\n\nf.format(\"%p\", new Date(2024, 2, 24, 18, 44, 52)); // \"p.m.\"\n```\n\n\n\n## 2. Predefined Handlers / Aligns for Default Formatters\n\n### 2.1 f-String like format\n\n| format | description |\n|---|---|\n|`s`| string (`.toString()`) |\n|`b`| binary number |\n|`c`| unicode character (`String.fromCodePoint()`) |\n|`d`| integer |\n|`o`| octal number |\n|`x`| hexadecimal number. Use `a`-`f` for 10 to 15 |\n|`X`| hexadecimal number. Use `A`-`F` for 10 to 15 |\n|`e`| scientific notation. Use `e` for exponent |\n|`E`| scientific notation. Use `E` for exponent |\n|`f`| fixed point notation. Use `nan` / `infinity` |\n|`F`| fixed point notation. Use `NAN` / `INFINITY` |\n|`%`| percentage. multuply 100, fixed notation follewed by `%` |\n\n\n| align | description |\n|---|---|\n|`\u003c`| align left |\n|`^`| align middle |\n|`\u003e` / (empty) | align right (default) |\n\n\n### 2.2 date like format\n\n| format | description |\n|---|---|\n|`%y`| year without century (2 digit) |\n|`%Y`| year with century (4 digit) |\n|`%m`| 2 digit month (`01` - `12`) |\n|`%d`| 2 digit date (`01` - `31`) |\n|`%H`| 24 hour 2 digit hour (`00` - `23`) |\n|`%I`| 12 hour 2 digit hour (`01` - `12`) |\n|`%p`| AM or PM |\n|`%M`| 2 digit minute (`00` - `59`) |\n|`%S`| 2 digit second (`00` - `59`) |\n|`%f`| 3 digit microsecond (`000` - `999`) |\n|`%s`| Unix Time. Elapsed seconds from 1970-01-01 00:00:00 UTC |\n|`%T`| Same as `%H:%M:%S` |\n|`%w`| day of the week as number. Sunday is `0`. (`0` - `6`) |\n|`%%`| `%` (escaped) |\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymd-h%2Fformat-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fymd-h%2Fformat-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymd-h%2Fformat-js/lists"}