Sha256: 562b4c5cc9f35d13ecb362bfe4f8a31cb4344ab8a072d656d4bd651adbc8b8f8

Contents?: true

Size: 1.66 KB

Versions: 28

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

# Strips leading spaces from a String
#
# This function is compatible with the stdlib function with the same name.
#
# The function does the following:
# * For a `String` the conversion removes all leading ASCII white space characters such as space, tab, newline, and return.
#   It does not remove other space-like characters like hard space (Unicode U+00A0). (Tip, `/^[[:space:]]/` regular expression
#   matches all space-like characters).
# * For an `Iterable[Variant[String, Numeric]]` (for example an `Array`) each value is processed and the conversion is not recursive.
# * If the value is `Numeric` it is simply returned (this is for backwards compatibility).
# * An error is raised for all other data types.
#
# @example Removing leading space from a String
# ```puppet
# "\n\thello ".lstrip()
# lstrip("\n\thello ")
# ```
# Would both result in `"hello"`
#
# @example Removing leading space from strings in an Array
# ```puppet
# ["\n\thello ", "\n\thi "].lstrip()
# lstrip(["\n\thello ", "\n\thi "])
# ```
# Would both result in `['hello', 'hi']`
#
Puppet::Functions.create_function(:lstrip) do
  dispatch :on_numeric do
    param 'Numeric', :arg
  end

  dispatch :on_string do
    param 'String', :arg
  end

  dispatch :on_iterable do
    param 'Iterable[Variant[String, Numeric]]', :arg
  end

  # unit function - since the old implementation skipped Numeric values
  def on_numeric(n)
    n
  end

  def on_string(s)
    s.lstrip
  end

  def on_iterable(a)
    a.map { |x| do_lstrip(x) }
  end

  def do_lstrip(x)
    # x can only be a String or Numeric because type constraints have been automatically applied
    x.is_a?(String) ? x.lstrip : x
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
puppet-8.10.0 lib/puppet/functions/lstrip.rb
puppet-8.10.0-x86-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.10.0-x64-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.10.0-universal-darwin lib/puppet/functions/lstrip.rb
puppet-8.9.0 lib/puppet/functions/lstrip.rb
puppet-8.9.0-x86-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.9.0-x64-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.9.0-universal-darwin lib/puppet/functions/lstrip.rb
puppet-8.8.1 lib/puppet/functions/lstrip.rb
puppet-8.8.1-x86-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.8.1-x64-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.8.1-universal-darwin lib/puppet/functions/lstrip.rb
puppet-8.7.0 lib/puppet/functions/lstrip.rb
puppet-8.7.0-x86-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.7.0-x64-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.7.0-universal-darwin lib/puppet/functions/lstrip.rb
puppet-8.6.0 lib/puppet/functions/lstrip.rb
puppet-8.6.0-x86-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.6.0-x64-mingw32 lib/puppet/functions/lstrip.rb
puppet-8.6.0-universal-darwin lib/puppet/functions/lstrip.rb