-
Notifications
You must be signed in to change notification settings - Fork 42
lec4-Step5.객체 Type #115
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
lec4-Step5.객체 Type #115
Changes from all commits
2a99f6b
99b0112
cf467b2
9f3de4b
a7b604d
1727718
9c0955d
9476029
1866700
c5c67e6
c5fd338
e67431d
ac1ace3
b7435bb
4086651
ae9212e
0a659ec
8249b68
7ef1e04
1a63c02
ea997ec
9f5c786
3a8bb6c
8f8b375
7915454
0feb928
66270a7
7dc109a
75fe5cd
e563e5c
528771a
e922b9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,16 +5,15 @@ class JSONData { | |
| this.child = child | ||
| } | ||
| } | ||
|
|
||
| const sentence = "['1a'3',[22,23,[11,[112233],112],55],33]".replace(/ /gi, '') | ||
| const sentence = "['1a3',[null,false,['11',[112233],{easy : ['hello', {a:''a'}, 'world']},112],55, '99'],{a:'str', b:[912,[5656,33],{key : 'innervalue', newkeys: [1,2,3,4,5]}]}, true]".replace(/ /gi, '') | ||
|
|
||
| class Tokenize { | ||
| constructor() { | ||
| this.wholeDataQueue = []; | ||
| } | ||
|
|
||
| getWholeDataQueue(sentence) { | ||
| while(sentence.length !== 0) { | ||
| while (sentence.length !== 0) { | ||
| const token = this.getToken(sentence) | ||
| this.wholeDataQueue.push(token) | ||
| sentence = sentence.replace(token, '') | ||
|
|
@@ -23,12 +22,16 @@ class Tokenize { | |
| } | ||
|
|
||
| getToken(str) { | ||
| if (str[0] === '[' || str[0] === ',' || str[0] === ']') { | ||
| if (str[0] === '[' || str[0] === ',' || str[0] === ']' || str[0] === '{' || str[0] === '}') { | ||
| return str.slice(0, 1) | ||
| } else if (str.indexOf(']') < str.indexOf(',')) { | ||
| return str.slice(0, str.indexOf(']')) | ||
| } else if (str.indexOf(',') === -1) { | ||
| return str.slice(0, str.indexOf(']')) | ||
| } else if (str.indexOf(':') !== -1 && str.indexOf(',') > str.indexOf(':')) { | ||
| return str.slice(0, str.indexOf(':') + 1) | ||
| } else if (str.indexOf('}') < str.indexOf(',')) { | ||
| return str.slice(0, str.indexOf('}')) | ||
| } else { | ||
| return str.slice(0, str.indexOf(',')) | ||
| } | ||
|
|
@@ -40,76 +43,103 @@ class Analyze { | |
| this.queueArr = queue | ||
| this.errorCheck = errorCheck | ||
| } | ||
|
|
||
| queue() { | ||
| while(this.queueArr.length !== 0) { | ||
| while (this.queueArr.length !== 0) { | ||
| const value = this.queueArr.shift() | ||
| if(value === '[') { | ||
| const child = this.getChild(this.queueArr, value) | ||
| return new JSONData('Array', 'Array Object', child) | ||
| if (value === '[') { | ||
| return this.makeArrayChild(this.queueArr, value) | ||
| } else if (value === '{') { | ||
| return this.makeObjectChild(this.queueArr, value) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| makeArrayChild(queueArr, value) { | ||
| const arrayChild = this.getChild(queueArr, value) | ||
| return new JSONData('Array', 'Array Object', arrayChild) | ||
| } | ||
|
|
||
| makeObjectChild(queueArr) { | ||
| const objectChild = this.getChild(queueArr, value) | ||
| return new JSONData('Object', 'Object Object', objectChild) | ||
| } | ||
|
|
||
| getChild(queueArr, checkingValue) { | ||
| let child = []; | ||
| debugger; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다음엔 debugger 코드 삭제해주세요~ 실제 서비스할때는 이런 디버깅코드는 당연히 없어야겠죠. console.log도 마찬가지로 제거해야하고요.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네....죄송합니다. 실수했네요 앞으로 꼭 debugger와 console.log를 검색으로 찾아서 다 지운 다음 보내겠습니다. 구현이 되었다는거에 설레서..하하하하 |
||
| while (checkingValue !== ']') { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getChild 의 조건문이 많은 건 어쩔 수 없어보여요. 조건상황을 함수로 모두 다 구현할 수도 있긴합니다. 예를들어, 그리고 참고로 조건문이 많아지면, 불필요한 체크를 조금이라도 줄이기 위해서, |
||
| checkingValue = queueArr.shift() | ||
| if(checkingValue === '[') { | ||
| if (checkingValue === '[') { | ||
| child.push(new JSONData('Array', 'Object Array', this.getChild(queueArr, checkingValue))) | ||
| continue; | ||
| } else if (checkingValue === ',') { | ||
| } else if (checkingValue === '{') { | ||
| child.push(new JSONData('Object', 'Object Object', this.getChild(queueArr, checkingValue))) | ||
| continue; | ||
| } else if (checkingValue === ']') { | ||
| } else if (checkingValue.indexOf(':') !== -1) { | ||
| child.push(new JSONData('object key', checkingValue.slice(0, checkingValue.indexOf(':')), [])) | ||
| continue; | ||
| } else if (checkingValue === '}' || checkingValue === ']') { | ||
| break; | ||
| } else if (checkingValue === ',') { | ||
| continue; | ||
| } else if (checkingValue === 'true' || checkingValue === 'false') { | ||
| child.push(new JSONData('Boolean', checkingValue, [])) | ||
| continue; | ||
| } else if (checkingValue === 'null') { | ||
| child.push(new JSONData('Null', checkingValue, [])) | ||
| continue; | ||
| } else if (checkingValue[0] === "'") { | ||
| if(this.errorCheck.checkString(checkingValue)) { | ||
| console.log(`${checkingValue}는 제대로된 문자열이 아닙니다.`) | ||
| return | ||
| } | ||
| if (this.errorCheck.checkString(checkingValue)) return | ||
| child.push(new JSONData('String', checkingValue, [])) | ||
| continue; | ||
| } | ||
| if(this.errorCheck.checkNumber(checkingValue)) { | ||
| console.log(`${checkingValue}은 알수없는 문자열입니다.`) | ||
| return | ||
| } | ||
| if (this.errorCheck.checkNumber(checkingValue)) return | ||
| child.push(new JSONData('Number', checkingValue, [])) | ||
| } | ||
| return child | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| class ErrorCheck { | ||
| checkString(token) { | ||
| debugger; | ||
| let count = 0 | ||
| for(let position = 0; position < token.length; position++) { | ||
| if(token[position] === "'") { | ||
| count++ | ||
| class ErrorCheck { | ||
| countQueueNum(token) { | ||
| let quotesNum = 0 | ||
| for(let position of token) { | ||
| if(position === "'") { | ||
| quotesNum++ | ||
| } | ||
| } | ||
| if(count === 2 && token[0] === "'" && token[token.length-1] === "'") { | ||
| return quotesNum | ||
| } | ||
|
|
||
| checkString(token) { | ||
| let quotesNum = this.countQueueNum(token) | ||
| if (quotesNum === 2 && token[0] === "'" && token[token.length - 1] === "'") { | ||
| return false | ||
| } | ||
| this.printErrorMessage('string', token) | ||
| return true | ||
| } | ||
|
|
||
| checkNumber(token) { | ||
| if(isNaN(Number(token)) === true) { | ||
| if (isNaN(Number(token))) { | ||
| this.printErrorMessage('number', token) | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function printJSONData(JSONData) { | ||
| printErrorMessage(type, token) { | ||
| if(type === 'string') { | ||
| console.log(`${token}는 제대로된 문자열이 아닙니다.`) | ||
| } | ||
| if(type === 'number') { | ||
| console.log(`${token}은 알수없는 데이터입니다.`) | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const print = function printJSONData(JSONData) { | ||
| console.log(JSON.stringify(JSONData, null, 2)) | ||
| } | ||
|
|
||
|
|
@@ -118,5 +148,5 @@ const tokenizedDataArr = tokenize.getWholeDataQueue(sentence) | |
| const errorCheck = new ErrorCheck | ||
| const analyze = new Analyze(tokenizedDataArr, errorCheck) | ||
| const jsonData = analyze.queue() | ||
| printJSONData(jsonData) | ||
| print(jsonData) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| * 토큰나누기를 할때, '{' 값과 '}' 값을 따로 나눔,:는 key값과 같이 있을 수 있도록 나눔"{'dominate':13}"를 나눈걸 예시로들면 | ||
| ['{','dominate:','13','}']이런 형식으로 나눌 수 있도록. ':'값이 존재하면 ':'값 이전까지 type이 object key인 value 값에 넣어 주도록 하고, child는 '[]' 로. | ||
|
|
||
| * 분석을 할때, { 값이 나오면 새로운 제이슨 데이터를 만들도록. child에는 getChild가 여전히 들어가도록 하고, { 다음에 오는 값은 무조건 ':' 를 포함하고 있을 테니, 그값만 key로 만들고 뒤의 값은 이전과 같은 방식으로 만들 수 있도록 하기. '}'가 나올때 까지. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
조건문이 너무 길어요. 이런것도 함수로 구현할 수 있으면 좋겠네요.