From 0673a2d62c29db0128fa150a1a718585d34c48d3 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Sun, 22 May 2016 20:44:04 +0530 Subject: [PATCH] Updated documentation, licenses, and other clean up --- LICENSE.txt | 21 + README.md | 573 ++++++++++++++++++++++--- build.gradle | 2 +- src/main/java/strman/Ascii.java | 26 ++ src/main/java/strman/HtmlEntities.java | 29 +- src/main/java/strman/Strman.java | 31 +- src/test/java/strman/StrmanTest.java | 35 +- 7 files changed, 645 insertions(+), 72 deletions(-) create mode 100644 LICENSE.txt diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..102608a --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License + +Copyright 2016 Shekhar Gulati . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index cd63411..f93a52c 100644 --- a/README.md +++ b/README.md @@ -1,120 +1,567 @@ strman-java ------ -A Java 8 string manipulation library without any dependencies. It is inspired by [dleitee/strman](https://github.com/dleitee/strman). +A Java 8 library for working with String. It is inspired by [dleitee/strman](https://github.com/dleitee/strman). +Getting Started +-------- + +To use strman in your application, you have to add `strman` in your classpath. strman is available on [Maven Central](http://search.maven.org/) so you just need to add dependency to your favorite build tool as show below. + +For Apache Maven users, please add following to your pom.xml. + +```xml + + + com.shekhargulati + strman + 0.1.0 + jar + + +``` + +Gradle users can add following to their build.gradle file. + +``` +compile(group: 'com.shekhargulati', name: 'strman', version: '0.1.0', ext: 'jar'){ + transitive=true +} +``` ## Available Functions -* [append](https://github.com/shekhargulati/strman-java#appendvalue-strings) -* [appendArray](https://github.com/shekhargulati/strman-java#appendarrayvalue-strings) -* [at](https://github.com/shekhargulati/strman-java#atvalue-index) -* [between](https://github.com/shekhargulati/strman-java#betweenvalue-start-end) -* [between](https://github.com/shekhargulati/strman-java#betweenvalue-start-end) -* [chars](https://github.com/shekhargulati/strman-java#charsvalue) -* [collapseWhitespace](https://github.com/shekhargulati/strman-java#collapsewhitespacevalue) +These are the available functions in current version of library: +## append -## append(value, strings...) - -This method will append `strings` to the `value`. +Appends Strings to value ```java -import static strman.Strman.append; -String title = "s"; -String result = append(title, "tr","m","an"); -// result = "strman" +import static strman.append +append("f", "o", "o", "b", "a", "r") +// result => "foobar" ``` +## appendArray -## appendArray(value, strings) - -This method will append all the values in `strings` array to the `value`. +Append an array of String to value ```java -import static strman.Strman.appendArray; -String title = "s"; -String result = appendArray(title, new String[]{"tr","m","an"}); -// result = "strman" +import static strman.appendArray +appendArray("f", new String[]{"o", "o", "b", "a", "r"} +// result => "foobar" ``` -## at(value, index) +## at -Gets the character at index. +Get the character at index. This method will take care of negative indexes. ```java -import static strman.Strman.at; -String result = at("foobar", 0); -// result = "f" +import static strman.at +at("foobar", 0) +// result => Optional("f") ``` -## between(value, start, end) +## between -Returns array with strings between `start` and `end`. +Returns an array with strings between start and end. ```java -import static strman.Strman.between; -String[] parts = between("[abc][def]", "[", "]"; -// parts = ["abc","def"] +import static strman.between +between("[abc][def]", "[", "]") +// result => ["abc","def"] ``` -## chars(value) +## chars Returns a String array consisting of the characters in the String. ```java -final String title = "title"; -char[] result = chars(title); -// result = ["t", "i", "t", "l", "e"] +import static strman.chars +chars("title") +// result => ["t", "i", "t", "l", "e"] ``` - -## collapseWhitespace(value) +## collapseWhitespace Replace consecutive whitespace characters with a single space. ```java -final String input = " foo bar "; -final String result = collapseWhitespace(title); -// result = "foo bar"; +import static strman.collapseWhitespace +collapseWhitespace("foo bar") +// result => "foo bar" ``` -## contains(value, needle, caseSensitive) +## contains -Verifies that the needle is contained in value. +Verifies that the needle is contained in the value. ```java -boolean result = contains("foo bar", "BAR", true); -// result = false +import static strman.contains +contains("foo bar","foo") +// result => true -boolean result = contains("foo bar", "FOO"); -// result = true +contains("foo bar","FOO", false) // turning off case sensitivity +// result => true ``` -## containsAll(value, needles, caseSensitive) +## containsAll -Verifies that all needles are contained in value. +Verifies that all needles are contained in value ```java - boolean result = containsAll("foo bar", new String[]{"FOO", "bar"}); - // result = true - - boolean result = containsAll("foo bar", new String[]{"FOO", "bar"}, true); - // result = false +import static strman.containsAll +containsAll("foo bar", new String[]{"foo", "bar"}) +// result => true + +containsAll("foo bar", new String[]{"FOO", "bar"},false) +// result => true ``` -## containsAny(value, needles, caseSensitive) - +## containsAny + Verifies that one or more of needles are contained in value. ```java -String title = "foo bar"; -String needles = ["foo", "bar", "test"]; -String result = containsAny(title, needles, true); -// result = true +import static strman.containsAny +containsAny("bar foo", new String[]{"FOO", "BAR", "Test"}, true) +// result => true ``` -## Other functions that can be added +## countSubstr -1. head -2. tail \ No newline at end of file +Count the number of times substr appears in value + +```java +import static strman.countSubstr +countSubstr("aaaAAAaaa", "aaa") +// result => 2 +countSubstr("aaaAAAaaa", "aaa", false, false) +// result => 3 +``` + +## endsWith + +Test if value ends with search. + +```java +import static strman.endsWith +endsWith("foo bar", "bar") +// result => true +endsWith("foo Bar", "BAR", false) +// result => true +``` + +## ensureLeft + +Ensures that the value begins with prefix. If it doesn't exist, it's prepended. + +```java +import static strman.ensureLeft +ensureLeft("foobar", "foo") +// result => "foobar" +ensureLeft("bar", "foo") +// result => "foobar" +ensureLeft("foobar", "FOO", false) +// result => "foobar" +``` + +## base64Decode + +Decodes data encoded with MIME base64 + +```java +import static strman.base64Decode +base64Decode("c3RybWFu") +// result => "strma" +``` + +## base64Encode + +Encodes data with MIME base64. + +```java +import static strman.base64Encode +base64Encode("strman") +// result => "c3RybWFu" +``` + +## binDecode + +Convert binary unicode (16 digits) string to string chars + +```java +import static strman.binDecode +binDecode("0000000001000001") +// result => "A" +``` + +## binEncode + +Convert string chars to binary unicode (16 digits) + +```java +import static strman.binEncode +binEncode("A") +// result => "0000000001000001" +``` + +## decDecode + +Convert decimal unicode (5 digits) string to string chars + +```java +import static strman.decDecode +decDecode("00065") +// result => "A" +``` + +## decEncode + +Convert string chars to decimal unicode (5 digits) + +```java +import static strman.decEncode +decEncode("A") +// result => "00065" +``` + +## ensureRight + +Ensures that the value ends with suffix. If it doesn't, it's appended. + +```java +import static strman.ensureRight +ensureRight("foo", "bar") +// result => "foobar" + +ensureRight("foobar", "bar") +// result => "foobar" + +ensureRight("fooBAR", "bar", false) +// result => "foobar" +``` + +## first + +Returns the first n chars of String + +```java +import static strman.first +first("foobar", 3) +// result => "foo" +``` + +## head + +Return the first char of String + +```java +import static strman.head +head("foobar") +// result => "f" +``` + +## hexDecode + +Convert hexadecimal unicode (4 digits) string to string chars + +```java +import static strman.hexDecode +hexDecode("0041") +// result => "A" +``` + +## hexEncode + +Convert string chars to hexadecimal unicode (4 digits) + +```java +import static strman.hexEncode +hexEncode("A") +// result => "0041" +``` + +## inequal + +Tests if two Strings are inequal + +```java +import static strman.inequal +inequal("a", "b") +// result => true +``` + +## insert + +Inserts 'substr' into the 'value' at the 'index' provided. + +```java +import static strman.insert +insert("fbar", "oo", 1) +// result => "foobar" +``` + +## last + +Return the last n chars of String + +```java +import static strman.last +last("foobarfoo", 3) +// result => "foo" +``` + +## leftPad + +Returns a new string of a given length such that the beginning of the string is padded. + +```java +import static strman.leftPad +leftPad("1", "0", 5) +// result => "00001" +``` + +## lastIndexOf + +This method returns the index within the calling String object of the last occurrence of the specified value, searching backwards from the offset. + +```java +import static strman.lastIndexOf +lastIndexOf("foobarfoobar", "F", false) +// result => 6 +``` + +## leftTrim + +Removes all spaces on left + +```java +import static strman.leftTrim +leftTrim(" strman") +// result => "strman" +``` + +## prepend + +Return a new String starting with prepends + +```java +prepend("r", "f", "o", "o", "b", "a") +// "foobar" +``` + +## removeEmptyStrings + +Remove empty Strings from string array + +```java +removeEmptyStrings(new String[]{"aa", "", " ", "bb", "cc", null}) +// result => ["aa", "bb", "cc"] +``` + +## removeLeft + +Returns a new String with the prefix removed, if present. + +```java +removeLeft("foofoo", "foo") +// "foo" +``` + +## removeNonWords + +Remove all non word characters. + +```java +removeNonWords("foo&bar-") +// result => "foobar" +``` + +## removeRight + +Returns a new string with the 'suffix' removed, if present. + +```java +removeRight("foobar", "bar") +// result => "foo" +removeRight("foobar", "BAR",false) +// result => "foo" +``` + +## removeSpaces + +Remove all spaces and replace for value. + +```java +removeSpaces("foo bar") +// result => "foobar" +``` + +## repeat + +Returns a repeated string given a multiplier. + +``` +repeat("1", 3) +// result => "111" +``` + +## reverse + +Reverse the input String + +```java +reverse("foo") +// result => "oof" +``` + +## rightPad + +Returns a new string of a given length such that the ending of the string is padded. + +```java +rightPad("1", "0", 5) +// result => "10000" +``` + +## rightTrim + +Remove all spaces on right. + +```java +rightTrim("strman ") +// result => "strman" +``` + +## safeTruncate + +Truncate the string securely, not cutting a word in half. It always returns the last full word. + +```java +safeTruncate("foo bar", 4, ".") +// result => "foo." +safeTruncate("A Javascript string manipulation library.", 16, "...") +// result => "A Javascript..." +``` + +## truncate + +Truncate the unsecured form string, cutting the independent string of required position. + +```java +truncate("A Javascript string manipulation library.", 14, "...") +// result => "A Javascrip..." +``` + +## htmlDecode + +Converts all HTML entities to applicable characters. + +```java +htmlDecode("Ш") +// result => Ш +``` + +## htmlEncode + +Convert all applicable characters to HTML entities. + +```java +htmlEncode("Ш") +// result => "Ш" +``` + +## shuffle + +It returns a string with its characters in random order. + +```java +shuffle("shekhar") +``` + +## slugify + +Convert a String to a slug + +```java +slugify("foo bar") +// result => "foo-bar" +``` + +## transliterate + +Remove all non valid characters. Example: change á => a or ẽ => e. + +```java +transliterate("fóõ bár") +// result => "foo bar" +``` + +## surround + +Surrounds a 'value' with the given 'prefix' and 'suffix'. + +```java +surround("div", "<", ">" +// result => "
s" +``` + +## tail + +```java +tail("foobar") +// result => "oobar" +``` + +## toCamelCase + +Transform to camelCase + +```java +toCamelCase("CamelCase") +// result => "camelCase" +toCamelCase("camel-case") +// result => "camelCase" +``` + +## toStudlyCase + +Transform to StudlyCaps. + +```java +toStudlyCase("hello world") +// result => "HelloWorld" +``` + +## toDecamelize + +Decamelize String + +```java +toDecamelize("helloWorld",null) +// result => "hello world" +``` + +## toKebabCase + +Transform to kebab-case. + +```java +toKebabCase("hello World") +// result => "hello-world" +``` + +## toSnakeCase + +Transform to snake_case. + +```java +toSnakeCase("hello world") +// result => "hello_world" +``` + +License +------- +strman is licensed under the MIT License - see the `LICENSE` file for details. \ No newline at end of file diff --git a/build.gradle b/build.gradle index 023435e..0568b64 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.shekhargulati' -version '1.0-SNAPSHOT' +version '0.1.0-SNAPSHOT' apply plugin: 'java' diff --git a/src/main/java/strman/Ascii.java b/src/main/java/strman/Ascii.java index 52b4a8e..5e57063 100644 --- a/src/main/java/strman/Ascii.java +++ b/src/main/java/strman/Ascii.java @@ -1,3 +1,29 @@ +/* + * + * * The MIT License + * * + * * Copyright 2016 Shekhar Gulati . + * * + * * Permission is hereby granted, free of charge, to any person obtaining a copy + * * of this software and associated documentation files (the "Software"), to deal + * * in the Software without restriction, including without limitation the rights + * * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * * copies of the Software, and to permit persons to whom the Software is + * * furnished to do so, subject to the following conditions: + * * + * * The above copyright notice and this permission notice shall be included in + * * all copies or substantial portions of the Software. + * * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * * THE SOFTWARE. + * + */ + package strman; import java.util.Arrays; diff --git a/src/main/java/strman/HtmlEntities.java b/src/main/java/strman/HtmlEntities.java index 9d23fc9..e467e5c 100644 --- a/src/main/java/strman/HtmlEntities.java +++ b/src/main/java/strman/HtmlEntities.java @@ -1,9 +1,36 @@ + +/* + * + * * The MIT License + * * + * * Copyright 2016 Shekhar Gulati . + * * + * * Permission is hereby granted, free of charge, to any person obtaining a copy + * * of this software and associated documentation files (the "Software"), to deal + * * in the Software without restriction, including without limitation the rights + * * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * * copies of the Software, and to permit persons to whom the Software is + * * furnished to do so, subject to the following conditions: + * * + * * The above copyright notice and this permission notice shall be included in + * * all copies or substantial portions of the Software. + * * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * * THE SOFTWARE. + * + */ + package strman; import java.util.HashMap; import java.util.Map; -public abstract class HtmlEntities { +abstract class HtmlEntities { public static final Map decodedEntities = new HashMap() { { put("Æ", "\u00C6"); diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 701c377..5c484df 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -1,3 +1,30 @@ + +/* + * + * * The MIT License + * * + * * Copyright 2016 Shekhar Gulati . + * * + * * Permission is hereby granted, free of charge, to any person obtaining a copy + * * of this software and associated documentation files (the "Software"), to deal + * * in the Software without restriction, including without limitation the rights + * * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * * copies of the Software, and to permit persons to whom the Software is + * * furnished to do so, subject to the following conditions: + * * + * * The above copyright notice and this permission notice shall be included in + * * all copies or substantial portions of the Software. + * * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * * THE SOFTWARE. + * + */ + package strman; import java.util.*; @@ -149,7 +176,7 @@ public abstract class Strman { */ public static boolean containsAll(final String value, final String[] needles) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); - return Arrays.stream(needles).allMatch(needle -> contains(value, needle, false)); + return Arrays.stream(needles).allMatch(needle -> contains(value, needle, true)); } /** @@ -197,7 +224,7 @@ public abstract class Strman { * @return count of times substring exists */ public static long countSubstr(final String value, final String subStr) { - return countSubstr(value, subStr, false, false); + return countSubstr(value, subStr, true, false); } /** diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index e9342f6..f7b1df9 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -1,5 +1,30 @@ -package strman; +/* + * + * * The MIT License + * * + * * Copyright 2016 Shekhar Gulati . + * * + * * Permission is hereby granted, free of charge, to any person obtaining a copy + * * of this software and associated documentation files (the "Software"), to deal + * * in the Software without restriction, including without limitation the rights + * * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * * copies of the Software, and to permit persons to whom the Software is + * * furnished to do so, subject to the following conditions: + * * + * * The above copyright notice and this permission notice shall be included in + * * all copies or substantial portions of the Software. + * * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * * THE SOFTWARE. + * + */ +package strman; import org.junit.Test; import java.util.Arrays; @@ -118,7 +143,7 @@ public class StrmanTest { "foobar", }; - Arrays.stream(fixture).forEach(el -> assertTrue(containsAll(el, new String[]{"FOO", "bar"}))); + Arrays.stream(fixture).forEach(el -> assertTrue(containsAll(el, new String[]{"foo", "bar"}))); } @Test @@ -153,12 +178,12 @@ public class StrmanTest { @Test public void countSubstr_shouldCountSubStrCountCaseInsensitiveWithoutOverlapInValue() throws Exception { - assertThat(countSubstr("aaaAAAaaa", "aaa"), equalTo(3L)); + assertThat(countSubstr("aaaAAAaaa", "aaa", false, false), equalTo(3L)); } @Test public void countSubstr_shouldCountSubStrCountCaseSensitiveWithoutOverlapInValue() throws Exception { - assertThat(countSubstr("aaaAAAaaa", "aaa", true, false), equalTo(2L)); + assertThat(countSubstr("aaaAAAaaa", "aaa"), equalTo(2L)); } @Test @@ -190,7 +215,7 @@ public class StrmanTest { "aaaaaaaf", "aaafaaaa" }; - Arrays.stream(fixture).forEach(el -> assertThat(countSubstr(el, "A"), equalTo(7L))); + Arrays.stream(fixture).forEach(el -> assertThat(countSubstr(el, "A", false, false), equalTo(7L))); } @Test