Skip to content

Commit 0575845

Browse files
committed
docs: documented advanced used cases. Providing custom cache or config
1 parent 691484f commit 0575845

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

README.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)