aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-core/functional/string.lua
blob: 8c66dbc061f0e828dfe61ff6543dece7023ce42f (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
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

return _