Skip to content

Commit

Permalink
fix: dasherize after leading underscore
Browse files Browse the repository at this point in the history
  • Loading branch information
DSchau committed Feb 12, 2018
1 parent 563b271 commit 3836521
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 9 additions & 3 deletions packages/gatsby/src/schema/__tests__/create-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ describe(`createKey`, () => {
it(`replaces invalid characters`, () => {
;[
[`/hello`, `_hello`],
[`~/path/to/some/module`, `_path_to_some_module`],
[`/*`, `_`],
[`/*.js`, `_js`],
[`~/path/to/some/module`, `_-path-to-some-module`],
[`/*`, `_-`],
[`/*.js`, `_--js`],
].forEach(([input, output]) => {
expect(createKey(input)).toBe(output)
})
})

it(`does not generate same key for different input`, () => {
;[[`/*.js`, `*js`]].forEach(([one, two]) => {
expect(createKey(one)).not.toBe(createKey(two))
})
})
})
4 changes: 2 additions & 2 deletions packages/gatsby/src/schema/create-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ module.exports = (key: string): string => {

const replaced = key.replace(nonAlphaNumericExpr, `_`)

// TODO: figure out what to replace this with _OR_ change the expr
// key is invalid; normalize with a leading underscore and dasherize rest
if (replaced.match(/^__/)) {
return replaced.replace(/^_{2,}/, `_`)
return replaced.replace(/_/g, (char, index) => (index === 0 ? `_` : `-`))
}

return replaced
Expand Down

0 comments on commit 3836521

Please sign in to comment.