lib/branch/name/normalizable.rb in branch-name-3.0.0 vs lib/branch/name/normalizable.rb in branch-name-3.2.1
- old
+ new
@@ -3,23 +3,38 @@
require_relative 'option_error'
module Branch
module Name
module Normalizable
- NON_WORD_CHARS_REGEX = /[\W_]/
+ # The regex used to split ticket and ticket description tokens
+ # to formulate a source control branch name.
+ BRANCH_NAME_REGEX = %r{[^/\w\x20]}
+ # The regex used to split ticket and ticket description tokens
+ # to formulate a project folder based on the branch name formulated.
+ PROJECT_FOLDER_REGEX = /[\W_]/
+
def normalize_branch_name(ticket_description, ticket)
formatted_branch_name = format_string_or_default
formatted_branch_name = formatted_branch_name.gsub('%t', ticket || '')
formatted_branch_name = formatted_branch_name.gsub('%d', ticket_description)
- formatted_branch_name = normalize_token formatted_branch_name
+ formatted_branch_name = formatted_branch_name.gsub('%u', Etc.getlogin)
+ normalize_token formatted_branch_name, BRANCH_NAME_REGEX
rescue Branch::Name::OptionError => e
raise unless block_given?
yield e
end
+ def project_folder_from(normalized_branch_name)
+ normalize_token normalized_branch_name, PROJECT_FOLDER_REGEX
+ rescue Branch::Name::OptionError => e
+ raise unless block_given?
+
+ yield e
+ end
+
private
def format_string_or_default
format_string = options[:format_string].presence ||
Configurable::DEFAULT_BRANCH_NAME_OPTIONS['create']['format_string']
@@ -32,23 +47,11 @@
end
format_string
end
- def normalize_ticket_description(ticket_description)
- normalize_token ticket_description
- end
-
- def normalize_ticket(ticket)
- return if ticket.blank?
-
- ticket.split(NON_WORD_CHARS_REGEX).filter_map do |token|
- normalize_token(token)
- end.join(options[:separator])
- end
-
- def normalize_token(token)
- token = token.gsub(NON_WORD_CHARS_REGEX, ' ')
+ def normalize_token(token, regex)
+ token = token.gsub(regex, ' ')
token = token.strip
.squeeze(' ')
.split.join(options[:separator])
token = token.squeeze(options[:separator])
token = token.downcase if options[:downcase]