w-stdlib 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/w-stdlib/core_ext/array.rb +14 -0
- data/lib/w-stdlib/core_ext/aws.rb +1 -0
- data/lib/w-stdlib/core_ext/file.rb +2 -1
- data/lib/w-stdlib/core_ext/hash.rb +4 -0
- data/lib/w-stdlib/core_ext/object.rb +16 -1
- data/lib/w-stdlib/core_ext/string.rb +116 -1
- data/w-stdlib.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de329ce379af0a89b8b8c7879ae89907b7e23922973ce14e83f94393b2725012
|
4
|
+
data.tar.gz: d7860bd01015f1344cb96a05769954ebbdcc36e14243b90bc3ba80fa4044a036
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc7f4dc141c22b3a7e1ba26b31c11000756a83cbfee52da34df43c04dbf1c3aa7fb3058efb9102495f286ecb142833b1289e2e6aabe97969cac6cc177fe45d00
|
7
|
+
data.tar.gz: 50058d76f55b2290a6cc25c65f1e7de23c2df24fdebb3c68fdc599d19c8119e81154ac4a9f83cb616fbce7688d432098a9ad6bf52c93cfa64677fafea83f29b4
|
@@ -74,5 +74,19 @@ module ArrayExt
|
|
74
74
|
def to_h_by
|
75
75
|
map { [yield(_1), _1] }.to_h
|
76
76
|
end
|
77
|
+
|
78
|
+
def frequencies
|
79
|
+
h = Hash.new(0)
|
80
|
+
each { h[_1] += 1 }
|
81
|
+
h
|
82
|
+
end
|
83
|
+
|
84
|
+
def sliding_window(n)
|
85
|
+
(0..length-n).map { self[_1..._1+n] }
|
86
|
+
end
|
87
|
+
|
88
|
+
def chunks(n)
|
89
|
+
(0..length/n).map { self[_1*n..._1*n+n] }
|
90
|
+
end
|
77
91
|
end
|
78
92
|
end
|
@@ -4,13 +4,28 @@ module ObjectExt
|
|
4
4
|
!self.nil?
|
5
5
|
end
|
6
6
|
|
7
|
-
|
7
|
+
alias_method :nn?, :not_nil?
|
8
|
+
|
9
|
+
def _?(default=nil)
|
8
10
|
return self if self.not_nil?
|
11
|
+
return default if default.not_nil?
|
9
12
|
yield
|
10
13
|
end
|
11
14
|
|
15
|
+
alias_method :fnil, :_?
|
16
|
+
|
12
17
|
def lift_array
|
13
18
|
kind_of?(Array) ? self : [self]
|
14
19
|
end
|
20
|
+
|
21
|
+
def encode_json
|
22
|
+
JSON.generate self
|
23
|
+
end
|
24
|
+
|
25
|
+
def encode_yaml
|
26
|
+
YAML.dump self
|
27
|
+
end
|
28
|
+
|
29
|
+
alias lift_a lift_array
|
15
30
|
end
|
16
31
|
end
|
@@ -2,9 +2,108 @@ require 'base64'
|
|
2
2
|
require 'json'
|
3
3
|
require 'cgi'
|
4
4
|
require 'yaml'
|
5
|
+
require 'digest'
|
5
6
|
|
6
7
|
module StringExt
|
7
8
|
refine String do
|
9
|
+
def is_downcase?
|
10
|
+
self.match?(/^[a-z]+$/)
|
11
|
+
end
|
12
|
+
|
13
|
+
def is_uppercase?
|
14
|
+
self.match?(/^[A-Z]+$/)
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :is_upper?, :is_uppercase?
|
18
|
+
alias_method :is_uppercase?, :is_upper?
|
19
|
+
alias_method :is_lowercase?, :is_downcase?
|
20
|
+
alias_method :is_lower?, :is_downcase?
|
21
|
+
|
22
|
+
|
23
|
+
def matches_glob?(pattern)
|
24
|
+
File.fnmatch pattern, self
|
25
|
+
end
|
26
|
+
|
27
|
+
# encoding
|
28
|
+
def b64
|
29
|
+
Base64.strict_encode64 self
|
30
|
+
end
|
31
|
+
|
32
|
+
def b64d
|
33
|
+
Base64.decode64 self
|
34
|
+
end
|
35
|
+
|
36
|
+
def encode_url
|
37
|
+
CGI.escape self
|
38
|
+
end
|
39
|
+
|
40
|
+
def decode_url
|
41
|
+
CGI.unescape self
|
42
|
+
end
|
43
|
+
|
44
|
+
def decode_json
|
45
|
+
JSON.parse self
|
46
|
+
end
|
47
|
+
|
48
|
+
def decode_yaml
|
49
|
+
YAML.safe_load self
|
50
|
+
end
|
51
|
+
|
52
|
+
# decodes either a single json element or lines of json elements
|
53
|
+
def decode_multi_json
|
54
|
+
JSON.parse self
|
55
|
+
rescue JSON::ParserError
|
56
|
+
lines.map(&:decode_json)
|
57
|
+
end
|
58
|
+
|
59
|
+
# escaping
|
60
|
+
def escape_regexp
|
61
|
+
Regexp.quote self
|
62
|
+
end
|
63
|
+
|
64
|
+
# hashing
|
65
|
+
def sha1
|
66
|
+
Digest::SHA1.hexdigest self
|
67
|
+
end
|
68
|
+
|
69
|
+
def sha256
|
70
|
+
Digest::SHA256.hexdigest self
|
71
|
+
end
|
72
|
+
|
73
|
+
def sha512
|
74
|
+
Digest::SHA512.hexdigest self
|
75
|
+
end
|
76
|
+
|
77
|
+
def md5
|
78
|
+
Digest::MD5.hexdigest self
|
79
|
+
end
|
80
|
+
|
81
|
+
# files
|
82
|
+
def read_file
|
83
|
+
File.read self
|
84
|
+
end
|
85
|
+
|
86
|
+
def write_file(name)
|
87
|
+
# write the file creating it if it doesnt exist
|
88
|
+
File.open(name, 'w') { |f| f.write self }
|
89
|
+
end
|
90
|
+
|
91
|
+
def append_file(name)
|
92
|
+
File.write name, self, mode: 'a'
|
93
|
+
end
|
94
|
+
|
95
|
+
def prepend_file(name)
|
96
|
+
File.write name, self + File.read(name)
|
97
|
+
end
|
98
|
+
|
99
|
+
def read_yaml
|
100
|
+
read_file.from_yaml
|
101
|
+
end
|
102
|
+
|
103
|
+
def read_json
|
104
|
+
read_file.from_json
|
105
|
+
end
|
106
|
+
|
8
107
|
def include_any?(ss)
|
9
108
|
ss.any? { include? _1 }
|
10
109
|
end
|
@@ -44,7 +143,9 @@ module StringExt
|
|
44
143
|
end
|
45
144
|
|
46
145
|
def to_bool
|
47
|
-
|
146
|
+
s = downcase
|
147
|
+
return nil if s != 'true' && s != 'false'
|
148
|
+
s == 'true'
|
48
149
|
end
|
49
150
|
|
50
151
|
def from_b64
|
@@ -107,6 +208,10 @@ module StringExt
|
|
107
208
|
self.lines.select { _1.matches_pat? pat }
|
108
209
|
end
|
109
210
|
|
211
|
+
def grepv(pat)
|
212
|
+
self.lines.reject { _1.matches_pat? pat }
|
213
|
+
end
|
214
|
+
|
110
215
|
def matches_pat?(pat)
|
111
216
|
case pat
|
112
217
|
when String
|
@@ -122,6 +227,10 @@ module StringExt
|
|
122
227
|
p + self
|
123
228
|
end
|
124
229
|
|
230
|
+
def suffix(s)
|
231
|
+
self + s
|
232
|
+
end
|
233
|
+
|
125
234
|
def to_bs
|
126
235
|
n, units = self.scan(/([0-9.]+)(B|KB|MB|GB)/).first
|
127
236
|
raise "Invalid size: #{self}" unless n && units
|
@@ -165,5 +274,11 @@ module StringExt
|
|
165
274
|
def indent_count
|
166
275
|
scan(/^\s+/).first&.length || 0
|
167
276
|
end
|
277
|
+
|
278
|
+
alias_method :is_number?, :is_numeric?
|
279
|
+
alias_method :suf?, :end_with?
|
280
|
+
alias_method :pref?, :start_with?
|
281
|
+
alias_method :starts_with?, :start_with?
|
282
|
+
alias_method :ends_with?, :end_with?
|
168
283
|
end
|
169
284
|
end
|
data/w-stdlib.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |g|
|
2
2
|
g.name = 'w-stdlib'
|
3
|
-
g.version = '0.0.
|
3
|
+
g.version = '0.0.15'
|
4
4
|
g.summary = 'Pre-alpha package that contains abstractions that I find useful when scripting. Not suitable for production use. Breaking changes highly likely even with minor version bumps. Use at your own risk'
|
5
5
|
g.description = g.summary
|
6
6
|
g.authors = ['will@btlr.dev']
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: w-stdlib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- will@btlr.dev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parallel
|