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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3355c40d71a6abcf9d8d1f74872c23fa251444c5105f3798f6cbbe90497635ed
4
- data.tar.gz: 17f0b9a70c22a681bfc6576e9f7b5789d9dd592bdc300803308c620595bb9740
3
+ metadata.gz: 99306665498093b2a9e70d81c1e1a6320a146b8861706a4194a9b15c7c4bc4b8
4
+ data.tar.gz: 875e7062c1762943c45893bf147b5d43a534df15b359f22830415d6cc80f0ef5
5
5
  SHA512:
6
- metadata.gz: 27a8fb9264d9cf7ff56d471107b2a45e9a3968fe5fe4188606ff6a2184fc5c9c32f62e5f52a323b5fe5c7433c23cccb69c23ffa146eedcf8d7d1a09a145d91ae
7
- data.tar.gz: 13aa45d3fd2fc54f0a7a74a93ae191fb49f725efad56877c934143f81ca1a1d64e654318d2260169d8d8dff05a8f8d2bc0cd28da550f738ee82914eb234a1986
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
@@ -1,6 +1,7 @@
1
1
  require_relative './string'
2
2
  require 'aws-sdk-iam'
3
3
  require 'aws-sdk-core/arn_parser'
4
+ require 'w-stdlib/prelude'
4
5
 
5
6
  using StringExt
6
7
 
@@ -13,7 +13,8 @@ module FileExt
13
13
  end
14
14
 
15
15
  def strings
16
- `strings '#{name}'`
16
+ n = name.gsub('\'', '\\\'')
17
+ `strings '#{n}'`
17
18
  end
18
19
  end
19
20
  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
- def _?
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
- self.downcase == 'true'
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
@@ -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.14'
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.14
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: 2022-07-07 00:00:00.000000000 Z
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parallel