|
| 1 | +package slugger |
| 2 | + |
| 3 | +// Language defines the language used for slugification. |
| 4 | +type Language int |
| 5 | + |
| 6 | +// Supported languages for slugification. |
| 7 | +const ( |
| 8 | + English Language = iota |
| 9 | + German |
| 10 | + Spanish |
| 11 | +) |
| 12 | + |
| 13 | +// Suffix defines the type of suffix to append to slugs. |
| 14 | +type Suffix int |
| 15 | + |
| 16 | +// Different suffix strategies. |
| 17 | +const ( |
| 18 | + None Suffix = iota |
| 19 | + Numbered |
| 20 | + TimestampBased |
| 21 | + HashBased |
| 22 | +) |
| 23 | + |
| 24 | +// Config holds configuration options for the slugger. |
| 25 | +type Config struct { |
| 26 | + Lowercase bool |
| 27 | + Separator string |
| 28 | + MaxLength int |
| 29 | + Language Language |
| 30 | + SuffixStrategy Suffix |
| 31 | + SuffixLength int |
| 32 | + TimestampTimezone string |
| 33 | + CounterProvider CounterProviderFunction |
| 34 | +} |
| 35 | + |
| 36 | +// ConfigOption defines a function type for configuring the Slugger. |
| 37 | +type ConfigOption func(*Config) |
| 38 | + |
| 39 | +// WithoutLowercase sets whether slugs should be lowercase. |
| 40 | +func WithoutLowercase() ConfigOption { |
| 41 | + return func(c *Config) { |
| 42 | + c.Lowercase = false |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// WithSeparator sets the separator character for slugs. |
| 47 | +func WithSeparator(separator string) ConfigOption { |
| 48 | + return func(c *Config) { |
| 49 | + c.Separator = separator |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// WithMaxLength sets the maximum length for slugs. |
| 54 | +func WithMaxLength(maxLength int) ConfigOption { |
| 55 | + return func(c *Config) { |
| 56 | + c.MaxLength = maxLength |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +// WithLanguage sets the language for slugification. |
| 62 | +func WithLanguage(language Language) ConfigOption { |
| 63 | + return func(c *Config) { |
| 64 | + c.Language = language |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// WithSuffixStrategy sets the suffix strategy for slugs. |
| 69 | +func WithSuffixStrategy(suffix Suffix) ConfigOption { |
| 70 | + return func(c *Config) { |
| 71 | + c.SuffixStrategy = suffix |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// WithHashSuffixLength sets the length of the hash-based suffix. |
| 76 | +func WithHashSuffixLength(length int) ConfigOption { |
| 77 | + return func(c *Config) { |
| 78 | + c.SuffixStrategy = HashBased |
| 79 | + c.SuffixLength = length |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// WithTimestampTimezone sets the timezone for timestamp-based suffixes. |
| 84 | +func WithTimestampTimezone(timezone string) ConfigOption { |
| 85 | + return func(c *Config) { |
| 86 | + c.SuffixStrategy = TimestampBased |
| 87 | + c.TimestampTimezone = timezone |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// WithNumberedCounterProvider sets the counter provider function for numbered suffixes. |
| 92 | +func WithNumberedCounterProvider(counterProvider CounterProviderFunction) ConfigOption { |
| 93 | + return func(c *Config) { |
| 94 | + c.SuffixStrategy = Numbered |
| 95 | + c.CounterProvider = counterProvider |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func WithNoSuffix() ConfigOption { |
| 100 | + return func(c *Config) { |
| 101 | + c.SuffixStrategy = None |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// NewConfig creates a new Config with the provided options. |
| 106 | +func NewConfig(options ...ConfigOption) *Config { |
| 107 | + config := &Config{ |
| 108 | + Lowercase: true, |
| 109 | + Separator: "-", |
| 110 | + MaxLength: 240, |
| 111 | + Language: English, |
| 112 | + SuffixStrategy: None, |
| 113 | + } |
| 114 | + |
| 115 | + for _, option := range options { |
| 116 | + option(config) |
| 117 | + } |
| 118 | + |
| 119 | + return config |
| 120 | +} |
0 commit comments