kirinnee_core 0.1.0 → 0.2.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: 4068190f854f11d75d60324df351a0d53617fe484cf447b8bbdc425fb5de43f6
4
- data.tar.gz: 9cbeff28d871508bc0f4c39896b30b4d9586997a6cbcc6f0498a09b940cc2ed2
3
+ metadata.gz: 7f4c87ce2cbfdebdb3300709a2be01ad032187ea1862fd1e57beebec6e03027c
4
+ data.tar.gz: ed9d0c8448169569302e264e485f98a311bfdf4afc8f918de879797f9c0d26b9
5
5
  SHA512:
6
- metadata.gz: 9a903a2a88285b81d32e819574cc3089246ef8f719c20200b89b2a1dd949e03119c805eeac4e33defdf52003e23a63b5938df936a68979423f8830f89cf8ece3
7
- data.tar.gz: f4a39ec03bf3d1c18a17424931c3864023934d5ad813726be15b25ca044f8c85e9039f767713bd0ee638f39662798bec4ed375fe69f2cc7cf4472866619bc357
6
+ metadata.gz: 29f6adf809bb257e53bc881d9a63025e3c9a1252ba48d86a8202aa9e1b3451140f445e7142062afece38d79843918f42b3e60d659e16f71f93c8911443d32b69
7
+ data.tar.gz: a8c29f487565169043cf78b066b6a57100dcabf19f39b252ba20c42b9e8e02e79555270d3d8e5f8753184593da1c22386374f526ba8d36eb043b57a86c8fcf2e
data/.gitignore CHANGED
@@ -14,3 +14,4 @@
14
14
  .idea/vcs.xml
15
15
  # rspec failure tracking
16
16
  .rspec_status
17
+ *.gem
data/.gitlab-ci.yml CHANGED
@@ -1,15 +1,38 @@
1
- stages:
2
- - test
3
- - build
4
- - publish
5
-
6
- Unit Test:
7
- stage: test
8
- script: bundle exec rspec
9
- artifacts:
10
-
11
-
12
-
13
- Publish:
14
- stage: publish
15
- script:
1
+ stages:
2
+ - test
3
+ - build
4
+ - publish
5
+
6
+ Unit Test:
7
+ tags:
8
+ - ruby
9
+ - kirinnee
10
+ stage: test
11
+ script:
12
+ - bundle install
13
+ - bundle exec rspec
14
+ Build:
15
+ tags:
16
+ - ruby
17
+ - kirinnee
18
+ stage: build
19
+ script: gem build $GEM_NAME
20
+ artifacts:
21
+ paths:
22
+ - ./*.gem
23
+ Publish:
24
+ tags:
25
+ - ruby
26
+ - kirinnee
27
+ stage: publish
28
+ script:
29
+ - mkdir ~/.gem
30
+ - "echo \":rubygems_api_key: $API_KEY\" > ~/.gem/credentials"
31
+ - chmod 0600 ~/.gem/credentials
32
+ - gem push $GEM_NAME-*.gem
33
+ after_script:
34
+ - shred -vfu ~/.gem/credentials
35
+ only:
36
+ - /v[0-9\.]*/
37
+ except:
38
+ - branches
data/.idea/encodings.xml CHANGED
@@ -1,4 +1,4 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="Encoding" addBOMForNewFiles="with NO BOM" />
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="Encoding" addBOMForNewFiles="with NO BOM" />
4
4
  </project>
@@ -1,55 +1,55 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="RUBY_MODULE" version="4">
3
- <component name="ModuleRunConfigurationManager">
4
- <shared>
5
- <configuration default="false" name="Unit Test" type="RSpecRunConfigurationType" factoryName="RSpec">
6
- <module name="kirinnee_core" />
7
- <predefined_log_file enabled="true" id="RUBY_RSPEC" />
8
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="" />
9
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="" />
10
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
11
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
12
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
13
- <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="false" />
14
- <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
15
- <EXTENSION ID="RubyCoverageRunConfigurationExtension" track_test_folders="true" runner="rcov" />
16
- <EXTENSION ID="net.ashald.envfile">
17
- <option name="IS_ENABLED" value="false" />
18
- <option name="IS_SUBST" value="false" />
19
- <option name="IS_PATH_MACRO_SUPPORTED" value="false" />
20
- <option name="IS_IGNORE_MISSING_FILES" value="false" />
21
- <ENTRIES>
22
- <ENTRY IS_ENABLED="true" PARSER="runconfig" />
23
- </ENTRIES>
24
- </EXTENSION>
25
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TESTS_FOLDER_PATH" VALUE="$MODULE_DIR$/spec" />
26
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_SCRIPT_PATH" VALUE="" />
27
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_RUNNER_PATH" VALUE="" />
28
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_FILE_MASK" VALUE="" />
29
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_EXAMPLE_NAME" VALUE="" />
30
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_TEST_TYPE" VALUE="ALL_IN_FOLDER" />
31
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_ARGS" VALUE="" />
32
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUNNER_VERSION" VALUE="" />
33
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="USE_CUSTOM_SPEC_RUNNER" VALUE="false" />
34
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="DRB" VALUE="false" />
35
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
36
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPRING" VALUE="false" />
37
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="FULL_BACKTRACE" VALUE="true" />
38
- <method v="2" />
39
- </configuration>
40
- </shared>
41
- </component>
42
- <component name="NewModuleRootManager">
43
- <content url="file://$MODULE_DIR$" />
44
- <orderEntry type="inheritedJdk" />
45
- <orderEntry type="sourceFolder" forTests="false" />
46
- <orderEntry type="library" scope="PROVIDED" name="bundler (v1.17.2, ruby-2.6.0-p0) [gem]" level="application" />
47
- <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.3, ruby-2.6.0-p0) [gem]" level="application" />
48
- <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.6.0-p0) [gem]" level="application" />
49
- <orderEntry type="library" scope="PROVIDED" name="rspec (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
50
- <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
51
- <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.8.2, ruby-2.6.0-p0) [gem]" level="application" />
52
- <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
53
- <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
54
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared>
5
+ <configuration default="false" name="Unit Test" type="RSpecRunConfigurationType" factoryName="RSpec">
6
+ <module name="kirinnee_core" />
7
+ <predefined_log_file enabled="true" id="RUBY_RSPEC" />
8
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="" />
9
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="" />
10
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
11
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
12
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
13
+ <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="false" />
14
+ <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
15
+ <EXTENSION ID="RubyCoverageRunConfigurationExtension" track_test_folders="true" runner="rcov" />
16
+ <EXTENSION ID="net.ashald.envfile">
17
+ <option name="IS_ENABLED" value="false" />
18
+ <option name="IS_SUBST" value="false" />
19
+ <option name="IS_PATH_MACRO_SUPPORTED" value="false" />
20
+ <option name="IS_IGNORE_MISSING_FILES" value="false" />
21
+ <ENTRIES>
22
+ <ENTRY IS_ENABLED="true" PARSER="runconfig" />
23
+ </ENTRIES>
24
+ </EXTENSION>
25
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TESTS_FOLDER_PATH" VALUE="$MODULE_DIR$/spec" />
26
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_SCRIPT_PATH" VALUE="" />
27
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_RUNNER_PATH" VALUE="" />
28
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_FILE_MASK" VALUE="" />
29
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_EXAMPLE_NAME" VALUE="" />
30
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="TEST_TEST_TYPE" VALUE="ALL_IN_FOLDER" />
31
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPEC_ARGS" VALUE="" />
32
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="RUNNER_VERSION" VALUE="" />
33
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="USE_CUSTOM_SPEC_RUNNER" VALUE="false" />
34
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="DRB" VALUE="false" />
35
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
36
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPRING" VALUE="false" />
37
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="FULL_BACKTRACE" VALUE="false" />
38
+ <method v="2" />
39
+ </configuration>
40
+ </shared>
41
+ </component>
42
+ <component name="NewModuleRootManager">
43
+ <content url="file://$MODULE_DIR$" />
44
+ <orderEntry type="inheritedJdk" />
45
+ <orderEntry type="sourceFolder" forTests="false" />
46
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.17.2, ruby-2.6.0-p0) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.3, ruby-2.6.0-p0) [gem]" level="application" />
48
+ <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.6.0-p0) [gem]" level="application" />
49
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
50
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
51
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.8.2, ruby-2.6.0-p0) [gem]" level="application" />
52
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
53
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.8.0, ruby-2.6.0-p0) [gem]" level="application" />
54
+ </component>
55
55
  </module>
data/.idea/misc.xml CHANGED
@@ -1,7 +1,7 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="JavaScriptSettings">
4
- <option name="languageLevel" value="ES6" />
5
- </component>
6
- <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.6.0-p0" project-jdk-type="RUBY_SDK" />
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.6.0-p0" project-jdk-type="RUBY_SDK" />
7
7
  </project>
data/.idea/modules.xml CHANGED
@@ -1,8 +1,8 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/kirinnee_core.iml" filepath="$PROJECT_DIR$/.idea/kirinnee_core.iml" />
6
- </modules>
7
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/kirinnee_core.iml" filepath="$PROJECT_DIR$/.idea/kirinnee_core.iml" />
6
+ </modules>
7
+ </component>
8
8
  </project>
@@ -31,7 +31,7 @@
31
31
  <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="DRB" VALUE="false" />
32
32
  <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="ZEUS" VALUE="false" />
33
33
  <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="SPRING" VALUE="false" />
34
- <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="FULL_BACKTRACE" VALUE="true" />
34
+ <RSPEC_RUN_CONFIG_SETTINGS_ID NAME="FULL_BACKTRACE" VALUE="false" />
35
35
  <method v="2" />
36
36
  </configuration>
37
37
  </component>
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kirinnee_core (0.1.0)
4
+ kirinnee_core (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Kirinnee Core
2
2
 
3
- Personal core library that monkey patches basic types such as string, integers and enumerables with utility method
4
- that I am used to.
3
+ Personal core library that monkey patches basic types such as string and enumerable with utility method.
4
+ How each method is used is list below.
5
5
 
6
6
  ## Installation
7
7
 
@@ -21,13 +21,348 @@ Or install it yourself as:
21
21
 
22
22
  ## Usage
23
23
 
24
- TODO: Write usage instructions here
24
+ ### Enumerable
25
+ Here are the monkey patched methods for enumerable
26
+
27
+ ---
28
+ ##### where `predicate`
29
+ Filters based on predicate passed via block. Variant of `select`, `filter` and `find_all` in ruby.
30
+
31
+ Does not mutate original enumerable
32
+ ```ruby
33
+ [1,2,3].where {|x| x%2!=0 } # => [1,3]
34
+ ```
35
+
36
+ ##### where! `predicate`
37
+ Filters base on predicate passed via block.
38
+ Variant of `select`, `filter`, `find_all` method in ruby
39
+
40
+ Mutates the original enumerable
41
+ ```ruby
42
+ [1,2,3].where! { |x| x%2!=0 } # => [1,3]
43
+ ```
44
+
45
+ ##### skip `x`
46
+ Skips the first x elements of the enumerable
47
+
48
+ Does not mutate the original enumerable
49
+ ```ruby
50
+ [1,2,3,4,5].skip 3 #=> [4,5]
51
+ {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}.skip 3 #=> {:d=>4, :e=>5}
52
+ ```
53
+
54
+ ##### skip_while `predicate`
55
+ Skips the elements of the enumerable while the predicate evaluates to true
56
+
57
+ Does not mutate the original enumerable
58
+ ```ruby
59
+ [1,2,3,2,1,0].skip_while {|x| x < 3} # => [3,2,1,0]
60
+ ```
61
+
62
+ ##### back `x`
63
+ Takes the last x element of the enumerable
64
+
65
+ Does not mutate the original enumerable
66
+ ```ruby
67
+ [1,2,3,4,5,6].back 3 #=> [4,5,6]
68
+ [1,2,3,4,5,6].back 100 #=> [1,2,3,4,5,6]
69
+ [1,2,3,4,5,6].back 0 #=> []
70
+ ```
71
+
72
+ ##### back_while `predicate`
73
+ Takes the last x element while predicate evaluates to true
74
+
75
+ Does not mutate the original enumerable
76
+ ```ruby
77
+ [1,2,3,2,1,0].back_while {|x| x <2} # => [1,0]
78
+ ```
79
+
80
+ ##### omit `x`
81
+ Omits the last x element of the enumerable
82
+
83
+ Does not mutate the original enumerable
84
+ ```ruby
85
+ [1,2,3,4,5].omit 2 # => [1,2,3]
86
+ [1,2,3,4,5].omit 100 #=> []
87
+ [1,2,3,4,5].omit 0 # => [1,2,3,4,5]
88
+ ```
89
+
90
+ ##### omit_while `predicate`
91
+ Omits the last element while the predicate evaluates to true
92
+
93
+ Does not mutate the original enumerable
94
+ ```ruby
95
+ [1,2,3,2,1,0].omit_while { |x| x <3} #=> [1,2,3]
96
+ ```
97
+
98
+ ##### remove `search`
99
+ Removes all occurrences of the element within the enumerable
100
+
101
+ Does not mutate the original enumerable
102
+ ```ruby
103
+ [1,2,3,1,2,3].remove 3 #=> [1,2,1,2]
104
+ %w(apple pear apple pear).remove "apple" #=> %w(pear pear)
105
+ ```
106
+
107
+ ##### has? `search`
108
+ Whether the enumerable contains at least 1 element that matches the input
109
+ ```ruby
110
+ [1,2,3,4,5,6,1].has? 1 # true
111
+ [1,2,3,4,5,6,1].has? 2 # true
112
+ [1,2,3,4,5,6,1].has? 7 # false
113
+ ```
114
+
115
+ ##### without `array`
116
+ Remove all occurrences of each element in provided array from target array
117
+
118
+ Does not mutate the original enumerable
119
+ ```ruby
120
+ [1,2,3,4,1,2,3,4].without [2,4] #=> [1,3,1,3]
121
+ ```
122
+ ##### without_index `indexes`
123
+ Remove all occurrences of each element in provided array from target array
124
+
125
+ Does not mutate the original enumerable
126
+ ```ruby
127
+ [1,2,3,4,1,2,3,4].without [2,4] #=> [1,3,1,3]
128
+ ```
129
+
130
+ ##### indexes `predicate`
131
+ Gets the indexes that matches the predicate
132
+
133
+ Does not mutate the original enumerable
134
+ ```ruby
135
+ [6,5,4,3,2].indexes {|x| x < 4} # => [3,4]
136
+ ```
137
+
138
+ ### String
139
+ Here are the monkey patched methods for Strings
140
+
141
+ ---
142
+ ##### replace_all `search`,`tareget`
143
+ Replaces the search string with the target string.
144
+
145
+ Does not mutate original string
146
+ ```ruby
147
+ "a=>b=>c".replace_all("=>","-") # => "a-b-c"
148
+ ```
149
+
150
+ ##### replace_all! `search`,`tareget`
151
+ Replaces the search string with the target string.
152
+
153
+ Mutates original string
154
+ ```ruby
155
+ "a=>b=>c".replace_all!("=>","-") # => "a-b-c"
156
+ ```
157
+
158
+ ##### take `x`
159
+ Takes the first x characters of the string
160
+
161
+ Does not mutate original string
162
+ ```ruby
163
+ "Singapore".take 4 # => "Sing"
164
+ "Singapore".take 100 # => "Singapore"
165
+ "Singapore".take 0 # => ""
166
+ ```
167
+
168
+ ##### take! `x`
169
+ Takes the first x characters of the string
170
+
171
+ Mutates original string
172
+ ```ruby
173
+ "Singapore".take! 4 # => "Sing"
174
+ "Singapore".take! 100 # => "Singapore"
175
+ "Singapore".take! 0 # => ""
176
+ ```
177
+
178
+ ##### skip `x`
179
+ Skips the first x characters of the string
180
+
181
+ Does not mutate the original string
182
+ ```ruby
183
+ "Singapore".skip 5 # => "pore"
184
+ "Singapore".skip 100 # => ""
185
+ "Singapore".skip 0 # => "Singapore"
186
+ ```
187
+
188
+ ##### skip! `x`
189
+ Skips the first x characters of the string
190
+
191
+ Mutate the original string
192
+ ```ruby
193
+ "Singapore".skip! 5 # => "pore"
194
+ "Singapore".skip! 100 # => ""
195
+ "Singapore".skip! 0 # => "Singapore"
196
+ ```
197
+
198
+ ##### omit `x`
199
+ Omits the last x characters of the string
200
+
201
+ Does not mutate the original string
202
+ ```ruby
203
+ "Singapore".omit 5 # => "Sing"
204
+ "Singapore".omit 100 # => ""
205
+ "Singapore".omit 0 # => "Singapore"
206
+ ```
207
+
208
+ ##### omit! `x`
209
+ Omits the last x characters of the string
210
+
211
+ Mutates the original string
212
+ ```ruby
213
+ "Singapore".omit! 5 # => "Sing"
214
+ "Singapore".omit! 100 # => ""
215
+ "Singapore".omit! 0 # => "Singapore"
216
+ ```
217
+
218
+ ##### back `x`
219
+ Takes the last x characters of the string
220
+
221
+ Does not mutate the original string
222
+
223
+ ```ruby
224
+ "Singapore".back 4 #=> "pore"
225
+ "Singapore".back 100 # =>"Singapore"
226
+ "Singapore".back 0 # => ""
227
+ ```
228
+
229
+ ##### back! `x`
230
+ Takes the last x characters of the string
231
+
232
+ Mutates the original string
233
+
234
+ ```ruby
235
+ "Singapore".back! 4 #=> "pore"
236
+ "Singapore".back! 100 # =>"Singapore"
237
+ "Singapore".back! 0 # => ""
238
+ ```
239
+
240
+ ##### remove_char_at `x`
241
+ Removes the character at the index.
242
+ Returns without removing characters if index or negative index
243
+ exceeds length of string
244
+
245
+ Does not mutate the original string
246
+
247
+ ```ruby
248
+ "Hey! scent!".remove_char_at 0 # => "ey! scent!"
249
+ "Hey! scent!".remove_char_at 3 # => "Hey scent!"
250
+ "Hey! scent!".remove_char_at -1 # => "Hey! scent"
251
+ "Hey! scent!".remove_char_at -6 #=> "Hey! cent!"
252
+ "Hey! scent!".remove_char_at 100 # => "Hey! scent!"
253
+ "Hey! scent!".remove_char_at -100 # => "Hey! scent!"
254
+ ```
255
+
256
+ ##### remove_char_at! `x`
257
+ Removes the character at the index.
258
+ Returns without removing characters if index or negative index
259
+ exceeds length of string
260
+
261
+ Does not mutate the original string
262
+
263
+ ```ruby
264
+ "Hey! scent!".remove_char_at! 0 # => "ey! scent!"
265
+ "Hey! scent!".remove_char_at! 3 # => "Hey scent!"
266
+ "Hey! scent!".remove_char_at! -1 # => "Hey! scent"
267
+ "Hey! scent!".remove_char_at! -6 #=> "Hey! cent!"
268
+ "Hey! scent!".remove_char_at! 100 # => "Hey! scent!"
269
+ "Hey! scent!".remove_char_at! -100 # => "Hey! scent!"
270
+ ```
271
+
272
+ ##### remove `search`
273
+ Removes all instance of the word
274
+
275
+ Does not mutate the original string
276
+
277
+ ```ruby
278
+ "a=>b=>c".remove "=>" # => "abc"
279
+ "a,b,c".remove "," # => "abc"
280
+ "a,b,c".remove "a" # => ",b,c"
281
+ ```
282
+
283
+ ##### remove! `search`
284
+ Removes all instance of the word
285
+
286
+ Mutates the original string
287
+
288
+ ```ruby
289
+ "a=>b=>c".remove! "=>" # => "abc"
290
+ "a,b,c".remove! "," # => "abc"
291
+ "a,b,c".remove! "a" # => ",b,c"
292
+ ```
293
+
294
+ ##### without `unwanted`
295
+
296
+ Remove any instance of the words in the array in order
297
+
298
+ Does not mutate the original string
299
+ ```ruby
300
+ "A=>B->C".without ["=>","->"] # => "ABC"
301
+ ```
302
+
303
+ ##### without! `unwanted`
304
+
305
+ Remove any instance of the words in the array in order
306
+
307
+ Mutates the original string
308
+ ```ruby
309
+ "A=>B->C".without ["=>","->"] # => "ABC"
310
+ ```
311
+
312
+ ##### repeat `x`
313
+ Repeats the string x number of times.
314
+ Input is absolute-d.
315
+
316
+ Does not mutates the original string
317
+ ```ruby
318
+ "Hello ".repeat 3 # => "Hello Hello Hello "
319
+ "Hello ".repeat -3 # => "Hello Hello Hello "
320
+ ```
321
+
322
+
323
+ ##### repeat! `x`
324
+ Repeats the string x number of times.
325
+ Input is absolute-d.
326
+
327
+ Mutates the original string
328
+ ```ruby
329
+ "Hello ".repeat! 3 # => "Hello Hello Hello "
330
+ "Hello ".repeat! -3 # => "Hello Hello Hello "
331
+ ```
332
+
333
+ ##### * `x`
334
+ Repeats the string x number of times.
335
+ Input is absolute-d.
336
+
337
+ Does not mutates the original string
338
+ ```ruby
339
+ "Hello " * 3 # => "Hello Hello Hello "
340
+ "Hello" * -3 # => "Hello Hello Hello "
341
+ ```
342
+
343
+ ##### count_occurrences `search`
344
+ Counts the number of times a string appears
345
+
346
+ ```ruby
347
+ "Hello".count_occurrences "l" # => 2
348
+ "one day one night".count_occurrences "one" # => 2
349
+ "one day one night".count_occurrences "day" # => 1
350
+ "one day one night".count_occurrences "morning" # => 0
351
+ "one day one night one".count_occurrences "one" # => 3
352
+ ```
353
+
354
+ ---
355
+
25
356
 
26
357
  ## Development
27
358
 
28
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
359
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
360
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
361
+
362
+ To install this gem onto your local machine, run `bundle exec rake install`.
363
+
364
+ To release a new version, please wait for merge request to be accepted.
29
365
 
30
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
366
 
32
367
  ## Contributing
33
368
 
@@ -39,9 +374,9 @@ code of conduct.
39
374
 
40
375
  ## License
41
376
 
42
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
377
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
43
378
 
44
379
  ## Code of Conduct
45
380
  Everyone interacting in the KirinneeCore project’s codebases, issue trackers,
46
381
  chat rooms and mailing lists is expected to follow the
47
- [code of conduct](https://github.com/[USERNAME]/kirinnee_core/blob/master/CODE_OF_CONDUCT.md).
382
+ [code of conduct](CODE_OF_CONDUCT.md).
@@ -0,0 +1,161 @@
1
+ module Enumerable
2
+
3
+ # Filters base on predicate passed via block.
4
+ # Variant of "select, filter, find all" method in vanilla ruby
5
+ # Does not modify the original array or enumerable
6
+ #
7
+ # [1,2,3].where { |x| x%2!=0 } # => [1,3]
8
+ #
9
+ # @param [Block] f predicate
10
+ # @return [Enumerable]
11
+ def where(&f)
12
+ select &f
13
+ end
14
+
15
+ # Filters base on predicate passed via block.
16
+ # Variant of "select, filter, find all" method in vanilla ruby
17
+ # Modifies the original array or enumerable
18
+ #
19
+ # [1,2,3].where! { |x| x%2!=0 } # => [1,3]
20
+ #
21
+ # @param [Block] f predicate
22
+ # @return [Enumerable]
23
+ def where!(&f)
24
+ select! &f
25
+ end
26
+
27
+ # Skips the first x elements of the enumerable
28
+ # Does not mutate the original enumerable
29
+ #
30
+ # [1,2,3,4,5].skip 3 #=> [4,5]
31
+ # {:a=>1, :b=>2, :c=>3, :d=>4, :e=>5}.skip 3 #=> {:d=>4, :e=>5}
32
+ #
33
+ #
34
+ # @param [Integer] x the number of elements to skip
35
+ # @return [Enumerable]
36
+ def skip(x)
37
+ drop(x)
38
+ end
39
+
40
+ # Skips the elements of the enumerable while the predicate evaluates to true
41
+ # Does not mutate the original enumerable
42
+ #
43
+ # [1,2,3,2,1,0].skip_while {|x| x < 3} # => [3,2,1,0]
44
+ #
45
+ # @param [Block] p predicate to decide whether to skip
46
+ # @return [Enumerable]
47
+ def skip_while(&p)
48
+ drop_while(&p)
49
+ end
50
+
51
+ # Takes the last x element of the enumerable
52
+ # Does not mutate the original enumerable
53
+ #
54
+ # [1,2,3,4,5,6].back 3 #=> [4,5,6]
55
+ # [1,2,3,4,5,6].back 100 #=> [1,2,3,4,5,6]
56
+ # [1,2,3,4,5,6].back 0 #=> []
57
+ #
58
+ # @param [Integer] x the number of elements to take
59
+ # @return [Enumerable]
60
+ def back(x)
61
+ reverse.take(x).reverse
62
+ end
63
+
64
+ # Takes the last x element while predicate evaluates to true
65
+ # Does not mutate the original enumerable
66
+ #
67
+ # [1,2,3,2,1,0].back_while {|x| x <2} # => [1,0]
68
+ #
69
+ # @param [Block] p the predicate the to evaluate while considering to take the last element
70
+ # @return [Enumerable]
71
+ def back_while(&p)
72
+ reverse_each.take_while(&p).reverse
73
+ end
74
+
75
+ # Omits the last x element of the enumerable
76
+ # Does not mutate the original enumerable
77
+ #
78
+ # [1,2,3,4,5].omit 2 # => [1,2,3]
79
+ # [1,2,3,4,5].omit 100 #=> []
80
+ # [1,2,3,4,5].omit 0 # => [1,2,3,4,5]
81
+ #
82
+ # @param [Integer] x the number of elements to omit
83
+ # @return [Enumerable]
84
+ def omit(x)
85
+ reverse_each.drop(x).reverse
86
+ end
87
+
88
+ # Omits the last element while the predicate evaluates to true
89
+ # Does not mutate the original enumerable
90
+ #
91
+ # [1,2,3,2,1,0].omit_while { |x| x <3} #=> [1,2,3]
92
+ #
93
+ # @param [Block] p the predicate to evaluate to consider to remove the last element
94
+ # @return [Enumerable]
95
+ def omit_while(&p)
96
+ reverse_each.drop_while(&p).reverse
97
+ end
98
+
99
+ # Removes all occurrences of the element within the enumerable
100
+ # Does not mutate the original enumerable
101
+ #
102
+ # [1,2,3,1,2,3].remove 3 #=> [1,2,1,2]
103
+ # %w(apple pear apple pear).remove "apple" #=> %w(pear pear)
104
+ #
105
+ # @param [Object] search the target to remove from the array
106
+ # @return [Enumerable]
107
+ def remove(search)
108
+ where {|x| x != search}
109
+ end
110
+
111
+ # Whether the enumerable contains at least 1 element that matches the input
112
+ #
113
+ # [1,2,3,4,5,6,1].has? 1 # true
114
+ # [1,2,3,4,5,6,1].has? 2 # true
115
+ # [1,2,3,4,5,6,1].has? 7 # false
116
+ #
117
+ # @param [Object] search target
118
+ # @return [Boolean]
119
+ def has?(search)
120
+ count(search) > 0
121
+ end
122
+
123
+ # Remove all occurrences of each element in provided array from target array
124
+ # Does not mutate the original enumerable
125
+ #
126
+ # [1,2,3,4,1,2,3,4].without [2,4] #=> [1,3,1,3]
127
+ #
128
+ # @param [Array] w the elements to remove
129
+ # @return [Enumerable]
130
+ def without(w)
131
+ where {|x| !w.has?(x)}
132
+ end
133
+
134
+ # Remove all elements has index of the input array
135
+ # Does not mutate the original enumerable
136
+ #
137
+ # [6,5,4,3,2].without_index [0,2] #=> [5,3,2]
138
+ # {:a => 1, :b => 2, :c => 3}.without_index([0, 2]).to_h # => {:b=>2}
139
+ # @param [Array] index the elements to remove
140
+ # @return [Enumerable]
141
+ def without_index(index)
142
+ each_with_index.where {|_, i| !index.has?(i)}.map {|x, _| x}
143
+ end
144
+
145
+ # Gets the indexes that matches the predicate
146
+ # Does not mutate the original enumerable
147
+ #
148
+ # [6,5,4,3,2].indexes {|x| x < 4} # => [3,4]
149
+ #
150
+ # @param [Block] p the predicate to find indexes
151
+ # @return [Array<Integer>]
152
+ def indexes(&p)
153
+ ret = []
154
+ each_with_index do |x, i|
155
+ if p.call(x, i)
156
+ ret.append i
157
+ end
158
+ end
159
+ ret
160
+ end
161
+ end
@@ -0,0 +1,282 @@
1
+ class String
2
+
3
+
4
+ # Replaces the search string with the target string.
5
+ # Returns a copy, does not modify original string
6
+ #
7
+ # "a=>b=>c".replace_all("=>","-") # => "a-b-c"
8
+ #
9
+ # @param [string] search the string to search for (to be replaced)
10
+ # @param [string] target the string to replace with
11
+ # @return [string]
12
+ def replace_all (search, target)
13
+ r = self.split(search).join target
14
+ if self.end_with? search
15
+ r += target
16
+ end
17
+ r
18
+ end
19
+
20
+ # Replaces the search string with the target string.
21
+ # Modifies original string
22
+ #
23
+ # "a=>b=>c".replace_all!("=>","-") # => "a-b-c"
24
+ #
25
+ # @param [string] search the string to search for (to be replaced)
26
+ # @param [string] target the string to replace with
27
+ # @return [string]
28
+ def replace_all!(search, target)
29
+ replace(replace_all(search, target))
30
+ end
31
+
32
+ # Takes the first x characters of the string
33
+ # Does not modify original string
34
+ #
35
+ # "Singapore".take 4 # => "Sing"
36
+ # "Singapore".take 100 # => "Singapore"
37
+ # "Singapore".take 0 # => ""
38
+ #
39
+ # @param [Integer] x number of characters to take
40
+ # @return [String]
41
+ def take(x)
42
+ self[0...x]
43
+ end
44
+
45
+ # Takes the first x characters of the string
46
+ # Modifies original string
47
+ #
48
+ # "Singapore".take! 4 # => "Sing"
49
+ # "Singapore".take! 100 # => "Singapore"
50
+ # "Singapore".take! 0 # => ""
51
+ #
52
+ # @param [Integer] x number of characters to take
53
+ # @return [String]
54
+ def take!(x)
55
+ replace(self[0...x])
56
+ end
57
+
58
+ # Skips the first x characters of the string
59
+ # Does not modify the original string
60
+ #
61
+ # "Singapore".skip 5 # => "pore"
62
+ # "Singapore".skip 100 # => ""
63
+ # "Singapore".skip 0 # => "Singapore"
64
+ #
65
+ # @param [Integer] x number of character to skip
66
+ # @return [String]
67
+ def skip(x)
68
+ if x > self.length
69
+ return ""
70
+ end
71
+ self[x..-1]
72
+ end
73
+
74
+ # Skips the first x characters of the string
75
+ # Modifies the original string
76
+ #
77
+ # "Singapore".skip! 5 # => "pore"
78
+ # "Singapore".skip! 100 # => ""
79
+ # "Singapore".skip! 0 # => "Singapore"
80
+ #
81
+ # @param [Integer] x number of character to skip
82
+ # @return [String]
83
+ def skip!(x)
84
+ replace(skip x)
85
+ end
86
+
87
+ # Omits the last x characters of the string
88
+ # Does not modify the original string
89
+ #
90
+ # "Singapore".omit 5 # => "Sing"
91
+ # "Singapore".omit 100 # => ""
92
+ # "Singapore".omit 0 # => "Singapore"
93
+ #
94
+ # @param [Integer] x the number of characters to omit
95
+ # @return [String]
96
+ def omit(x)
97
+ self[0..-(x + 1)]
98
+ end
99
+
100
+ # Omits the last x characters of the string
101
+ # Modifies the original string
102
+ #
103
+ # "Singapore".omit 5 # => "Sing"
104
+ # "Singapore".omit 100 # => ""
105
+ # "Singapore".omit 0 # => "Singapore"
106
+ #
107
+ # @param [Integer] x the number of characters to omit
108
+ # @return [String]
109
+ def omit!(x)
110
+ replace(omit x)
111
+ end
112
+
113
+ # Takes the last x characters of the string
114
+ # Does not modify the original string
115
+ #
116
+ # "Singapore".back 4 #=> "pore"
117
+ # "Singapore".back 100 # =>"Singapore"
118
+ # "Singapore".back 0 # => ""
119
+ #
120
+ # @param [Integer] x the number of character to take
121
+ # @return [String]
122
+ def back(x)
123
+ if x > self.length
124
+ return self
125
+ end
126
+ self[self.length - x..-1]
127
+ end
128
+
129
+ # Takes the last x characters of the string
130
+ # Modify the original string
131
+ #
132
+ # "Singapore".back! 4 #=> "pore"
133
+ # "Singapore".back! 100 # =>"Singapore"
134
+ # "Singapore".back! 0 # => ""
135
+ #
136
+ # @param [Integer] x the number of character to take
137
+ # @return [String]
138
+ def back!(x)
139
+ replace(back x)
140
+ end
141
+
142
+ # Removes the character at the index
143
+ # Does not modify the original string
144
+ #
145
+ # Returns without removing characters if index or negative index
146
+ # exceeds length of string
147
+ #
148
+ # "Hey! scent!".remove_char_at 0 # => "ey! scent!"
149
+ # "Hey! scent!".remove_char_at 3 # => "Hey scent!"
150
+ # "Hey! scent!".remove_char_at -1 # => "Hey! scent"
151
+ # "Hey! scent!".remove_char_at -6 #=> "Hey! cent!"
152
+ # "Hey! scent!".remove_char_at 100 # => "Hey! scent!"
153
+ # "Hey! scent!".remove_char_at -100 # => "Hey! scent!"
154
+ #
155
+ # @param [Integer] x the position to remove. Negative index counts from back
156
+ # @param [String]
157
+ def remove_char_at(x)
158
+ i = x + (x < 0 ? length : 0)
159
+ i < 0 ? self : take(i) + skip(i + 1)
160
+ end
161
+
162
+ # Removes the character at the index
163
+ # Modifies the original string
164
+ #
165
+ # Returns without removing characters if index or negative index
166
+ # exceeds length of string
167
+ #
168
+ # "Hey! scent!".remove_char_at! 0 # => "ey! scent!"
169
+ # "Hey! scent!".remove_char_at! 3 # => "Hey scent!"
170
+ # "Hey! scent!".remove_char_at! -1 # => "Hey! scent"
171
+ # "Hey! scent!".remove_char_at! -6 #=> "Hey! cent!"
172
+ # "Hey! scent!".remove_char_at! 100 # => "Hey! scent!"
173
+ # "Hey! scent!".remove_char_at! -100 # => "Hey! scent!"
174
+ #
175
+ # @param [Integer] x the position to remove. Negative index counts from back
176
+ # @param [String]
177
+ def remove_char_at!(x)
178
+ replace remove_char_at x
179
+ end
180
+
181
+ # Removes all instance of the word
182
+ # Does not modify the original string
183
+ #
184
+ # "a=>b=>c".remove "=>" # => "abc"
185
+ # "a,b,c".remove "," # => "abc"
186
+ # "a,b,c".remove "a" # => ",b,c"
187
+ #
188
+ # @param [String] search string to remove
189
+ # @return [String]
190
+ def remove(search)
191
+ replace_all(search, "")
192
+ end
193
+
194
+ # Removes all instance of the word
195
+ # Modifies the original string
196
+ #
197
+ # "a=>b=>c".remove! "=>" # => "abc"
198
+ # "a,b,c".remove! "," # => "abc"
199
+ # "a,b,c".remove! "a" # => ",b,c"
200
+ #
201
+ # @param [String] search string to remove
202
+ # @return [String]
203
+ def remove!(search)
204
+ replace remove search
205
+ end
206
+
207
+ # Remove any instance of the words in the array in order
208
+ # Does not modify the original string
209
+ #
210
+ # "A=>B->C".without ["=>","->"] # => "ABC"
211
+ #
212
+ # @param [Array<String>] unwanted the strings to remove
213
+ # @return [String]
214
+ def without(unwanted)
215
+ unwanted.reduce(self, &:remove)
216
+ end
217
+
218
+ # Remove any instance of the words in the array in order
219
+ # Modifies the original string
220
+ #
221
+ # "A=>B->C".without! ["=>","->"] # => "ABC"
222
+ #
223
+ # @param [Array<String>] unwanted the strings to remove
224
+ # @return [String]
225
+ def without!(unwanted)
226
+ replace without unwanted
227
+ end
228
+
229
+ # Repeats the string x number of times
230
+ # Does not modify the original string
231
+ # Input is absolute-d
232
+ #
233
+ # "Hello ".repeat 3 # => "Hello Hello Hello "
234
+ # "Hello ".repeat -3 # => "Hello Hello Hello "
235
+ #
236
+ # @param [Integer] x number of times to repeat
237
+ # @return [String]
238
+ def repeat(x)
239
+ Array.new(x.abs, self).reduce("", &:+)
240
+ end
241
+
242
+ # Repeats the string x number of times
243
+ # Modifies the original string
244
+ # Input is absolute-d
245
+ #
246
+ # "Hello ".repeat! 3 # => "Hello Hello Hello "
247
+ # "Hello ".repeat! -3 # => "Hello Hello Hello "
248
+ #
249
+ # @param [Integer] x number of times to repeat
250
+ # @return [String]
251
+ def repeat!(x)
252
+ replace repeat(x)
253
+ end
254
+
255
+ # Repeats the string x number of times
256
+ # Does not modify the original string
257
+ # Input is absolute-d
258
+ #
259
+ # "Hello ".repeat 3 # => "Hello Hello Hello "
260
+ # "Hello ".repeat -3 # => "Hello Hello Hello "
261
+ #
262
+ # @param [Integer] x number of times to repeat
263
+ # @return [String]
264
+ def *(x)
265
+ repeat x
266
+ end
267
+
268
+ # Counts the number of times a string appears
269
+ #
270
+ # "Hello".count_occurrences "l" # => 2
271
+ # "one day one night".count_occurrences "one" # => 2
272
+ # "one day one night".count_occurrences "day" # => 1
273
+ # "one day one night".count_occurrences "morning" # => 0
274
+ # "one day one night one".count_occurrences "one" # => 3
275
+ #
276
+ # @param [String] search the string to count
277
+ # @return [Integer]
278
+ def count_occurrences(search)
279
+ (length - remove(search).length).abs / search.length
280
+ end
281
+
282
+ end
@@ -1,3 +1,3 @@
1
1
  module KirinneeCore
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/kirinnee_core.rb CHANGED
@@ -1,7 +1,4 @@
1
1
  require "kirinnee_core/version"
2
+ require "kirinnee_core/string"
3
+ require "kirinnee_core/enumerable"
2
4
 
3
- module KirinneeCore
4
- class Error < StandardError;
5
- end
6
- # Your code goes here...
7
- end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kirinnee_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kirinnee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-01 00:00:00.000000000 Z
11
+ date: 2019-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,6 +77,8 @@ files:
77
77
  - bin/setup
78
78
  - kirinnee_core.gemspec
79
79
  - lib/kirinnee_core.rb
80
+ - lib/kirinnee_core/enumerable.rb
81
+ - lib/kirinnee_core/string.rb
80
82
  - lib/kirinnee_core/version.rb
81
83
  homepage: https://gitlab.com/ruby-gem/kirinnee-core
82
84
  licenses: