export function addr(vcard){
    const match = vcard.match(/^ADR.*:(.+)$/m);
    let adr = {
        box: null,
        ext: null,
        street: null,
        locality: null,
        region: null,
        code: null,
        country: null
    }
    if (match){
        const parts = match[1].trim().split(';');
        adr.box = parts[0];
        adr.ext = parts[1];
        adr.street = parts[2];
        adr.locality = parts[3];
        adr.region = parts[4];
        adr.post_code = parts[5];
        adr.country = parts[6];
    }
    return adr;
}

export function email(vcard){
    const match = vcard.match(/^EMAIL.*:(.+)$/m);
    return match ? match[1].trim() : '';
}

export function extra(code){
    const match = code.match(/^X-([^:]+):(.+)/)
    return match ? {name:match[1],value:match[2]} : null
}

export function fn(vcard){
    const match = vcard.match(/^FN.*:(.+)$/m);
    return match ? match[1].trim() : '';
}

export function number(vcard){
    const match = vcard.match(/^TEL.*:(.+)$/m);
    return match ? match[1].trim() : '';
}


export function org(vcard){
    const match = vcard.match(/^ORG:(.+)$/m);
    return match ? match[1].trim() : '';
}

export function url(vcard){
    const match = vcard.match(/^URL:(.+)$/m);
    return match ? match[1].trim() : '';
}

export function name(vcard){
    const match = vcard.match(/^N:(.+)$/m);
    let name = {
        family: null,
        given: null,
        additional: null,
        prefix: null,
        suffix: null
    }
    if (match){
        const parts = match[1].trim().split(';');
        name.family = parts[0];
        name.given = parts[1];
        name.additional = parts[2];
        name.prefix = parts[3];
        name.suffix = parts[4];
    }
    return name;
}


export function byName(a,b){
    return fn(a.vcard).localeCompare(fn(b.vcard));
}