{"id":22551619,"url":"https://github.com/gsantiago/extract-data-options","last_synced_at":"2025-04-10T03:33:20.641Z","repository":{"id":57232035,"uuid":"83247433","full_name":"gsantiago/extract-data-options","owner":"gsantiago","description":"Extract `data-(namespace)-*` options from a HTML element","archived":false,"fork":false,"pushed_at":"2017-02-26T23:24:29.000Z","size":8,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T02:37:21.303Z","etag":null,"topics":["attributes","data","element","extract","html","options"],"latest_commit_sha":null,"homepage":"","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/gsantiago.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}},"created_at":"2017-02-26T23:09:31.000Z","updated_at":"2020-12-04T10:52:57.000Z","dependencies_parsed_at":"2022-08-31T20:50:54.485Z","dependency_job_id":null,"html_url":"https://github.com/gsantiago/extract-data-options","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsantiago%2Fextract-data-options","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsantiago%2Fextract-data-options/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsantiago%2Fextract-data-options/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsantiago%2Fextract-data-options/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gsantiago","download_url":"https://codeload.github.com/gsantiago/extract-data-options/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248151210,"owners_count":21056043,"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":["attributes","data","element","extract","html","options"],"created_at":"2024-12-07T17:13:27.685Z","updated_at":"2025-04-10T03:33:20.604Z","avatar_url":"https://github.com/gsantiago.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# extract-data-options\n[![Build Status](https://travis-ci.org/gsantiago/extract-data-options.svg?branch=master)](https://travis-ci.org/gsantiago/extract-data-options)\n[![Build Status](https://saucelabs.com/buildstatus/extactdataoptions)](https://saucelabs.com/beta/builds/7e2581ea500d489a9d2e434435c5e72c)\n[![npm version](https://badge.fury.io/js/extract-data-options.svg)](https://badge.fury.io/js/extract-data-options)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://saucelabs.com/u/extactdataoptions\"\u003e\n    \u003cimg src=\"https://saucelabs.com/browser-matrix/extactdataoptions.svg\" alt=\"Sauce Test Status\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nExtract options from data attributes:\n\n```html\n\u003cdiv class=\"js-carousel\"\n     data-carousel-index=\"0\"\n     data-carousel-auto-play=\"true\"\n     data-carousel-controls.prev=\".js-prev\"\n     data-carousel-controls.next=\".js-next\"\n     data-carousel-dots=\".my-dots\"\u003e\n\u003c/div\u003e\n```\n\n```js\n// Import the module\nvar extractDataOptions = require('extract-data-options')\n\n// Get the element\nvar element = document.querySelector('.js-carousel')\n\n// Extract the options from `data-carousel-*` attributes\nvar options = extractDataOptions(element, 'carousel')\n```\n\nThe function will return:\n\n```js\n{\n  index: 0,\n  autoPlay: true,\n  controls: {\n    prev: '.js-prev',\n    next: '.js-next'\n  },\n  dots: '.my-dots'\n}\n```\n\n## installation\n\n`npm install extract-data-options --save`\n\n## usage\n\n### function\n\n`extractDataOptions(element: HTMLElement, namespace: String) : Object`\n\nIf you don't pass a namespace, it will return all data-* options.\n\n### camel case\n\nThis module will automatically convert the attributes names into camelCase. So, an attribute like `data-example-my-option` will be turned into `myOption`.\n\n### nested properties\n\nYou can use nested properties too. Just use a dot (`.`) to define the keypath:\n\n`data-example-prop.show.example=\"hello\"`, will result into:\n\n```js\n{\n  prop: {\n    show: {\n      example: 'hello'\n    }\n  }\n}\n```\n\n**OBS:** Don't worry about this notation with dots. It is a [valid HTML5 standard](https://www.w3.org/TR/html/syntax.html#elements-attributes).\n\n### json\n\nThis module will always attempt to parse the values as JSON, otherwise it will set the value as a String.\n\nThe following options:\n\n```html\n\u003cdiv data-option-boolean=\"true\"\n     data-option-number=\"3.14\"\n     data-option-object='{\"key\": \"value\"}'\n     data-option-array=\"[1, 2, 3]\"\n     data-option-string=\"A simple string\"\u003e\n\u003c/div\u003e\n```\n\nWill produce the result below:\n\n```js\n{\n  boolean: true,\n  number: 3.14,\n  object: {key: 'value'},\n  array: [1, 2, 3],\n  string: 'A simple string'\n}\n```\n\n## tests\n\nInstall all dependencies:\n\n`npm install`\n\nRun the tests:\n\n`npm test`\n\n## license\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsantiago%2Fextract-data-options","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgsantiago%2Fextract-data-options","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsantiago%2Fextract-data-options/lists"}