Skip to content

Commit 357c873

Browse files
EmilTholinadamwathan
authored andcommitted
Add support for passing a function as the prefix
1 parent cd2f855 commit 357c873

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

__tests__/applyClassPrefix.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,27 @@ test('it prefixes classes with the provided prefix', () => {
1616
expect(result.css).toEqual(expected)
1717
expect(result.warnings().length).toBe(0)
1818
})
19+
20+
test('it handles a function as the prefix', () => {
21+
const input = postcss.parse(`
22+
.foo { color: red; }
23+
.apple, .pear { color: green; }
24+
`)
25+
26+
const expected = `
27+
.tw-foo { color: red; }
28+
.apple, .pear { color: green; }
29+
`
30+
31+
const prefixFunc = selector => {
32+
if (selector === '.foo') {
33+
return 'tw-'
34+
}
35+
36+
return ''
37+
}
38+
39+
const result = applyClassPrefix(input, prefixFunc).toResult()
40+
expect(result.css).toEqual(expected)
41+
expect(result.warnings().length).toBe(0)
42+
})

src/util/applyClassPrefix.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export default function(css, prefix) {
2+
const prefixIsFunc = typeof prefix === 'function'
23
css.walkRules(rule => {
3-
rule.selectors = rule.selectors.map(selector => `.${prefix}${selector.slice(1)}`)
4+
rule.selectors = rule.selectors.map(
5+
selector => `.${prefixIsFunc ? prefix(selector) : prefix}${selector.slice(1)}`
6+
)
47
})
58
return css
69
}

0 commit comments

Comments
 (0)