Sha256: de35af64cd79a091cb421241f9b5203c253ef2a38c5d48fab3edcfc2e4fc0141

Contents?: true

Size: 1.23 KB

Versions: 26

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Language
    module BlockString
      # Remove leading and trailing whitespace from a block string.
      # See "Block Strings" in https://github.com/facebook/graphql/blob/master/spec/Section%202%20--%20Language.md
      def self.trim_whitespace(str)
        lines = str.split("\n")
        common_indent = nil

        # find the common whitespace
        lines.each_with_index do |line, idx|
          if idx == 0
            next
          end
          line_length = line.size
          line_indent = line[/\A */].size
          if line_indent < line_length && (common_indent.nil? || line_indent < common_indent)
            common_indent = line_indent
          end
        end

        # Remove the common whitespace
        if common_indent
          lines.each_with_index do |line, idx|
            if idx == 0
              next
            else
              line[0, common_indent] = ""
            end
          end
        end

        # Remove leading & trailing blank lines
        while lines.first.empty?
          lines.shift
        end
        while lines.last.empty?
          lines.pop
        end

        # Rebuild the string
        lines.join("\n")
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
graphql-1.9.0.pre1 lib/graphql/language/block_string.rb
graphql-1.8.11 lib/graphql/language/block_string.rb
graphql-1.8.10 lib/graphql/language/block_string.rb
graphql-1.8.9 lib/graphql/language/block_string.rb
graphql-1.8.8 lib/graphql/language/block_string.rb
graphql-1.8.7 lib/graphql/language/block_string.rb
graphql-1.8.6 lib/graphql/language/block_string.rb
graphql-1.8.5 lib/graphql/language/block_string.rb
graphql-1.8.4 lib/graphql/language/block_string.rb
graphql-1.8.3 lib/graphql/language/block_string.rb
graphql-1.8.2 lib/graphql/language/block_string.rb
graphql-1.8.1 lib/graphql/language/block_string.rb
graphql-1.8.0 lib/graphql/language/block_string.rb
graphql-1.8.0.pre11 lib/graphql/language/block_string.rb
graphql-1.8.0.pre10 lib/graphql/language/block_string.rb
graphql-1.7.14 lib/graphql/language/block_string.rb
graphql-1.8.0.pre9 lib/graphql/language/block_string.rb
graphql-1.8.0.pre8 lib/graphql/language/block_string.rb
graphql-1.7.13 lib/graphql/language/block_string.rb
graphql-1.8.0.pre7 lib/graphql/language/block_string.rb