2017-11-03 3 views
0


내 질문에 약간의 차이가있을 수 있습니다. 전에도 같은 질문을 했었지만 읽는 방법으로 광산을 풀 수있는 방법을 찾지 못했습니다. 더 명확한 안내가 필요합니다.regExp없이 색상 값 확인

색상을 확인하기 위해 이미 세 가지 기능을 만들었습니다. (나는 그들과 함께 문제가없는 나는이 크기 때문에 여기에 포함하고 있지 않다..) 우리가 같은 세 가지 작업 기능이 있다고 가정하자 :

function checkHex(input) { 
    // returns boolean value if input is hex color 
} 
checkHex("#1234a6"); // returns true 

function checkRGB(input) { 
    // returns boolean value if input is RGB color 
} 
checkRGB("rgb(255, 255, 112)"); // returns true 

function checkHSL(input) { 
    // returns boolean value if input is hsl color 
} 
checkHSL("hsl(122, 1, 1)"); // returns true 

내가 4 기능을 가지고 (checkColor를) 데를 혼합 색상 값 확인 :

function checkColor(input) { 
    // returns boolean value if input belong to right color value 
} 

checkColor("#ccccff"); // should return true 
checkColor("rgb(255,255,200)"); // should return true 
checkColor("hls(46,0.66,0.21)"); // should return true 

질문 : 나는 4 일 (checkColor)에 세 가지 기능 (checkHex, checkRGB, checkHSL)를 포함해야합니까? 어떻게해야합니까? 나는 그것에 대해 조사하고 해결할 몇 가지 방법을 시도했지만 할 수 없었습니다.
RegExp를 사용하지 않고이 작업을 수행하려고합니다. 프로그래밍에 익숙하지 않고 이전에는 여러 기능을 병합하지 않았습니다.
"여러 기능을 결합하는"것에 대해 나와 공유 할 수있는 추가 리소스가 있으면 도움이 될 것입니다.

시간과 노력에 감사드립니다.

+0

'return checkHex (input) || checkRGB (입력) || checkHSL (입력); '? –

+0

'input' 문자로 시작하는 문자를 확인한 다음 올바른 함수를 호출하십시오. 'rgba()'를 잊지 마라. –

답변

0

작성한 처음 세 함수는 종종 술어라고합니다. 술어를 그룹화하는 좋은 방법이 있지만 @FelixKling은 주석에서 가장 단순한 언급입니다. 이 목적을 위해 특별히 기능을 만들 수도 있습니다.

// composition function 
 
const any = (...predicates) => subject => predicates.reduce((state, predicate) => (state || predicate(subject)), false); 
 

 
// predicates 
 
const biggerThan5 = x => x>5; 
 
const isOdd = x => !!(x % 2); 
 

 
// results and usage 
 
console.log(any(biggerThan5, isOdd)(10)); // true 
 
console.log(any(biggerThan5, isOdd)(2)); // false

0

당신은 다른 기능에 기능을 포함 할 수 없습니다, 당신은 단지 다른 사람의 내부에 사용할 수 있습니다. 예 :

function checkColor(input) { 
    var isHex = checkHex(input), //store the result of function 
     isRGB = checkRGB(input), //store the result of function 
     isHLS = checkHSL(input); //store the result of function 
    return isHex || isRGB || isHLS; //returns true if one of the options is true. 
} 

function checkHex(input) { 
    // returns boolean value if input is hex color 
} 
function checkRGB(input) { 
    // returns boolean value if input is RGB color 
} 
function checkHSL(input) { 
    // returns boolean value if input is hsl color 
}