Last active
April 13, 2021 05:58
-
-
Save yangtaeho/e2c1f9c1b05f2855adea29829d31dde0 to your computer and use it in GitHub Desktop.
s*89-2 과제
This file contains hidden or 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
const arrayStringify = (arr => { | |
const quote = str=>`\"${str}\"`; | |
const _escape = str=>str.replace(/\"/g,'\\\"'); | |
const NULL_STR = 'null'; | |
const _arrayStringify = (arr) => { | |
let acc = []; | |
for (const item of arr) { | |
//const item = obj[k]; | |
//console.log('k', k, 'item', item); | |
//console.log('item', item); | |
switch(typeof item) { | |
case 'undefined': | |
case 'function': | |
case 'symbol': | |
acc.push(NULL_STR); | |
break; | |
case 'number': | |
acc.push(item); | |
break; | |
case 'string': | |
acc.push(quote(_escape(item))); | |
break; | |
case 'object': | |
if (item instanceof Array) { | |
acc.push('[' + _arrayStringify(item) + ']'); | |
} else if (item == null) { | |
acc.push(NULL_STR); | |
} | |
break; | |
default: | |
acc.push(item); | |
break; | |
} | |
} | |
return acc.join(','); | |
} | |
let acc = []; | |
const arrayStringify = arr => { | |
acc = []; | |
if (!arr instanceof Array) { | |
throw `Invalid argument. ${arr}`; | |
} | |
return '[' + _arrayStringify(arr) + ']'; | |
} | |
return arrayStringify; | |
})(); | |
const arrayStringifyTail = (arr => { | |
const quote = str=>`\"${str}\"`; | |
const _escape = str=>str.replace(/\"/g,'\\\"'); | |
const NULL_STR = 'null'; | |
const _arrayStringify = (arr, acc, i)=>{ | |
if (i < arr.length) { | |
const item = arr[i]; | |
//const item = obj[k]; | |
//console.log('k', k, 'item', item); | |
//console.log('item', item); | |
switch(typeof item) { | |
case 'undefined': | |
case 'function': | |
case 'symbol': | |
acc.push(NULL_STR); | |
break; | |
case 'number': | |
acc.push(item); | |
break; | |
case 'string': | |
acc.push(quote(_escape(item))); | |
break; | |
case 'object': | |
if (item instanceof Array) { | |
acc.push('[' + _arrayStringify(item, [], 0) + ']'); | |
} else if (item == null) { | |
acc.push(NULL_STR); | |
} | |
break; | |
default: | |
acc.push(item); | |
break; | |
} | |
return _arrayStringify(arr, acc, i + 1); | |
} else { | |
return '[' + acc.join(',') + ']'; | |
} | |
} | |
let acc = []; | |
const arrayStringify = arr => { | |
acc = []; | |
if (!arr instanceof Array) { | |
throw `Invalid argument. ${arr}`; | |
} | |
return '[' + _arrayStringify(arr, acc, 0) + ']'; | |
} | |
return arrayStringify; | |
})(); | |
const arrayStringifyFor = (arr => { | |
const quote = str=>`\"${str}\"`; | |
const _escape = str=>str.replace(/\"/g,'\\\"'); | |
const NULL_STR = 'null'; | |
const _arrayStringify = (arr, acc) => { | |
for (let i = 0; i < arr.length; i = i + 1) { | |
const item = arr[i]; | |
//const item = obj[k]; | |
//console.log('k', k, 'item', item); | |
//console.log('item', item); | |
switch(typeof item) { | |
case 'undefined': | |
case 'function': | |
case 'symbol': | |
acc.push(NULL_STR); | |
break; | |
case 'number': | |
acc.push(item); | |
break; | |
case 'string': | |
acc.push(quote(_escape(item))); | |
break; | |
case 'object': | |
if (item instanceof Array) { | |
acc.push('[' + _arrayStringify(item) + ']'); | |
} else if (item == null) { | |
acc.push(NULL_STR); | |
} | |
break; | |
default: | |
acc.push(item); | |
break; | |
} | |
} | |
return '[' + acc.join(',') + ']'; | |
} | |
let acc = []; | |
const arrayStringify = arr => { | |
acc = []; | |
if (!arr instanceof Array) { | |
throw `Invalid argument. ${arr}`; | |
} | |
return _arrayStringify(arr, acc); | |
} | |
return arrayStringify; | |
})(); | |
const test_stringify = (f, v)=>{ | |
if (!v) return console.log(`Invalid arg. v: '${v}'`); | |
console.log(`\n[s]${f['name']} :: res: ${f(v) == JSON.stringify(v)}\n`, '\nQUES ', v, '\nJSON ', JSON.stringify(v), '\nSTDY ', f(v), '\n===[e]'); | |
}; | |
test_stringify(arrayStringify, []); | |
test_stringify(arrayStringify, [,'test', 1]); | |
test_stringify(arrayStringify, [1, "ab\"c", true, null, _=>3]); | |
test_stringify(arrayStringify, [1, "ab\"c", true, undefined, null, _=>3, Symbol()]); | |
test_stringify(arrayStringify, [1, "ab\"c", true, undefined, null, ["ab\"c", true, undefined, null], _=>3, Symbol()]); | |
test_stringify(arrayStringifyTail, []); | |
test_stringify(arrayStringifyTail, [,'test', 1]); | |
test_stringify(arrayStringifyTail, [1, "ab\"c", true, null, _=>3]); | |
test_stringify(arrayStringifyTail, [1, "ab\"c", true, undefined, null, _=>3, Symbol()]); | |
test_stringify(arrayStringifyTail, [1, "ab\"c", true, undefined, null, ["ab\"c", true, undefined, null], _=>3, Symbol()]); | |
test_stringify(arrayStringifyFor, []); | |
test_stringify(arrayStringifyFor, [,'test', 1]); | |
test_stringify(arrayStringifyFor, [1, "ab\"c", true, null, _=>3]); | |
test_stringify(arrayStringifyFor, [1, "ab\"c", true, undefined, null, _=>3, Symbol()]); | |
test_stringify(arrayStringifyFor, [1, "ab\"c", true, undefined, null, ["ab\"c", true, undefined, null], _=>3, Symbol()]); |
This file contains hidden or 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
// _v0_3_210411 수업 듣고 개선 버전 | |
const stringify210413 = (arr => { | |
const isDebug =_=>!1; | |
const quote = str=>`\"${str}\"`; | |
const _escape = { | |
table: [[/\"/g,'\\\"'] | |
,[/\t/g, '\\t'] | |
,[/[\r]/g, '\\r'] | |
,[/[\n]/g, '\\n'] | |
,[/[\l]/g, 'l']] | |
,exe(str){ | |
let acc = str; | |
for (const item of this.table) { | |
acc = acc.replace(item[0], item[1]); | |
} | |
return acc; | |
} | |
}; | |
const NULL_STR = 'null'; | |
const DELIM = ','; | |
const el = { | |
table: { | |
"null":v=>NULL_STR | |
,'undefined': v=>NULL_STR | |
,'function': v=>NULL_STR | |
,'symbol': v=>NULL_STR | |
,'number': v=>v.toString() | |
,'string': v=>quote(_escape.exe(v)) | |
,'array': v=>{}//v=>'[' + _arrayStringify(v) + ']' | |
,'object': v=>NULL_STR | |
,boolean: v=>v.toString() | |
}, | |
stringifyRouter(item) { | |
return (this.table[typeof item] ?? this.table[!item ? NULL_STR : Array.isArray(item) ? "array" : "object"])?.(item) ?? NULL_STR; | |
} | |
}; | |
const _arrayStringify = (arr, acc, i)=>{ | |
isDebug() && console.log('debug', i, item, acc); | |
if (i == arr.length) { | |
console.log('acc ===> ', acc); | |
} | |
return i < arr.length ? acc + _arrayStringify(arr, `,${el.stringifyRouter(arr[i])}`, i + 1) : acc; | |
}; | |
const arrayStringify = arr => { | |
if (!arr instanceof Array) { | |
throw `Invalid argument. ${arr}`; | |
} | |
return 0 < arr.length ? '[' + _arrayStringify(arr, '', 0) + ']' : '[]'; | |
}; | |
const _arrayStringifyTail = (arr, acc, i)=>{ | |
const item = arr[i]; | |
return i < arr.length ? _arrayStringifyTail(arr, acc + `,${el.stringifyRouter(item)}`, i + 1) : `${acc.substr(1)}`; | |
}; | |
const arrayStringifyTail = arr => { | |
if (!arr instanceof Array) { | |
throw `Invalid argument. ${arr}`; | |
} | |
return 0 < arr.length ? '[' + _arrayStringifyTail(arr, '', 0) + ']' : '[]'; | |
}; | |
const EMPTY = {}; | |
const arrayStringifyFor = arr => { | |
if (!arr instanceof Array) { | |
throw `Invalid argument. ${arr}`; | |
} | |
let result = EMPTY; | |
if(arr.length == 0) result = "[]"; | |
else { | |
let acc = "", i = 0; | |
while(i < arr.length) { | |
acc += `,${el.stringifyRouter(arr[i])}`; | |
i = i + 1; // 바디에서 결정됐지 문에서 계획하지 않았다.. | |
} | |
result = `[${acc.substr(1)}]`; | |
} | |
if (result === EMPTY) throw "no processed"; | |
return result; | |
}; | |
return { | |
arrayStringify | |
, arrayStringifyTail | |
, arrayStringifyFor | |
} | |
})(); | |
const test_stringify = (f, v)=>{ | |
if (!v) return console.log(`Invalid arg. v: '${v}'`); | |
console.log(`\n[s]${f['name']} ====` | |
, `\n\tJSON.stringify와 일치: ${f(v) == JSON.stringify(v)}`, (f(v) == JSON.stringify(v) ? '' : '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') | |
, '\n\tQ문제 ', v | |
, '\n\t답안 ', f(v) | |
, '\n\tJSON.stingify ', JSON.stringify(v) | |
, '\n[e]========'); | |
}; | |
test_stringify(stringify210413.arrayStringify, []); | |
test_stringify(stringify210413.arrayStringify, [,'test', 1]); | |
test_stringify(stringify210413.arrayStringify, [1, "ab\"c", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringify, [1, "ab\"\nc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringify, [1, "ab\"\rc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringify, [1, "ab\"\lc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringify, [1, "ab\"\r\nc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringify, [1, "ab\"c", true, undefined, null, _=>3, Symbol()]); | |
test_stringify(stringify210413.arrayStringify, [1, "ab\"c", true, undefined, null, ["ab\"c", true, undefined, null], _=>3, Symbol()]); | |
test_stringify(stringify210413.arrayStringifyTail, []); | |
test_stringify(stringify210413.arrayStringifyTail, [,'test', 1]); | |
test_stringify(stringify210413.arrayStringifyTail, [1, "ab\"c", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyTail, [1, "ab\"\nc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyTail, [1, "ab\"\rc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyTail, [1, "ab\"\lc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyTail, [1, "ab\"\r\nc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyTail, [1, "ab\"c", true, undefined, null, _=>3, Symbol()]); | |
test_stringify(stringify210413.arrayStringifyTail, [1, "ab\"c", true, undefined, null, ["ab\"c", true, undefined, null], _=>3, Symbol()]); | |
test_stringify(stringify210413.arrayStringifyFor, []); | |
test_stringify(stringify210413.arrayStringifyFor, [,'test', 1]); | |
test_stringify(stringify210413.arrayStringifyFor, [1, "ab\"c", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyFor, [1, "ab\"\nc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyFor, [1, "ab\"\rc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyFor, [1, "ab\"\lc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyFor, [1, "ab\"\r\nc", true, null, _=>3]); | |
test_stringify(stringify210413.arrayStringifyFor, [1, "ab\"c", true, undefined, null, _=>3, Symbol()]); | |
test_stringify(stringify210413.arrayStringifyFor, [1, "ab\"c", true, undefined, null, ["ab\"c", true, undefined, null], _=>3, Symbol()]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment