filename_cleaner 0.1.1 → 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
  SHA1:
3
- metadata.gz: ba0764b038df0404b93814f17a7309ada921ccf4
4
- data.tar.gz: 1c6f72706886d64a1d895d221049c4da92b78cb6
3
+ metadata.gz: 8e4c0aad87875fbeee0976b1682cb3b2f30e0c3c
4
+ data.tar.gz: 8af35d698b9762048de68a92aa8af73d3e21a85e
5
5
  SHA512:
6
- metadata.gz: 0ebc349f1f3a7bf4920086b78a588b65c48a9a1f8824454ebaad7f067ec343b25f4afd6411c8d9081a22d5ae6fcf2591d30fdc03bc0e410baaded9f9c3ae543a
7
- data.tar.gz: 208964761eb2829daa8be48bcbe1d4d10dd17b5ed8bd7cac10363aac316b302d2f88ebc5709e44a9b520e570168bad863cd00cf05ae0c57ae481b1d97bd2067f
6
+ metadata.gz: 894666704a1dfe9bdfbe320ac027cc6873ef65fa5e81bb13380f2894df2084027895b623fdf69bc34b7fa1d30e4e80281714a2e2eac541e08ff29872cadaebdc
7
+ data.tar.gz: 4ff5423c8deb7ce03ed0ff9728d812155e3be8e80aef7d6b27454c6e0cd3294e4a7ec095c30fcdc532bc20fc3ac4d0e55c559b624c34a4d32e5453faac70c5f1
data/CHANGELOGS.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ### Changelogs
2
2
 
3
+ #### 0.2.0
4
+
5
+ - Cleanup the APIs
6
+ * rename `sanitize_filename` to `sanitize_name_with_extension`
7
+ * add `sanitize_name`
8
+ - Don't check for extension if we are dealing with a directory name
9
+ - Add core_ext/object/blank.rb
10
+
3
11
  #### 0.1.1
4
12
 
5
13
  - Remove the last char from the filename if it is not numbers or letters
data/README.md CHANGED
@@ -79,18 +79,13 @@ Example Usage:
79
79
 
80
80
  ```ruby
81
81
  require 'filename_cleaner'
82
- clean_name = FilenameCleaner::sanitize_filename('some b@d fil$name.txt')
83
- puts clean_name # => 'some.b.d.fil.name.txt'
84
-
85
- ```
86
-
87
- - Specify the separator string
88
-
89
- ```ruby
90
- require 'filename_cleaner'
91
- clean_name = FilenameCleaner::sanitize_filename('some b@d fil$name.txt', '_')
82
+ # work with the file that have extension
83
+ clean_name = FilenameCleaner::sanitize_name_with_extension('some b@d fil$name.txt', '_')
92
84
  puts clean_name # => 'some_b_d_fil_name.txt'
93
85
 
86
+ # or to work with the file without extension
87
+ clean_name = FilenameCleaner::sanitize_name('some b@d fil$name.txt', '_')
88
+ puts clean_name # => 'some_b_d_fil_name_txt'
94
89
  ```
95
90
 
96
91
  ### Contributing
@@ -2,3 +2,4 @@ require 'uri'
2
2
  require_relative './filename_cleaner/version'
3
3
  require_relative './filename_cleaner/cli'
4
4
  require_relative './filename_cleaner/filename_cleaner'
5
+ require_relative './filename_cleaner/core_ext/object/blank'
@@ -72,7 +72,7 @@ Options:
72
72
  puts "FYI: process : #{index + 1} of #{files.size}"
73
73
  dirname = File.dirname(File.expand_path(file))
74
74
  filename = File.basename(file)
75
- sanitized_name = FilenameCleaner::sanitize_filename(filename, options[:sep_char])
75
+ sanitized_name = FilenameCleaner::sanitize_name_with_extension(filename, options[:sep_char])
76
76
  old_name = File.expand_path(file)
77
77
  new_name = File.expand_path([dirname, sanitized_name].join(File::SEPARATOR))
78
78
 
@@ -0,0 +1,105 @@
1
+ # encoding: utf-8
2
+ # from: active_support/core_ext/object/blank.rb
3
+ class Object
4
+ # An object is blank if it's false, empty, or a whitespace string.
5
+ # For example, '', ' ', +nil+, [], and {} are all blank.
6
+ #
7
+ # This simplifies:
8
+ #
9
+ # if address.nil? || address.empty?
10
+ #
11
+ # ...to:
12
+ #
13
+ # if address.blank?
14
+ def blank?
15
+ respond_to?(:empty?) ? empty? : !self
16
+ end
17
+
18
+ # An object is present if it's not <tt>blank?</tt>.
19
+ def present?
20
+ !blank?
21
+ end
22
+
23
+ # Returns object if it's <tt>present?</tt> otherwise returns +nil+.
24
+ # <tt>object.presence</tt> is equivalent to <tt>object.present? ? object : nil</tt>.
25
+ #
26
+ # This is handy for any representation of objects where blank is the same
27
+ # as not present at all. For example, this simplifies a common check for
28
+ # HTTP POST/query parameters:
29
+ #
30
+ # state = params[:state] if params[:state].present?
31
+ # country = params[:country] if params[:country].present?
32
+ # region = state || country || 'US'
33
+ #
34
+ # ...becomes:
35
+ #
36
+ # region = params[:state].presence || params[:country].presence || 'US'
37
+ def presence
38
+ self if present?
39
+ end
40
+ end
41
+
42
+ class NilClass
43
+ # +nil+ is blank:
44
+ #
45
+ # nil.blank? # => true
46
+ def blank?
47
+ true
48
+ end
49
+ end
50
+
51
+ class FalseClass
52
+ # +false+ is blank:
53
+ #
54
+ # false.blank? # => true
55
+ def blank?
56
+ true
57
+ end
58
+ end
59
+
60
+ class TrueClass
61
+ # +true+ is not blank:
62
+ #
63
+ # true.blank? # => false
64
+ def blank?
65
+ false
66
+ end
67
+ end
68
+
69
+ class Array
70
+ # An array is blank if it's empty:
71
+ #
72
+ # [].blank? # => true
73
+ # [1,2,3].blank? # => false
74
+ alias_method :blank?, :empty?
75
+ end
76
+
77
+ class Hash
78
+ # A hash is blank if it's empty:
79
+ #
80
+ # {}.blank? # => true
81
+ # { key: 'value' }.blank? # => false
82
+ alias_method :blank?, :empty?
83
+ end
84
+
85
+ class String
86
+ # A string is blank if it's empty or contains whitespaces only:
87
+ #
88
+ # ''.blank? # => true
89
+ # ' '.blank? # => true
90
+ # ' '.blank? # => true
91
+ # ' something here '.blank? # => false
92
+ def blank?
93
+ self !~ /[^[:space:]]/
94
+ end
95
+ end
96
+
97
+ class Numeric #:nodoc:
98
+ # No number is blank:
99
+ #
100
+ # 1.blank? # => false
101
+ # 0.blank? # => false
102
+ def blank?
103
+ false
104
+ end
105
+ end
@@ -1,47 +1,48 @@
1
1
  module FilenameCleaner
2
2
  DOT = '.'
3
3
  class << self
4
+ # Sanitize the name without any extension
5
+ def sanitize_name(name, sep_char = '.')
6
+ replace_dot!(sanitize_with_dot(name), sep_char)
7
+ end
8
+
4
9
  # Sanitize filename that have the extension
5
10
  #
6
11
  # @param [String] filename the input filename with extension
7
- # @retyrn [String] the output file with special characters replaced.
8
- def sanitize_filename(filename, sep_char = '.')
12
+ # @return [String] the output file with special characters replaced.
13
+ def sanitize_name_with_extension(filename, sep_char = '.')
9
14
  extension = File.extname(filename)
10
- if extension.empty?
11
- replace_dot!(sanitize(filename), sep_char)
12
- else
13
- name_only = File.basename(filename, ".*")
14
- name_only = replace_dot!(sanitize(name_only), sep_char)
15
- "#{name_only}#{extension}"
16
- end
15
+ name_only = File.basename(filename, ".*")
16
+ name_only = replace_dot!(sanitize_with_dot(name_only), sep_char)
17
+ "#{name_only}#{extension}"
17
18
  end
18
19
 
19
- # Clean the the input string to remove the special characters
20
+ private
21
+
22
+ # Replace the multipe special characters with a dot
20
23
  #
21
- # @param [String] filename input file
22
- # @return [String] the new file name with special characters replaced or removed.
23
- def sanitize(filename)
24
- # remove anything that is not letters, numbers, dash, underscore or space
25
- # Note: we intentionally ignore dot from the list
26
- filename.gsub!(/[^0-9A-Za-z\-_ ]/, DOT)
27
-
28
- # replace multiple occurrences of a given char with a dot
24
+ # @param [String] name input file
25
+ # @return [String] the new name with special characters replaced or removed.
26
+ def sanitize_with_dot(name)
27
+ # Replace any special characters with a dot
28
+ name.gsub!(/[^0-9A-Za-z\-_ ]/, DOT)
29
+
30
+ # Replace multiple occurrences of a given character with a dot
29
31
  ['-', '_', ' '].each do |c|
30
- filename.gsub!(/#{Regexp.quote(c)}+/, DOT)
32
+ name.gsub!(/#{Regexp.quote(c)}+/, DOT)
31
33
  end
32
34
 
33
- # replace multiple occurrence of dot with one dot
34
- filename.gsub!(/#{Regexp.quote(DOT)}+/, DOT)
35
+ # Replace multiple occurrence of dot with one dot
36
+ name.gsub!(/#{Regexp.quote(DOT)}+/, DOT)
35
37
 
36
- # remove the last char if it is a dot
37
- filename.gsub!(/\.$/, '') if filename[-1] == DOT
38
+ # Remove the last char if it is a dot
39
+ name.gsub!(/\.$/, '') if name[-1] == DOT
38
40
 
39
- filename
41
+ # return the result
42
+ name
40
43
  end
41
44
 
42
- private
43
-
44
- # replace 'dot' string with a agiven string if any
45
+ # replace 'dot' string with a given string if specified
45
46
  def replace_dot!(string, replace = nil)
46
47
  string.gsub!(/#{Regexp.quote(DOT)}+/, replace) if replace
47
48
  string
@@ -1,3 +1,3 @@
1
1
  module FilenameCleaner
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -1,58 +1,58 @@
1
1
  require_relative '../../test_helper'
2
2
  describe FilenameCleaner do
3
- context '#sanitize' do
4
- it 'works with simple input' do
5
- FilenameCleaner.sanitize('any txt').must_equal 'any.txt'
6
- FilenameCleaner.sanitize('this is a long filename.txt').must_equal 'this.is.a.long.filename.txt'
7
- end
8
- it 'works with text with extension' do
9
- FilenameCleaner.sanitize('filename.txt').must_equal 'filename.txt'
10
- end
11
- it 'strips the end of string if not letters or numbers' do
12
- FilenameCleaner.sanitize('filename .txt').must_equal 'filename.txt'
13
- FilenameCleaner.sanitize('filename .txt').must_equal 'filename.txt'
14
- FilenameCleaner.sanitize('filename !.txt').must_equal 'filename.txt'
15
- end
16
- end
17
- context '#sanitize_filename' do
3
+ # context '#sanitize_with_dot' do
4
+ # it 'works with simple input' do
5
+ # FilenameCleaner.sanitize_with_dot('any txt').must_equal 'any.txt'
6
+ # FilenameCleaner.sanitize_with_dot('this is a long filename.txt').must_equal 'this.is.a.long.filename.txt'
7
+ # end
8
+ # it 'works with text with extension' do
9
+ # FilenameCleaner.sanitize_with_dot('filename.txt').must_equal 'filename.txt'
10
+ # end
11
+ # it 'strips the end of string if not letters or numbers' do
12
+ # FilenameCleaner.sanitize_with_dot('filename .txt').must_equal 'filename.txt'
13
+ # FilenameCleaner.sanitize_with_dot('filename .txt').must_equal 'filename.txt'
14
+ # FilenameCleaner.sanitize_with_dot('filename !.txt').must_equal 'filename.txt'
15
+ # end
16
+ # end
17
+ context '#sanitize_name_with_extension' do
18
18
  describe 'file with extension' do
19
19
  it 'replaces mutilple consecutive chars with one' do
20
- FilenameCleaner.sanitize_filename('some!!!$file$:%.txt').must_equal 'some.file.txt'
20
+ FilenameCleaner.sanitize_name_with_extension('some!!!$file$:%.txt').must_equal 'some.file.txt'
21
21
  end
22
22
  it 'works with default separator' do
23
- FilenameCleaner.sanitize_filename('some file.txt').must_equal 'some.file.txt'
23
+ FilenameCleaner.sanitize_name_with_extension('some file.txt').must_equal 'some.file.txt'
24
24
  end
25
25
  it 'works with non-default separator' do
26
- FilenameCleaner.sanitize_filename('some file.txt', '_').must_equal 'some_file.txt'
26
+ FilenameCleaner.sanitize_name_with_extension('some file.txt', '_').must_equal 'some_file.txt'
27
27
  end
28
28
  end
29
29
  describe 'file without extension' do
30
30
  it 'replaces mutilple consecutive chars with one' do
31
- FilenameCleaner.sanitize_filename('some!!!$file$:%.').must_equal 'some.file'
31
+ FilenameCleaner.sanitize_name_with_extension('some!!!$file$:%.').must_equal 'some.file'
32
32
  end
33
33
  context 'using default separator' do
34
34
  it 'works with simple input' do
35
- FilenameCleaner.sanitize_filename('Gemfile').must_equal 'Gemfile'
35
+ FilenameCleaner.sanitize_name_with_extension('Gemfile').must_equal 'Gemfile'
36
36
  end
37
37
  it 'works with complex input' do
38
- FilenameCleaner.sanitize_filename('File$without!extension').must_equal 'File.without.extension'
38
+ FilenameCleaner.sanitize_name_with_extension('File$without!extension').must_equal 'File.without.extension'
39
39
  end
40
40
  end
41
41
  context 'with non-default separator char' do
42
42
  it 'works with simple input' do
43
- FilenameCleaner.sanitize_filename('Gemfile', '_').must_equal 'Gemfile'
43
+ FilenameCleaner.sanitize_name_with_extension('Gemfile', '_').must_equal 'Gemfile'
44
44
  end
45
45
  it 'works with complex input' do
46
- FilenameCleaner.sanitize_filename('File$without!extension', '-').must_equal 'File-without-extension'
46
+ FilenameCleaner.sanitize_name_with_extension('File$without!extension', '-').must_equal 'File-without-extension'
47
47
  end
48
48
  end
49
49
  context 'end of the filename' do
50
50
  it 'strips the end of string if not letters or numbers' do
51
- FilenameCleaner.sanitize_filename('filename .txt').must_equal 'filename.txt'
52
- FilenameCleaner.sanitize_filename('filename .txt').must_equal 'filename.txt'
53
- FilenameCleaner.sanitize_filename('filename !.txt').must_equal 'filename.txt'
51
+ FilenameCleaner.sanitize_name_with_extension('filename .txt').must_equal 'filename.txt'
52
+ FilenameCleaner.sanitize_name_with_extension('filename .txt').must_equal 'filename.txt'
53
+ FilenameCleaner.sanitize_name_with_extension('filename !.txt').must_equal 'filename.txt'
54
54
  end
55
55
  end
56
- end
56
+ end
57
57
  end
58
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filename_cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Burin Choomnuan
@@ -215,6 +215,7 @@ files:
215
215
  - filename_cleaner.gemspec
216
216
  - lib/filename_cleaner.rb
217
217
  - lib/filename_cleaner/cli.rb
218
+ - lib/filename_cleaner/core_ext/object/blank.rb
218
219
  - lib/filename_cleaner/filename_cleaner.rb
219
220
  - lib/filename_cleaner/version.rb
220
221
  - rubocop-todo.yml