Skip to content

Commit

Permalink
Merge pull request #47 from Iiqbal2000/go/chr
Browse files Browse the repository at this point in the history
Feat-Go: add Chr function
  • Loading branch information
aldy505 authored Oct 8, 2022
2 parents 6aa5376 + 7dd2538 commit f9d48b1
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
15 changes: 15 additions & 0 deletions go/chr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pehape

import "bytes"

// Chr is a function that returns a character from a number.
// Parameter :
// - codepoint => it can be in decimal, octal, or hex values.
// Return :
// - single-byte string
func Chr(codepoint int) string {
var result bytes.Buffer
result.WriteByte(byte(codepoint))

return result.String()
}
57 changes: 57 additions & 0 deletions go/chr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package pehape_test

import (
"fmt"
"strings"
"testing"

PHP "github.com/teknologi-umum/pehape/go"
)

func TestChr(t *testing.T) {
testcases := []struct {
input int
wantOuput string
}{
{
input: 65,
wantOuput: "A",
},
{
input: 053,
wantOuput: "+",
},
{
input: 0x52,
wantOuput: "R",
},
{
input: -159,
wantOuput: "a",
},
{
input: 833,
wantOuput: "A",
},
}

for i, c := range testcases {
t.Run(fmt.Sprint(i+1), func(t *testing.T) {
got := PHP.Chr(c.input)

if c.wantOuput != got {
t.Errorf("want: %q, got: %q", c.wantOuput, got)
}
})
}

t.Run("symbol", func(t *testing.T) {
want := "🐘"

got := PHP.Chr(240) + PHP.Chr(159) + PHP.Chr(144) + PHP.Chr(152)

if !strings.EqualFold(want, got) {
t.Errorf("want: %q, got: %q", want, got)
}
})
}
20 changes: 19 additions & 1 deletion go/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,22 @@ fmt.Println(pehape.Ucwords(bar))
fmt.Println(pehape.Strrev(bar))
//result : "kasur rusak"
```


* `Chr`
```go
var charFromNumber = pehape.Chr(65)
fmt.Println(charFromNumber)
//result : "A"

var charFromOctal = pehape.Chr(053)
fmt.Println(charFromOctal)
//result: "+"

var charFromHex = pehape.Chr(0x52)
fmt.Println(charFromHex)
//result: "R"

var symbol = pehape.Chr(240) + pehape.Chr(159) + pehape.Chr(144) + pehape.Chr(152)
fmt.Println(symbol)
//result : "🐘"
```

0 comments on commit f9d48b1

Please sign in to comment.