antfu / case-police

🚨 Make the case correct, PLEASE!
MIT License
1.15k stars 80 forks source link

fix: deduplicate items #147

Closed johannpinson closed 1 year ago

johannpinson commented 1 year ago

Hi,

This is a little update to remove a duplicate entry for CodePen from the previous release and the PR #145. After running a litte test, I see another duplicate item for CodeSandbox.

I didn't push my test because it's the first I write, so I'm not sure it was done well:

describe('duplicate', () => {
  it('detection', async () => {
    const presets = await Promise.all(
      [
        resolvePreset('abbreviates'),
        resolvePreset('brands'),
        resolvePreset('general'),
        resolvePreset('products'),
        resolvePreset('softwares'),
      ],
    )
    const count = presets.reduce((sum, item) => sum + Object.keys(item).length, 0)

    const allPresets = await loadAllPresets()
    const allPresetsCount = Object.keys(allPresets).length

    expect(count).toEqual(allPresetsCount)
  })
})
 FAIL  test/index.test.ts > duplicate > detection
AssertionError: expected 477 to deeply equal 476
 ❯ test/index.test.ts:66:18
     64|     const allPresetsCount = Object.keys(allPresets).length
     65| 
     66|     expect(count).toEqual(allPresetsCount)
       |                  ^
     67|   })
     68| })

  - Expected   "476"
  + Received   "477"

I could have used a while/for statement to remove entry until detect the duplicate item, but I don't know if it must be inside the test 🤷‍♂️.

johannpinson commented 1 year ago

Updated version of the test to get the duplicate entry

describe('duplicate', () => {
  it('detection', async () => {
    const presets = await Promise.all(
      [
        resolvePreset('abbreviates'),
        resolvePreset('brands'),
        resolvePreset('general'),
        resolvePreset('products'),
        resolvePreset('softwares'),
      ],
    )

    const allPresets = await loadAllPresets()

    presets.forEach((preset) => {
      Object.keys(preset).forEach((key) => {
        const index = Object.keys(allPresets).findIndex(k => k === key)

        if (index >= 0)
          delete allPresets[Object.keys(allPresets)[index]]
        else
          throw new Error(key)
      })
    })

    expect(Object.keys(allPresets).length).toEqual(0)
  })
})
 FAIL  test/index.test.ts > duplicate > detection
Error: sql
 ❯ test/index.test.ts:77:16
     75|           delete allPresets[Object.keys(allPresets)[index]]
     76|         else 
     77|           throw new Error(key)
       |                ^
     78|          
     79|       })
 ❯ test/index.test.ts:69:26
 ❯ test/index.test.ts:68:12

sql can be found inside the abbreviates.json and softwares.json