Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(clojure): add implode and explode #8

Merged
merged 6 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/pr_clojure.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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: ./csharp
elianiva marked this conversation as resolved.
Show resolved Hide resolved
timeout-minutes: 5
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: lein deps
- name: Run tests
run: lein cloverage --codecov
- 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 empty"))
elianiva marked this conversation as resolved.
Show resolved Hide resolved
(nil? content) (throw (IllegalArgumentException. "content cannot be empty"))
elianiva marked this conversation as resolved.
Show resolved Hide resolved
:else (str/split content (re-pattern separator))))
([separator content limit]
(cond
(or (nil? separator)
(= separator "")) (throw (IllegalArgumentException. "separator cannot be empty"))
elianiva marked this conversation as resolved.
Show resolved Hide resolved
(nil? content) (throw (IllegalArgumentException. "content cannot be empty"))
elianiva marked this conversation as resolved.
Show resolved Hide resolved
(> 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))
47 changes: 47 additions & 0 deletions clojure/test/pehape/string_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(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 Throws Exception"
#_{:clj-kondo/ignore [:inline-def]}
(def content "Hello pehape world")
(is (thrown-with-msg? IllegalArgumentException #"separator cannot be empty" (explode "" content)))))


(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) ""))))