|
1 | 1 | import resolveConfig from '../src/public/resolve-config'
|
2 | 2 | import { createContext } from '../src/lib/setupContextUtils'
|
| 3 | +import bigSign from '../src/util/bigSign' |
| 4 | + |
| 5 | +/** |
| 6 | + * This is a function that the prettier-plugin-tailwindcss would use. It would |
| 7 | + * do the actual sorting based on the classes and order we return from `getClassOrder`. |
| 8 | + * |
| 9 | + * This way the actual sorting logic is done in the plugin which allows you to |
| 10 | + * put unknown classes at the end for example. |
| 11 | + * |
| 12 | + * @param {Array<[string, bigint]>} arrayOfTuples |
| 13 | + * @returns {string} |
| 14 | + */ |
| 15 | +function defaultSort(arrayOfTuples) { |
| 16 | + return arrayOfTuples |
| 17 | + .sort(([, a], [, z]) => { |
| 18 | + if (a === z) return 0 |
| 19 | + if (a === null) return -1 |
| 20 | + if (z === null) return 1 |
| 21 | + return bigSign(a - z) |
| 22 | + }) |
| 23 | + .map(([className]) => className) |
| 24 | + .join(' ') |
| 25 | +} |
| 26 | + |
| 27 | +it('should return a list of tuples with the sort order', () => { |
| 28 | + let input = 'font-bold underline hover:font-medium unknown' |
| 29 | + let config = {} |
| 30 | + let context = createContext(resolveConfig(config)) |
| 31 | + expect(context.getClassOrder(input.split(' '))).toEqual([ |
| 32 | + ['font-bold', expect.any(BigInt)], |
| 33 | + ['underline', expect.any(BigInt)], |
| 34 | + ['hover:font-medium', expect.any(BigInt)], |
| 35 | + |
| 36 | + // Unknown values receive `null` |
| 37 | + ['unknown', null], |
| 38 | + ]) |
| 39 | +}) |
3 | 40 |
|
4 | 41 | it.each([
|
5 | 42 | // Utitlies
|
@@ -33,7 +70,7 @@ it.each([
|
33 | 70 | ])('should sort "%s" based on the order we generate them in to "%s"', (input, output) => {
|
34 | 71 | let config = {}
|
35 | 72 | let context = createContext(resolveConfig(config))
|
36 |
| - expect(context.sortClassList(input.split(' ')).join(' ')).toEqual(output) |
| 73 | + expect(defaultSort(context.getClassOrder(input.split(' ')))).toEqual(output) |
37 | 74 | })
|
38 | 75 |
|
39 | 76 | it.each([
|
@@ -73,6 +110,6 @@ it.each([
|
73 | 110 | (input, output) => {
|
74 | 111 | let config = { prefix: 'tw-' }
|
75 | 112 | let context = createContext(resolveConfig(config))
|
76 |
| - expect(context.sortClassList(input.split(' ')).join(' ')).toEqual(output) |
| 113 | + expect(defaultSort(context.getClassOrder(input.split(' ')))).toEqual(output) |
77 | 114 | }
|
78 | 115 | )
|
0 commit comments