Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

func of count runes of string #18

Closed
yingzhuo opened this issue Oct 25, 2019 · 5 comments
Closed

func of count runes of string #18

yingzhuo opened this issue Oct 25, 2019 · 5 comments

Comments

@yingzhuo
Copy link
Contributor

I do know this is NOT a bug of frep itself.

template :

{{ len "中文" }}

if i run frep like this:

frep /path/to/template --dryrun

i got :

6

So, I think chines users need a new func.

How about:

func CountRune(s string) int {
    return len([]rune(s))
}

and register is:

funcMap := template.FuncMap() {
    "countRune": CountRune,
    // other funcs
}
@yingzhuo
Copy link
Contributor Author

Another func:

func Join(separator string, value []interface{}) string {
	defer func() {
		recover()
	}()

	sb := &strings.Builder{}
	for i, v := range value {
		_, _ = fmt.Fprintf(sb, "%v", v)
		if i != len(value)-1 {
			_, _ = fmt.Fprintf(sb, separator)
		}
	}
	return sb.String()
}

I can user it like this:

{{ .foo | join "," }}

@subchen
Copy link
Owner

subchen commented Oct 25, 2019

pull request is welcome 😄

@subchen
Copy link
Owner

subchen commented Oct 25, 2019

Actually, we already have join

@yingzhuo
Copy link
Contributor Author

OK.
I'll do the late. It's busy now.

@yingzhuo
Copy link
Contributor Author

PR #19 .

merge please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants