From 2a64174530f400d04c924f375aca29fc45415bd8 Mon Sep 17 00:00:00 2001 From: Roshan Ranabhat Date: Tue, 7 Apr 2020 10:42:58 +0545 Subject: [PATCH] First n string/ Last n string function added --- README.md | 27 +++++++++++++++++++++++++++ example/main.go | 8 ++++++++ message.go | 18 ++++++++++-------- stringy.go | 26 +++++++++++++++++++++++++- stringy_test.go | 16 ++++++++++++++++ 5 files changed, 86 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 6f999ae..e56f4c2 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,11 @@ A Golang string manipulation utility which have many useful functionality like c ToUpper UcFirst + + First + ToUpper + + @@ -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 diff --git a/example/main.go b/example/main.go index 44ff0a1..38705dd 100644 --- a/example/main.go +++ b/example/main.go @@ -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 + } diff --git a/message.go b/message.go index da20b62..f21f798 100644 --- a/message.go +++ b/message.go @@ -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 diff --git a/stringy.go b/stringy.go index 102d498..313c48e 100644 --- a/stringy.go +++ b/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 { diff --git a/stringy_test.go b/stringy_test.go index 1ea5c39..62d5ac3 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -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) + } +}