2009-02-21 5 views

Répondre

7
function getSecondPart(str) { 
    if(str === undefined || 
     typeof str != 'string' || 
     str.indexOf('-') == -1) return false; 
    return str.split('-')[1]; 
} 
console.log(getSecondPart({}); // false 
console.log(getSecondPart([]); // false 
console.log(getSecondPart()); // false 
console.log(getSecondPart('')); // false 
console.log(getSecondPart('test')); // false 
console.log(getSecondPart('asdf-test')); // test 
+0

C'est à peu près aussi défensive que ça va get: x –

+0

Ne faites pas trop compliqué. – Gumbo

+0

Ouais. Je l'ai simplifié un peu. –

1

Je dirais:

function getSecondPart(str) { 
    if (typeof str !== "string" || str.indexOf("-") === -1) return false; 
    return str.split("-")[1]; 
} 
Questions connexes