Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use arrow functions where possible to help with minification #449

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/lib/class-group-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ interface ClassValidatorObject {

const CLASS_PART_SEPARATOR = '-'

export function createClassGroupUtils(config: GenericConfig) {
export const createClassGroupUtils = (config: GenericConfig) => {
const classMap = createClassMap(config)
const { conflictingClassGroups, conflictingClassGroupModifiers } = config

function getClassGroupId(className: string) {
const getClassGroupId = (className: string) => {
const classParts = className.split(CLASS_PART_SEPARATOR)

// Classes like `-inset-1` produce an empty string as first classPart. We assume that classes for negative values are used correctly and remove it from classParts.
Expand All @@ -37,10 +37,10 @@ export function createClassGroupUtils(config: GenericConfig) {
return getGroupRecursive(classParts, classMap) || getGroupIdForArbitraryProperty(className)
}

function getConflictingClassGroupIds(
const getConflictingClassGroupIds = (
classGroupId: GenericClassGroupIds,
hasPostfixModifier: boolean,
) {
) => {
const conflicts = conflictingClassGroups[classGroupId] || []

if (hasPostfixModifier && conflictingClassGroupModifiers[classGroupId]) {
Expand All @@ -56,10 +56,10 @@ export function createClassGroupUtils(config: GenericConfig) {
}
}

function getGroupRecursive(
const getGroupRecursive = (
classParts: string[],
classPartObject: ClassPartObject,
): GenericClassGroupIds | undefined {
): GenericClassGroupIds | undefined => {
if (classParts.length === 0) {
return classPartObject.classGroupId
}
Expand All @@ -85,7 +85,7 @@ function getGroupRecursive(

const arbitraryPropertyRegex = /^\[(.+)\]$/

function getGroupIdForArbitraryProperty(className: string) {
const getGroupIdForArbitraryProperty = (className: string) => {
if (arbitraryPropertyRegex.test(className)) {
const arbitraryPropertyClassName = arbitraryPropertyRegex.exec(className)![1]
const property = arbitraryPropertyClassName?.substring(
Expand All @@ -103,7 +103,7 @@ function getGroupIdForArbitraryProperty(className: string) {
/**
* Exported for testing only
*/
export function createClassMap(config: Config<GenericClassGroupIds, GenericThemeGroupIds>) {
export const createClassMap = (config: Config<GenericClassGroupIds, GenericThemeGroupIds>) => {
const { theme, prefix } = config
const classMap: ClassPartObject = {
nextPart: new Map<string, ClassPartObject>(),
Expand All @@ -122,12 +122,12 @@ export function createClassMap(config: Config<GenericClassGroupIds, GenericTheme
return classMap
}

function processClassesRecursively(
const processClassesRecursively = (
classGroup: ClassGroup<GenericThemeGroupIds>,
classPartObject: ClassPartObject,
classGroupId: GenericClassGroupIds,
theme: ThemeObject<GenericThemeGroupIds>,
) {
) => {
classGroup.forEach((classDefinition) => {
if (typeof classDefinition === 'string') {
const classPartObjectToEdit =
Expand Down Expand Up @@ -166,7 +166,7 @@ function processClassesRecursively(
})
}

function getPart(classPartObject: ClassPartObject, path: string) {
const getPart = (classPartObject: ClassPartObject, path: string) => {
let currentClassPartObject = classPartObject

path.split(CLASS_PART_SEPARATOR).forEach((pathPart) => {
Expand All @@ -183,14 +183,13 @@ function getPart(classPartObject: ClassPartObject, path: string) {
return currentClassPartObject
}

function isThemeGetter(func: ClassValidator | ThemeGetter): func is ThemeGetter {
return (func as ThemeGetter).isThemeGetter
}
const isThemeGetter = (func: ClassValidator | ThemeGetter): func is ThemeGetter =>
(func as ThemeGetter).isThemeGetter

function getPrefixedClassGroupEntries(
const getPrefixedClassGroupEntries = (
classGroupEntries: Array<[classGroupId: string, classGroup: ClassGroup<GenericThemeGroupIds>]>,
prefix: string | undefined,
): Array<[classGroupId: string, classGroup: ClassGroup<GenericThemeGroupIds>]> {
): Array<[classGroupId: string, classGroup: ClassGroup<GenericThemeGroupIds>]> => {
if (!prefix) {
return classGroupEntries
}
Expand Down
12 changes: 5 additions & 7 deletions src/lib/config-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import { GenericConfig } from './types'

export type ConfigUtils = ReturnType<typeof createConfigUtils>

export function createConfigUtils(config: GenericConfig) {
return {
cache: createLruCache<string, string>(config.cacheSize),
parseClassName: createParseClassName(config),
...createClassGroupUtils(config),
}
}
export const createConfigUtils = (config: GenericConfig) => ({
cache: createLruCache<string, string>(config.cacheSize),
parseClassName: createParseClassName(config),
...createClassGroupUtils(config),
})
2 changes: 1 addition & 1 deletion src/lib/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
isTshirtSize,
} from './validators'

export function getDefaultConfig() {
export const getDefaultConfig = () => {
const colors = fromTheme('colors')
const spacing = fromTheme('spacing')
const blur = fromTheme('blur')
Expand Down
7 changes: 3 additions & 4 deletions src/lib/extend-tailwind-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ConfigExtension, DefaultClassGroupIds, DefaultThemeGroupIds, GenericCon

type CreateConfigSubsequent = (config: GenericConfig) => GenericConfig

export function extendTailwindMerge<
export const extendTailwindMerge = <
AdditionalClassGroupIds extends string = never,
AdditionalThemeGroupIds extends string = never,
>(
Expand All @@ -16,11 +16,10 @@ export function extendTailwindMerge<
>
| CreateConfigSubsequent,
...createConfig: CreateConfigSubsequent[]
) {
return typeof configExtension === 'function'
) =>
typeof configExtension === 'function'
? createTailwindMerge(getDefaultConfig, configExtension, ...createConfig)
: createTailwindMerge(
() => mergeConfigs(getDefaultConfig(), configExtension),
...createConfig,
)
}
4 changes: 2 additions & 2 deletions src/lib/from-theme.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DefaultThemeGroupIds, NoInfer, ThemeGetter, ThemeObject } from './types'

export function fromTheme<
export const fromTheme = <
AdditionalThemeGroupIds extends string = never,
DefaultThemeGroupIdsInner extends string = DefaultThemeGroupIds,
>(key: NoInfer<DefaultThemeGroupIdsInner | AdditionalThemeGroupIds>): ThemeGetter {
>(key: NoInfer<DefaultThemeGroupIdsInner | AdditionalThemeGroupIds>): ThemeGetter => {
const themeGetter = (theme: ThemeObject<DefaultThemeGroupIdsInner | AdditionalThemeGroupIds>) =>
theme[key] || []

Expand Down
4 changes: 2 additions & 2 deletions src/lib/lru-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface LruCache<Key, Value> {
}

// LRU cache inspired from hashlru (https://github.com/dominictarr/hashlru/blob/v1.0.4/index.js) but object replaced with Map to improve performance
export function createLruCache<Key, Value>(maxCacheSize: number): LruCache<Key, Value> {
export const createLruCache = <Key, Value>(maxCacheSize: number): LruCache<Key, Value> => {
if (maxCacheSize < 1) {
return {
get: () => undefined,
Expand All @@ -18,7 +18,7 @@ export function createLruCache<Key, Value>(maxCacheSize: number): LruCache<Key,
let cache = new Map<Key, Value>()
let previousCache = new Map<Key, Value>()

function update(key: Key, value: Value) {
const update = (key: Key, value: Value) => {
cache.set(key, value)
cacheSize++

Expand Down
2 changes: 1 addition & 1 deletion src/lib/merge-classlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IMPORTANT_MODIFIER, sortModifiers } from './parse-class-name'

const SPLIT_CLASSES_REGEX = /\s+/

export function mergeClassList(classList: string, configUtils: ConfigUtils) {
export const mergeClassList = (classList: string, configUtils: ConfigUtils) => {
const { parseClassName, getClassGroupId, getConflictingClassGroupIds } = configUtils

/**
Expand Down
16 changes: 8 additions & 8 deletions src/lib/merge-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ConfigExtension, GenericConfig } from './types'
* @param baseConfig Config where other config will be merged into. This object will be mutated.
* @param configExtension Partial config to merge into the `baseConfig`.
*/
export function mergeConfigs<ClassGroupIds extends string, ThemeGroupIds extends string = never>(
export const mergeConfigs = <ClassGroupIds extends string, ThemeGroupIds extends string = never>(
baseConfig: GenericConfig,
{
cacheSize,
Expand All @@ -14,7 +14,7 @@ export function mergeConfigs<ClassGroupIds extends string, ThemeGroupIds extends
extend = {},
override = {},
}: ConfigExtension<ClassGroupIds, ThemeGroupIds>,
) {
) => {
overrideProperty(baseConfig, 'cacheSize', cacheSize)
overrideProperty(baseConfig, 'prefix', prefix)
overrideProperty(baseConfig, 'separator', separator)
Expand All @@ -37,31 +37,31 @@ export function mergeConfigs<ClassGroupIds extends string, ThemeGroupIds extends
return baseConfig
}

function overrideProperty<T extends object, K extends keyof T>(
const overrideProperty = <T extends object, K extends keyof T>(
baseObject: T,
overrideKey: K,
overrideValue: T[K] | undefined,
) {
) => {
if (overrideValue !== undefined) {
baseObject[overrideKey] = overrideValue
}
}

function overrideConfigProperties(
const overrideConfigProperties = (
baseObject: Partial<Record<string, readonly unknown[]>>,
overrideObject: Partial<Record<string, readonly unknown[]>> | undefined,
) {
) => {
if (overrideObject) {
for (const key in overrideObject) {
overrideProperty(baseObject, key, overrideObject[key])
}
}
}

function mergeConfigProperties(
const mergeConfigProperties = (
baseObject: Partial<Record<string, readonly unknown[]>>,
mergeObject: Partial<Record<string, readonly unknown[]>> | undefined,
) {
) => {
if (mergeObject) {
for (const key in mergeObject) {
const mergeValue = mergeObject[key]
Expand Down
10 changes: 4 additions & 6 deletions src/lib/parse-class-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { GenericConfig } from './types'

export const IMPORTANT_MODIFIER = '!'

export function createParseClassName(config: GenericConfig) {
export const createParseClassName = (config: GenericConfig) => {
const { separator, experimentalParseClassName } = config
const isSeparatorSingleCharacter = separator.length === 1
const firstSeparatorCharacter = separator[0]
const separatorLength = separator.length

// parseClassName inspired by https://github.com/tailwindlabs/tailwindcss/blob/v3.2.2/src/util/splitAtTopLevelOnly.js
function parseClassName(className: string) {
const parseClassName = (className: string) => {
const modifiers = []

let bracketDepth = 0
Expand Down Expand Up @@ -65,9 +65,7 @@ export function createParseClassName(config: GenericConfig) {
}

if (experimentalParseClassName) {
return function parseClassNameExperimental(className: string) {
return experimentalParseClassName({ className, parseClassName })
}
return (className: string) => experimentalParseClassName({ className, parseClassName })
}

return parseClassName
Expand All @@ -78,7 +76,7 @@ export function createParseClassName(config: GenericConfig) {
* - Predefined modifiers are sorted alphabetically
* - When an arbitrary variant appears, it must be preserved which modifiers are before and after it
*/
export function sortModifiers(modifiers: string[]) {
export const sortModifiers = (modifiers: string[]) => {
if (modifiers.length <= 1) {
return modifiers
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/tw-join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function twJoin() {
return string
}

function toValue(mix: ClassNameArray | string) {
const toValue = (mix: ClassNameArray | string) => {
if (typeof mix === 'string') {
return mix
}
Expand Down
Loading
Loading