Class: Dependabot::NpmAndYarn::PackageName

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/npm_and_yarn/package_name.rb

Defined Under Namespace

Classes: InvalidPackageName

Constant Summary collapse

PACKAGE_NAME_REGEX =

NPM package naming rules are defined by the following projects:

%r{
    \A                                          # beginning of string
    (?=.{1,214}\z)                              # enforce length (1 - 214)
    (@(?<scope>                                 # capture 'scope' if present
      (?=[^\.])                                 # reject leading dot
      [a-z0-9\-\_\.\!\~\*\'\(\)]+               # URL-safe characters
    )\/)?
    (?<name>                                    # capture package name
      (?=[^\.\_])                               # reject leading dot or underscore
      [a-z0-9\-\_\.\!\~\*\'\(\)]+               # URL-safe characters
    )
    \z                                          # end of string
}xi.freeze
TYPES_PACKAGE_NAME_REGEX =

multi-line/case-insensitive

%r{
    \A                                          # beginning of string
    @types\/                                    # starts with @types/
    ((?<scope>.+)__)?                           # capture scope
    (?<name>.+)                                 # capture name
    \z                                          # end of string
}xi.freeze

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ PackageName

Returns a new instance of PackageName.

Raises:



33
34
35
36
37
38
39
# File 'lib/dependabot/npm_and_yarn/package_name.rb', line 33

def initialize(string)
  match = PACKAGE_NAME_REGEX.match(string.to_s)
  raise InvalidPackageName unless match

  @scope = match[:scope]
  @name  = match[:name]
end

Instance Method Details

#<=>(other) ⇒ Object



57
58
59
# File 'lib/dependabot/npm_and_yarn/package_name.rb', line 57

def <=>(other)
  to_s.casecmp(other.to_s)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/dependabot/npm_and_yarn/package_name.rb', line 49

def eql?(other)
  self.class == other.class && to_s == other.to_s
end

#hashObject



53
54
55
# File 'lib/dependabot/npm_and_yarn/package_name.rb', line 53

def hash
  to_s.downcase.hash
end

#library_nameObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dependabot/npm_and_yarn/package_name.rb', line 61

def library_name
  return unless types_package?

  @library_name ||=
    begin
      match = TYPES_PACKAGE_NAME_REGEX.match(to_s)
      if match[:scope]
        self.class.new("@#{match[:scope]}/#{match[:name]}")
      else
        self.class.new(match[:name].to_s)
      end
    end
end

#to_sObject



41
42
43
44
45
46
47
# File 'lib/dependabot/npm_and_yarn/package_name.rb', line 41

def to_s
  if scoped?
    "@#{@scope}/#{@name}"
  else
    @name.to_s
  end
end

#types_package_nameObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/dependabot/npm_and_yarn/package_name.rb', line 75

def types_package_name
  return if types_package?

  @types_package_name ||=
    if scoped?
      self.class.new("@types/#{@scope}__#{@name}")
    else
      self.class.new("@types/#{@name}")
    end
end