handlebars-helpers 0.0.5 → 0.0.65
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +3 -3
- data/.gitignore +1 -0
- data/.handlebars_helpers.json +901 -0
- data/.handlebars_string_formatters.json +137 -0
- data/.rubocop.yml +4 -2
- data/.vscode/settings.json +6 -0
- data/Gemfile +3 -0
- data/Rakefile +1 -0
- data/STORIES.md +44 -6
- data/handlebars-helpers.gemspec +2 -0
- data/lib/handlebars/helpers.rb +2 -0
- data/lib/handlebars/helpers/base_helper.rb +36 -0
- data/lib/handlebars/helpers/base_safe_string_helper.rb +17 -0
- data/lib/handlebars/helpers/code_ruby/classify.rb +40 -0
- data/lib/handlebars/helpers/code_ruby/deconstantize.rb +48 -0
- data/lib/handlebars/helpers/code_ruby/demodulize.rb +58 -0
- data/lib/handlebars/helpers/code_ruby/foreign_key.rb +49 -0
- data/lib/handlebars/helpers/code_ruby/tableize.rb +35 -0
- data/lib/handlebars/helpers/comparison/and.rb +44 -0
- data/lib/handlebars/helpers/comparison/default.rb +66 -0
- data/lib/handlebars/helpers/comparison/eq.rb +35 -0
- data/lib/handlebars/helpers/comparison/gt.rb +35 -0
- data/lib/handlebars/helpers/comparison/gte.rb +43 -0
- data/lib/handlebars/helpers/comparison/lt.rb +35 -0
- data/lib/handlebars/helpers/comparison/lte.rb +43 -0
- data/lib/handlebars/helpers/comparison/ne.rb +35 -0
- data/lib/handlebars/helpers/comparison/or.rb +56 -0
- data/lib/handlebars/helpers/configuration.rb +72 -0
- data/lib/handlebars/helpers/inflection/ordinal.rb +58 -0
- data/lib/handlebars/helpers/inflection/ordinalize.rb +59 -0
- data/lib/handlebars/helpers/inflection/pluralize.rb +36 -0
- data/lib/handlebars/helpers/inflection/pluralize_by_number.rb +60 -0
- data/lib/handlebars/helpers/inflection/singularize.rb +35 -0
- data/lib/handlebars/helpers/register_helpers.rb +73 -0
- data/lib/handlebars/helpers/string_formatting/append_if.rb +42 -0
- data/lib/handlebars/helpers/string_formatting/back_slash.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/camel.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/constantize.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/dasherize.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/dotirize.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/double_colon.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/downcase.rb +31 -0
- data/lib/handlebars/helpers/string_formatting/format_as.rb +57 -0
- data/lib/handlebars/helpers/string_formatting/humanize.rb +39 -0
- data/lib/handlebars/helpers/string_formatting/lamel.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/padl.rb +58 -0
- data/lib/handlebars/helpers/string_formatting/padr.rb +59 -0
- data/lib/handlebars/helpers/string_formatting/pluserize.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/prepend_if.rb +42 -0
- data/lib/handlebars/helpers/string_formatting/singularize.rb +35 -0
- data/lib/handlebars/helpers/string_formatting/slash.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/snake.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/string.js +511 -0
- data/lib/handlebars/helpers/string_formatting/surround.rb +49 -0
- data/lib/handlebars/helpers/string_formatting/surround_if.rb +43 -0
- data/lib/handlebars/helpers/string_formatting/titleize.rb +39 -0
- data/lib/handlebars/helpers/string_formatting/upcase.rb +31 -0
- data/lib/handlebars/helpers/string_tokenizer.rb +43 -0
- data/lib/handlebars/helpers/template.rb +68 -0
- data/lib/handlebars/helpers/version.rb +1 -1
- metadata +82 -3
@@ -0,0 +1,49 @@
|
|
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/string_formatting/format_as'
|
8
|
+
|
9
|
+
module Handlebars
|
10
|
+
module Helpers
|
11
|
+
# String manipulation methods for case formatting
|
12
|
+
module StringFormatting
|
13
|
+
# Surround will surround a value with prefix and suffix, an empty value is considered valid data
|
14
|
+
class Surround < Handlebars::Helpers::BaseSafeStringHelper
|
15
|
+
# Parse will surround a value with prefix and suffix, an empty value is considered valid data
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
#
|
19
|
+
# puts Surround.new.parse('product category', '"', '"', 'titleize')
|
20
|
+
#
|
21
|
+
# "Product Category"
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
#
|
25
|
+
# puts Surround.new.parse(nil, '"', '"', 'titleize')
|
26
|
+
#
|
27
|
+
# ""
|
28
|
+
#
|
29
|
+
# @param [String] value - value to surround
|
30
|
+
# @param [String] prefix - prefix to insert in front of value
|
31
|
+
# @param [String] suffix - suffix to append to value
|
32
|
+
# @param [String] formats - list of formats to apply to value, defaults to none
|
33
|
+
# @return [String] prefix + value + suffix
|
34
|
+
def parse(value, prefix, suffix, formats)
|
35
|
+
format_as = Handlebars::Helpers::StringFormatting::FormatAs.new
|
36
|
+
"#{prefix}#{format_as.parse(value, formats)}#{suffix}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def handlebars_helper
|
40
|
+
proc do |_context, value, prefix, suffix, formats|
|
41
|
+
# Handle optional: formats
|
42
|
+
formats = nil if formats.is_a?(V8::Object)
|
43
|
+
wrapper(parse(value, prefix, suffix, formats))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,43 @@
|
|
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/string_formatting/format_as'
|
8
|
+
|
9
|
+
module Handlebars
|
10
|
+
module Helpers
|
11
|
+
# String manipulation methods for case formatting
|
12
|
+
module StringFormatting
|
13
|
+
# Surround If will surround a value with prefix and suffix, if value is not empty
|
14
|
+
class SurroundIf < Handlebars::Helpers::BaseSafeStringHelper
|
15
|
+
# Parse will surround a value with prefix and suffix, if value is not empty
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
#
|
19
|
+
# puts SurroundIf.new.parse('product category', '["', '"]', 'titleize')
|
20
|
+
#
|
21
|
+
# ["Product Category"]
|
22
|
+
#
|
23
|
+
# @param [String] value - value to surround
|
24
|
+
# @param [String] prefix - prefix to insert in front of value
|
25
|
+
# @param [String] suffix - suffix to append to value
|
26
|
+
# @param [String] formats - list of formats to apply to value, defaults to none
|
27
|
+
# @return [String] value surrounded by prefix and suffix
|
28
|
+
def parse(value, prefix, suffix, formats)
|
29
|
+
format_as = Handlebars::Helpers::StringFormatting::FormatAs.new
|
30
|
+
value.present? ? "#{prefix}#{format_as.parse(value, formats)}#{suffix}" : ''
|
31
|
+
end
|
32
|
+
|
33
|
+
def handlebars_helper
|
34
|
+
proc do |_context, value, prefix, suffix, formats|
|
35
|
+
# Handle optional: formats
|
36
|
+
formats = nil if formats.is_a?(V8::Object)
|
37
|
+
wrapper(parse(value, prefix, suffix, formats))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
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
|
+
# titleize case the characters in the given 'string', aka heading case
|
13
|
+
class Titleize < Handlebars::Helpers::BaseHelper
|
14
|
+
# Parse will titleize case the characters in the given 'string', aka heading case
|
15
|
+
#
|
16
|
+
# @side effects
|
17
|
+
#
|
18
|
+
# Text casing set to upper case for first letters.
|
19
|
+
# Numbers will maintain their spacing
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
#
|
23
|
+
# puts Titleize.new.parse('the quick brown fox 99')
|
24
|
+
#
|
25
|
+
# The Quick Brown Fox 99
|
26
|
+
#
|
27
|
+
# @return [String] value converted to titleize 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
|
+
.titleize
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
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
|
+
# Upcase/Uppercase all of the characters in the given string.
|
13
|
+
class Upcase < Handlebars::Helpers::BaseHelper
|
14
|
+
# Parse will Upcase/Uppercase all of the characters in the given string.
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
#
|
18
|
+
# puts Upcase.new.parse('the quick brown fox 99')
|
19
|
+
#
|
20
|
+
# THE QUICK BROWN FOX 99
|
21
|
+
#
|
22
|
+
# @return [String] value in uppercase
|
23
|
+
def parse(value)
|
24
|
+
return '' if value.nil?
|
25
|
+
|
26
|
+
value.upcase
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/string'
|
4
|
+
|
5
|
+
module Handlebars
|
6
|
+
module Helpers
|
7
|
+
# String tokenizer will clean up a string so that all sorts of formatted strings can be
|
8
|
+
# represented in a consistent fashion
|
9
|
+
class StringTokenizer
|
10
|
+
# Tokenize string
|
11
|
+
# rubocop:disable Metrics/ParameterLists
|
12
|
+
def parse(value,
|
13
|
+
preserve_case: false,
|
14
|
+
compress_prefix_numerals: true,
|
15
|
+
compress_suffix_numerals: true,
|
16
|
+
separator: '-',
|
17
|
+
forced_separator: false)
|
18
|
+
return '' if value.nil?
|
19
|
+
|
20
|
+
# Insert space before any lowercaseUppercase
|
21
|
+
value = value.gsub(/(?<=[a-z])(?=[A-Z])/, ' ')
|
22
|
+
|
23
|
+
# make sure that any numbers followed by space and then some text has the white space removed
|
24
|
+
value = value.gsub(/^(\d*)(\s*)/, '\1') if compress_prefix_numerals
|
25
|
+
|
26
|
+
# Technique1: make sure that trailing space followed by number is compressed
|
27
|
+
# NOTE: named groups don't seem to work with \1, \2 etc.
|
28
|
+
# ex = /(?<space>[\s]*)(?<number>[\d]*)$/
|
29
|
+
# value =value.sub(ex) { |_| Regexp.last_match[:number] }
|
30
|
+
|
31
|
+
# Technique2: make sure that trailing space followed by number is compressed
|
32
|
+
value = value.gsub(/(\s*)(\d*)$/, '\2') if compress_suffix_numerals
|
33
|
+
|
34
|
+
value = value.parameterize(preserve_case: preserve_case, separator: separator) # (separator: ' ')
|
35
|
+
|
36
|
+
value = value.gsub(/[-_]/, separator) if forced_separator
|
37
|
+
|
38
|
+
value
|
39
|
+
end
|
40
|
+
# rubocop:enable Metrics/ParameterLists
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'handlebars'
|
4
|
+
require 'handlebars/helpers/register_helpers'
|
5
|
+
|
6
|
+
module Handlebars
|
7
|
+
module Helpers
|
8
|
+
# Register handlebars helpers
|
9
|
+
class Template
|
10
|
+
# Render template will take the template, compile it and merge it with the data
|
11
|
+
#
|
12
|
+
# @param [String] template The handlebars template to render against
|
13
|
+
# @param [Hash, Array] data The data to merge with the template
|
14
|
+
# NOTE: I'm considering where to put the configuration block, &block)
|
15
|
+
def self.render(template, data = {}, &block)
|
16
|
+
register = if block_given?
|
17
|
+
Handlebars::Helpers::RegisterHelpers.new(&block)
|
18
|
+
else
|
19
|
+
Handlebars::Helpers::RegisterHelpers.new
|
20
|
+
end
|
21
|
+
|
22
|
+
handlebars = register.handlebars
|
23
|
+
compiled_template = handlebars.compile(template)
|
24
|
+
|
25
|
+
begin
|
26
|
+
obj = case data
|
27
|
+
when String
|
28
|
+
data
|
29
|
+
when Array
|
30
|
+
process_array(data)
|
31
|
+
else
|
32
|
+
process_hash(data)
|
33
|
+
end
|
34
|
+
|
35
|
+
compiled_template.call(obj)
|
36
|
+
rescue StandardError => e
|
37
|
+
puts 'Failed to process template'
|
38
|
+
puts e.message
|
39
|
+
# L.block 'Failed to process template', e.message
|
40
|
+
# L.exception e
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.process_array(data)
|
45
|
+
data.map do |item|
|
46
|
+
if item.is_a?(String)
|
47
|
+
item # Handle array of strings
|
48
|
+
else
|
49
|
+
process_hash(item)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.process_hash(data)
|
55
|
+
obj = data.to_h
|
56
|
+
|
57
|
+
obj.each_key do |key|
|
58
|
+
obj[key] = obj[key].to_h if obj[key].instance_of?(OpenStruct)
|
59
|
+
end
|
60
|
+
|
61
|
+
obj
|
62
|
+
end
|
63
|
+
|
64
|
+
private_class_method :process_array
|
65
|
+
private_class_method :process_hash
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handlebars-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.65
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Cruwys
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-01-
|
12
|
-
dependencies:
|
11
|
+
date: 2021-01-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: handlebars
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
description: " Handlebars Helpers provides (Nx) handlebars helpers across (Ny)
|
14
42
|
categories\n"
|
15
43
|
email:
|
@@ -20,9 +48,12 @@ extra_rdoc_files: []
|
|
20
48
|
files:
|
21
49
|
- ".github/workflows/ruby.yml"
|
22
50
|
- ".gitignore"
|
51
|
+
- ".handlebars_helpers.json"
|
52
|
+
- ".handlebars_string_formatters.json"
|
23
53
|
- ".rspec"
|
24
54
|
- ".rubocop.yml"
|
25
55
|
- ".rubocop_todo.yml"
|
56
|
+
- ".vscode/settings.json"
|
26
57
|
- CODE_OF_CONDUCT.md
|
27
58
|
- Gemfile
|
28
59
|
- Guardfile
|
@@ -40,6 +71,54 @@ files:
|
|
40
71
|
- hooks/pre-commit
|
41
72
|
- hooks/update-version
|
42
73
|
- lib/handlebars/helpers.rb
|
74
|
+
- lib/handlebars/helpers/base_helper.rb
|
75
|
+
- lib/handlebars/helpers/base_safe_string_helper.rb
|
76
|
+
- lib/handlebars/helpers/code_ruby/classify.rb
|
77
|
+
- lib/handlebars/helpers/code_ruby/deconstantize.rb
|
78
|
+
- lib/handlebars/helpers/code_ruby/demodulize.rb
|
79
|
+
- lib/handlebars/helpers/code_ruby/foreign_key.rb
|
80
|
+
- lib/handlebars/helpers/code_ruby/tableize.rb
|
81
|
+
- lib/handlebars/helpers/comparison/and.rb
|
82
|
+
- lib/handlebars/helpers/comparison/default.rb
|
83
|
+
- lib/handlebars/helpers/comparison/eq.rb
|
84
|
+
- lib/handlebars/helpers/comparison/gt.rb
|
85
|
+
- lib/handlebars/helpers/comparison/gte.rb
|
86
|
+
- lib/handlebars/helpers/comparison/lt.rb
|
87
|
+
- lib/handlebars/helpers/comparison/lte.rb
|
88
|
+
- lib/handlebars/helpers/comparison/ne.rb
|
89
|
+
- lib/handlebars/helpers/comparison/or.rb
|
90
|
+
- lib/handlebars/helpers/configuration.rb
|
91
|
+
- lib/handlebars/helpers/inflection/ordinal.rb
|
92
|
+
- lib/handlebars/helpers/inflection/ordinalize.rb
|
93
|
+
- lib/handlebars/helpers/inflection/pluralize.rb
|
94
|
+
- lib/handlebars/helpers/inflection/pluralize_by_number.rb
|
95
|
+
- lib/handlebars/helpers/inflection/singularize.rb
|
96
|
+
- lib/handlebars/helpers/register_helpers.rb
|
97
|
+
- lib/handlebars/helpers/string_formatting/append_if.rb
|
98
|
+
- lib/handlebars/helpers/string_formatting/back_slash.rb
|
99
|
+
- lib/handlebars/helpers/string_formatting/camel.rb
|
100
|
+
- lib/handlebars/helpers/string_formatting/constantize.rb
|
101
|
+
- lib/handlebars/helpers/string_formatting/dasherize.rb
|
102
|
+
- lib/handlebars/helpers/string_formatting/dotirize.rb
|
103
|
+
- lib/handlebars/helpers/string_formatting/double_colon.rb
|
104
|
+
- lib/handlebars/helpers/string_formatting/downcase.rb
|
105
|
+
- lib/handlebars/helpers/string_formatting/format_as.rb
|
106
|
+
- lib/handlebars/helpers/string_formatting/humanize.rb
|
107
|
+
- lib/handlebars/helpers/string_formatting/lamel.rb
|
108
|
+
- lib/handlebars/helpers/string_formatting/padl.rb
|
109
|
+
- lib/handlebars/helpers/string_formatting/padr.rb
|
110
|
+
- lib/handlebars/helpers/string_formatting/pluserize.rb
|
111
|
+
- lib/handlebars/helpers/string_formatting/prepend_if.rb
|
112
|
+
- lib/handlebars/helpers/string_formatting/singularize.rb
|
113
|
+
- lib/handlebars/helpers/string_formatting/slash.rb
|
114
|
+
- lib/handlebars/helpers/string_formatting/snake.rb
|
115
|
+
- lib/handlebars/helpers/string_formatting/string.js
|
116
|
+
- lib/handlebars/helpers/string_formatting/surround.rb
|
117
|
+
- lib/handlebars/helpers/string_formatting/surround_if.rb
|
118
|
+
- lib/handlebars/helpers/string_formatting/titleize.rb
|
119
|
+
- lib/handlebars/helpers/string_formatting/upcase.rb
|
120
|
+
- lib/handlebars/helpers/string_tokenizer.rb
|
121
|
+
- lib/handlebars/helpers/template.rb
|
43
122
|
- lib/handlebars/helpers/version.rb
|
44
123
|
homepage: http://appydave.com/gems/handlebars-helpers
|
45
124
|
licenses:
|