Skip to content

Commit

Permalink
fix: unfold failed if key has included '$' (#13)
Browse files Browse the repository at this point in the history
* fix: unfold failed if key has included '$'

* Create tricky-bulldogs-know.md
  • Loading branch information
mew-ton authored Sep 14, 2023
1 parent e92c9ca commit cf78dfb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-bulldogs-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"json-origami": patch
---

fix: unfold failed if key has included '$'
3 changes: 1 addition & 2 deletions src/unfold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function omitHeadKey(key: string, opt: FixedUnfoldOption): string {
if (typeof k === 'number') {
return opt.arrayIndex === 'dot' ? `${k}` : `\\[${k}\\]`
}
return k
return k.replace(/\$/g, '\\$')
})()

return key.replace(headKey === undefined ? '' : new RegExp(`^${headKey}\\.?`), '')
Expand Down Expand Up @@ -113,7 +113,6 @@ function unfoldInternal(entries: Array<[string, unknown]>, opt: FixedUnfoldOptio
}

const keys = Array.from(new Set(entries.map(([key]) => extractHeadKey(key, opt) as string)))

return keys.reduce((acc, key) => {
const filteredEntries = entries.filter(([k]) => extractHeadKey(k, opt) === key)
const unfolded = unfoldInternal(
Expand Down
20 changes: 20 additions & 0 deletions test/unfold.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,24 @@ describe('unfold', () => {
}
])
})

it('include special characters', () => {
const kv = {
'theme.$color_primary': '#25adc9',
'theme.$color_secondary': '#333333',
'theme.$color_black': '#191919',
'feature.@mention': 'true'
}

expect(unfold(kv)).toEqual({
feature: {
'@mention': 'true'
},
theme: {
$color_primary: '#25adc9',
$color_secondary: '#333333',
$color_black: '#191919'
}
})
})
})

0 comments on commit cf78dfb

Please sign in to comment.