Skip to main content
clarity
Source Link
FizzyTea
  • 251
  • 1
  • 7

Well, you could remove the RegExp. But whether that helps is anyone'sperformance is anyone's guess.

var d2p = function(s){
    var one=[], two=[], a=s.split('-');
    one.push(a.shift());
    a.forEach(function(part){
        one.push('(' + part);
        two.push(')');
    });
    return one.join('') + two.join('');
};

If you don't mind having the result fully parenthesised, then you can do this:

var d2p = function(s){
    return s.split('-').reduceRight(function(whole, part){
        return '(' + part + whole + ')';
    }, '');
};

Well, you could remove the RegExp. But whether that helps is anyone's guess.

var d2p = function(s){
    var one=[], two=[], a=s.split('-');
    one.push(a.shift());
    a.forEach(function(part){
        one.push('(' + part);
        two.push(')');
    });
    return one.join('') + two.join('');
};

If you don't mind having the result fully parenthesised, then you can do this:

var d2p = function(s){
    return s.split('-').reduceRight(function(whole, part){
        return '(' + part + whole + ')';
    }, '');
};

Well, you could remove the RegExp. But whether that helps performance is anyone's guess.

var d2p = function(s){
    var one=[], two=[], a=s.split('-');
    one.push(a.shift());
    a.forEach(function(part){
        one.push('(' + part);
        two.push(')');
    });
    return one.join('') + two.join('');
};

If you don't mind having the result fully parenthesised, then you can do this:

var d2p = function(s){
    return s.split('-').reduceRight(function(whole, part){
        return '(' + part + whole + ')';
    }, '');
};
Source Link
FizzyTea
  • 251
  • 1
  • 7

Well, you could remove the RegExp. But whether that helps is anyone's guess.

var d2p = function(s){
    var one=[], two=[], a=s.split('-');
    one.push(a.shift());
    a.forEach(function(part){
        one.push('(' + part);
        two.push(')');
    });
    return one.join('') + two.join('');
};

If you don't mind having the result fully parenthesised, then you can do this:

var d2p = function(s){
    return s.split('-').reduceRight(function(whole, part){
        return '(' + part + whole + ')';
    }, '');
};