First n string/ Last n string function added

This commit is contained in:
Roshan Ranabhat 2020-04-07 10:42:58 +05:45
parent 9839207054
commit 2a64174530
5 changed files with 86 additions and 9 deletions

View File

@ -46,6 +46,11 @@ A Golang string manipulation utility which have many useful functionality like c
<td><a href="#toupper-string">ToUpper</a></td>
<td><a href="#ucfirst-string">UcFirst</a></td>
</tr>
<tr>
<td><a href="#firstlength-int-string">First</a></td>
<td><a href="#lastlength-int-string">ToUpper</a></td>
<td></td>
</tr>
</table>
@ -142,6 +147,17 @@ Delimited is variadic function that takes two params delimiter and slice of stri
You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
#### First(length int) string
First returns first n characters from provided input. It removes all spaces in string before doing so.
```go
fcn := stringy.New("4111 1111 1111 1111")
first := fcn.First(4)
fmt.Println(first) // 4111
```
#### Get() string
Get simply returns result and can be chained on function which returns StringManipulation interface view above examples
@ -160,6 +176,17 @@ KebabCase/slugify is variadic function that takes one Param slice of strings nam
You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
#### Last(length int) string
Last returns last n characters from provided input. It removes all spaces in string before doing so.
```go
lcn := stringy.New("4111 1111 1111 1348")
last := lcn.Last(4)
fmt.Println(last) // 1348
```
#### LcFirst() string
LcFirst simply returns result by lower casing first letter of string and it can be chained on function which return StringManipulation interface

View File

@ -66,4 +66,12 @@ func main() {
kebabStr := str.KebabCase("?", "")
fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU
fcn := stringy.New("4111 1111 1111 1111")
first := fcn.First(4)
fmt.Println(first) // 4111
lcn := stringy.New("4111 1111 1111 1348")
last := lcn.Last(4)
fmt.Println(last) // 1348
}

View File

@ -2,14 +2,16 @@ package stringy
// const below are used in packages
const (
First = "first"
Last = "last"
Left = "left"
Right = "right"
Both = "both"
OddError = "odd number rule provided please provide in even count"
SelectCapital = "([a-z])([A-Z])"
ReplaceCapital = "$1 $2"
First = "first"
Last = "last"
Left = "left"
Right = "right"
Both = "both"
OddError = "odd number rule provided please provide in even count"
SelectCapital = "([a-z])([A-Z])"
ReplaceCapital = "$1 $2"
LengthError = "passed length cannot be greater than input length"
InvalidLogicalString = "invalid string value to test boolean value"
)
// False is slice of array for false logical representation in string

View File

@ -23,8 +23,10 @@ type StringManipulation interface {
CamelCase(rule ...string) string
ContainsAll(check ...string) bool
Delimited(delimiter string, rule ...string) StringManipulation
First(length int) string
Get() string
KebabCase(rule ...string) StringManipulation
Last(length int) string
LcFirst() string
Lines() []string
Pad(length int, with, padType string) string
@ -86,7 +88,7 @@ func (i *input) Boolean() bool {
if on {
return true
}
panic(errors.New("invalid string value to test boolean value"))
panic(errors.New(InvalidLogicalString))
}
// CamelCase is variadic function which takes one Param rule i.e slice of strings and it returns
@ -131,6 +133,16 @@ func (i *input) Delimited(delimiter string, rule ...string) StringManipulation {
return i
}
// First returns first n characters from provided input. It removes all spaces in string before doing so.
func (i *input) First(length int) string {
input := getInput(*i)
input = strings.ReplaceAll(input, " ", "")
if len(input) < length {
panic(errors.New(LengthError))
}
return input[0:length]
}
// Get simply returns result and can be chained on function which
// returns StringManipulation interface
func (i *input) Get() string {
@ -150,6 +162,18 @@ func (i *input) KebabCase(rule ...string) StringManipulation {
return i
}
// Last returns last n characters from provided input. It removes all spaces in string before doing so.
func (i *input) Last(length int) string {
input := getInput(*i)
input = strings.ReplaceAll(input, " ", "")
inputLen := len(input)
if len(input) < length {
panic(errors.New(LengthError))
}
start := inputLen - length
return input[start:inputLen]
}
// LcFirst simply returns result by lower casing first letter of string and it can be chained on
// function which return StringManipulation interface
func (i *input) LcFirst() string {

View File

@ -158,3 +158,19 @@ func TestInput_UcFirst(t *testing.T) {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
}
func TestInput_First(t *testing.T) {
fcn := New("4111 1111 1111 1111")
against := "4111"
if first := fcn.First(4); first != against {
t.Errorf("Expected: to be %s but got: %s", against, first)
}
}
func TestInput_Last(t *testing.T) {
lcn := New("4111 1111 1111 1348")
against := "1348"
if last := lcn.Last(4); last != against {
t.Errorf("Expected: to be %s but got: %s", against, last)
}
}