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

fix: #75 twist 関数で文字列の前方一致していたところを、キーの前方一致判定に変更 #76

Merged
merged 2 commits into from
Dec 4, 2023
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: 5 additions & 0 deletions .changeset/khaki-sheep-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"json-origami": patch
---

fix: #75 twist 関数で文字列の前方一致していたところを、キーの前方一致判定に変更
9 changes: 8 additions & 1 deletion .vscode/project.code-workspace
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"folders": [{ "path": ".." }]
"folders": [
{
"path": ".."
}
],
"settings": {
"typescript.tsdk": "node_modules/typescript/lib"
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ついで

}
3 changes: 2 additions & 1 deletion src/twist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fold } from './fold'
import { unfold } from './unfold'
import type { Dictionary, MoveMap, Twist, TwistOption } from './type'
import { includesKey } from './utils'

/**
*
Expand All @@ -16,7 +17,7 @@ export function twist<D extends Dictionary, M extends MoveMap<D>>(

const twisted = Object.fromEntries(
Object.entries(folded).map(([key, value]) => {
const found = Object.keys(moveMap).find((k) => key.startsWith(k))
const found = Object.keys(moveMap).find((k) => includesKey(key, k, option))

if (found) {
const newKey = key.replace(found, moveMap[found]!)
Comment on lines 18 to 23
Copy link

@coderabbitai coderabbitai bot Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

includesKey関数の使用方法が間違っています。Object.keys(moveMap).findを使う代わりに、直接includesKeyを使ってマッチするキーを見つけるべきです。

- const found = Object.keys(moveMap).find((k) => includesKey(key, k, option))
+ const found = includesKey(key, Object.keys(moveMap), option)

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const twisted = Object.fromEntries(
Object.entries(folded).map(([key, value]) => {
const found = Object.keys(moveMap).find((k) => key.startsWith(k))
const found = Object.keys(moveMap).find((k) => includesKey(key, k, option))
if (found) {
const newKey = key.replace(found, moveMap[found]!)
const twisted = Object.fromEntries(
Object.entries(folded).map(([key, value]) => {
const found = includesKey(key, Object.keys(moveMap), option)
if (found) {
const newKey = key.replace(found, moveMap[found]!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

間違ってないです。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mew-ton 承知しました。現在の実装を維持することにします。もし他に何か手伝えることがあれば、お知らせください。

Expand Down
36 changes: 36 additions & 0 deletions test/twist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,40 @@ describe('twist', () => {
}
})
})

// map データのキーが前方一致する組み合わせがある場合. e.g. foo.bar, foo.bar1
it('should be twisted correctly when the prefix is the same string.', () => {
const target = {
foo: {
bar: 0,
bar1: 1,
bar2: 2,
barbar: 3,
},
foo1: {
bar: 4,
bar1: 5,
bar2: 6
}
}

const map = {
'foo.bar': 'baz',
'foo.bar1': 'qux',
'foo.bar2': 'quux',
'foo.barbar': 'quux1'
}

expect(twist(target, map)).toEqual({
foo1: {
bar: 4,
bar1: 5,
bar2: 6
},
baz: 0,
qux: 1,
quux: 2,
quux1: 3
})
})
})