包详细信息

html-attribute-sorter

shufo75.8kMIT0.4.3

An html attribute sorter

html, sorter

自述文件

npm CI codecov

html-attribute-sorter

An html attribute sorter.

You can sort attributes by alphabetical, code guide, vue/attributes-order, idiomatic or any custom order using regex.

Installation

$ npm install html-attribute-sorter
# yarn
$ yarn add html-attribute-sorter

Usage

const { sortAttributes } = require("html-attribute-sorter");

// pass html tags that includes attributes
// default: code guide order
const sorted = sortAttributes(`<img src="foo" class="img b-30" id="img_10">`);
console.log(sorted);
// => <img class="img b-30" id="img_10" src="foo">

// alphabetical order
const sorted = sortAttributes(
  `<img src="foo" alt="title" class="img b-30" id="img_10">`,
  {
    order: "alphabetical",
  }
);
console.log(sorted);
// => <img alt="title" class="img b-30" id="img_10" src="foo">

// idiomatic order
const sorted = sortAttributes(
  `<img src="foo" alt="title" class="img b-30" id="img_10">`,
  {
    order: "idiomatic",
  }
);
console.log(sorted);
// => <img class="img b-30" id="img_10" alt="title" src="foo">

// code guide order
const sorted = sortAttributes(
  `<img src="foo" alt="title" class="img b-30" id="img_10">`,
  {
    order: "code-guide",
  }
);
console.log(sorted);
// => <img class="img b-30" id="img_10" src="foo">

// custom order
const sorted = sortAttributes(
  `<img src="foo" alt="title" class="img b-30" id="img_10">`,
  {
    order: "custom",
    customRegexes: ["id", "class", "sr.+", "alt"], // you can use regex for attritube names
  }
);
console.log(sorted);
// => <img id="img_10" class="img b-30" src="foo" alt="title" >

API

functions

sortAttributes(body: string, options: ISortOption)

body: html tag string e.g. <img src="..." alt="title" class="pg-10">

Interfaces

ISortOption

export interface ISortOption {
  order: string;
  customRegexes?: string[];
}
key value required default
order An order to sort attributes. You can specify alphabetical, code-guide, idiomatic, vuejs or custom. yes code-guide
customRegexes When order is specified to custom, html attributes set here will be used as order. You can use regex for attribute names. e.g. ["data-.+", "class", "src"]. no []

Testing

$ yarn install
$ yarn run test

Benchmarking

$ yarn run benchmark

image

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

LICENSE

MIT

更新日志

Changelog

0.4.3 (2022-12-25)

Miscellaneous

  • deps: update dependency esbuild to ^0.15.0 (86bf865)
  • deps: update pozil/auto-assign-issue action to v1.10.0 (577a966)
  • deps: update pozil/auto-assign-issue action to v1.11.0 (635d4ff)
  • deps: update thollander/actions-comment-pull-request action to v2 (bb16552)

Fixes

  • 🐛 fix: attribute doubles when v-else-if exists (441c815)

0.4.2 (2022-08-09)

Fixes

  • 🐛 unexpected reordering in eattributes with no space (b83fe6d)

0.4.1 (2022-08-08)

Fixes

  • 🐛 does not capture when quotations are not present (2ca7ec8)
  • 🐛 not captured if there is a space around equal (0c7c52e)

0.4.0 (2022-08-07)

Features

  • 🎸 add custom order strategy (64e5639)

0.3.2 (2022-08-06)

Fixes

  • 🐛 do not sort remaining attributes other than spec (d46daa1)

Miscellaneous

  • deps: update peter-evans/commit-comment action to v2 (5a00b31)

0.3.1 (2022-08-06)

Fixes

  • 🐛 change parameter value from code_guide to code-guide (f2b49ad)

Miscellaneous

  • deps: update googlecloudplatform/release-please-action action to v3 (e0a6b9a)
  • deps: update jest monorepo to v28 (8d1a01b)

0.3.0 (2022-08-06)

Features

  • 🎸 add vuejs sorting strategy (3d4219f)

Fixes

  • 🐛 unexpected custom directive position for vuejs order (c40d0ad)

0.2.1 (2022-08-06)

Fixes

  • 🐛 regex does not match specific tag (6d94431)

Miscellaneous

  • deps: update actions/cache action to v3 (094a395)
  • deps: update actions/checkout action to v3 (b109c93)
  • deps: update actions/setup-node action to v3 (0ca780e)
  • deps: update codecov/codecov-action action to v3 (38b87de)

0.2.0 (2022-08-05)

Features

  • 🎸 change name of option parameter (5b5951b)

0.1.0 (2022-08-05)

Miscellaneous

  • 🤖 require min node engine version as 12.0.0 (6526bbc)
  • deps: update pozil/auto-assign-issue action to v1.9.0 (fc1aa07)
  • Initialized (5fb24cf)