HTMLのdata属性を取得して、特定の関数を実行するためのプラグイン。 同じプラグインで、複数オプションで使いたい場合にHTMLを修正するだけでプラグインに渡すオプションを変えることができます。
A plugin that takes the data attribute of HTML and executes a specific function. If you want to use the same plugin with multiple options, you can simply modify the HTML to change the options passed to the plugin.
npm install process-data-attr-as-func
yarn add process-data-attr-as-func
<ul class="menu">
<li class="menu-item" data-color="red">menu1</li>
<li class="menu-item" data-color="yellow">menu2</li>
<li class="menu-item" data-color="pink">menu3</li>
<li class="menu-item" data-color="blue">menu4</li>
</ul>
<p
data-num="1"
data-str="test"
data-boo="true"
data-obj="{'test': 1}"
data-ary="[1, 2, 3]"
data-func="testFunction"
>
</p>
それぞれ変換Number
, String
, Boolean
, Array
, Object
, 'Function'に変換されます。
Array
, Object
はJSON.parse('')で変換されます。keyは'
(シングルクオート)で囲います。
Function
はOptionのfunctionsもしくはGlobalにその名前の関数が存在していた場合に関数に変換されます。
import { ProcessDataAttrAsFunc } from 'process-data-attr-as-func'
interface Test {
color: string
}
const body = new ProcessDataAttrAsFunc<Test>(
'body',
(element, props: Partial<Test>) => {
element.style.backgroundColor = props.color!
}
)
const menu = new ProcessDataAttrAsFunc<Test>(
'.menu-item',
(element, props: Partial<Test>) => {
element.style.backgroundColor = props.color!
}
)
プラグイン実行で返ってくるdataはPartial<Test>
となります。
※ HTMLタグのdataから取得するため、存在が担保できないためPartial
にしています。
<script src="../js/process-data-attr-as-func.min.js"></script>
const body = new ProcessDataAttrAsFunc(
'body',
(element, props) => {
element.style.backgroundColor = props.color
}
)
const menu = new ProcessDataAttrAsFunc(
'.menu-item',
(element, props) => {
element.style.backgroundColor = props.color
}
)
Option Name | Description | Type | default |
---|---|---|---|
element | タグ | HTMLElement string | - |
plugin | 関数 | (element: HTMLElement, value: Partial) => void | - |
functions | dataで取れた文字列がここのオブジェクトに存在していた場合、その関数をオプションとして渡します | { [key: string] : () => void } | {} |
export type Functions = { [key: string] : () => void }
export type PluginType<T> = (element: HTMLElement, value: Partial<T>) => void
export interface ProcessDataAttrAsFuncOptions<T> {
element: HTMLElement
plugin: PluginType<T>
functions?: Functions
}
#convertFromString(value: string): Value {
// 数値の場合
if(/^[+,-]?([1-9]\d*|0)(\.\d+)?$/.test(value)) {
return parseFloat(value)
}
// 配列 or オブジェクトの場合
if(/^(\[|\{)/g.test(value)) {
try {
return JSON.parse(value.replace(/\'/g,'\"'));
} catch(e) {
}
}
// Booleanの場合
if(value === 'true' || value === 'false') {
return value === 'true'
}
// 関数の場合
const functions = this.#functions || globalThis
if (typeof functions[value] === 'function') {
return functions[value]
}
return value
}
yarn dev
yarn serve
npm run build
or
yarn build