(input: string, exists: (input: string) => boolean)
| 860 | ] |
| 861 | |
| 862 | function* findRoots(input: string, exists: (input: string) => boolean): Iterable<Root> { |
| 863 | // If there is an exact match, then that's the root. |
| 864 | if (exists(input)) { |
| 865 | yield [input, null] |
| 866 | } |
| 867 | |
| 868 | // Otherwise test every permutation of the input by iteratively removing |
| 869 | // everything after the last dash. |
| 870 | let idx = input.lastIndexOf('-') |
| 871 | |
| 872 | // Determine the root and value by testing permutations of the incoming input. |
| 873 | // |
| 874 | // In case of a candidate like `bg-red-500`, this looks like: |
| 875 | // |
| 876 | // `bg-red-500` -> No match |
| 877 | // `bg-red` -> No match |
| 878 | // `bg` -> Match |
| 879 | while (idx > 0) { |
| 880 | let maybeRoot = input.slice(0, idx) |
| 881 | |
| 882 | if (exists(maybeRoot)) { |
| 883 | let root: Root = [maybeRoot, input.slice(idx + 1)] |
| 884 | |
| 885 | // If the leftover value is an empty string, it means that the value is an |
| 886 | // invalid named value, e.g.: `bg-`. This makes the candidate invalid and we |
| 887 | // can skip any further parsing. |
| 888 | if (root[1] === '') break |
| 889 | |
| 890 | // Edge case: `@-…` is not valid as a variant or a utility so we want to |
| 891 | // skip if an `@` is followed by a `-`. Otherwise `@-2xl:flex` and |
| 892 | // `@-2xl:flex` would be considered the same. |
| 893 | if (root[0] === '@' && exists('@') && input[idx] === '-') break |
| 894 | |
| 895 | yield root |
| 896 | } |
| 897 | |
| 898 | idx = input.lastIndexOf('-', idx - 1) |
| 899 | } |
| 900 | |
| 901 | // Try '@' variant after permutations. This allows things like `@max` of `@max-foo-bar` |
| 902 | // to match before looking for `@`. |
| 903 | if (input[0] === '@' && exists('@')) { |
| 904 | yield ['@', input.slice(1)] |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | export function printCandidate(designSystem: DesignSystem, candidate: Candidate) { |
| 909 | let parts: string[] = [] |
no outgoing calls
no test coverage detected