mirror of https://github.com/gobeam/stringy.git
First n string/ Last n string function added
This commit is contained in:
parent
9839207054
commit
2a64174530
27
README.md
27
README.md
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ const (
|
|||
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
|
||||
|
|
26
stringy.go
26
stringy.go
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue