-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
normalizeMediaQuery: Parses query into a workable object
- Loading branch information
1 parent
43373e6
commit 85c57d2
Showing
4 changed files
with
85 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 17 additions & 12 deletions
29
packages/atomic-layout-core/src/utils/styles/normalizeQuery/normalizeQuery.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,25 @@ | ||
import { Numeric, Breakpoint } from '../../../const/defaultOptions' | ||
import isset from '../../functions/isset' | ||
import toDashedString from '../../strings/toDashedString' | ||
import toLowerCaseFirst from '../../strings/toLowerCaseFirst' | ||
|
||
export interface NormalizedQueryParam { | ||
prefix: string | ||
name: string | ||
value: Numeric | ||
} | ||
|
||
/** | ||
* Normalizes given media query object to a list of [propName, propValue]. | ||
* @example | ||
* normalizeQuery({ minWidth: 120 }) | ||
* // [['min-width', 120]] | ||
*/ | ||
export default function normalizeQuery( | ||
queryProps: Breakpoint, | ||
): Array<[string, Numeric]> { | ||
return Object.entries<Numeric>(queryProps) | ||
.filter(([_, propValue]) => isset(propValue)) | ||
.map<[string, Numeric]>(([propName, propValue]) => [ | ||
toDashedString(propName), | ||
propValue, | ||
]) | ||
breakpoint: Breakpoint, | ||
): NormalizedQueryParam[] { | ||
return Object.entries<Numeric>(breakpoint) | ||
.filter(([_, value]) => isset(value)) | ||
.map(([propName, value]) => { | ||
const [_, prefix, restName] = propName.match(/(min|max)?(.+)/) | ||
const normalizedName = toLowerCaseFirst(restName) | ||
|
||
return { prefix, name: normalizedName, value } | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters