Skip to content

Commit

Permalink
Merge pull request #44 from kanga333/fix-16
Browse files Browse the repository at this point in the history
Fix a problem with escape characters
  • Loading branch information
kanga333 committed May 6, 2021
2 parents c4e68cf + 45ad728 commit 52efcfc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
32 changes: 32 additions & 0 deletions __tests__/mapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ describe('JSONMapper', () => {
}).toThrow()
})

describe('Complex Regular Expression Key', () => {
const mapper = new JSONMapper(
'{"^key(-[\\\\w-]*)?$":{"env1":"value1"},".*":{"env1":"value2"}}',
'first_match'
)

it('holds the order of keys', () => {
const expects = ['^key(-[\\w-]*)?$', '.*']
for (const [index, pair] of mapper.pairs.entries()) {
expect(pair.key).toBe(expects[index])
}
})

it('can be matched with regular expressions', () => {
const got = mapper.match('key-match-string')
if (!got) {
throw new Error('No match')
}
expect(got.key).toBe('^key(-[\\w-]*)?$')
expect(got.variables).toMatchObject(new Map([['env1', 'value1']]))
})

it('can not be matched with regular expressions', () => {
const got = mapper.match('key-not+match+string')
if (!got) {
throw new Error('No match')
}
expect(got.key).toBe('.*')
expect(got.variables).toMatchObject(new Map([['env1', 'value2']]))
})
})

describe('Overwrite Matcher', () => {
const overwrite = new JSONMapper(
'{"k.y":{"env1":"value1","env2":"value2"},".*":{"env2":"overwrite"}}',
Expand Down
3 changes: 2 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ export class JSONMapper extends Mapper {
const tmpPairs = new Array<KeyVariablesPair>()
const minify = rawJSON.replace(/\s/g, '')
for (const key in parsed) {
const json_key = JSON.stringify(key)
//Gets the position of the input keys to keep their order.
const idx = minify.indexOf(`"${key}":{`)
const idx = minify.indexOf(`${json_key}:{`)
if (idx === -1) {
throw new Error(`Failed to get key index of ${key}`)
}
Expand Down

0 comments on commit 52efcfc

Please sign in to comment.