Sha256: b697d5ba010512a1ccb7e15f935ba4af19bae1fdaf30939e08b861db1aecfa8c

Contents?: true

Size: 1004 Bytes

Versions: 26

Compression:

Stored size: 1004 Bytes

Contents

# Cleans up a filename to ensure it will work on filesystem.
def File.sanitize(filename)
  filename = File.basename(filename.gsub("\\", "/")) # work-around for IE
  filename.gsub!(/[^a-zA-Z0-9\.\-\+_]/,"_")
  filename = "_#{filename}" if filename =~ /^\.+$/
  filename
end


#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#

=begin test

  require 'test/unit'

  class TCFile < Test::Unit::TestCase

    def test_01_001
      assert_equal( "This_is_a_test", File.sanitize('This is a test') )
    end

    def test_01_001
      assert_equal( "test", File.sanitize('This\is\test') )
    end

    def test_01_001
      assert_equal( "test", File.sanitize('This/is/test') )
    end

    def test_01_001
      assert_equal( "te_____st", File.sanitize('This/te#$#@!st') )
    end

    def test_01_001
      assert_equal( "_.", File.sanitize('.') )
    end

    def test_01_001
      assert_equal( "_....", File.sanitize('....') )
    end

  end

=end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
facets-1.0.0 lib/facet/file/self/sanitize.rb
facets-0.9.0 lib/nano/file/self/sanitize.rb
facets-1.0.3 packages/core/lib/facet/file/self/sanitize.rb
facets-1.3.0 lib/facets/core/file/self/sanitize.rb
facets-1.1.0 lib/facet/file/self/sanitize.rb
facets-1.2.0 lib/facets/core/file/self/sanitize.rb
facets-1.2.1 lib/facets/core/file/self/sanitize.rb
facets-1.3.2 lib/facets/core/file/self/sanitize.rb
facets-1.3.1 lib/facets/core/file/self/sanitize.rb
facets-1.3.3 lib/facets/core/file/self/sanitize.rb
facets-1.4.0 lib/facets/core/file/self/sanitize.rb
facets-1.4.2 lib/facets/core/file/self/sanitize.rb
facets-1.4.1 lib/facets/core/file/self/sanitize.rb
facets-1.4.3 lib/facets/core/file/self/sanitize.rb
facets-1.4.5 lib/facets/core/file/self/sanitize.rb
facets-1.4.4 lib/facets/core/file/self/sanitize.rb
facets-1.7.30 lib/facets/core/file/self/sanitize.rb
facets-1.7.38 lib/facets/core/file/self/sanitize.rb
facets-1.7.0 lib/facets/core/file/self/sanitize.rb
facets-1.7.46 lib/facets/core/file/self/sanitize.rb