Skip to content

Commit 3abe515

Browse files
committed
chore: move version checks to constants
1 parent f2ba578 commit 3abe515

File tree

5 files changed

+22
-54
lines changed

5 files changed

+22
-54
lines changed

src/components/__tests__/FontAwesomeIcon.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import semver from 'semver'
88
import {
99
ICON_PACKS_STARTING_VERSION,
1010
SVG_CORE_VERSION,
11-
} from '../../utils/get-class-list-from-props'
11+
} from '../../utils/constants'
1212
import {
1313
coreHasFeature,
1414
REFERENCE_ICON_USING_STRING,

src/utils/__tests__/get-class-list-from-props.test.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import {
66
import semver from 'semver'
77

88
import { FontAwesomeIconProps } from '../../types/icon-props'
9-
import {
10-
getClassListFromProps,
11-
ICON_PACKS_STARTING_VERSION,
12-
SVG_CORE_VERSION,
13-
} from '../get-class-list-from-props'
9+
import { ICON_PACKS_STARTING_VERSION, SVG_CORE_VERSION } from '../constants'
10+
import { getClassListFromProps } from '../get-class-list-from-props'
1411

1512
describe('get class list', () => {
1613
const props = {

src/utils/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import {
33
RotateProp,
44
SizeProp,
55
} from '@fortawesome/fontawesome-svg-core'
6+
import { version as SVGCorePackageVersion } from '@fortawesome/fontawesome-svg-core/package.json'
7+
import semver from 'semver'
8+
9+
export const ICON_PACKS_STARTING_VERSION = '7.0.0-alpha1'
10+
11+
// Try to get version from installed package first, fallback to env var, then default
12+
export const SVG_CORE_VERSION =
13+
SVGCorePackageVersion || process.env.FA_VERSION || '7.0.0-alpha8'
14+
15+
// Cache the version check result since it never changes during runtime
16+
export const IS_VERSION_7_OR_LATER = semver.gte(
17+
SVG_CORE_VERSION,
18+
ICON_PACKS_STARTING_VERSION,
19+
)
620

721
export const ANIMATION_CLASSES = {
822
beat: 'fa-beat',

src/utils/get-class-list-from-props.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
import { version as SVGCorePackageVersion } from '@fortawesome/fontawesome-svg-core/package.json'
2-
import semver from 'semver'
3-
41
import {
52
ANIMATION_CLASSES,
3+
IS_VERSION_7_OR_LATER,
64
PULL_CLASSES,
75
ROTATE_CLASSES,
86
SIZE_CLASSES,
97
STYLE_CLASSES,
108
} from './constants'
119
import { FontAwesomeIconProps } from '../types/icon-props'
1210

13-
export const ICON_PACKS_STARTING_VERSION = '7.0.0-alpha1'
14-
15-
// Try to get version from installed package first, fallback to env var, then default
16-
export const SVG_CORE_VERSION =
17-
SVGCorePackageVersion || process.env.FA_VERSION || '7.0.0-alpha8'
18-
19-
// Cache the version check result since it never changes during runtime
20-
const IS_VERSION_7_OR_LATER = semver.gte(
21-
SVG_CORE_VERSION,
22-
ICON_PACKS_STARTING_VERSION,
23-
)
24-
2511
/**
2612
* Get CSS class list from a props object.
2713
* This function maps our React props to the corresponding CSS class names from Font Awesome.

src/utils/normalize-icon-args.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,9 @@ export function normalizeIconArgs(
2323
return undefined
2424
}
2525

26-
switch (true) {
27-
// If we already have an `IconDefinition` object, which means it was probably
28-
// imported directly from an icon svg package, return it immediately since
29-
// it already has everything that it needs to be rendered
30-
case isIconDefinition(icon): {
31-
return icon
32-
}
33-
// If the `parse.icon` function is available from svg-core, use it to parse the icon.
34-
// This should be the final case since the function is available since around 2020.
35-
// Any cases after this are fallbacks for people using old versions of svg-core.
36-
case !!faParse.icon: {
37-
return faParse.icon(icon)
38-
}
39-
// If we have an icon name and a prefix, return it as an `IconLookup` object
40-
case typeof icon === 'object' &&
41-
'prefix' in icon &&
42-
'iconName' in icon &&
43-
!!icon.prefix &&
44-
!!icon.iconName: {
45-
return icon
46-
}
47-
// If it's an array with length of two, use the first item as prefix, second as icon name
48-
case Array.isArray(icon) && icon.length === 2: {
49-
return { prefix: icon[0], iconName: icon[1] }
50-
}
51-
// If the icon is a string, we assume it's an icon name and return it as an `IconLookup`
52-
case typeof icon === 'string': {
53-
return { prefix: 'fas', iconName: icon }
54-
}
55-
// If none of the above cases match, we return undefined
56-
default: {
57-
return undefined
58-
}
26+
if (isIconDefinition(icon)) {
27+
return icon
5928
}
29+
30+
return faParse.icon(icon)
6031
}

0 commit comments

Comments
 (0)