Sha256: b39c12e698d25d4377d23ce717ef1564fb4bb13a2b3d161ba50d613050a26505

Contents?: true

Size: 892 Bytes

Versions: 1

Compression:

Stored size: 892 Bytes

Contents

# frozen_string_literal: true

require "core"

module Git
  module Lint
    module Validators
      # Validates content has no repeated words.
      class RepeatedWord
        PATTERN = /
          \w+(?=\s)         # Match word with trailing space.
          |                 # Or.
          (?<=\s)\w+(?=\s)  # Match word between two spaces.
          |                 # Or.
          (?<=\s)\w+        # Match word with leading space.
        /x

        def initialize pattern: PATTERN
          @pattern = pattern
        end

        def call(content) = content ? scan(content) : Core::EMPTY_ARRAY

        private

        attr_reader :pattern

        def scan content
          content.scan(pattern).each_cons(2).with_object [] do |(current, future), repeats|
            repeats.append future if current.casecmp(future).zero?
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git-lint-7.0.0 lib/git/lint/validators/repeated_word.rb