-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.moon
91 lines (80 loc) · 3.38 KB
/
utils.moon
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
-- Copyright (c) 2014 by Adam Hellberg <[email protected]>.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
NAME, T = ...
{:GetRealmName, :LARGE_NUMBER_SEPERATOR} = _G
T.string =
split: (str, delim using nil) ->
return nil unless str
t = {}
if delim then
return {str} unless str\find delim
pattern = '(.-)' .. delim .. '()'
local lastpos
for part, pos in str\gmatch pattern
t[#t + 1] = part
lastpos = pos
t[#t + 1] = str\sub lastpos
else
for token in str\gmatch '[^%s]+'
t[#t + 1] = token
t
T.number =
-- Format function courtesy of SDPhantom from WoWInterface
-- http://www.wowinterface.com/forums/showpost.php?p=270917&postcount=11
format: (number using LARGE_NUMBER_SEPERATOR) ->
number = tostring number
number\gsub '^(%d)(%d+)', (pre, post) ->
pre .. post\reverse!\gsub('(%d%d%d)', '%1' .. LARGE_NUMBER_SEPERATOR)\reverse!
local equal
T.table =
equal: (first, second using equal) ->
first_type = type first
second_type = type second
return false if first_type != 'table' or second_type != 'table'
for k, v in pairs first
if type(k) == 'table'
error 'Comparison with tables as keys not yet implemented'
if type(v) == 'table'
return false unless equal v, second[k]
else
return false if v != second[k]
true
import equal from T.table
T.misc =
fix_name: (name, realm using GetRealmName) ->
dashindex = name\find '-'
return name if dashindex and dashindex != name\len!
return name .. '-' .. realm\gsub '%s', '' if realm
name = name\sub 1, dashindex - 1 if dashindex
name .. '-' .. GetRealmName!\gsub '%s', ''
decorate: (text, color using nil) ->
'\124cff%s%s\124r'\format color, text
-- This basically just calls loadstring on a text
-- representation of a Lua object like a number,
-- string or table
deserialize: (str) ->
func, err = loadstring "return #{str}"
return false, err unless type(func) == 'function'
setfenv func, {} -- Don't give access to anything
pcall func
is_any_nil: (...) ->
for item in *{...}
return true if type(item) == 'nil'
false