{"id":18048528,"url":"https://github.com/bent10/attributes-parser","last_synced_at":"2025-06-23T01:41:51.726Z","repository":{"id":195465828,"uuid":"692951927","full_name":"bent10/attributes-parser","owner":"bent10","description":"Tokenize and parse attributes string into meaningful tokens and key-value pairs.","archived":false,"fork":false,"pushed_at":"2025-01-18T11:09:31.000Z","size":205,"stargazers_count":2,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-09T10:17:48.651Z","etag":null,"topics":["attr","attribute","attributes","attrs","html","markdown","object","prop","properties","property","props","serialize","tokenize","tokens"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/attributes-parser","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/bent10.png","metadata":{"files":{"readme":"readme.md","changelog":"changelog.md","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}},"created_at":"2023-09-18T03:24:38.000Z","updated_at":"2025-01-18T11:08:25.000Z","dependencies_parsed_at":"2023-12-19T06:44:57.040Z","dependency_job_id":"7f8674a4-0551-4e0b-babe-bd00ddfbe9dc","html_url":"https://github.com/bent10/attributes-parser","commit_stats":null,"previous_names":["bent10/attributes-parser"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bent10%2Fattributes-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bent10%2Fattributes-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bent10%2Fattributes-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bent10%2Fattributes-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bent10","download_url":"https://codeload.github.com/bent10/attributes-parser/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bent10%2Fattributes-parser/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":258892407,"owners_count":22773838,"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":["attr","attribute","attributes","attrs","html","markdown","object","prop","properties","property","props","serialize","tokenize","tokens"],"created_at":"2024-10-30T20:13:17.214Z","updated_at":"2025-06-12T06:03:27.155Z","avatar_url":"https://github.com/bent10.png","language":"TypeScript","readme":"# Attributes Parser\n\nA utility for parsing and tokenizing attributes string into meaningful tokens and key-value pairs.\n\n## Install\n\nYou can install this module using npm or yarn, it's only `2.68 kB | min: 1.10 kB`:\n\n```bash\nnpm i attributes-parser\n# or\nyarn add attributes-parser\n```\n\nAlternatively, you can also include this module directly in your HTML file from [CDN](https://www.jsdelivr.com/package/npm/attributes-parser?tab=files\u0026path=dist):\n\n| Type | URL                                                                |\n| :--- | :----------------------------------------------------------------- |\n| ESM  | `https://cdn.jsdelivr.net/npm/attributes-parser/+esm`              |\n| CJS  | `https://cdn.jsdelivr.net/npm/attributes-parser/dist/index.cjs`    |\n| UMD  | `https://cdn.jsdelivr.net/npm/attributes-parser/dist/index.umd.js` |\n\n## Usage\n\n```js\nimport parseAttrs from 'attributes-parser'\n\nconst attr = `#my-id.foo.bar class=\"baz\" num=3.14 numNeg=-3.14 data-num=\"3.14\" data-value=\"123\" data-value=1_000_000 options=\\'{\"key\": \"value\", \"array\": [1, 2, 3]}\\' data-list=\"[1, 2, 3]\" punc=\"a=b,c,d,e\" checked=false checked=false data-checked=\"false\" disabled`\nconst parsedAttr = parseAttrs(attr)\n\nconsole.log(parsedAttr)\n// use `parsedAttr.toString()` to turn it back into a string\n// use `parsedAttr.getTokens()` to get the tokens array\n```\n\nYields:\n\n```js\n{\n  id: 'my-id', //  from shorthand attr #my-id\n  class: 'foo bar baz', //  from shorthand attr .foo.bar and class=\"baz\"\n  num: 3.14,  // number\n  numNeg: -3.14,  // negative number\n  'data-num': '3.14',  // preserve string\n  'data-value': 1000000,  // any duplicate key but `class`, last value is kept\n  options: { key: 'value', array: [ 1, 2, 3 ] },\n  'data-list': [ 1, 2, 3 ],\n  punc: 'a=b,c,d,e',  // allowed, no ambiguous ampersand\n  checked: false,  // boolean\n  'data-checked': 'false',  // preserve string\n  disabled: \"disabled\"  // shorthand\n}\n```\n\n## Attribute Validation\n\nThis module ensure that attribute names and values adhere to the syntax rules:\n\n- Follows the HTML specification for valid attribute names and values. It uses regular expressions to validate and tokenize attributes based on the rules defined in the specification.\n\n- Valid attribute names and values will be correctly tokenized and parsed, providing you with meaningful results.\n\n- Invalid attributes that do not adhere to HTML syntax rules may result in unexpected behavior. It's essential to ensure that the input attributes string comply with HTML standards to get accurate results.\n\n- If an attributes string contains invalid characters or does not follow the HTML syntax for attributes, the parsing may not produce the desired output.\n\nIt is better to validate and sanitize your attributes string to ensure they conform to HTML standards before using this module for parsing. For more information on HTML syntax attributes, refer to the [HTML syntax attributes specification](https://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#syntax-attributes).\n\nBelow are the test cases that demonstrate valid and invalid attribute patterns according to these rules:\n\n### `AttributeName`\n\n#### Valid\n\n- `validname`\n- `validName`\n- `valid_name`\n- `valid-name`\n- `valid42`\n- `valid-42`\n- `-valid`\n- `_valid`\n- `$valid`\n- `@valid`\n- `valid@name`\n- `:valid`\n- `valid:name`\n\n#### Invalid\n\n- `\\x07Fvalid` (Contains prohibited character)\n- `invalid name` (Contains space character)\n- `\"invalidName\"` (Contains prohibited character)\n- `name\u003e` (Contains prohibited character)\n- `name=` (Contains prohibited character)\n- `name\\x00` (Contains prohibited character)\n- `name\\n` (Contains prohibited character)\n\n### `AttributeShorthand`\n\n- `#bar` (Shorthand for attribute `id=\"bar\"`)\n- `.foo` (Shorthand for attribute `class=\"foo\"`)\n\n### `BooleanLiteral`\n\n- `true`\n- `false`\n\n### `NumericLiteral`\n\n#### Valid\n\n- `0x1A3` (hexLiteral)\n- `0o755` (octalLiteral)\n- `0b1101` (binaryLiteral)\n- `123.456` (decimalLiteral)\n- `1.23e-45` (scientificLiteral)\n- `0` (zeroLiteral)\n- `1_000_000` (underscoredLiteral)\n- `42` (integerLiteral)\n- `1e3` (scientificNoFraction)\n\n#### Invalid\n\n- `12.34e` (scientificNoExponent)\n\n### `StringLiteral` (Single-quoted)\n\n#### Valid\n\n- `'valid value'`\n- `\"valid@value\"`\n- `'valid \u0026 value'`\n- `'valid \u0026; value'`\n- `'42'`\n- `'-42'`\n- `'3.14'`\n- `'0.5'`\n- `'-0.5'`\n- `'.5'`\n- `'escaped single quote: \\\\''`\n- `'newline: \\n'`\n- `'escaped newline: \\\\n'`\n- `'[1, 2, 3]'`\n- `'{foo: \"bar\"}'`\n\n#### Invalid\n\n- `'invalid value\"` (Contains prohibited character)\n- `'invalid value''` (Contains prohibited character)\n- `'invalid \u0026value;'` (Contains an ambiguous ampersand, e.g. `\u0026amp;`)\n\n### `StringLiteral` (Double-quoted)\n\n#### Valid\n\n- `\"valid value\"`\n- `\"valid@value\"`\n- `\"valid \u0026 value\"`\n- `\"valid \u0026; value\"`\n- `\"42\"`\n- `\"-42\"`\n- `\"3.14\"`\n- `\"0.5\"`\n- `\"-0.5\"`\n- `\".5\"`\n- `\"escaped double quote: \\\"\"`\n- `\"newline: \\n\"`\n- `\"escaped newline: \\n\"`\n- `\"[1, 2, 3]\"`\n- `\"{foo: 'bar'}\"`\n\n#### Invalid\n\n- `\"invalid value'` (Contains prohibited character)\n- `\"invalid value\"\"` (Contains prohibited character)\n- `\"invalid \u0026value;\"` (Contains an ambiguous ampersand, e.g. `\u0026amp;`)\n\n### `StringLiteral` (Unquoted)\n\n#### Valid\n\n- `validValue`\n- `valid@value`\n- `42`\n- `-42`\n- `3.14`\n- `0.5`\n- `-0.5`\n- `.5`\n- `true`\n- `false`\n\n#### Invalid\n\n- `invalid value` (Contains prohibited character)\n- `value\"` (Contains prohibited character)\n- `value'` (Contains prohibited character)\n- `value`\\` (Contains prohibited character)\n- `=value` (Contains prohibited character)\n- `value\u003e` (Contains prohibited character)\n- `value\u003c` (Contains prohibited character)\n- `value\\x00` (Contains prohibited character)\n- `value\\n` (Contains prohibited character)\n\n## Related\n\n- [json-loose](https://github.com/bent10/json-loose) – Transforms loosely structured plain object strings into valid JSON strings.\n\n## Contributing\n\nWe 💛\u0026nbsp; issues.\n\nWhen committing, please conform to [the semantic-release commit standards](https://www.conventionalcommits.org/). Please install `commitizen` and the adapter globally, if you have not already.\n\n```bash\nnpm i -g commitizen cz-conventional-changelog\n```\n\nNow you can use `git cz` or just `cz` instead of `git commit` when committing. You can also use `git-cz`, which is an alias for `cz`.\n\n```bash\ngit add . \u0026\u0026 git cz\n```\n\n## License\n\n![GitHub](https://img.shields.io/github/license/bent10/attributes-parser)\n\nA project by [Stilearning](https://stilearning.com) \u0026copy; 2023.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbent10%2Fattributes-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbent10%2Fattributes-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbent10%2Fattributes-parser/lists"}