File size: 1,406 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { Dict } from "../utils"
import type { Condition, ConditionConfig } from "./types"

const mapEntries = <T extends Dict, R extends Dict>(
  obj: T,
  fn: (key: string, value: T[keyof T]) => [string, R[keyof R]],
): R => {
  return Object.fromEntries(
    Object.entries(obj).map(([key, value]) => fn(key, value)),
  ) as R
}

export const createConditions = (options: ConditionConfig): Condition => {
  const { breakpoints, conditions: conds = {} } = options

  const conditions = mapEntries(conds, (key, value) => [`_${key}`, value])
  const values = Object.assign({}, conditions, breakpoints.conditions)

  function keys() {
    return Object.keys(values)
  }

  function has(key: string) {
    return keys().includes(key) || /^@|&|&$/.test(key) || key.startsWith("_")
  }

  function sort(paths: string[]) {
    return paths
      .filter((v) => v !== "base")
      .sort((a, b) => {
        const aa = has(a)
        const bb = has(b)
        if (aa && !bb) return 1
        if (!aa && bb) return -1
        return 0
      })
  }

  function expandAtRule(key: string) {
    if (!key.startsWith("@breakpoint")) return key
    return breakpoints.getCondition(key.replace("@breakpoint ", ""))
  }

  function resolve(key: string) {
    return Reflect.get(values, key) || key
  }

  return {
    keys,
    sort,
    has,
    resolve,
    breakpoints: breakpoints.keys(),
    expandAtRule,
  }
}