aboutsummaryrefslogtreecommitdiff
path: root/node_modules/gettext-parser/lib/shared.js
blob: 44bfb86f7e975c73865b4bdc2a49130a3fcee7f9 (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';

// Expose to the world
module.exports.parseHeader = parseHeader;
module.exports.generateHeader = generateHeader;
module.exports.formatCharset = formatCharset;
module.exports.foldLine = foldLine;

/**
 * Parses a header string into an object of key-value pairs
 *
 * @param {String} str Header string
 * @return {Object} An object of key-value pairs
 */
function parseHeader(str) {
    var lines = (str || '').split('\n'),
        headers = {};

    lines.forEach(function(line) {
        var parts = line.trim().split(':'),
            key = (parts.shift() || '').trim().toLowerCase(),
            value = parts.join(':').trim();
        if (!key) {
            return;
        }
        headers[key] = value;
    });

    return headers;
}

/**
 * Convert first letters after - to uppercase, other lowercase
 *
 * @param {String} str String to be updated
 * @return {String} A string with uppercase words
 */
function upperCaseWords(str) {
    return (str || '').toLowerCase().trim().replace(/^(MIME|POT?(?=\-)|[a-z])|\-[a-z]/gi, function(str) {
        return str.toUpperCase();
    });
}

/**
 * Joins a header object of key value pairs into a header string
 *
 * @param {Object} header Object of key value pairs
 * @return {String} Header string
 */
function generateHeader(header) {
    var lines = [];

    Object.keys(header || {}).forEach(function(key) {
        if (key) {
            lines.push(upperCaseWords(key) + ': ' + (header[key] || '').trim());
        }
    });

    return lines.join('\n') + (lines.length ? '\n' : '');
}

/**
 * Normalizes charset name. Converts utf8 to utf-8, WIN1257 to windows-1257 etc.
 *
 * @param {String} charset Charset name
 * @return {String} Normalized charset name
 */
function formatCharset(charset, defaultCharset) {
    return (charset || 'iso-8859-1').toString().toLowerCase().
    replace(/^utf[\-_]?(\d+)$/, 'utf-$1').
    replace(/^win(?:dows)?[\-_]?(\d+)$/, 'windows-$1').
    replace(/^latin[\-_]?(\d+)$/, 'iso-8859-$1').
    replace(/^(us[\-_]?)?ascii$/, 'ascii').
    replace(/^charset$/, defaultCharset || 'iso-8859-1').
    trim();
}

/**
 * Folds long lines according to PO format
 *
 * @param {String} str PO formatted string to be folded
 * @param {Number} [maxLen=76] Maximum allowed length for folded lines
 * @return {Array} An array of lines
 */
function foldLine(str, maxLen) {

    maxLen = maxLen || 76;

    var lines = [],
        curLine = '',
        pos = 0,
        len = str.length,
        match;

    while (pos < len) {
        curLine = str.substr(pos, maxLen);

        // ensure that the line never ends with a partial escaping
        // make longer lines if needed
        while (curLine.substr(-1) === '\\' && pos + curLine.length < len) {
            curLine += str.charAt(pos + curLine.length);
        }

        // ensure that if possible, line breaks are done at reasonable places
        if ((match = curLine.match(/\\n/))) {
            curLine = curLine.substr(0, match.index + 2);
        } else if (pos + curLine.length < len) {
            if ((match = curLine.match(/(\s+)[^\s]*$/)) && match.index > 0) {
                curLine = curLine.substr(0, match.index + match[1].length);
            } else if ((match = curLine.match(/([\x21-\x40\x5b-\x60\x7b-\x7e]+)[^\x21-\x40\x5b-\x60\x7b-\x7e]*$/)) && match.index > 0) {
                curLine = curLine.substr(0, match.index + match[1].length);
            }
        }

        lines.push(curLine);
        pos += curLine.length;
    }

    return lines;
}