From 469f31e8a5a6202949f21596b864e803ba249972 Mon Sep 17 00:00:00 2001 From: Matt Welke Date: Mon, 12 Jul 2021 23:35:45 -0400 Subject: [PATCH] iss11 * update readme, remove redundant types * delete CircleCI config, remove unused struct * fix mattwelke GitHub username in licence * changed title casing functionality for strings to lowercase strings then apply std lib strings.Title function --- .circleci/config.yml | 26 -------------------------- LICENCE | 2 +- README.md | 2 +- sanitize_test.go | 9 ++++----- string.go | 18 +----------------- string_test.go | 18 +++++++++++++++--- 6 files changed, 22 insertions(+), 53 deletions(-) delete mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 66515b4..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,26 +0,0 @@ -# Golang CircleCI 2.0 configuration file -# -# Check https://circleci.com/docs/2.0/language-go/ for more details -version: 2 -jobs: - build: - docker: - # specify the version - - image: circleci/golang:1.12 - - # Specify service dependencies here if necessary - # CircleCI maintains a library of pre-built images - # documented at https://circleci.com/docs/2.0/circleci-images/ - # - image: circleci/postgres:9.4 - - #### TEMPLATE_NOTE: go expects specific checkout path representing url - #### expecting it in the form of - #### /go/src/github.com/circleci/go-tool - #### /go/src/bitbucket.org/circleci/go-tool - working_directory: /go/src/github.com/go-sanitize/sanitize - steps: - - checkout - - # specify any bash command here prefixed with `run: ` - - run: go get -v -t -d ./... - - run: go test -v ./... \ No newline at end of file diff --git a/LICENCE b/LICENCE index b8ed8d1..f2fb7f6 100644 --- a/LICENCE +++ b/LICENCE @@ -185,7 +185,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2019 Matt Welke (https://github.com/welkie), Henrique S. Coelho (https://github.com/hscasn) + Copyright 2021 Matt Welke (https://github.com/mattwelke), Henrique S. Coelho (https://github.com/hscasn) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 0904857..00cdc5a 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ s := sanitizer.New(sanitizer.OptionDateFormat{ 1. **trim** - Remove trailing spaces left and right 1. **lower** - Lowercase all characters in the string 1. **upper** - Uppercase all characters in the string -1. **title** - First character of every word is changed to uppercase, the rest to lowercase +1. **title** - First character of every word is changed to uppercase, the rest to lowercase. Uses Go's built in `strings.Title()` function. 1. **cap** - Only the first letter of the string will be changed to uppercase, the rest to lowercase 1. **def=``** (only available for pointers) - Sets a default `` value in case the pointer is `nil` 1. **xss** - Will remove brackets such as <>[](){} and the characters !=? from the string diff --git a/sanitize_test.go b/sanitize_test.go index b2667fe..b771d66 100644 --- a/sanitize_test.go +++ b/sanitize_test.go @@ -372,11 +372,11 @@ func Test_Sanitize(t *testing.T) { }, }, SliPtrSubPtr1: &[]*TestStructMixedRecursiveSub{ - &TestStructMixedRecursiveSub{ + { StrField: " subtest1 ", StrPtrField: &arg71, }, - &TestStructMixedRecursiveSub{ + { StrField: " subtest2 ", StrPtrField: &arg72, }, @@ -487,11 +487,11 @@ func Test_Sanitize(t *testing.T) { }, }, SliPtrSubPtr1: &[]*TestStructMixedRecursiveSub{ - &TestStructMixedRecursiveSub{ + { StrField: "su", StrPtrField: &res71, }, - &TestStructMixedRecursiveSub{ + { StrField: "su", StrPtrField: &res72, }, @@ -831,4 +831,3 @@ func Test_SliceSanitize(t *testing.T) { }) } } - diff --git a/string.go b/string.go index e93443e..8e103f5 100644 --- a/string.go +++ b/string.go @@ -100,23 +100,7 @@ func sanitizeStrField(s Sanitizer, structValue reflect.Value, idx int) error { } func toTitle(s string) string { - b := make([]byte, len(s)) - casediff := byte('a' - 'A') - inWord := false - for i := 0; i < len(s); i++ { - b[i] = s[i] - c := b[i] - isLower := c >= 'a' && c <= 'z' - isUpper := c >= 'A' && c <= 'Z' - if !inWord && isLower { // Not inside a word and it's lower case - b[i] -= casediff - } - if inWord && isUpper { // Inside a word and it's upper case - b[i] += casediff - } - inWord = isLower || isUpper - } - return string(b) + return strings.Title(strings.ToLower((s))) } func toCap(s string) string { diff --git a/string_test.go b/string_test.go index 9c39108..08ab2cf 100644 --- a/string_test.go +++ b/string_test.go @@ -60,9 +60,6 @@ func Test_sanitizeStrField(t *testing.T) { type TestStrStructPtrCap struct { Field *string `san:"cap"` } - type TestStrStructPtrDef struct { - Field *string `san:"def=et"` - } type TestStrStructPtrTruncTrimLowerDef struct { Field *string `san:"max=2,trim,lower,def=et"` } @@ -86,6 +83,8 @@ func Test_sanitizeStrField(t *testing.T) { resString7 := " Test Test Test Test " argString8 := " tEst TeSt test TEST " resString8 := " Test test test test " + argString9 := " hernández " + resString9 := " Hernández " type args struct { v interface{} @@ -331,6 +330,19 @@ func Test_sanitizeStrField(t *testing.T) { }, wantErr: false, }, + { + name: "Title cases a *string field on a struct with the tag when the string has a character with a punctuation mark.", + args: args{ + v: &TestStrStructPtrTitle{ + Field: &argString9, + }, + idx: 0, + }, + want: &TestStrStructPtrTitle{ + Field: &resString9, + }, + wantErr: false, + }, { name: "Puts a default value for a *string field that was nil on a struct with the tag.", args: args{