/**
* 키/메소드 체이닝 함수, optional chaining 대체용. 인자 순서대로 chain 진행
* @param firstPort{any} 좌항. 무조건 optional
* @param ports{{port: string, parameter: any[]|any, optional: undefined|boolean}|string} 추가 항. optional 가능
* @returns {any|undefined} chaining 진행 결과. optional port가 nullish인 경우 undefined 반환
*/
function chain(firstPort, ...ports) {
// pointer : chaining 진행 변수
let pointer = firstPort;
// optional : 좌항의 nullish check 플래그
let optional = true;
// chaining 진행
for(const port of ports) {
// 좌항 optional 검사
if(optional) {
// nullish일 경우 undefined 반환
if(pointer === null || typeof pointer === "undefined") {
return undefined;
}
// nullish가 아닐 경우 optional값 초기화
optional = false;
}
// chain
if(port.parameter) {
// method
if(!Array.isArray(port.parameter)) {
port.parameter = [port.parameter];
}
pointer = pointer[port.port](...port.parameter);
} else {
// index
pointer = pointer[(typeof port === "string") ? port : port.port];
}
// optional 여부 저장
if(port.optional) {
optional = true;
}
}
// chaining 종료 후 pointer 반환
return pointer;
}
* 키/메소드 체이닝 함수, optional chaining 대체용. 인자 순서대로 chain 진행
* @param firstPort{any} 좌항. 무조건 optional
* @param ports{{port: string, parameter: any[]|any, optional: undefined|boolean}|string} 추가 항. optional 가능
* @returns {any|undefined} chaining 진행 결과. optional port가 nullish인 경우 undefined 반환
*/
function chain(firstPort, ...ports) {
// pointer : chaining 진행 변수
let pointer = firstPort;
// optional : 좌항의 nullish check 플래그
let optional = true;
// chaining 진행
for(const port of ports) {
// 좌항 optional 검사
if(optional) {
// nullish일 경우 undefined 반환
if(pointer === null || typeof pointer === "undefined") {
return undefined;
}
// nullish가 아닐 경우 optional값 초기화
optional = false;
}
// chain
if(port.parameter) {
// method
if(!Array.isArray(port.parameter)) {
port.parameter = [port.parameter];
}
pointer = pointer[port.port](...port.parameter);
} else {
// index
pointer = pointer[(typeof port === "string") ? port : port.port];
}
// optional 여부 저장
if(port.optional) {
optional = true;
}
}
// chaining 종료 후 pointer 반환
return pointer;
}
들여쓰기 깨지는건 알아서해라
상위 1% EvaluationPropertyAccessWithExpressionKey, EvaluatePropertyAccessWithidentifierKey, ChainEvaluation