aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/functional/string.lua
blob: e9e060f7a5431a62760158e189db0896537bdff7 (plain) (blame)
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
126
127
128
local fun = require "mason-core.functional.function"

local _ = {}

---@param pattern string
---@param str string
_.matches = fun.curryN(function(pattern, str)
    return str:match(pattern) ~= nil
end, 2)

_.match = fun.curryN(function(pattern, str)
    return { str:match(pattern) }
end, 2)

---@param template string
---@param str string
_.format = fun.curryN(function(template, str)
    return template:format(str)
end, 2)

---@param sep string
---@param str string
_.split = fun.curryN(function(sep, str)
    return vim.split(str, sep)
end, 2)

---@param pattern string
---@param repl string|function|table
---@param str string
_.gsub = fun.curryN(function(pattern, repl, str)
    return string.gsub(str, pattern, repl)
end, 3)

_.trim = fun.curryN(function(str)
    return vim.trim(str)
end, 1)

---https://github.com/nvim-lua/nvim-package-specification/blob/93475e47545b579fd20b6c5ce13c4163e7956046/lua/packspec/schema.lua#L8-L37
---@param str string
---@return string
_.dedent = fun.curryN(function(str)
    local lines = {}
    local indent = nil

    for line in str:gmatch "[^\n]*\n?" do
        if indent == nil then
            if not line:match "^%s*$" then
                -- save pattern for indentation from the first non-empty line
                indent, line = line:match "^(%s*)(.*)$"
                indent = "^" .. indent .. "(.*)$"
                table.insert(lines, line)
            end
        else
            if line:match "^%s*$" then
                -- replace empty lines with a single newline character.
                -- empty lines are handled separately to allow the
                -- closing "]]" to be one indentation level lower.
                table.insert(lines, "\n")
            else
                -- strip indentation on non-empty lines
                line = assert(line:match(indent), "inconsistent indentation")
                table.insert(lines, line)
            end
        end
    end

    lines = table.concat(lines)
    -- trim trailing whitespace
    return lines:match "^(.-)%s*$"
end, 1)

---@param prefix string
---@str string
_.starts_with = fun.curryN(function(prefix, str)
    return vim.startswith(str, prefix)
end, 2)

---@param str string
_.to_upper = function(str)
    return str:upper()
end

---@param str string
_.to_lower = function(str)
    return str:lower()
end

---@param pattern string
---@param str string
_.trim_start_matches = fun.curryN(function(pattern, str)
    for i = 1, #str do
        if not str:sub(i, i):match(pattern) then
            return str:sub(i)
        end
    end
    return str
end, 2)

---@param pattern string
---@param str string
_.trim_end_matches = fun.curryN(function(pattern, str)
    for i = #str, 1, -1 do
        if not str:sub(i, i):match(pattern) then
            return str:sub(1, i)
        end
    end
    return str
end, 2)

_.strip_prefix = fun.curryN(function(prefix_pattern, str)
    local _, start = string.find(str, "^" .. prefix_pattern)
    if start then
        return str:sub(start + 1)
    else
        return str
    end
end, 2)

_.strip_suffix = fun.curryN(function(suffix_pattern, str)
    local stop = string.find(str, suffix_pattern .. "$")
    if stop then
        return str:sub(1, stop - 1)
    else
        return str
    end
end, 2)

return _