Skip to content

Commit d8ef7af

Browse files
authored
Merge pull request #5 from Oudwins/import-refactor
feat: better support & docs for providing a custom configuration or cache
2 parents a7c8968 + 0575845 commit d8ef7af

File tree

8 files changed

+156
-24
lines changed

8 files changed

+156
-24
lines changed

README.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Utility function to efficiently merge Tailwind CSS classes in Golang without sty
1010
import (
1111
"fmt"
1212

13-
"github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
13+
twmerge "github.com/Oudwins/tailwind-merge-go"
1414
)
1515

1616
func main() {
@@ -34,15 +34,74 @@ func main() {
3434

3535
- See [tailwind-merge](https://github.com/dcastil/tailwind-merge/blob/v2.2.1/docs/limitations.md)
3636

37+
## Advanced Examples
38+
39+
You might also want to check out the advanced example at `/cmd/examples/advanced`
40+
41+
### Provide Your Own or Extend Default Config
42+
43+
```go
44+
import (
45+
// Note the import path here is different from the default path. This is so you have access to all the custom functions, structs, etc that are used to build the twmerge config
46+
twmerge "github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
47+
)
48+
var TwMerger twmerge.TwMergeFn
49+
func main() {
50+
// get the default config
51+
config := twmerge.MakeDefaultConfig()
52+
53+
// do your modifications here
54+
55+
// create the merger
56+
TwMerger = twmerge.CreateTwMerge(config, nil) // config, cache (if nil default will be used)
57+
58+
59+
// example usage
60+
m := TwMerger("px-4 px-10", "p-20")
61+
fmt.Println(m) // output: "p-20"
62+
}
63+
```
64+
65+
### Provide your own Cache
66+
67+
The default cache is a LRU Cache and should be acceptable for most use cases. However, you might want to provide your own cache or modify the default creation parameters. Your cache must implement the interface defined at `/pkg/cache/cache.go`
68+
69+
```go
70+
type ICache interface {
71+
Get(string) string
72+
Set(string, string) // key, value
73+
}
74+
```
75+
76+
Here is an example of manually creating the default cache with a custom max capacity
77+
78+
```go
79+
import (
80+
twmerge "github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
81+
lru "github.com/Oudwins/tailwind-merge-go/pkg/lru"
82+
)
83+
var TwMerger twmerge.TwMergeFn
84+
func main() {
85+
customCapacity := 10000
86+
cache := lru.make(customCapacity)
87+
88+
89+
// create the merger
90+
TwMerger = twmerge.CreateTwMerge(nil, cache) // config, cache (if nil default will be used)
91+
92+
// example usage
93+
m := TwMerger("px-4 px-10", "p-20")
94+
fmt.Println(m) // output: "p-20"
95+
}
96+
```
97+
3798
## Contributing
3899

39100
Checkout the [contributing docs](./CONTRIBUTING.md)
40101

41102
## Roadmap
42103

43-
- Improve current docs
44104
- Improve cache concurrent performance by locking on a per key basis -> https://github.com/EagleChen/mapmutex
45-
- Split code into multiple pkgs so in the twmerge pkg there is only the Merge & CreateTailwindMerge functions
46105
- Build the class map on initialization and have a simple config style
47106
- replace regex with more performant solution
48107
- Move arbitrary value delimeters '[' & ']' to config somehow?
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
lru "github.com/Oudwins/tailwind-merge-go/pkg/lru"
7+
twmerge "github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
8+
)
9+
10+
var TwMerger twmerge.TwMergeFn
11+
12+
func main() {
13+
// get the default config
14+
config := twmerge.MakeDefaultConfig()
15+
16+
// make cache
17+
cache := lru.Make(10000)
18+
19+
// do your modifications here
20+
21+
// create the merger
22+
TwMerger = twmerge.CreateTwMerge(config, cache)
23+
24+
// example usage
25+
m := TwMerger("px-4 px-10", "p-20")
26+
fmt.Println(m) // output: "p-20"
27+
}

cmd/examples/default/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
twmerge "github.com/Oudwins/tailwind-merge-go"
7+
)
8+
9+
func main() {
10+
// mainly used for manual tests
11+
12+
// example usage
13+
m := twmerge.Merge("px-4 px-10", "p-20")
14+
fmt.Println(m)
15+
}

cmd/main/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
6+
twmerge "github.com/Oudwins/tailwind-merge-go"
77
)
88

99
func main() {

pkg/cache/cache.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cache
2+
3+
type ICache interface {
4+
Get(string) string
5+
Set(string, string)
6+
}

pkg/cache/lru.go renamed to pkg/lru/lru.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package lru
22

3-
import "sync"
3+
import (
4+
"sync"
5+
6+
"github.com/Oudwins/tailwind-merge-go/pkg/cache"
7+
)
48

59
type node struct {
610
key string
@@ -9,11 +13,6 @@ type node struct {
913
next *node
1014
}
1115

12-
type Cache interface {
13-
Get(string) string
14-
Set(string, string)
15-
}
16-
1716
type LRU struct {
1817
maxCapacity int
1918
capacity int
@@ -81,7 +80,7 @@ func (lru *LRU) remove(n *node) {
8180
lru.capacity--
8281
}
8382

84-
func Make(maxCapacity int) Cache {
83+
func Make(maxCapacity int) cache.ICache {
8584
head := &node{}
8685
tail := &node{}
8786
tail.next = head

pkg/twmerge/create-tailwind-merge.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@ package twmerge
33
import (
44
"strings"
55

6-
lru "github.com/Oudwins/tailwind-merge-go/pkg/cache"
6+
cache "github.com/Oudwins/tailwind-merge-go/pkg/cache"
7+
lru "github.com/Oudwins/tailwind-merge-go/pkg/lru"
78
)
89

9-
func CreateTwMerge(config *TwMergeConfig, cache lru.Cache) func(args ...string) string {
10-
if config == nil {
11-
config = MakeDefaultConfig()
12-
}
13-
if cache == nil {
14-
cache = lru.Make(config.MaxCacheSize)
15-
}
10+
type TwMergeFn func(args ...string) string
1611

17-
splitModifiers := MakeSplitModifiers(config)
12+
func CreateTwMerge(config *TwMergeConfig, cache cache.ICache) TwMergeFn {
1813

19-
getClassGroupId := MakeGetClassGroupId(config)
14+
var fnToCall TwMergeFn
15+
var splitModifiers SplitModifiersFn
16+
var getClassGroupId GetClassGroupIdfn
17+
var mergeClassList func(classList string) string
2018

21-
mergeClassList := MakeMergeClassList(config, splitModifiers, getClassGroupId)
22-
23-
return func(args ...string) string {
19+
merger := func(args ...string) string {
2420
classList := strings.Join(args, " ")
2521
cached := cache.Get(classList)
2622
if cached != "" {
@@ -31,6 +27,29 @@ func CreateTwMerge(config *TwMergeConfig, cache lru.Cache) func(args ...string)
3127
cache.Set(classList, merged)
3228
return merged
3329
}
30+
31+
init := func(args ...string) string {
32+
if config == nil {
33+
config = MakeDefaultConfig()
34+
}
35+
if cache == nil {
36+
cache = lru.Make(config.MaxCacheSize)
37+
}
38+
39+
splitModifiers = MakeSplitModifiers(config)
40+
41+
getClassGroupId = MakeGetClassGroupId(config)
42+
43+
mergeClassList = MakeMergeClassList(config, splitModifiers, getClassGroupId)
44+
45+
fnToCall = merger
46+
return fnToCall(args...)
47+
}
48+
49+
fnToCall = init
50+
return func(args ...string) string {
51+
return fnToCall(args...)
52+
}
3453
}
3554

3655
var Merge = CreateTwMerge(nil, nil)

twmerge.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package twmerge
2+
3+
import (
4+
twmerge "github.com/Oudwins/tailwind-merge-go/pkg/twmerge"
5+
)
6+
7+
var Merge = twmerge.Merge

0 commit comments

Comments
 (0)