w-stdlib 0.0.14 → 0.1.0
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 +8 -0
- data/lib/w-stdlib/core_ext/object.rb +16 -1
- data/lib/w-stdlib/core_ext/string.rb +115 -1
- data/lib/w-stdlib/prelude.rb +6 -0
- 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: 99306665498093b2a9e70d81c1e1a6320a146b8861706a4194a9b15c7c4bc4b8
|
4
|
+
data.tar.gz: 875e7062c1762943c45893bf147b5d43a534df15b359f22830415d6cc80f0ef5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d02a46a406cff16e17834b22d460a3b19532c51acc5d079cac669751e409da1bcbb95406f71656e91fc0da3838943e58839ea80f0402b55a6c3c2d176985fe8a
|
7
|
+
data.tar.gz: 6973d4ffbb55fac0bed17fde82c23486b3f240cd20514d67acfb266e3c8353327cc671f1f898070260896d7fa8a10979dfa1bb0bb6bddd2e4356c03d672c0046
|
@@ -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
|
@@ -20,8 +20,16 @@ module HashExt
|
|
20
20
|
select { _1.to_s.matches_pat?(pat) }
|
21
21
|
end
|
22
22
|
|
23
|
+
def search_vals(pat)
|
24
|
+
select { _2.to_s.matches_pat?(pat) }
|
25
|
+
end
|
26
|
+
|
23
27
|
def puts_all
|
24
28
|
each { puts "#{_1}: #{_2}"}
|
25
29
|
end
|
30
|
+
|
31
|
+
def pluck(*keys)
|
32
|
+
select { keys.include? _1 }
|
33
|
+
end
|
26
34
|
end
|
27
35
|
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,107 @@ 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
|
+
def matches_glob?(pattern)
|
23
|
+
File.fnmatch pattern, self
|
24
|
+
end
|
25
|
+
|
26
|
+
# encoding
|
27
|
+
def b64
|
28
|
+
Base64.strict_encode64 self
|
29
|
+
end
|
30
|
+
|
31
|
+
def b64d
|
32
|
+
Base64.decode64 self
|
33
|
+
end
|
34
|
+
|
35
|
+
def encode_url
|
36
|
+
CGI.escape self
|
37
|
+
end
|
38
|
+
|
39
|
+
def decode_url
|
40
|
+
CGI.unescape self
|
41
|
+
end
|
42
|
+
|
43
|
+
def decode_json
|
44
|
+
JSON.parse self
|
45
|
+
end
|
46
|
+
|
47
|
+
def decode_yaml
|
48
|
+
YAML.safe_load self
|
49
|
+
end
|
50
|
+
|
51
|
+
# decodes either a single json element or lines of json elements
|
52
|
+
def decode_multi_json
|
53
|
+
JSON.parse self
|
54
|
+
rescue JSON::ParserError
|
55
|
+
lines.map(&:decode_json)
|
56
|
+
end
|
57
|
+
|
58
|
+
# escaping
|
59
|
+
def escape_regexp
|
60
|
+
Regexp.quote self
|
61
|
+
end
|
62
|
+
|
63
|
+
# hashing
|
64
|
+
def sha1
|
65
|
+
Digest::SHA1.hexdigest self
|
66
|
+
end
|
67
|
+
|
68
|
+
def sha256
|
69
|
+
Digest::SHA256.hexdigest self
|
70
|
+
end
|
71
|
+
|
72
|
+
def sha512
|
73
|
+
Digest::SHA512.hexdigest self
|
74
|
+
end
|
75
|
+
|
76
|
+
def md5
|
77
|
+
Digest::MD5.hexdigest self
|
78
|
+
end
|
79
|
+
|
80
|
+
# files
|
81
|
+
def read_file
|
82
|
+
File.read self
|
83
|
+
end
|
84
|
+
|
85
|
+
def write_file(name)
|
86
|
+
# write the file creating it if it doesnt exist
|
87
|
+
File.open(name, 'w') { |f| f.write self }
|
88
|
+
end
|
89
|
+
|
90
|
+
def append_file(name)
|
91
|
+
File.write name, self, mode: 'a'
|
92
|
+
end
|
93
|
+
|
94
|
+
def prepend_file(name)
|
95
|
+
File.write name, self + File.read(name)
|
96
|
+
end
|
97
|
+
|
98
|
+
def read_yaml
|
99
|
+
read_file.from_yaml
|
100
|
+
end
|
101
|
+
|
102
|
+
def read_json
|
103
|
+
read_file.from_json
|
104
|
+
end
|
105
|
+
|
8
106
|
def include_any?(ss)
|
9
107
|
ss.any? { include? _1 }
|
10
108
|
end
|
@@ -44,7 +142,9 @@ module StringExt
|
|
44
142
|
end
|
45
143
|
|
46
144
|
def to_bool
|
47
|
-
|
145
|
+
s = downcase
|
146
|
+
return nil if s != 'true' && s != 'false'
|
147
|
+
s == 'true'
|
48
148
|
end
|
49
149
|
|
50
150
|
def from_b64
|
@@ -107,6 +207,10 @@ module StringExt
|
|
107
207
|
self.lines.select { _1.matches_pat? pat }
|
108
208
|
end
|
109
209
|
|
210
|
+
def grepv(pat)
|
211
|
+
self.lines.reject { _1.matches_pat? pat }
|
212
|
+
end
|
213
|
+
|
110
214
|
def matches_pat?(pat)
|
111
215
|
case pat
|
112
216
|
when String
|
@@ -122,6 +226,10 @@ module StringExt
|
|
122
226
|
p + self
|
123
227
|
end
|
124
228
|
|
229
|
+
def suffix(s)
|
230
|
+
self + s
|
231
|
+
end
|
232
|
+
|
125
233
|
def to_bs
|
126
234
|
n, units = self.scan(/([0-9.]+)(B|KB|MB|GB)/).first
|
127
235
|
raise "Invalid size: #{self}" unless n && units
|
@@ -165,5 +273,11 @@ module StringExt
|
|
165
273
|
def indent_count
|
166
274
|
scan(/^\s+/).first&.length || 0
|
167
275
|
end
|
276
|
+
|
277
|
+
alias_method :is_number?, :is_numeric?
|
278
|
+
alias_method :suf?, :end_with?
|
279
|
+
alias_method :pref?, :start_with?
|
280
|
+
alias_method :starts_with?, :start_with?
|
281
|
+
alias_method :ends_with?, :end_with?
|
168
282
|
end
|
169
283
|
end
|
data/lib/w-stdlib/prelude.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
require 'http'
|
2
2
|
require 'json'
|
3
3
|
require_relative 'core_ext/string'
|
4
|
+
require_relative 'core_ext/array'
|
5
|
+
require_relative 'core_ext/hash'
|
6
|
+
require_relative 'core_ext/object'
|
4
7
|
|
5
8
|
using StringExt
|
9
|
+
using ArrayExt
|
10
|
+
using HashExt
|
11
|
+
using ObjectExt
|
6
12
|
|
7
13
|
def fatal(msg, code=1, pref='[!] ')
|
8
14
|
STDERR.puts pref + msg
|
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.1.0'
|
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.1.0
|
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
|