11import css from "css" ;
22import { assert } from "tsafe/assert" ;
3+ import { objectKeys } from "tsafe/objectKeys" ;
4+ import { exclude } from "tsafe/exclude" ;
35
46export type BreakpointsValues = {
57 unit : string /* em, px ... */ ;
@@ -41,7 +43,7 @@ export function parseBreakpointsValues(rawCssCode: string): BreakpointsValues {
4143 assert ( values . length === 4 ) ;
4244 assert ( prevUnit !== undefined ) ;
4345
44- const [ sm , md , lg , xl ] = values ;
46+ const [ sm , md , lg , xl ] = values . sort ( ) ;
4547
4648 return {
4749 "unit" : prevUnit ,
@@ -53,15 +55,42 @@ export function parseBreakpointsValues(rawCssCode: string): BreakpointsValues {
5355}
5456
5557export function generateBreakpointsValuesTsCode ( breakpointsValues : BreakpointsValues ) : string {
58+ const sortedKeys = objectKeys ( breakpointsValues )
59+ . filter ( exclude ( "unit" ) )
60+ . sort ( ( a , b ) => breakpointsValues [ a ] - breakpointsValues [ b ] ) ;
61+
5662 return [
57- `export const breakpointsValues = {` ,
58- JSON . stringify ( breakpointsValues , null , 2 )
63+ `import { assert } from "tsafe/assert";` ,
64+ `import type { Extends } from "tsafe";` ,
65+ `` ,
66+ `export const breakpointValuesUnit = "em";` ,
67+ `` ,
68+ `export const breakpointKeys = ["xs", ${ sortedKeys
69+ . map ( key => `"${ key } "` )
70+ . join ( ", " ) } ] as const;`,
71+ `` ,
72+ `export type BreakpointKeys = typeof breakpointKeys[number];` ,
73+ `` ,
74+ `export const breakpointValues = {` ,
75+ JSON . stringify (
76+ Object . fromEntries (
77+ ( [ "xs" , ...sortedKeys ] as const ) . map ( key => [
78+ key ,
79+ key === "xs" ? 0 : breakpointsValues [ key ]
80+ ] )
81+ ) ,
82+ null ,
83+ 2
84+ )
5985 . replace ( / ^ { \n / , "" )
6086 . replace ( / \n } $ / , "" )
6187 . split ( "\n" )
6288 . map ( line => line . replace ( / ^ [ ] { 2 } / , "" ) )
6389 . map ( line => ` ${ line } ` )
6490 . join ( "\n" ) ,
65- `} as const;`
91+ `} as const;` ,
92+ `` ,
93+ `assert<Extends<typeof breakpointValues, Record<BreakpointKeys, number>>>();` ,
94+ ``
6695 ] . join ( "\n" ) ;
6796}
0 commit comments