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="#toupper-string">ToUpper</a></td>
<td><a href="#ucfirst-string">UcFirst</a></td> <td><a href="#ucfirst-string">UcFirst</a></td>
</tr> </tr>
<tr>
<td><a href="#firstlength-int-string">First</a></td>
<td><a href="#lastlength-int-string">ToUpper</a></td>
<td></td>
</tr>
</table> </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. 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() string
Get simply returns result and can be chained on function which returns StringManipulation interface view above examples 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. 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() string
LcFirst simply returns result by lower casing first letter of string and it can be chained on function which return StringManipulation interface 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("?", "") kebabStr := str.KebabCase("?", "")
fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU 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

@ -10,6 +10,8 @@ const (
OddError = "odd number rule provided please provide in even count" OddError = "odd number rule provided please provide in even count"
SelectCapital = "([a-z])([A-Z])" SelectCapital = "([a-z])([A-Z])"
ReplaceCapital = "$1 $2" 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 // False is slice of array for false logical representation in string

View File

@ -23,8 +23,10 @@ type StringManipulation interface {
CamelCase(rule ...string) string CamelCase(rule ...string) string
ContainsAll(check ...string) bool ContainsAll(check ...string) bool
Delimited(delimiter string, rule ...string) StringManipulation Delimited(delimiter string, rule ...string) StringManipulation
First(length int) string
Get() string Get() string
KebabCase(rule ...string) StringManipulation KebabCase(rule ...string) StringManipulation
Last(length int) string
LcFirst() string LcFirst() string
Lines() []string Lines() []string
Pad(length int, with, padType string) string Pad(length int, with, padType string) string
@ -86,7 +88,7 @@ func (i *input) Boolean() bool {
if on { if on {
return true 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 // 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 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 // Get simply returns result and can be chained on function which
// returns StringManipulation interface // returns StringManipulation interface
func (i *input) Get() string { func (i *input) Get() string {
@ -150,6 +162,18 @@ func (i *input) KebabCase(rule ...string) StringManipulation {
return i 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 // LcFirst simply returns result by lower casing first letter of string and it can be chained on
// function which return StringManipulation interface // function which return StringManipulation interface
func (i *input) LcFirst() string { 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) 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)
}
}