# All files / lib/formatter stringformatter.js

100% Statements 11/11
100% Branches 4/4
100% Functions 2/2
100% Lines 11/11

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    322x

    4x
    1x

    3x

    2x

    1x

    1x
    1x

    1x

    1x

    2x
     var StringFormatter = function(pattern) {

        if (!pattern || pattern === 'undefined') {
            throw new Error('StringFormatter need a pattern');
        }

        this.pattern = pattern;
    };

    StringFormatter.prototype.format = function(data) {

        var strings = [];

        for (var i = 0; i < data.length; i++) {
            var str = this.pattern
                .replace(/%n/, data[i].streetNumber)
                .replace(/%S/, data[i].streetName)
                .replace(/%z/, data[i].zipcode)
                .replace(/%P/, data[i].country)
                .replace(/%p/, data[i].countryCode)
                .replace(/%c/, data[i].city)
                .replace(/%T/, data[i].state)
                .replace(/%t/, data[i].stateCode);

            strings.push(str);
        }

        return strings;
    };

    module.exports = StringFormatter;