handlebars-helpers 0.0.5 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +3 -3
  3. data/.gitignore +1 -0
  4. data/.handlebars_helpers.json +901 -0
  5. data/.handlebars_string_formatters.json +137 -0
  6. data/.rubocop.yml +4 -2
  7. data/.vscode/settings.json +6 -0
  8. data/Gemfile +3 -0
  9. data/Rakefile +1 -0
  10. data/STORIES.md +44 -6
  11. data/handlebars-helpers.gemspec +2 -0
  12. data/lib/handlebars/helpers.rb +2 -0
  13. data/lib/handlebars/helpers/base_helper.rb +36 -0
  14. data/lib/handlebars/helpers/base_safe_string_helper.rb +17 -0
  15. data/lib/handlebars/helpers/code_ruby/classify.rb +40 -0
  16. data/lib/handlebars/helpers/code_ruby/deconstantize.rb +48 -0
  17. data/lib/handlebars/helpers/code_ruby/demodulize.rb +58 -0
  18. data/lib/handlebars/helpers/code_ruby/foreign_key.rb +49 -0
  19. data/lib/handlebars/helpers/code_ruby/tableize.rb +35 -0
  20. data/lib/handlebars/helpers/comparison/and.rb +44 -0
  21. data/lib/handlebars/helpers/comparison/default.rb +66 -0
  22. data/lib/handlebars/helpers/comparison/eq.rb +35 -0
  23. data/lib/handlebars/helpers/comparison/gt.rb +35 -0
  24. data/lib/handlebars/helpers/comparison/gte.rb +43 -0
  25. data/lib/handlebars/helpers/comparison/lt.rb +35 -0
  26. data/lib/handlebars/helpers/comparison/lte.rb +43 -0
  27. data/lib/handlebars/helpers/comparison/ne.rb +35 -0
  28. data/lib/handlebars/helpers/comparison/or.rb +56 -0
  29. data/lib/handlebars/helpers/configuration.rb +72 -0
  30. data/lib/handlebars/helpers/inflection/ordinal.rb +58 -0
  31. data/lib/handlebars/helpers/inflection/ordinalize.rb +59 -0
  32. data/lib/handlebars/helpers/inflection/pluralize.rb +36 -0
  33. data/lib/handlebars/helpers/inflection/pluralize_by_number.rb +60 -0
  34. data/lib/handlebars/helpers/inflection/singularize.rb +35 -0
  35. data/lib/handlebars/helpers/register_helpers.rb +73 -0
  36. data/lib/handlebars/helpers/string_formatting/append_if.rb +42 -0
  37. data/lib/handlebars/helpers/string_formatting/back_slash.rb +33 -0
  38. data/lib/handlebars/helpers/string_formatting/camel.rb +29 -0
  39. data/lib/handlebars/helpers/string_formatting/constantize.rb +29 -0
  40. data/lib/handlebars/helpers/string_formatting/dasherize.rb +33 -0
  41. data/lib/handlebars/helpers/string_formatting/dotirize.rb +33 -0
  42. data/lib/handlebars/helpers/string_formatting/double_colon.rb +33 -0
  43. data/lib/handlebars/helpers/string_formatting/downcase.rb +31 -0
  44. data/lib/handlebars/helpers/string_formatting/format_as.rb +57 -0
  45. data/lib/handlebars/helpers/string_formatting/humanize.rb +39 -0
  46. data/lib/handlebars/helpers/string_formatting/lamel.rb +29 -0
  47. data/lib/handlebars/helpers/string_formatting/padl.rb +58 -0
  48. data/lib/handlebars/helpers/string_formatting/padr.rb +59 -0
  49. data/lib/handlebars/helpers/string_formatting/pluserize.rb +29 -0
  50. data/lib/handlebars/helpers/string_formatting/prepend_if.rb +42 -0
  51. data/lib/handlebars/helpers/string_formatting/singularize.rb +35 -0
  52. data/lib/handlebars/helpers/string_formatting/slash.rb +33 -0
  53. data/lib/handlebars/helpers/string_formatting/snake.rb +33 -0
  54. data/lib/handlebars/helpers/string_formatting/string.js +511 -0
  55. data/lib/handlebars/helpers/string_formatting/surround.rb +49 -0
  56. data/lib/handlebars/helpers/string_formatting/surround_if.rb +43 -0
  57. data/lib/handlebars/helpers/string_formatting/titleize.rb +39 -0
  58. data/lib/handlebars/helpers/string_formatting/upcase.rb +31 -0
  59. data/lib/handlebars/helpers/string_tokenizer.rb +43 -0
  60. data/lib/handlebars/helpers/template.rb +68 -0
  61. data/lib/handlebars/helpers/version.rb +1 -1
  62. metadata +82 -3
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # camel case the characters in the given 'string'.
13
+ class Camel < Handlebars::Helpers::BaseHelper
14
+ # Parse will camel case the characters in the given 'string'.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Camel.new.parse('the quick brown fox 99')
19
+ #
20
+ # TheQuickBrownFox99
21
+ #
22
+ # @return [String] value converted to camel case
23
+ def parse(value)
24
+ tokenizer.parse(value).underscore.camelize
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # CONSTANT case the characters in the given 'string'.
13
+ class Constantize < Handlebars::Helpers::BaseHelper
14
+ # Parse will CONSTANT case the characters in the given 'string'.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Constantize.new.parse('the quick brown fox 99')
19
+ #
20
+ # THE_QUICK_BROWN_FOX99
21
+ #
22
+ # @return [String] value converted to constant case
23
+ def parse(value)
24
+ tokenizer.parse(value, separator: '_').upcase
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # convert to dash notation
13
+ class Dasherize < Handlebars::Helpers::BaseHelper
14
+ # Parse will convert to dash notation
15
+ #
16
+ # @side effects
17
+ #
18
+ # All text is in lower case
19
+ #
20
+ # @example
21
+ #
22
+ # puts Dasherize.new.parse('the quick brown fox 99')
23
+ #
24
+ # the-quick-brown-fox99
25
+ #
26
+ # @return [String] value converted to dash notation
27
+ def parse(value)
28
+ tokenizer.parse(value)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # convert to dot notation
13
+ class Dotirize < Handlebars::Helpers::BaseHelper
14
+ # Parse will convert to dot notation
15
+ #
16
+ # @side effects
17
+ #
18
+ # All text is in lower case
19
+ #
20
+ # @example
21
+ #
22
+ # puts Dotirize.new.parse('the quick brown fox 99')
23
+ #
24
+ # the.quick.brown.fox99
25
+ #
26
+ # @return [String] value converted to dot notation
27
+ def parse(value)
28
+ tokenizer.parse(value, separator: '.')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # double_colon notation for the characters in the given 'string'. aka ruby namespace
13
+ class DoubleColon < Handlebars::Helpers::BaseHelper
14
+ # Parse will double_colon notation for the characters in the given 'string'. aka ruby namespace
15
+ #
16
+ # @side effects
17
+ #
18
+ # Text casing is preserved.
19
+ #
20
+ # @example
21
+ #
22
+ # puts DoubleColon.new.parse('the quick brown fox 99')
23
+ #
24
+ # The::Quick::Brown::Fox99
25
+ #
26
+ # @return [String] value converted to double_colon notation
27
+ def parse(value)
28
+ tokenizer.parse(value, preserve_case: true, separator: '::')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # Downcase/Lowercase all of the characters in the given string.
13
+ class Downcase < Handlebars::Helpers::BaseHelper
14
+ # Parse will Downcase/Lowercase all of the characters in the given string.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Downcase.new.parse('The Quick Brown Fox 99')
19
+ #
20
+ # the quick brown fox 99
21
+ #
22
+ # @return [String] value in lowercase
23
+ def parse(value)
24
+ return '' if value.nil?
25
+
26
+ value.downcase
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # Format As: Chain a list of string formatters to run sequentially
13
+ class FormatAs < Handlebars::Helpers::BaseHelper
14
+ # Parse will execute a chain of string formatters and run them sequentially
15
+ #
16
+ # @example
17
+ #
18
+ # puts FormatAs.new.parse('the quick brown fox', 'pluralize,dashify')
19
+ #
20
+ # the-quick-brown-foxes
21
+ #
22
+ # @param [String] value - value to format
23
+ # @param [String] formats - comma delimited list of formats
24
+ # @return [String] returns a value that has been processed by multiple formatters
25
+ def parse(value, formats)
26
+ return '' if value.nil?
27
+ return value if formats.nil? || formats.empty?
28
+
29
+ formats = formats.split(',') if formats.is_a?(String)
30
+ formats = formats.map(&:to_sym)
31
+ formats.each do |format|
32
+ value = format_value(value, format)
33
+ end
34
+ value
35
+ end
36
+
37
+ def handlebars_helper
38
+ proc { |_context, value, formats| wrapper(parse(value, formats)) }
39
+ end
40
+
41
+ private
42
+
43
+ def format_value(value, format)
44
+ return value if format == :none
45
+
46
+ formatter = Handlebars::Helpers.configuration.string_formatter_config[format]
47
+ unless formatter
48
+ puts 'Logger not found: format'
49
+ return value
50
+ end
51
+
52
+ formatter.parse(value)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # humanize wil convert text to human case, aka capitalize
13
+ class Humanize < Handlebars::Helpers::BaseHelper
14
+ # Parse will humanize wil convert text to human case, aka capitalize
15
+ #
16
+ # @side effects
17
+ #
18
+ # Text casing set to upper case for first letter only.
19
+ # Numbers will maintain their spacing
20
+ #
21
+ # @example
22
+ #
23
+ # puts Humanize.new.parse('the quick brown fox 99')
24
+ #
25
+ # The quick brown fox 99
26
+ #
27
+ # @return [String] value converted to sentence case
28
+ def parse(value)
29
+ tokenizer.parse(value,
30
+ separator: ' ',
31
+ preserve_case: true,
32
+ compress_prefix_numerals: false,
33
+ compress_suffix_numerals: false)
34
+ .humanize
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # lamel case is the same as camel case except with the first character as lower case
13
+ class Lamel < Handlebars::Helpers::BaseHelper
14
+ # Parse will lamel case is the same as camel case except with the first character as lower case
15
+ #
16
+ # @example
17
+ #
18
+ # puts Lamel.new.parse('the quick brown fox 99')
19
+ #
20
+ # theQuickBrownFox99
21
+ #
22
+ # @return [String] value converted to lamel case
23
+ def parse(value)
24
+ tokenizer.parse(value, separator: '_').camelize(:lower)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_safe_string_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # Add padding to the left of the value.
13
+ class Padl < Handlebars::Helpers::BaseSafeStringHelper
14
+ # Parse will Add padding to the left of the value.
15
+ #
16
+ # @example
17
+ #
18
+ # puts "[#{Padl.new.parse('aaa', 10)}]"
19
+ #
20
+ # [ aaa]
21
+ #
22
+ # @example
23
+ #
24
+ # puts "[#{Padl.new.parse('aaa')}]"
25
+ #
26
+ # [ aaa]
27
+ #
28
+ # @example
29
+ #
30
+ # puts Padl.new.parse('aaa', '10', '-')
31
+ #
32
+ # -------aaa
33
+ #
34
+ # @param [String] value - value to apply padding to
35
+ # @param [Integer] count - how much padding to apply. defaults to configuration.padl_count
36
+ # @param [String] char - character to pad with. defaults to configuration.padl_char
37
+ # @return [String] value with padding to left
38
+ def parse(value, count, char)
39
+ value = '' if value.nil?
40
+ count = Handlebars::Helpers.configuration.padl_count if count.nil?
41
+ count = count.to_i if count.is_a?(String)
42
+ char = Handlebars::Helpers.configuration.padl_char if char.nil?
43
+ value.rjust(count, char)
44
+ end
45
+
46
+ def handlebars_helper
47
+ proc do |_context, value, count, char|
48
+ # Handle optional: value, count and char
49
+ value = nil if value.is_a?(V8::Object)
50
+ count = nil if count.is_a?(V8::Object)
51
+ char = nil if char.is_a?(V8::Object)
52
+ wrapper(parse(value, count, char))
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_safe_string_helper'
7
+ require 'handlebars/helpers/configuration'
8
+
9
+ module Handlebars
10
+ module Helpers
11
+ # String manipulation methods for case formatting
12
+ module StringFormatting
13
+ # Add padding to the right of the value.
14
+ class Padr < Handlebars::Helpers::BaseSafeStringHelper
15
+ # Parse will Add padding to the right of the value.
16
+ #
17
+ # @example
18
+ #
19
+ # puts "[#{Padr.new.parse('aaa', 10)}]"
20
+ #
21
+ # [aaa ]
22
+ #
23
+ # @example
24
+ #
25
+ # puts "[#{Padr.new.parse('aaa')}]"
26
+ #
27
+ # [aaa ]
28
+ #
29
+ # @example
30
+ #
31
+ # puts Padr.new.parse('aaa', '10', '-')
32
+ #
33
+ # aaa-------
34
+ #
35
+ # @param [String] value - value to apply padding to
36
+ # @param [Integer] count - how much padding to apply. defaults to configuration.padr_count
37
+ # @param [String] char - character to pad with. defaults to configuration.padr_char
38
+ # @return [String] value with padding to right
39
+ def parse(value, count = nil, char = nil)
40
+ value = '' if value.nil?
41
+ count = Handlebars::Helpers.configuration.padr_count if count.nil?
42
+ count = count.to_i if count.is_a?(String)
43
+ char = Handlebars::Helpers.configuration.padr_char if char.nil?
44
+ value.ljust(count, char)
45
+ end
46
+
47
+ def handlebars_helper
48
+ proc do |_context, value, count, char|
49
+ # Handle optional: value, count and char
50
+ value = nil if value.is_a?(V8::Object)
51
+ count = nil if count.is_a?(V8::Object)
52
+ char = nil if char.is_a?(V8::Object)
53
+ wrapper(parse(value, count, char))
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # convert to plus notation
13
+ class Pluserize < Handlebars::Helpers::BaseHelper
14
+ # Parse will convert to plus notation
15
+ #
16
+ # @example
17
+ #
18
+ # puts Pluserize.new.parse('the quick brown fox 99')
19
+ #
20
+ # the+quick+brown+fox99
21
+ #
22
+ # @return [String] value converted to plus notation
23
+ def parse(value)
24
+ tokenizer.parse(value, separator: '+')
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end