-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from prgrms-fe-devcourse/100-feature/replacet…
…extproperties Feat: replaceTextProperties
- Loading branch information
Showing
2 changed files
with
37 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* 객체의 '_text' 속성을 해당 값으로 대체하는 함수 | ||
* | ||
* @template T - 입력 객체의 타입 | ||
* @param {T} obj - 가장 안쪽의 값을 추출할 객체 | ||
* @returns {T} '_text' 속성이 해당 값으로 대체된 입력 객체와 동일한 구조의 새로운 객체 | ||
*/ | ||
export default function replaceTextProperties<T>(obj: T): T { | ||
if (typeof obj !== 'object' || obj === null) { | ||
return obj; | ||
} | ||
|
||
if (Array.isArray(obj)) { | ||
return obj.map(replaceTextProperties) as unknown as T; | ||
} | ||
|
||
if (obj && typeof obj === 'object' && '_text' in obj) { | ||
const textValue = obj._text; | ||
if (typeof textValue === 'string') { | ||
const parsedValue = +textValue; | ||
if (!isNaN(parsedValue)) { | ||
return parsedValue as T; | ||
} | ||
} | ||
return textValue as T; | ||
} | ||
|
||
const result: { [key: string]: T[keyof T] } = {}; | ||
|
||
for (const [key, value] of Object.entries(obj)) { | ||
result[key] = replaceTextProperties(value); | ||
} | ||
|
||
return result as T; | ||
} |