This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
60 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,58 @@ | ||
// オブジェクトの実装 | ||
const person = { | ||
firstName: "Ken", | ||
lastName: "Takahashi", | ||
age: 29, | ||
gender: "male", | ||
interests: [ | ||
{ | ||
name: "programming", | ||
emoji: "💻", | ||
}, | ||
{ | ||
name: "motorcycle", | ||
emoji: "🏍", | ||
}, | ||
], | ||
greeting: function () { | ||
alert("Hi! I'm " + this.firstName + " " + this.lastName); | ||
}, | ||
}; | ||
|
||
// オブジェクト→JSON | ||
const personObj = { | ||
firstName: "Ken", | ||
lastName: "Takahashi", | ||
age: 29, | ||
gender: "male", | ||
interests: [ | ||
{ | ||
name: "programming", | ||
emoji: "💻", | ||
}, | ||
{ | ||
name: "motorcycle", | ||
emoji: "🏍", | ||
}, | ||
], | ||
export const func1 = () => { | ||
const personDetail = { | ||
...person, | ||
gender: "male", | ||
interests: [ | ||
{ | ||
name: "programming", | ||
emoji: "💻", | ||
}, | ||
{ | ||
name: "motorcycle", | ||
emoji: "🏍", | ||
}, | ||
], | ||
}; | ||
return personDetail; | ||
}; | ||
|
||
export const func2 = () => { | ||
const personStr = JSON.stringify(person); | ||
return personStr; | ||
}; | ||
const personStr = JSON.stringify(personObj); | ||
|
||
// JSON→オブジェクト | ||
const jsonStr = ` | ||
{ | ||
"id": 1, | ||
"name": "Leanne Graham", | ||
"username": "Bret", | ||
"email": "[email protected]", | ||
"address": { | ||
"street": "Kulas Light", | ||
"suite": "Apt. 556", | ||
"city": "Gwenborough", | ||
"zipcode": "92998-3874", | ||
"geo": { | ||
"lat": "-37.3159", | ||
"lng": "81.1496" | ||
} | ||
}, | ||
"phone": "1-770-736-8031 x56442", | ||
"website": "hildegard.org", | ||
"company": { | ||
"name": "Romaguera-Crona", | ||
"catchPhrase": "Multi-layered client-server neural-net", | ||
"bs": "harness real-time e-markets" | ||
} | ||
} | ||
`; | ||
const user = JSON.parse(jsonStr); | ||
console.log(user.email); | ||
export const func3 = () => { | ||
const jsonStr = ` | ||
{ | ||
"id": 1, | ||
"name": "Leanne Graham", | ||
"username": "Bret", | ||
"email": "[email protected]", | ||
"address": { | ||
"street": "Kulas Light", | ||
"suite": "Apt. 556", | ||
"city": "Gwenborough", | ||
"zipcode": "92998-3874", | ||
"geo": { | ||
"lat": "-37.3159", | ||
"lng": "81.1496" | ||
} | ||
}, | ||
"phone": "1-770-736-8031 x56442", | ||
"website": "hildegard.org", | ||
"company": { | ||
"name": "Romaguera-Crona", | ||
"catchPhrase": "Multi-layered client-server neural-net", | ||
"bs": "harness real-time e-markets" | ||
} | ||
} | ||
`; | ||
const user = JSON.parse(jsonStr); | ||
return `${user.name},${user.email}${user.company.name}`; | ||
}; |
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,31 @@ | ||
import { test, expect } from "vitest"; | ||
import { func1, func2, func3 } from "./script"; | ||
|
||
test("func1", () => { | ||
expect(func1()).toMatchObject({ | ||
firstName: "Ken", | ||
lastName: "Takahashi", | ||
age: 29, | ||
gender: "male", | ||
interests: [ | ||
{ | ||
name: "programming", | ||
emoji: "💻", | ||
}, | ||
{ | ||
name: "motorcycle", | ||
emoji: "🏍", | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test("func2", () => { | ||
expect(func2()).toEqual( | ||
'{"firstName":"Ken","lastName":"Takahashi","age":29}' | ||
); | ||
}); | ||
|
||
test("func3", () => { | ||
expect(func3()).toEqual("Leanne Graham,[email protected]"); | ||
}); |