-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from teknologi-umum/clojure/explode-implode
feat(clojure): add implode and explode
- Loading branch information
Showing
8 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Clojure PR Check | ||
|
||
on: | ||
push: | ||
branches: ["master"] | ||
paths: ["clojure/**"] | ||
pull_request: | ||
branches: ["master"] | ||
paths: ["clojure/**"] | ||
|
||
jobs: | ||
build: | ||
name: Build and Test | ||
runs-on: ubuntu-latest | ||
|
||
env: | ||
working-directory: ./clojure | ||
timeout-minutes: 5 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install dependencies | ||
run: lein deps | ||
working-directory: ${{ env.working-directory }} | ||
- name: Run tests | ||
run: lein cloverage --codecov | ||
working-directory: ${{ env.working-directory }} | ||
- name: Report coverage | ||
uses: codecov/codecov-action@v2 | ||
with: | ||
flags: clojure | ||
working-directory: ${{ env.working-directory }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
pom.xml | ||
pom.xml.asc | ||
*.jar | ||
*.class | ||
/lib/ | ||
/classes/ | ||
/target/ | ||
/checkouts/ | ||
.lein-deps-sum | ||
.lein-repl-history | ||
.lein-plugins/ | ||
.lein-failures | ||
.nrepl-port | ||
.cpcache/ | ||
|
||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
!.vscode/*.code-snippets | ||
.calva | ||
.clj-kondo | ||
.lsp | ||
.cpcache | ||
|
||
.history/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"recommendations": [ | ||
"betterthantomorrow.calva" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"calva.useTestExplorer": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
(defproject com.teknologiumum/pehape-clj "0.0.1" | ||
:description "PHP stdlib global functions implemented in Clojure" | ||
:url "https://github.com/teknologi-umum/pehape" | ||
:plugins [[lein-cloverage "1.2.2"]] | ||
:dependencies [[org.clojure/clojure "1.10.3"]] | ||
:profiles {:dev {:dependencies [[cider/cider-nrepl "0.28.3"]]}} | ||
:target-path "target/%s" | ||
:main pehape.core | ||
:aot [pehape.core]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(ns pehape.core) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(ns pehape.string | ||
(:require [clojure.string :as str])) | ||
|
||
|
||
(defn explode | ||
"The Explode function breaks a string into an array. | ||
Arguments: | ||
- `separator` Specifies where to break the string. | ||
- `content` The string to split. | ||
- `limit` Specifies the number of array elements to return. | ||
If limit is greater than 0, then an array with a maximum of limit | ||
elements is returned. If limit is less than 0 then an array excluding | ||
the last -limit elements is returned. If limit is 0 then an array with | ||
one element is returned. | ||
Returns: | ||
- An array of strings." | ||
{:added "0.0.1"} | ||
([separator content] | ||
(cond | ||
(or (nil? separator) | ||
(= separator "")) (throw (IllegalArgumentException. "separator cannot be nil or empty")) | ||
(nil? content) (throw (IllegalArgumentException. "content cannot be nil")) | ||
:else (str/split content (re-pattern separator)))) | ||
([separator content limit] | ||
(cond | ||
(or (nil? separator) | ||
(= separator "")) (throw (IllegalArgumentException. "separator cannot be nil or empty")) | ||
(nil? content) (throw (IllegalArgumentException. "content cannot be nil")) | ||
(> limit 0) (take limit (str/split content (re-pattern separator))) | ||
(< limit 0) (drop-last (- limit) (str/split content (re-pattern separator))) | ||
:else (take 1 (str/split content (re-pattern separator)))))) | ||
|
||
|
||
(defn implode | ||
"The Implode function joins array elements with a string. | ||
Arguments: | ||
- `separator` Specifies what to put between the array elements. | ||
- `array` The array to join to a string. | ||
Returns: | ||
- A string from elements of an array" | ||
{:added "0.0.1"} | ||
[separator array] | ||
(str/join separator array)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
(ns pehape.string-test | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[pehape.string :refer [explode implode]])) | ||
|
||
|
||
(deftest explode-test | ||
(testing "Can Explode Space Separated String By Space" | ||
#_{:clj-kondo/ignore [:inline-def]} | ||
(def content "Hello pehape world") | ||
(is (= (explode " " content) ["Hello" "pehape" "world"])) | ||
(is (= (explode " " content 0) ["Hello"])) | ||
(is (= (explode " " content 1) ["Hello"])) | ||
(is (= (explode " " content 5) ["Hello" "pehape" "world"])) | ||
(is (= (explode " " content -1) ["Hello" "pehape"])) | ||
(is (= (explode " " content -3) []))) | ||
(testing "Can Explode Double Space Separated String By Space" | ||
#_{:clj-kondo/ignore [:inline-def]} | ||
(def content "Hello pehape world") | ||
(is (= (explode " " content) ["Hello" "" "pehape" "" "world"]))) | ||
(testing "Can Explode Space Separated String By Double Space" | ||
#_{:clj-kondo/ignore [:inline-def]} | ||
(def content "Hello pehape world") | ||
(is (= (explode " " content) ["Hello pehape world"]))) | ||
(testing "Explode Space Separated String By Empty String Or Nil Throws Exception" | ||
#_{:clj-kondo/ignore [:inline-def]} | ||
(def content "Hello pehape world") | ||
(is (thrown-with-msg? IllegalArgumentException #"separator cannot be nil or empty" (explode "" content))) | ||
(is (thrown-with-msg? IllegalArgumentException #"separator cannot be nil or empty" (explode nil content))) | ||
(is (thrown-with-msg? IllegalArgumentException #"separator cannot be nil or empty" (explode "" content 1))) | ||
(is (thrown-with-msg? IllegalArgumentException #"separator cannot be nil or empty" (explode nil content 1)))) | ||
(testing "Explode Space Separated Nil Content Throws Exception" | ||
(is (thrown-with-msg? IllegalArgumentException #"content cannot be nil" (explode " " nil))) | ||
(is (thrown-with-msg? IllegalArgumentException #"content cannot be nil" (explode " " nil 1))))) | ||
|
||
|
||
(deftest implode-test | ||
(testing "Can Implode Array Of String" | ||
#_{:clj-kondo/ignore [:inline-def]} | ||
(def array ["Hello" "world"]) | ||
(is (= (implode " " array) "Hello world")) | ||
(is (= (implode " " array) "Hello world")) | ||
(is (= (implode "" array) "Helloworld")) | ||
(is (= (implode nil array) "Helloworld")) | ||
(is (= (implode " wkwkwk " array) "Hello wkwkwk world")) | ||
(is (= (implode "\n" array) "Hello\nworld"))) | ||
(testing "Can Implode Array Of Int" | ||
#_{:clj-kondo/ignore [:inline-def]} | ||
(def array [1 2 3 4 5]) | ||
(is (= (implode ":. " array) "1:. 2:. 3:. 4:. 5"))) | ||
(testing "Can Implode Empty Array" | ||
#_{:clj-kondo/ignore [:inline-def]} | ||
(def array []) | ||
(is (= (implode " " array) "")))) |