Skip to content

Commit 2ddf496

Browse files
committed
addressed PR comments - created function to get attributes
1 parent fa00a10 commit 2ddf496

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ interface Config {
6666
position?: 'before' | 'after'
6767
removeTarget?: boolean
6868
}
69-
attributes?: any
69+
attributes?: { [name: string]: string }
7070
}
7171
```
7272

src/core/base-plugin.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,27 @@ export class BasePlugin {
6666
}
6767
}
6868

69+
protected getAttributesString(config: Config): string {
70+
if (config.attributes && typeof config.attributes === 'object') {
71+
return (
72+
' ' +
73+
Object.keys(config.attributes)
74+
.map((key) => `${key}="${config.attributes![key] || ''}"`)
75+
.join(' ')
76+
)
77+
}
78+
79+
if (config.attributes === undefined && config.attributes === null) {
80+
throw new Error(
81+
`Please provide a key/value object if intending to use the attributes option, not ${
82+
config.attributes
83+
}`,
84+
)
85+
}
86+
87+
return ''
88+
}
89+
6990
protected addStyle({
7091
html,
7192
htmlFileName,
@@ -75,25 +96,9 @@ export class BasePlugin {
7596
htmlFileName: string
7697
style: string
7798
}) {
78-
let attributesString = ''
79-
80-
if (this.config.attributes) {
81-
if (
82-
typeof this.config.attributes !== 'object' &&
83-
this.config.attributes === null
84-
) {
85-
throw new Error(
86-
`Please provide a key/value object if intending to use the attributes option`,
87-
)
88-
}
89-
90-
Object.keys(this.config.attributes).map((key) => {
91-
const value = this.config.attributes[key] || ''
92-
attributesString += ` ${key}="${value}"`
93-
})
94-
}
95-
96-
const styleString = `<style${attributesString}>${style}</style>`
99+
const styleString = `<style${this.getAttributesString(
100+
this.config,
101+
)}>${style}</style>`
97102

98103
const replaceValues = [styleString, this.replaceConfig.target]
99104

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface Config {
1414
filter?(fileName: string): boolean
1515
leaveCSSFile?: boolean
1616
replace?: ReplaceConfig
17-
attributes?: any
17+
attributes?: { [name: string]: string }
1818
}
1919

2020
export interface FileCache {

0 commit comments

Comments
 (0)