aboutsummaryrefslogtreecommitdiffstats
path: root/lua/nvim-lsp-installer/data.lua
blob: 901c293ad76198bba0498072b8594bf5cad1d084 (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
local Data = {}

function Data.enum(values)
    local result = {}
    for i = 1, #values do
        local v = values[i]
        result[v] = v
    end
    return result
end

function Data.set_of(list)
    local set = {}
    for i = 1, #list do
        set[list[i]] = true
    end
    return set
end

function Data.list_reverse(list)
    local result = {}
    for i = #list, 1, -1 do
        result[#result + 1] = list[i]
    end
    return result
end

function Data.list_map(fn, list)
    local result = {}
    for i = 1, #list do
        result[#result + 1] = fn(list[i], i)
    end
    return result
end

function Data.tbl_pack(...)
    return { n = select("#", ...), ... }
end

function Data.when(condition, value)
    return condition and value or nil
end

function Data.coalesce(...)
    local args = Data.tbl_pack(...)
    for i = 1, args.n do
        local variable = args[i]
        if variable ~= nil then
            return variable
        end
    end
end

function Data.list_copy(list)
    local result = {}
    for i = 1, #list do
        result[#result + 1] = list[i]
    end
    return result
end

function Data.list_find_first(list, predicate)
    local result
    for i = 1, #list do
        local entry = list[i]
        if predicate(entry) then
            return entry
        end
    end
    return result
end

function Data.list_any(list, predicate)
    for i = 1, #list do
        if predicate(list[i]) then
            return true
        end
    end
    return false
end

function Data.json_decode(data)
    if vim.json and vim.json.decode then
        return vim.json.decode(data)
    else
        return vim.fn.json_decode(data)
    end
end

return Data