Skip to content

Commit

Permalink
Add quote, squote. Let add and biggest take varargs.
Browse files Browse the repository at this point in the history
This fixes #6.
This partially addresses #3.
  • Loading branch information
technosophos committed Feb 2, 2016
1 parent 93e0202 commit fd057ca
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Release 1.2.0 (2016-02-01)

- Added quote and squote
- Added b32enc and b32dec
- add now takes varargs
- biggest now takes varargs

# Release 1.1.0 (2015-12-29)

- Added #4: Added contains function. strings.Contains, but with the arguments
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ HELLO!HELLO!HELLO!HELLO!HELLO!
- wrapWith: Wrap text at the given column count, and with the given
string for a line terminator: `wrap 50 "\n\t" $string`
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
- quote: Wrap strings in double quotes. `quote "a" "b"` returns `"a"
"b"`
- squote: Wrap strings in single quotes.

### String Slice Functions:

Expand Down Expand Up @@ -141,12 +144,13 @@ HELLO!HELLO!HELLO!HELLO!HELLO!
### Math Functions:

- add1: Increment an integer by 1
- add: Sum two integers
- add: Sum integers. `add 1 2 3` renders `6`
- sub: Subtract the second integer from the first
- div: Divide the first integer by the second
- mod: Module of first integer divided by second
- mul: Multiply two integers
- biggest: Return the biggest of two integers
- biggest: Return the biggest of a series of integers. `biggest 1 2 3`
returns `3`.


## Principles:
Expand Down
42 changes: 34 additions & 8 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ String Functions
- wrap: Force a line wrap at the given width. `wrap 80 "imagine a longer string"`
- wrapWith: Wrap a line at the given length, but using 'sep' instead of a newline. `wrapWith 50, "<br>", $html`
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
- quote: Wrap string(s) in double quotation marks.
- squote: Wrap string(s) in double quotation marks.
String Slice Functions:
Expand Down Expand Up @@ -95,12 +97,12 @@ Reflection:
Math Functions:
- add1: Increment an integer by 1
- add: Sum two integers
- add: Sum an arbitrary number of integers
- sub: Subtract the second integer from the first
- div: Divide the first integer by the second
- mod: Module of first integer divided by second
- mul: Multiply two integers
- biggest: Return the biggest of two integers
- biggest: Return the biggest of a series of one or more integers
REMOVED (implemented in Go 1.2)
Expand Down Expand Up @@ -180,6 +182,8 @@ var genericMap = map[string]interface{}{
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
// Switch order so that "foobar" | contains "foo"
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
"quote": quote,
"squote": squote,

// Wrap Atoi to stop errors.
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
Expand All @@ -193,8 +197,14 @@ var genericMap = map[string]interface{}{
"split": split,

// VERY basic arithmetic.
"add1": func(i int) int { return i + 1 },
"add": func(a, b int) int { return a + b },
"add1": func(i int) int { return i + 1 },
"add": func(i ...int) int {
a := 0
for _, b := range i {
a += b
}
return a
},
"sub": func(a, b int) int { return a - b },
"div": func(a, b int) int { return a / b },
"mod": func(a, b int) int { return a % b },
Expand Down Expand Up @@ -292,11 +302,13 @@ func dateModify(fmt string, date time.Time) time.Time {
return date.Add(d)
}

func biggest(a, b int) int {
if a > b {
return a
func biggest(a int, i ...int) int {
for _, b := range i {
if b > a {
a = b
}
}
return b
return a
}

// dfault checks whether `given` is set, and returns default if not set.
Expand Down Expand Up @@ -434,3 +446,17 @@ func randNumeric(count int) string {
func untitle(str string) string {
return util.Uncapitalize(str)
}

func quote(str ...string) string {
for i, s := range str {
str[i] = fmt.Sprintf("%q", s)
}
return strings.Join(str, " ")
}

func squote(str ...string) string {
for i, s := range str {
str[i] = fmt.Sprintf("'%s'", s)
}
return strings.Join(str, " ")
}
32 changes: 32 additions & 0 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ func TestSubstr(t *testing.T) {
}
}

func TestQuote(t *testing.T) {
tpl := `{{quote "a" "b" "c"}}`
if err := runt(tpl, `"a" "b" "c"`); err != nil {
t.Error(err)
}
}
func TestSquote(t *testing.T) {
tpl := `{{squote "a" "b" "c"}}`
if err := runt(tpl, `'a' 'b' 'c'`); err != nil {
t.Error(err)
}
}

func TestAdd(t *testing.T) {
tpl := `{{ 3 | add 1 2}}`
if err := runt(tpl, `6`); err != nil {
t.Error(err)
}
}

func TestBiggest(t *testing.T) {
tpl := `{{ biggest 1 2 3 345 5 6 7}}`
if err := runt(tpl, `345`); err != nil {
t.Error(err)
}

tpl = `{{ biggest 345}}`
if err := runt(tpl, `345`); err != nil {
t.Error(err)
}
}

func TestTrimall(t *testing.T) {
tpl := `{{"$foo$" | trimall "$"}}`
if err := runt(tpl, "foo"); err != nil {
Expand Down

0 comments on commit fd057ca

Please sign in to comment.