summaryrefslogtreecommitdiff
path: root/school/node_modules/string-collapse-leading-whitespace/dist/string-collapse-leading-whitespace.dev.umd.js
blob: 5774a311de6cd998e3ac4d8784b8a635f1168b1c (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
121
122
123
124
125
/**
 * @name string-collapse-leading-whitespace
 * @fileoverview Collapse the leading and trailing whitespace of a string
 * @version 5.1.0
 * @author Roy Revelt, Codsen Ltd
 * @license MIT
 * {@link https://codsen.com/os/string-collapse-leading-whitespace/}
 */

(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.stringCollapseLeadingWhitespace = {}));
}(this, (function (exports) { 'use strict';

var version$1 = "5.1.0";

const version = version$1;
function collWhitespace(str, originallineBreakLimit = 1) {
    const rawNbsp = "\u00A0";
    // helpers
    function reverse(s) {
        return Array.from(s).reverse().join("");
    }
    // replaces the leading/trailing whitespace chunks with final strings
    function prep(whitespaceChunk, limit, trailing) {
        // when processing the leading whitespace, it's \n\r --- CR - LF
        // when processing the trailing whitespace, we're processing inverted order,
        // so it's \n\r --- LF - CR
        // for this reason, we set first and second linebreak according to direction,
        // the "trailing" boolean:
        const firstBreakChar = trailing ? "\n" : "\r";
        const secondBreakChar = trailing ? "\r" : "\n";
        if (!whitespaceChunk) {
            return whitespaceChunk;
        }
        let crlfCount = 0;
        let res = "";
        // let beginning = true;
        for (let i = 0, len = whitespaceChunk.length; i < len; i++) {
            if (whitespaceChunk[i] === firstBreakChar ||
                (whitespaceChunk[i] === secondBreakChar &&
                    whitespaceChunk[i - 1] !== firstBreakChar)) {
                crlfCount++;
            }
            if (`\r\n`.includes(whitespaceChunk[i]) ||
                whitespaceChunk[i] === rawNbsp) {
                if (whitespaceChunk[i] === rawNbsp) {
                    res += whitespaceChunk[i];
                }
                else if (whitespaceChunk[i] === firstBreakChar) {
                    if (crlfCount <= limit) {
                        res += whitespaceChunk[i];
                        if (whitespaceChunk[i + 1] === secondBreakChar) {
                            res += whitespaceChunk[i + 1];
                            i++;
                        }
                    }
                }
                else if (whitespaceChunk[i] === secondBreakChar &&
                    (!whitespaceChunk[i - 1] ||
                        whitespaceChunk[i - 1] !== firstBreakChar) &&
                    crlfCount <= limit) {
                    res += whitespaceChunk[i];
                }
            }
            else {
                if (!whitespaceChunk[i + 1] && !crlfCount) {
                    res += " ";
                }
            }
        }
        return res;
    }
    if (typeof str === "string" && str.length) {
        // without a fuss, set the max allowed line breaks as a leading/trailing whitespace:
        let lineBreakLimit = 1;
        if (typeof +originallineBreakLimit === "number" &&
            Number.isInteger(+originallineBreakLimit) &&
            +originallineBreakLimit >= 0) {
            lineBreakLimit = +originallineBreakLimit;
        }
        // plan: extract what would String.prototype() would remove, front and back parts
        let frontPart = "";
        let endPart = "";
        if (!str.trim()) {
            frontPart = str;
        }
        else if (!str[0].trim()) {
            for (let i = 0, len = str.length; i < len; i++) {
                if (str[i].trim()) {
                    frontPart = str.slice(0, i);
                    break;
                }
            }
        }
        // if whole string is whitespace, endPart is empty string
        if (str.trim() &&
            (str.slice(-1).trim() === "" || str.slice(-1) === rawNbsp)) {
            for (let i = str.length; i--;) {
                // console.log(
                //   `${`\u001b[${36}m${`----------------------------------------------\niterating through: ${JSON.stringify(
                //     str[i],
                //     null,
                //     4
                //   )}`}\u001b[${39}m`}`
                // );
                if (str[i].trim()) {
                    endPart = str.slice(i + 1);
                    break;
                }
            }
        }
        // -------------------------------------------------------------------------
        return `${prep(frontPart, lineBreakLimit, false)}${str.trim()}${reverse(prep(reverse(endPart), lineBreakLimit, true))}`;
    }
    return str;
}

exports.collWhitespace = collWhitespace;
exports.version = version;

Object.defineProperty(exports, '__esModule', { value: true });

})));