Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
オブジェクトの問題を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
takahash committed Oct 9, 2023
1 parent 2a486df commit e9f96e0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 60 deletions.
110 changes: 50 additions & 60 deletions object/script.js
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}`;
};
31 changes: 31 additions & 0 deletions object/script.test.js
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]");
});

0 comments on commit e9f96e0

Please sign in to comment.