From d92d30392c9cb877e4ec3faff7b07de39e880ca2 Mon Sep 17 00:00:00 2001
From: Matheus Horstmann <mch15@inf.ufpr.br>
Date: Tue, 5 Feb 2019 14:55:13 -0200
Subject: [PATCH] Issue #4: Create EnumHandler and InputType

Signed-off-by: Matheus Horstmann <mch15@inf.ufpr.br>
---
 .dockerignore                 |  1 +
 .gitignore                    |  2 ++
 CHANGELOG.md                  |  5 +++
 package.json                  |  2 +-
 src/utils/enumHandler.spec.ts | 46 +++++++++++++++++++++++++
 src/utils/enumHandler.ts      | 63 +++++++++++++++++++++++++++++++++++
 6 files changed, 118 insertions(+), 1 deletion(-)
 create mode 100644 src/utils/enumHandler.spec.ts
 create mode 100644 src/utils/enumHandler.ts

diff --git a/.dockerignore b/.dockerignore
index c625663..1d8309a 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -7,3 +7,4 @@ Dockerfile
 package-lock.json
 node_modules
 *.bak
+.editorconfig
diff --git a/.gitignore b/.gitignore
index 6e5cd7f..73ebd01 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,5 @@
 /dist
 *.bak
 .env
+docker-compose.yaml
+.editorconfig
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bc0fd63..0bc14ae 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## 0.0.2 - 05-02-2019
+### Added
+- EnunHandler to handle types of inputs #4 (Horstmann)
+
+
 ## 0.0.1 - 04-02-2019
 ### Added
 - This CHANGELOG file to hopefully serve as an evolving example of a  standardized open source project CHANGELOG.
diff --git a/package.json b/package.json
index f20ba8a..e2ae362 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "form-creator-api",
-  "version": "0.0.1",
+  "version": "0.0.2",
   "description": "RESTful API used to manage and answer forms.",
   "main": "index.js",
   "scripts": {
diff --git a/src/utils/enumHandler.spec.ts b/src/utils/enumHandler.spec.ts
new file mode 100644
index 0000000..d861f6b
--- /dev/null
+++ b/src/utils/enumHandler.spec.ts
@@ -0,0 +1,46 @@
+/*
+ * form-creator-api. RESTful API to manage and answer forms.
+ * Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of form-creator-api.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+import { expect } from "chai";
+import { EnumHandler, InputType } from "./enumHandler";
+
+describe("Enum Handler", () => {
+
+    it("should stringfy input ", () => {
+
+        const inputNone = EnumHandler.stringfyInputType(InputType.NONE);
+        const inputText = EnumHandler.stringfyInputType(InputType.TEXT);
+        expect(inputNone).to.be.equal("");
+        expect(inputText).to.be.equal("text");
+    });
+
+    it("should parse string to InputType", () => {
+        const inputText = EnumHandler.parseInputType("text");
+        const inputTextCapitalLetters = EnumHandler.parseInputType("TEXT");
+        const inputNone = EnumHandler.parseInputType("");
+        const inputFOOL = EnumHandler.parseInputType("fool");
+        expect(inputText).to.be.equal(InputType.TEXT);
+        expect(inputTextCapitalLetters).to.be.equal(InputType.TEXT);
+        expect(inputNone).to.be.equal(InputType.NONE);
+        expect(inputFOOL).to.be.equal(InputType.NONE);
+    });
+
+});
diff --git a/src/utils/enumHandler.ts b/src/utils/enumHandler.ts
new file mode 100644
index 0000000..3e35941
--- /dev/null
+++ b/src/utils/enumHandler.ts
@@ -0,0 +1,63 @@
+/*
+ * form-creator-api. RESTful API to manage and answer forms.
+ * Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
+ * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+ *
+ * This file is part of form-creator-api.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
+/**
+ * Available Input types
+ */
+export enum InputType {
+     /** Used as error code. No suitable input found. */
+     NONE,
+     /** Text type, when input is a text */
+     TEXT
+}
+
+/**
+ * Enum's handler. Manage parse through the project.
+ */
+
+export class EnumHandler {
+    /**
+     * Parse an enum(Input type) to string.
+     * @param a - Input type to be stringified.
+     */
+    public static stringfyInputType(a: InputType): string {
+        switch (a) {
+            case InputType.TEXT:
+                return "text";
+            default:
+                return  "";
+        }
+    }
+    /**
+     * Parse a string to enum(InputType).
+     * @param str - InputType in string format.
+     */
+    public static parseInputType(str: string): InputType {
+        str = str.toLocaleLowerCase();
+        switch (str) {
+            case "text":
+                return InputType.TEXT;
+            default:
+                return InputType.NONE;
+        }
+    }
+
+ }
-- 
GitLab