Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom contextSeparator fix #1008

Merged
merged 1 commit into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function mergeHashes(source, target, options = {}, resetValues = {}) {
const fullKeyPrefix = options.fullKeyPrefix || ''
const keySeparator = options.keySeparator || '.'
const pluralSeparator = options.pluralSeparator || '_'
const contextSeparator = options.contextSeparator || '_'

for (const key in source) {
const hasNestedEntries =
Expand Down Expand Up @@ -180,7 +181,9 @@ function mergeHashes(source, target, options = {}, resetValues = {}) {
const pluralMatch = key !== singularKey

// support for context in keys
const contextRegex = /_([^_]+)?$/
const contextRegex = new RegExp(
`\\${contextSeparator}([^\\${contextSeparator}]+)?$`
)
const contextMatch = contextRegex.test(singularKey)
const rawKey = singularKey.replace(contextRegex, '')

Expand Down
26 changes: 26 additions & 0 deletions test/helpers/mergeHashes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ describe('mergeHashes helper function', () => {
done()
})

it('restores context keys when the singular one exists (custom contextSeparator)', (done) => {
const source = { key1: '', 'key1|context': 'value1' }
const target = { key1: '' }
const res = mergeHashes(source, target, { contextSeparator: '|' })

assert.deepEqual(res.new, { key1: '', 'key1|context': 'value1' })
assert.deepEqual(res.old, {})
assert.strictEqual(res.mergeCount, 1)
assert.strictEqual(res.pullCount, 1)
assert.strictEqual(res.oldCount, 0)
done()
})

it('does not restore context keys when the singular one does not', (done) => {
const source = { key1: '', key1_context: 'value1' }
const target = { key2: '' }
Expand All @@ -150,6 +163,19 @@ describe('mergeHashes helper function', () => {
done()
})

it('does not restore context keys when the singular one does not (custom contextSeparator)', (done) => {
const source = { key1: '', 'key1|context': 'value1' }
const target = { key2: '' }
const res = mergeHashes(source, target, { contextSeparator: '|' })

assert.deepEqual(res.new, { key2: '' })
assert.deepEqual(res.old, { key1: '', 'key1|context': 'value1' })
assert.strictEqual(res.mergeCount, 0)
assert.strictEqual(res.pullCount, 0)
assert.strictEqual(res.oldCount, 2)
done()
})

it('works with deep objects', (done) => {
const source = {
key1: 'value1',
Expand Down