string_splitter 0.4.0 → 0.5.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: 6e73d7818b793f4c8dcbba35afb97045428085f17c07df328875383b2d162818
4
- data.tar.gz: 6a15e18b265779aeaf5d8b18c5e13d70299ed7abbc52da815e2cee46419ce9a1
3
+ metadata.gz: ce843d4e98a02b464296d78d6be21f9fd7ce179b8156889f799280594250072c
4
+ data.tar.gz: 4a378aaa5c951224b3091c3e1bf5d55c5a61f6c5395f9cf820ba008b6b1b7af6
5
5
  SHA512:
6
- metadata.gz: dee0e70311f3718d0f7b68f77b56c657e137c8a658905ddc58f4fbef536b80071dced6a9836d0ad531b93e15c16c5f25ec42965f02a0a1e016de179b133bf746
7
- data.tar.gz: 72a237dd80e3e06aad4a5bcc4545e6ea4d3d8167beb9afa6b502d964d510857947fe6071a88ded9f83df77a54da059c97abc1b3e70d8c14a03c8b77b0a01a675
6
+ metadata.gz: 68f9104ab3c5145322e5027814062a52692e854240a47df3e456d3a5d5c04f213a7875741aa00c6dd7bd5abb71e48f9e09b3d9dc5eb9bb488ef314849952c277
7
+ data.tar.gz: b2cdb8d72ef41459085580192db7e2792025ed16a84bda7a27b82e148bcd40eddb9b9fe2b5b866d9417085b8560905e1667516db843cd57aa8712639462766f9
data/CHANGELOG.md CHANGED
@@ -1,10 +1,16 @@
1
+ ## 0.5.0 - 2018-06-26
2
+
3
+ - don't treat string delimiters as patterns
4
+ - add a `reject`/`exclude` option which rejects splits at the specified positions
5
+ - add a `select` alias for `at`
6
+
1
7
  ## 0.4.0 - 2018-06-24
2
8
 
3
9
  - **breaking change**: remove the `offset` alias for `split.index`
4
10
 
5
11
  ## 0.3.1 - 2018-06-24
6
12
 
7
- - remove trailing empty field when the separator is empty (#1)
13
+ - remove trailing empty field when the separator is empty ([#1](https://github.com/chocolateboy/string_splitter/issues/1))
8
14
 
9
15
  ## 0.3.0 - 2018-06-23
10
16
 
data/README.md CHANGED
@@ -179,7 +179,7 @@ ss.split(line, at: [1..5, 8])
179
179
 
180
180
  # VERSION
181
181
 
182
- 0.4.0
182
+ 0.5.0
183
183
 
184
184
  # SEE ALSO
185
185
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class StringSplitter
4
- VERSION = '0.4.0'
4
+ VERSION = '0.5.0'
5
5
  end
@@ -12,7 +12,7 @@ require 'values'
12
12
  # These enhancements allow splits to handle many cases that otherwise require bigger
13
13
  # guns e.g. regex matching or parsing.
14
14
  class StringSplitter
15
- ACCEPT = ->(_split) { true }
15
+ ACCEPT_ALL = ->(_split) { true }
16
16
  DEFAULT_DELIMITER = /\s+/
17
17
  NO_SPLITS = []
18
18
 
@@ -38,8 +38,22 @@ class StringSplitter
38
38
 
39
39
  attr_reader :default_delimiter, :include_captures, :remove_empty, :spread_captures
40
40
 
41
- def split(string, delimiter = @default_delimiter, at: nil, &block)
42
- result, block, splits, count, index = split_common(string, delimiter, at, block)
41
+ def split(
42
+ string,
43
+ delimiter = @default_delimiter,
44
+ at: nil,
45
+ select: at,
46
+ exclude: nil,
47
+ reject: exclude,
48
+ &block
49
+ )
50
+ result, block, splits, count, index = split_common(
51
+ string: string,
52
+ delimiter: delimiter,
53
+ select: select,
54
+ reject: reject,
55
+ block: block
56
+ )
43
57
 
44
58
  splits.each do |split|
45
59
  split = Split.with(split.merge({ index: (index += 1), count: count }))
@@ -66,8 +80,22 @@ class StringSplitter
66
80
 
67
81
  alias lsplit split
68
82
 
69
- def rsplit(string, delimiter = @default_delimiter, at: nil, &block)
70
- result, block, splits, count, index = split_common(string, delimiter, at, block)
83
+ def rsplit(
84
+ string,
85
+ delimiter = @default_delimiter,
86
+ at: nil,
87
+ select: at,
88
+ exclude: nil,
89
+ reject: exclude,
90
+ &block
91
+ )
92
+ result, block, splits, count, index = split_common(
93
+ string: string,
94
+ delimiter: delimiter,
95
+ select: select,
96
+ reject: reject,
97
+ block: block
98
+ )
71
99
 
72
100
  splits.reverse!.each do |split|
73
101
  split = Split.with(split.merge({ index: (index += 1), count: count }))
@@ -129,19 +157,31 @@ class StringSplitter
129
157
  end
130
158
 
131
159
  # setup common to both split methods
132
- def split_common(string, delimiter, at, block)
160
+ def split_common(string:, delimiter:, select:, reject:, block:)
133
161
  unless (match = string.match(delimiter))
134
162
  result = (@remove_empty && string.empty?) ? [] : [string]
135
163
  return [result, block, NO_SPLITS, 0, -1]
136
164
  end
137
165
 
166
+ select = Array(select)
167
+ reject = Array(reject)
168
+
169
+ if !reject.empty?
170
+ positions = reject
171
+ action = :reject
172
+ elsif !select.empty?
173
+ positions = select
174
+ action = :select
175
+ end
176
+
138
177
  ncaptures = match.captures.length
178
+ delimiter = Regexp.quote(delimiter) if delimiter.is_a?(String)
139
179
  delimiter = increment_backrefs(delimiter, ncaptures)
140
180
  parts = string.split(/(#{delimiter})/, -1)
141
181
  remove_trailing_empty_field!(parts, ncaptures)
142
182
  result, splits = splits_for(parts, ncaptures)
143
183
  count = splits.length
144
- block ||= at ? match_positions(at, count) : ACCEPT
184
+ block ||= positions ? match_positions(positions, action, count) : ACCEPT_ALL
145
185
 
146
186
  [result, block, splits, count, -1]
147
187
  end
@@ -218,7 +258,7 @@ class StringSplitter
218
258
  parts.pop(count)
219
259
  end
220
260
 
221
- def match_positions(positions, nsplits)
261
+ def match_positions(positions, action, nsplits)
222
262
  positions = Array(positions).map do |position|
223
263
  if position.is_a?(Integer) && position.negative?
224
264
  # translate negative indices to 1-based non-negative indices e.g:
@@ -242,8 +282,10 @@ class StringSplitter
242
282
  end
243
283
  end
244
284
 
285
+ match = action == :select
286
+
245
287
  lambda do |split|
246
- case split.position when *positions then true else false end
288
+ case split.position when *positions then match else !match end
247
289
  end
248
290
  end
249
291
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_splitter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chocolateboy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-24 00:00:00.000000000 Z
11
+ date: 2018-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: values