Skip to content

Commit

Permalink
Merge pull request #8 from teknologi-umum/clojure/explode-implode
Browse files Browse the repository at this point in the history
feat(clojure): add implode and explode
  • Loading branch information
ronnygunawan authored Mar 19, 2022
2 parents 1b6d576 + 8bcb764 commit 66e141b
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/pr_clojure.yml
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 }}
27 changes: 27 additions & 0 deletions clojure/.gitignore
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/
5 changes: 5 additions & 0 deletions clojure/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"betterthantomorrow.calva"
]
}
3 changes: 3 additions & 0 deletions clojure/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"calva.useTestExplorer": true
}
9 changes: 9 additions & 0 deletions clojure/project.clj
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])
1 change: 1 addition & 0 deletions clojure/src/pehape/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns pehape.core)
45 changes: 45 additions & 0 deletions clojure/src/pehape/string.clj
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))
53 changes: 53 additions & 0 deletions clojure/test/pehape/string_test.clj
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) ""))))

0 comments on commit 66e141b

Please sign in to comment.