Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HomoEfficio/1201360382b41265f1c4e68f6c60306e to your computer and use it in GitHub Desktop.
Save HomoEfficio/1201360382b41265f1c4e68f6c60306e to your computer and use it in GitHub Desktop.
CodeSpitz 75 - Day 1 숙제
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CodeSpitz71-1</title>
</head>
<body>
<section id="data"></section>
<script>
const Info = class {
constructor(json) {
const {title, header, items} = json;
if(typeof title != 'string' || !title) throw "invalid title";
if(!Array.isArray(header) || !header.length) throw "invalid header";
if(!Array.isArray(items) || !items.length) throw "invalid items";
const validItems = items.filter(
(item) => (Array.isArray(item) && item.length === header.length)
);
this._private = {title, headers: header, items: validItems};
}
get title() { return this._private.title; }
get headers() { return this._private.headers; }
get items() { return this._private.items; }
};
const Data = class {
async getData() {
const json = await this._getData();
return new Info(json);
}
async _getData() {
throw "_getData() must be overriden."
}
};
const JsonData = class extends Data {
constructor(url) {
super();
this._url = url;
}
async _getData() {
if (typeof this._url !== 'string') throw "url must be string";
let json;
const response = await fetch(this._url);
if (!response.ok) throw "invalid response";
json = await response.json();
return json;
}
};
const Renderer = class {
async render(info) {
if (!(info instanceof Info)) throw "data is NOT Info type";
this._title = info.title;
this._headers = info.headers;
this._items = info.items;
this._render();
}
_render() {
throw "_render() must be overriden."
}
};
const TableRenderer = class extends Renderer {
constructor(parent) {
if (typeof parent != 'string' || !parent) {
throw "invalid parent";
}
super();
this._parent = parent;
}
_render() {
const parent = document.querySelector(this._parent);
if (!parent) throw "parent does NOT exist.";
parent.innerHTML = "";
const [table, caption] = "table,caption".split(',').map(v => document.createElement(v));
caption.innerHTML = this._title;
table.appendChild(caption);
table.appendChild(
this._headers.reduce(
(thead, header) => (thead.appendChild(document.createElement('th')).innerHTML = header, thead),
document.createElement('thead')
)
);
parent.appendChild(
this._items.reduce(
(table, row) => (
table.appendChild(
row.reduce(
(tr, col) => (tr.appendChild(document.createElement('td')).innerHTML = col, tr),
document.createElement('tr')
)
), table
),
table
)
);
}
};
const data = new JsonData("https://gist.githubusercontent.com/hikaMaeng/717dc66225e40a8fe8d1c40366d40957/raw/447d44b800ed98817b0d29681be90aa1ec36e4ac/71_1.json");
const infoPromise = data.getData();
infoPromise.then(info => {
const renderer = new TableRenderer("#data");
renderer.render(info);
});
</script>
</body>
</html>
@hikaMaeng
Copy link

1번(14) : 10

  • JSON file(2) : 0 (오류가 포함된 json그대로 사용 중 ^^)
  • Data class(2) : 2
  • JsonData class(2) : 2
  • Renderer class(2) : 1 (호스트코드에 비동기처리 등의 주요 플로우에 대한 캡슐화가 깨짐)
  • TableRenderer class(2) : 2
  • Info class(4) : 3 (다른 밸리데이션은 throw정책인데 비해 레코드 검증은 삭제 방식을 사용하여 일관성이 깨짐)

2번(4) : 4

  • JsonData(2) : 2
  • TableRenderer(2) : 2

14/18 = 78

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment