{"id":17036010,"url":"https://github.com/phucbm/get-options-from-html-attr","last_synced_at":"2026-05-16T06:13:36.028Z","repository":{"id":246780850,"uuid":"822980336","full_name":"phucbm/get-options-from-html-attr","owner":"phucbm","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-02T08:12:38.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-17T21:22:15.299Z","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/phucbm.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-07-02T07:46:48.000Z","updated_at":"2024-07-02T08:09:31.000Z","dependencies_parsed_at":"2024-07-06T03:21:16.194Z","dependency_job_id":null,"html_url":"https://github.com/phucbm/get-options-from-html-attr","commit_stats":null,"previous_names":["phucbm/get-options-from-html-attr"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phucbm%2Fget-options-from-html-attr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phucbm%2Fget-options-from-html-attr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phucbm%2Fget-options-from-html-attr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phucbm%2Fget-options-from-html-attr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phucbm","download_url":"https://codeload.github.com/phucbm/get-options-from-html-attr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240023526,"owners_count":19735674,"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":[],"created_at":"2024-10-14T08:48:55.753Z","updated_at":"2026-05-16T06:13:35.981Z","avatar_url":"https://github.com/phucbm.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @phucbm/get-options-from-html-attr\n\n![Test Status](https://github.com/phucbm/get-options-from-html-attr/actions/workflows/test.yml/badge.svg)\n![NPM Publish](https://img.shields.io/github/actions/workflow/status/phucbm/get-options-from-html-attr/publish.yml?branch=main\u0026label=npm%20publish\u0026logo=npm)\n![NPM Version](https://img.shields.io/npm/v/@phucbm/get-options-from-html-attr)\n\n\nA utility function to parse options from a specified attribute of a target element, with support for default values, numeric conversion, and JSON validation.\n\n## Installation\n\nFirst, ensure you have Node.js and npm installed. Then, install the package using npm:\n\n```bash\nnpm i @phucbm/get-options-from-html-attr\n```\n\n## Usage\n\n### Importing the Function\n\n```typescript\nimport { getOptionsFromAttribute } from '@phucbm/get-options-from-html-attr';\n```\n\n### Function Parameters\n\nThe `getOptionsFromAttribute` function accepts an object with the following properties:\n\n| Parameter        | Type                     | Default Value | Description                                          |\n|------------------|--------------------------|---------------|------------------------------------------------------|\n| `target`         | `HTMLElement`            | `null`        | `null`                                               | The target HTML element.                            |\n| `attributeName`  | `string`                 | `''`          | The name of the attribute to parse.                  |\n| `defaultOptions` | `Record\u003cstring, any\u003e`    | `{}`          | The default options to return if parsing fails.      |\n| `numericValues`  | `string[]`               | `[]`          | An array of property names to convert to float.      |\n| `onIsString`     | `(data: string) =\u003e void` | `undefined`   | A callback function to execute if value is not JSON. |\n| `dev`            | `boolean`                | `false`       | Enable or disable development mode.                  |\n\n### Example Usage\n\n```typescript\nimport { getOptionsFromAttribute } from '@phucbm/get-options-from-html-attr';\n\nconst targetElement = document.getElementById('example-element');\n\nconst options = getOptionsFromAttribute({\n  target: targetElement,\n  attributeName: 'data-options',\n  defaultOptions: { key: 'defaultValue' },\n  numericValues: ['numericValue'],\n  onIsString: (data) =\u003e { console.log('Not a JSON string:', data); },\n  dev: true,\n});\n\nconsole.log(options);\n```\n\n### Example HTML\n\n```html\n\u003cdiv id=\"example-element\" data-options='{\"key\":\"value\", \"numericValue\":\"123.45\", \"isTrue\":\"true\", \"isFalse\":\"false\"}'\u003e\u003c/div\u003e\n```\n\n### Expected Output\n\nWhen the above HTML and JavaScript are used, the `options` object will be:\n\n```javascript\n{\n  key: 'value',\n  numericValue: 123.45,\n  isTrue: true,\n  isFalse: false\n}\n```\n\n## Testing\n\n```bash\nnpx jest\n```\n\nEnsure all tests pass.\n\n## License\n\nThis project is licensed under the MIT License.\n\n---","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphucbm%2Fget-options-from-html-attr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphucbm%2Fget-options-from-html-attr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphucbm%2Fget-options-from-html-attr/lists"}