{
  "name": "vue-directive-normalizer",
  "version": "1.0.0",
  "description": "Normalizing directive binding into an option bag.",
  "module": "dist/index.esm.js",
  "main": "dist/index.umd.js",
  "keywords": [
    "vue",
    "directives"
  ],
  "author": "Justineo",
  "license": "MIT",
  "devDependencies": {
    "@rollup/plugin-buble": "^0.21.3",
    "ava": "3",
    "eslint": "^8.14.0",
    "eslint-config-standard": "^17.0.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-n": "^15.2.0",
    "eslint-plugin-promise": "^6.0.0",
    "esm": "^3.2.25",
    "jsdom": "^19.0.0",
    "jsdom-global": "^3.0.2",
    "rollup": "^2.70.2",
    "rollup-plugin-terser": "^7.0.2",
    "vue": "^2.6.14"
  },
  "ava": {
    "require": [
      "esm"
    ]
  },
  "scripts": {
    "test": "ava",
    "lint": "eslint src",
    "build": "rollup -c"
  },
  "readme": "# Vue Directive Normalizer\n\nA tool helps you normalizing directive arguments, modifiers and value.\n\n## Get started\n\n### Installation\n\n```sh\n$ npm i -D vue-directive-normalizer\n```\n\n### Usage\n\n```js\nimport { normalize } from 'vue-directive-normalizer'\n\nconst DRAG_SCHEMA = {\n  arg: 'targets[]',\n  modifiers: {\n    type: ['translate', 'rotate'],\n    axis: [null, 'y', 'x']\n  },\n  value: 'drag'\n})\n\nVue.directive('drag', {\n  bind (el, binding) {\n    const options = normalize(binding, DRAG_SCHEMA)\n\n    // ...other logic...\n  }\n})\n```\n\nSee [*motivation*](#motivation) for schema details.\n\n\n## Motivation\n\nDirectives in Vue.js have the form of `v-directive:arg.modifierA.modifierB=\"value\"`.\n\nFor directive authors, in directive hooks Vue gives us a `binding` object which contains (mainly) the following properties:\n\n* `value: *`\n* `arg: string`\n* `modifiers: Object<string, boolean>`\n\n...along with a bunch of others.\n\nLet's assume we are implementing a `v-drag` directive which support the following options:\n\n* `type: 'translate' | 'rotate' | 'scale'` - The effect of the drag behavior.\n* `targets: string[]` - The `ref` of elements that move along with current “handle” element.\n* `axis: null | 'x' | 'y'` - Wether restrict the moving direction within x/y axis.\n* `drag: function` - The callback when the element is being dragged.\n* `dragstart: function` - The callback when the element starts being dragged.\n* `dragend: function` - The callback when the element stops being dragged.\n\nTo leverage the simplicity and consistency of directive syntax, a possible pattern of designing directive API could be as follows.\n\n### Option bag\n\nTo be most verbose and thorough, we accept an “option bag” for the `value` part:\n\n```html\n<div v-drag=\"{\n  type: 'translate',\n  targets: 'dialog',\n  axis: 'x',\n  drag: handleDrag,\n  dragstart: handleDragStart\n}\"></div>\n```\n\n### Using `modifiers`\n\nFor boolean and enum options, we can leverage `modifiers` for simpler usage:\n\n```html\n<div v-drag.x=\"{\n  targets: 'dialog',\n  drag: handleDrag,\n  dragstart: handleDragStart\n}\"></div>\n```\n\nFor a normalizer to recognize `modifiers` properly, we may need some schema definition like:\n\n```js\n{\n  modifiers: {\n    // list all possible values for string enums\n    type: ['translate', 'rotate', 'scale'],\n    // we can use the first item as the default value,\n    // use `null` to indicate the option isn't required\n    axis: [null, 'x', 'y']\n  }\n}\n```\n\nUsing this schema, we can extract our modifiers into the normalized option bag:\n\n```js\n{\n  type: 'translate', // default value\n  axis: 'x'\n}\n```\n\n### Using `arg`\n\nWe can move sting values into `arg`, lets say we use arg to pass `targets`:\n\n```html\n<div v-drag:dialog.x=\"{\n  drag: handleDrag,\n  dragstart: handleDragStart\n}\"></div>\n```\n\nAs the option name suggests, we may pass multiple refs into `targets`. We can use a micro-syntax here that different refs are separated with `,`:\n\n```html\n<div v-drag:dialog,tooltip.x=\"{\n  drag: handleDrag,\n  dragstart: handleDragStart\n}\"></div>\n```\n\nFor a normalizer to recognize `arg` properly, we may need some schema definition like:\n\n```js\n{\n  // just use the key name that maps the option to the arg\n  arg: 'targets'\n}\n```\n\nFor multiple values, we can use a micro-syntax as follows and the arg will automatically splitted into a string array:\n\n```js\n{\n  arg: 'targets[]'\n}\n```\n\nSo that `v-drag:dialog,tooltip` will produce:\n\n```js\n{\n  targets: ['dialog', 'tooltip']\n}\n```\n\n### Simplifying value\n\nIn some cases, we may want a “main” option that act like a major callback function or something instructs how the directive work in general. Let's say in our `v-drag` directive, it might be the `drag` callback.\n\nSo we can make the `value` implicitly stands for `drag` if `value` isn't a plain object and we are not passing in any other options apart from those can be expressed in `modifiers` and `arg`.\n\neg. When we don't want to pass in the `dragstart` callback, we can just use `handleDrag` as `value`:\n\n```html\n<div v-drag:dialog,tooltip.x=\"handleDrag\"></div>\n```\n\nIn order to map `value` to `drag`, we need to add this to our schema:\n\n```js\n{\n  value: 'drag'\n}\n```\n\nAnd we'll get:\n\n```js\n{\n  drag: handleDrag,\n  targets: ['dialog', 'tooltip'],\n  type: 'translate',\n  axis: 'x'\n}\n```\n\nIf we let the directive do all the work and don't want to provide a callback at all, we can use:\n\n```html\n<div v-drag:dialog,tooltip.x></div>\n```\n\nWhich produces:\n\n```js\n{\n  targets: ['dialog', 'tooltip'],\n  type: 'translate',\n  axis: 'x'\n}\n```\n\n## License\n\nMIT\n"
}