diff --git a/src/routes/api/array/reverseArray.js b/src/routes/api/array/reverseArray.js new file mode 100644 index 0000000..86b1c72 --- /dev/null +++ b/src/routes/api/array/reverseArray.js @@ -0,0 +1,14 @@ +const express = require('express'); +const router = express.Router(); + +router.post('/', (req, res) => { + const { input } = req.body; + + if (!Array.isArray(input)) { + return res.status(400).json({ error: 'input must be an array' }); + } + + res.json({ ret: [...input].reverse() }); +}); + +module.exports = router; diff --git a/src/routes/api/sort.js b/src/routes/api/array/sort.js similarity index 100% rename from src/routes/api/sort.js rename to src/routes/api/array/sort.js diff --git a/src/routes/api/isEven.js b/src/routes/api/checks/isEven.js similarity index 93% rename from src/routes/api/isEven.js rename to src/routes/api/checks/isEven.js index d185580..40e6957 100644 --- a/src/routes/api/isEven.js +++ b/src/routes/api/checks/isEven.js @@ -4,14 +4,12 @@ const router = express.Router(); router.post('/', (req, res) => { const { input } = req.body; - res.json({ ret: isEven(Number(input)) }); }); router.get('/:number', (req, res) => { const input = req.params.number; - res.json({ ret: isEven(Number(input)) }); }); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/src/routes/api/isNumber.js b/src/routes/api/checks/isNumber.js similarity index 75% rename from src/routes/api/isNumber.js rename to src/routes/api/checks/isNumber.js index feadaee..2927b89 100644 --- a/src/routes/api/isNumber.js +++ b/src/routes/api/checks/isNumber.js @@ -3,14 +3,12 @@ const router = express.Router(); router.post('/', (req, res) => { const { input } = req.body; - - res.json({ ret: ! isNaN(input) }); + res.json({ ret: !isNaN(input) }); }); router.get('/:number', (req, res) => { const input = req.params.number; - - res.json({ ret: ! isNaN(input) }); + res.json({ ret: !isNaN(input) }); }); module.exports = router; diff --git a/src/routes/api/isOdd.js b/src/routes/api/checks/isOdd.js similarity index 99% rename from src/routes/api/isOdd.js rename to src/routes/api/checks/isOdd.js index fbc6eec..c5d8e9b 100644 --- a/src/routes/api/isOdd.js +++ b/src/routes/api/checks/isOdd.js @@ -4,13 +4,11 @@ const router = express.Router(); router.post('/', (req, res) => { const { input } = req.body; - res.json({ ret: isOdd(Number(input)) }); }); router.get('/:number', (req, res) => { const input = req.params.number; - res.json({ ret: isOdd(Number(input)) }); }); diff --git a/src/routes/api/index.js b/src/routes/api/index.js new file mode 100644 index 0000000..bb8aa7c --- /dev/null +++ b/src/routes/api/index.js @@ -0,0 +1,37 @@ +const express = require('express'); +const router = express.Router(); + +// Math +router.use('/add', require('./math/add')); +router.use('/subtract', require('./math/subtract')); +router.use('/multiply', require('./math/multiply')); +router.use('/divide', require('./math/divide')); +router.use('/modulo', require('./math/modulo')); + +// String +router.use('/toString', require('./string/toString')); +router.use('/stringSplit', require('./string/stringSplit')); +router.use('/reverseString', require('./string/reverseString')); +router.use('/isPalindrome', require('./string/isPalindrome')); +router.use('/stringLength', require('./string/stringLength')); +router.use('/countWords', require('./string/countWords')); +router.use('/toUpperCase', require('./string/toUpperCase')); +router.use('/toLowerCase', require('./string/toLowerCase')); + +// Array +router.use('/sort', require('./array/sort')); +router.use('/reverseArray', require('./array/reverseArray')); + +// Checks +router.use('/isEven', require('./checks/isEven')); +router.use('/isOdd', require('./checks/isOdd')); +router.use('/isNumber', require('./checks/isNumber')); + +// Meme sorts +router.use('/stalinSort', require('./sorts/stalinSort')); +router.use('/bogoSort', require('./sorts/bogoSort')); +router.use('/thanoSort', require('./sorts/thanoSort')); +router.use('/sleepSort', require('./sorts/sleepSort')); +router.use('/miracleSort', require('./sorts/miracleSort')); + +module.exports = router; diff --git a/src/routes/api/add.js b/src/routes/api/math/add.js similarity index 92% rename from src/routes/api/add.js rename to src/routes/api/math/add.js index f606162..c239157 100644 --- a/src/routes/api/add.js +++ b/src/routes/api/math/add.js @@ -3,7 +3,6 @@ const router = express.Router(); router.post('/', (req, res) => { const { input } = req.body; - res.json({ ret: input.a + input.b }); }); @@ -11,4 +10,4 @@ router.get('/:a/:b', (req, res) => { res.json({ ret: Number(req.params.a) + Number(req.params.b) }); }); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/src/routes/api/divide.js b/src/routes/api/math/divide.js similarity index 99% rename from src/routes/api/divide.js rename to src/routes/api/math/divide.js index 104d766..3c2baca 100644 --- a/src/routes/api/divide.js +++ b/src/routes/api/math/divide.js @@ -11,4 +11,3 @@ router.get('/:a/:b', (req, res) => { }); module.exports = router; - diff --git a/src/routes/api/math/modulo.js b/src/routes/api/math/modulo.js new file mode 100644 index 0000000..147ba88 --- /dev/null +++ b/src/routes/api/math/modulo.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); + +router.post('/', (req, res) => { + const { input } = req.body; + res.json({ ret: input.a % input.b }); +}); + +router.get('/:a/:b', (req, res) => { + res.json({ ret: Number(req.params.a) % Number(req.params.b) }); +}); + +module.exports = router; diff --git a/src/routes/api/multiply.js b/src/routes/api/math/multiply.js similarity index 99% rename from src/routes/api/multiply.js rename to src/routes/api/math/multiply.js index bed6c32..28228d1 100644 --- a/src/routes/api/multiply.js +++ b/src/routes/api/math/multiply.js @@ -11,4 +11,3 @@ router.get('/:a/:b', (req, res) => { }); module.exports = router; - diff --git a/src/routes/api/subtract.js b/src/routes/api/math/subtract.js similarity index 99% rename from src/routes/api/subtract.js rename to src/routes/api/math/subtract.js index aec90d0..bdeaba1 100644 --- a/src/routes/api/subtract.js +++ b/src/routes/api/math/subtract.js @@ -11,4 +11,3 @@ router.get('/:a/:b', (req, res) => { }); module.exports = router; - diff --git a/src/routes/api/sorts/bogoSort.js b/src/routes/api/sorts/bogoSort.js new file mode 100644 index 0000000..0d6d72d --- /dev/null +++ b/src/routes/api/sorts/bogoSort.js @@ -0,0 +1,43 @@ +const express = require('express'); +const router = express.Router(); + +function isSorted(arr) { + for (let i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) return false; + } + return true; +} + +function shuffle(arr) { + const a = [...arr]; + for (let i = a.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [a[i], a[j]] = [a[j], a[i]]; + } + return a; +} + +router.post('/', (req, res) => { + const { input } = req.body; + + if (!Array.isArray(input)) { + return res.status(400).json({ error: 'input must be an array' }); + } + + const MAX_ATTEMPTS = 1000; + let arr = [...input]; + let attempts = 0; + + while (!isSorted(arr) && attempts < MAX_ATTEMPTS) { + arr = shuffle(arr); + attempts++; + } + + res.json({ + ret: arr, + attempts, + success: isSorted(arr) + }); +}); + +module.exports = router; diff --git a/src/routes/api/sorts/miracleSort.js b/src/routes/api/sorts/miracleSort.js new file mode 100644 index 0000000..7140926 --- /dev/null +++ b/src/routes/api/sorts/miracleSort.js @@ -0,0 +1,25 @@ +const express = require('express'); +const router = express.Router(); + +function isSorted(arr) { + for (let i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) return false; + } + return true; +} + +router.post('/', (req, res) => { + const { input } = req.body; + + if (!Array.isArray(input)) { + return res.status(400).json({ error: 'input must be an array' }); + } + + if (isSorted(input)) { + res.json({ ret: input, miracle: true, message: 'A miracle has occurred!' }); + } else { + res.json({ ret: input, miracle: false, message: 'Waiting for cosmic miracle... Please try again later.' }); + } +}); + +module.exports = router; diff --git a/src/routes/api/sorts/sleepSort.js b/src/routes/api/sorts/sleepSort.js new file mode 100644 index 0000000..6f5fe57 --- /dev/null +++ b/src/routes/api/sorts/sleepSort.js @@ -0,0 +1,21 @@ +const express = require('express'); +const router = express.Router(); + +router.post('/', (req, res) => { + const { input } = req.body; + + if (!Array.isArray(input)) { + return res.status(400).json({ error: 'input must be an array' }); + } + + const sorted = [...input].sort((a, b) => a - b); + const totalSleepMs = input.reduce((sum, n) => sum + Math.abs(Number(n)), 0); + + res.json({ + ret: sorted, + totalSleepTimeMs: totalSleepMs, + note: "We simulated the sleep. You're welcome." + }); +}); + +module.exports = router; diff --git a/src/routes/api/stalinSort.js b/src/routes/api/sorts/stalinSort.js similarity index 100% rename from src/routes/api/stalinSort.js rename to src/routes/api/sorts/stalinSort.js diff --git a/src/routes/api/sorts/thanoSort.js b/src/routes/api/sorts/thanoSort.js new file mode 100644 index 0000000..2e2d90e --- /dev/null +++ b/src/routes/api/sorts/thanoSort.js @@ -0,0 +1,38 @@ +const express = require('express'); +const router = express.Router(); + +function isSorted(arr) { + for (let i = 1; i < arr.length; i++) { + if (arr[i] < arr[i - 1]) return false; + } + return true; +} + +router.post('/', (req, res) => { + const { input } = req.body; + + if (!Array.isArray(input)) { + return res.status(400).json({ error: 'input must be an array' }); + } + + let arr = [...input]; + let snaps = 0; + + while (!isSorted(arr) && arr.length > 1) { + const survivors = Math.ceil(arr.length / 2); + const indices = new Set(); + while (indices.size < survivors) { + indices.add(Math.floor(Math.random() * arr.length)); + } + arr = arr.filter((_, i) => indices.has(i)); + snaps++; + } + + res.json({ + ret: arr, + snaps, + perfectly_balanced: isSorted(arr) + }); +}); + +module.exports = router; diff --git a/src/routes/api/string/countWords.js b/src/routes/api/string/countWords.js new file mode 100644 index 0000000..7db4b63 --- /dev/null +++ b/src/routes/api/string/countWords.js @@ -0,0 +1,17 @@ +const express = require('express'); +const router = express.Router(); + +function countWords(str) { + return String(str).trim().split(/\s+/).filter(w => w.length > 0).length; +} + +router.post('/', (req, res) => { + const { input } = req.body; + res.json({ ret: countWords(input) }); +}); + +router.get('/:string', (req, res) => { + res.json({ ret: countWords(req.params.string) }); +}); + +module.exports = router; diff --git a/src/routes/api/string/isPalindrome.js b/src/routes/api/string/isPalindrome.js new file mode 100644 index 0000000..2b155d7 --- /dev/null +++ b/src/routes/api/string/isPalindrome.js @@ -0,0 +1,18 @@ +const express = require('express'); +const router = express.Router(); + +function checkPalindrome(str) { + const clean = String(str).toLowerCase().replace(/[^a-z0-9]/g, ''); + return clean === clean.split('').reverse().join(''); +} + +router.post('/', (req, res) => { + const { input } = req.body; + res.json({ ret: checkPalindrome(input) }); +}); + +router.get('/:string', (req, res) => { + res.json({ ret: checkPalindrome(req.params.string) }); +}); + +module.exports = router; diff --git a/src/routes/api/string/reverseString.js b/src/routes/api/string/reverseString.js new file mode 100644 index 0000000..3d2bf74 --- /dev/null +++ b/src/routes/api/string/reverseString.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); + +router.post('/', (req, res) => { + const { input } = req.body; + res.json({ ret: String(input).split('').reverse().join('') }); +}); + +router.get('/:string', (req, res) => { + res.json({ ret: req.params.string.split('').reverse().join('') }); +}); + +module.exports = router; diff --git a/src/routes/api/string/stringLength.js b/src/routes/api/string/stringLength.js new file mode 100644 index 0000000..7a7eae8 --- /dev/null +++ b/src/routes/api/string/stringLength.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); + +router.post('/', (req, res) => { + const { input } = req.body; + res.json({ ret: String(input).length }); +}); + +router.get('/:string', (req, res) => { + res.json({ ret: req.params.string.length }); +}); + +module.exports = router; diff --git a/src/routes/api/stringSplit.js b/src/routes/api/string/stringSplit.js similarity index 99% rename from src/routes/api/stringSplit.js rename to src/routes/api/string/stringSplit.js index 6eceebe..b53dbb4 100644 --- a/src/routes/api/stringSplit.js +++ b/src/routes/api/string/stringSplit.js @@ -3,7 +3,6 @@ const router = express.Router(); router.post('/', (req, res) => { const { input } = req.body; - res.json({ ret: input.string.toString().split(input.seperator) }); }); diff --git a/src/routes/api/string/toLowerCase.js b/src/routes/api/string/toLowerCase.js new file mode 100644 index 0000000..61cc5df --- /dev/null +++ b/src/routes/api/string/toLowerCase.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); + +router.post('/', (req, res) => { + const { input } = req.body; + res.json({ ret: String(input).toLowerCase() }); +}); + +router.get('/:string', (req, res) => { + res.json({ ret: req.params.string.toLowerCase() }); +}); + +module.exports = router; diff --git a/src/routes/api/toString.js b/src/routes/api/string/toString.js similarity index 99% rename from src/routes/api/toString.js rename to src/routes/api/string/toString.js index 71be48e..c4e0666 100644 --- a/src/routes/api/toString.js +++ b/src/routes/api/string/toString.js @@ -3,7 +3,6 @@ const router = express.Router(); router.post('/', (req, res) => { const { input } = req.body; - res.json({ ret: input.toString() }); }); diff --git a/src/routes/api/string/toUpperCase.js b/src/routes/api/string/toUpperCase.js new file mode 100644 index 0000000..58390ae --- /dev/null +++ b/src/routes/api/string/toUpperCase.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); + +router.post('/', (req, res) => { + const { input } = req.body; + res.json({ ret: String(input).toUpperCase() }); +}); + +router.get('/:string', (req, res) => { + res.json({ ret: req.params.string.toUpperCase() }); +}); + +module.exports = router; diff --git a/src/routes/docs/openapi.json b/src/routes/docs/openapi.json index e074948..18db2bd 100644 --- a/src/routes/docs/openapi.json +++ b/src/routes/docs/openapi.json @@ -15,281 +15,17 @@ "description": "Local server" } ], + "tags": [ + { "name": "Math", "description": "Basic arithmetic operations" }, + { "name": "String", "description": "String manipulation" }, + { "name": "Array", "description": "Array operations" }, + { "name": "Checks", "description": "Value checking" }, + { "name": "Sorts", "description": "Sorting algorithms (serious and meme)" } + ], "paths": { - "/sort": { - "post": { - "summary": "Sorts an array", - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "input": { - "type": "array", - "items": {}, - "example": [3, 1, 4, 1, 5, 9] - } - } - } - } - } - }, - "responses": { - "200": { - "description": "Sorted array", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "ret": { - "type": "array", - "items": {} - } - } - } - } - } - } - } - } - }, - "/stalinSort": { - "post": { - "summary": "Sorts an array using Stalin Sort", - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "input": { - "type": "array", - "items": {}, - "example": [3, 1, 4, 1, 5, 9] - } - } - } - } - } - }, - "responses": { - "200": { - "description": "Sorted array and execution count", - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "ret": { - "type": "array", - "items": {} - }, - "executions": { - "type": "number" - } - } - } - } - } - } - } - } - }, - "/isEven": { - "post": { - "summary": "Checks if number is even", - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "input": { - "type": "number" - } - } - } - } - } - }, - "responses": { - "200": { - "description": "Success" - } - } - } - }, - "/isEven/{number}": { - "get": { - "summary": "Checks if number is even", - "parameters": [ - { - "name": "number", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "Success" - } - } - } - }, - "/isOdd": { - "post": { - "summary": "Checks if number is odd", - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "input": { - "type": "number" - } - } - } - } - } - }, - "responses": { - "200": { - "description": "Success" - } - } - } - }, - "/isOdd/{number}": { - "get": { - "summary": "Checks if number is odd", - "parameters": [ - { - "name": "number", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "Success" - } - } - } - }, - "/isNumber": { - "post": { - "summary": "Checks if input is a number", - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "input": {} - } - } - } - } - }, - "responses": { - "200": { - "description": "Success" - } - } - } - }, - "/isNumber/{number}": { - "get": { - "summary": "Checks if param is a number", - "parameters": [ - { - "name": "number", - "in": "path", - "required": true, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "Success" - } - } - } - }, - "/toString": { - "post": { - "summary": "Converts input to string", - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "input": {} - } - } - } - } - }, - "responses": { - "200": { - "description": "Success" - } - } - } - }, - "/stringSplit": { - "post": { - "summary": "Splits a string", - "requestBody": { - "required": true, - "content": { - "application/json": { - "schema": { - "type": "object", - "properties": { - "input": { - "type": "object", - "properties": { - "string": { - "type": "string" - }, - "seperator": { - "type": "string" - } - } - } - } - } - } - } - }, - "responses": { - "200": { - "description": "Success" - } - } - } - }, "/add": { "post": { + "tags": ["Math"], "summary": "Adds two numbers", "requestBody": { "required": true, @@ -301,14 +37,8 @@ "input": { "type": "object", "properties": { - "a": { - "type": "number", - "example": 5 - }, - "b": { - "type": "number", - "example": 10 - } + "a": { "type": "number", "example": 5 }, + "b": { "type": "number", "example": 10 } } } } @@ -316,45 +46,23 @@ } } }, - "responses": { - "200": { - "description": "Success" - } - } + "responses": { "200": { "description": "Success" } } } }, "/add/{a}/{b}": { "get": { + "tags": ["Math"], "summary": "Adds two numbers from path parameters", "parameters": [ - { - "name": "a", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "5" - }, - { - "name": "b", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "10" - } + { "name": "a", "in": "path", "required": true, "schema": { "type": "string" }, "example": "5" }, + { "name": "b", "in": "path", "required": true, "schema": { "type": "string" }, "example": "10" } ], - "responses": { - "200": { - "description": "Success" - } - } + "responses": { "200": { "description": "Success" } } } }, "/subtract": { "post": { + "tags": ["Math"], "summary": "Subtracts second number from first", "requestBody": { "required": true, @@ -366,14 +74,8 @@ "input": { "type": "object", "properties": { - "a": { - "type": "number", - "example": 10 - }, - "b": { - "type": "number", - "example": 5 - } + "a": { "type": "number", "example": 10 }, + "b": { "type": "number", "example": 5 } } } } @@ -381,45 +83,23 @@ } } }, - "responses": { - "200": { - "description": "Success" - } - } + "responses": { "200": { "description": "Success" } } } }, "/subtract/{a}/{b}": { "get": { + "tags": ["Math"], "summary": "Subtracts second number from first from path parameters", "parameters": [ - { - "name": "a", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "10" - }, - { - "name": "b", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "5" - } + { "name": "a", "in": "path", "required": true, "schema": { "type": "string" }, "example": "10" }, + { "name": "b", "in": "path", "required": true, "schema": { "type": "string" }, "example": "5" } ], - "responses": { - "200": { - "description": "Success" - } - } + "responses": { "200": { "description": "Success" } } } }, "/multiply": { "post": { + "tags": ["Math"], "summary": "Multiplies two numbers", "requestBody": { "required": true, @@ -431,14 +111,8 @@ "input": { "type": "object", "properties": { - "a": { - "type": "number", - "example": 5 - }, - "b": { - "type": "number", - "example": 10 - } + "a": { "type": "number", "example": 5 }, + "b": { "type": "number", "example": 10 } } } } @@ -446,45 +120,23 @@ } } }, - "responses": { - "200": { - "description": "Success" - } - } + "responses": { "200": { "description": "Success" } } } }, "/multiply/{a}/{b}": { "get": { + "tags": ["Math"], "summary": "Multiplies two numbers from path parameters", "parameters": [ - { - "name": "a", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "5" - }, - { - "name": "b", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "10" - } + { "name": "a", "in": "path", "required": true, "schema": { "type": "string" }, "example": "5" }, + { "name": "b", "in": "path", "required": true, "schema": { "type": "string" }, "example": "10" } ], - "responses": { - "200": { - "description": "Success" - } - } + "responses": { "200": { "description": "Success" } } } }, "/divide": { "post": { + "tags": ["Math"], "summary": "Divides first number by second", "requestBody": { "required": true, @@ -496,14 +148,8 @@ "input": { "type": "object", "properties": { - "a": { - "type": "number", - "example": 10 - }, - "b": { - "type": "number", - "example": 2 - } + "a": { "type": "number", "example": 10 }, + "b": { "type": "number", "example": 2 } } } } @@ -511,39 +157,564 @@ } } }, - "responses": { - "200": { - "description": "Success" - } - } + "responses": { "200": { "description": "Success" } } } }, "/divide/{a}/{b}": { "get": { + "tags": ["Math"], "summary": "Divides first number by second from path parameters", "parameters": [ - { - "name": "a", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "10" - }, - { - "name": "b", - "in": "path", - "required": true, - "schema": { - "type": "string" - }, - "example": "2" - } + { "name": "a", "in": "path", "required": true, "schema": { "type": "string" }, "example": "10" }, + { "name": "b", "in": "path", "required": true, "schema": { "type": "string" }, "example": "2" } ], + "responses": { "200": { "description": "Success" } } + } + }, + "/modulo": { + "post": { + "tags": ["Math"], + "summary": "Calculates the remainder of a division (a % b)", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { + "type": "object", + "properties": { + "a": { "type": "number", "example": 10 }, + "b": { "type": "number", "example": 3 } + } + } + } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/modulo/{a}/{b}": { + "get": { + "tags": ["Math"], + "summary": "Calculates the remainder from path parameters", + "parameters": [ + { "name": "a", "in": "path", "required": true, "schema": { "type": "string" }, "example": "10" }, + { "name": "b", "in": "path", "required": true, "schema": { "type": "string" }, "example": "3" } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/toString": { + "post": { + "tags": ["String"], + "summary": "Converts input to string", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { "input": {} } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/stringSplit": { + "post": { + "tags": ["String"], + "summary": "Splits a string by a separator", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { + "type": "object", + "properties": { + "string": { "type": "string" }, + "seperator": { "type": "string" } + } + } + } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/reverseString": { + "post": { + "tags": ["String"], + "summary": "Reverses a string", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { "input": { "type": "string", "example": "hello" } } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/reverseString/{string}": { + "get": { + "tags": ["String"], + "summary": "Reverses a string from path parameter", + "parameters": [ + { "name": "string", "in": "path", "required": true, "schema": { "type": "string" }, "example": "hello" } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/isPalindrome": { + "post": { + "tags": ["String"], + "summary": "Checks if a string is a palindrome", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { "input": { "type": "string", "example": "racecar" } } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/isPalindrome/{string}": { + "get": { + "tags": ["String"], + "summary": "Checks if a string is a palindrome from path parameter", + "parameters": [ + { "name": "string", "in": "path", "required": true, "schema": { "type": "string" }, "example": "racecar" } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/stringLength": { + "post": { + "tags": ["String"], + "summary": "Returns the length of a string", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { "input": { "type": "string", "example": "hello" } } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/stringLength/{string}": { + "get": { + "tags": ["String"], + "summary": "Returns the length of a string from path parameter", + "parameters": [ + { "name": "string", "in": "path", "required": true, "schema": { "type": "string" }, "example": "hello" } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/countWords": { + "post": { + "tags": ["String"], + "summary": "Counts the number of words in a string", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { "input": { "type": "string", "example": "hello world foo" } } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/countWords/{string}": { + "get": { + "tags": ["String"], + "summary": "Counts the number of words from path parameter", + "parameters": [ + { "name": "string", "in": "path", "required": true, "schema": { "type": "string" }, "example": "hello world" } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/toUpperCase": { + "post": { + "tags": ["String"], + "summary": "Converts a string to uppercase", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { "input": { "type": "string", "example": "hello" } } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/toUpperCase/{string}": { + "get": { + "tags": ["String"], + "summary": "Converts a string to uppercase from path parameter", + "parameters": [ + { "name": "string", "in": "path", "required": true, "schema": { "type": "string" }, "example": "hello" } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/toLowerCase": { + "post": { + "tags": ["String"], + "summary": "Converts a string to lowercase", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { "input": { "type": "string", "example": "HELLO" } } + } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/toLowerCase/{string}": { + "get": { + "tags": ["String"], + "summary": "Converts a string to lowercase from path parameter", + "parameters": [ + { "name": "string", "in": "path", "required": true, "schema": { "type": "string" }, "example": "HELLO" } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/sort": { + "post": { + "tags": ["Array"], + "summary": "Sorts an array", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { "type": "array", "items": {}, "example": [3, 1, 4, 1, 5, 9] } + } + } + } + } + }, + "responses": { "200": { "description": "Sorted array" } } + } + }, + "/reverseArray": { + "post": { + "tags": ["Array"], + "summary": "Reverses an array", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { "type": "array", "items": {}, "example": [1, 2, 3, 4, 5] } + } + } + } + } + }, + "responses": { "200": { "description": "Reversed array" } } + } + }, + "/isEven": { + "post": { + "tags": ["Checks"], + "summary": "Checks if number is even", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { "type": "object", "properties": { "input": { "type": "number" } } } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/isEven/{number}": { + "get": { + "tags": ["Checks"], + "summary": "Checks if number is even from path parameter", + "parameters": [ + { "name": "number", "in": "path", "required": true, "schema": { "type": "string" } } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/isOdd": { + "post": { + "tags": ["Checks"], + "summary": "Checks if number is odd", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { "type": "object", "properties": { "input": { "type": "number" } } } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/isOdd/{number}": { + "get": { + "tags": ["Checks"], + "summary": "Checks if number is odd from path parameter", + "parameters": [ + { "name": "number", "in": "path", "required": true, "schema": { "type": "string" } } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/isNumber": { + "post": { + "tags": ["Checks"], + "summary": "Checks if input is a number", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { "type": "object", "properties": { "input": {} } } + } + } + }, + "responses": { "200": { "description": "Success" } } + } + }, + "/isNumber/{number}": { + "get": { + "tags": ["Checks"], + "summary": "Checks if param is a number", + "parameters": [ + { "name": "number", "in": "path", "required": true, "schema": { "type": "string" } } + ], + "responses": { "200": { "description": "Success" } } + } + }, + "/stalinSort": { + "post": { + "tags": ["Sorts"], + "summary": "Sorts an array using Stalin Sort", + "description": "Elements that are not in order get purged. No re-education, just removal.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { "type": "array", "items": {}, "example": [3, 1, 4, 1, 5, 9] } + } + } + } + } + }, "responses": { "200": { - "description": "Success" + "description": "Purged array and execution count", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "ret": { "type": "array", "items": {} }, + "executions": { "type": "number" } + } + } + } + } + } + } + } + }, + "/bogoSort": { + "post": { + "tags": ["Sorts"], + "summary": "Sorts an array using Bogo Sort", + "description": "Randomly shuffles the array until it is sorted. Max 1000 attempts. Good luck.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { "type": "array", "items": {}, "example": [3, 1, 2] } + } + } + } + } + }, + "responses": { + "200": { + "description": "Result with attempt count and success flag", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "ret": { "type": "array", "items": {} }, + "attempts": { "type": "number" }, + "success": { "type": "boolean" } + } + } + } + } + } + } + } + }, + "/thanoSort": { + "post": { + "tags": ["Sorts"], + "summary": "Sorts an array using Thanos Sort", + "description": "Randomly removes half the elements until the array is sorted. Perfectly balanced, as all things should be.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { "type": "array", "items": {}, "example": [5, 3, 1, 4, 2] } + } + } + } + } + }, + "responses": { + "200": { + "description": "Surviving elements, snap count, and balance status", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "ret": { "type": "array", "items": {} }, + "snaps": { "type": "number" }, + "perfectly_balanced": { "type": "boolean" } + } + } + } + } + } + } + } + }, + "/sleepSort": { + "post": { + "tags": ["Sorts"], + "summary": "Sorts an array using Sleep Sort", + "description": "Each element sleeps for n milliseconds, then wakes up and joins the output. We simulated the sleep for you.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { "type": "array", "items": { "type": "number" }, "example": [3, 1, 4, 1, 5] } + } + } + } + } + }, + "responses": { + "200": { + "description": "Sorted array with total simulated sleep time", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "ret": { "type": "array", "items": {} }, + "totalSleepTimeMs": { "type": "number" }, + "note": { "type": "string" } + } + } + } + } + } + } + } + }, + "/miracleSort": { + "post": { + "tags": ["Sorts"], + "summary": "Sorts an array using Miracle Sort", + "description": "Checks if the array is already sorted. If not, it waits for a cosmic miracle. Please try again later.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "input": { "type": "array", "items": {}, "example": [1, 2, 3, 4, 5] } + } + } + } + } + }, + "responses": { + "200": { + "description": "Array with miracle status and message", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "ret": { "type": "array", "items": {} }, + "miracle": { "type": "boolean" }, + "message": { "type": "string" } + } + } + } + } } } } diff --git a/src/server.js b/src/server.js index 97b22ac..399a093 100644 --- a/src/server.js +++ b/src/server.js @@ -1,41 +1,16 @@ const express = require('express'); const bodyParser = require('body-parser'); const path = require('path'); -const sortRoute = require('./routes/api/sort'); -const stalinSortRoute = require('./routes/api/stalinSort'); -const isEvenRoute = require('./routes/api/isEven'); -const isOddRoute = require('./routes/api/isOdd'); -const toStringRoute = require('./routes/api/toString'); -const isNumberRoute = require('./routes/api/isNumber'); -const stringSplitRoute = require('./routes/api/stringSplit'); -const addRoute = require('./routes/api/add'); -const subtractRoute = require('./routes/api/subtract'); -const multiplyRoute = require('./routes/api/multiply'); -const divideRoute = require('./routes/api/divide'); - +const apiRoutes = require('./routes/api'); const docsRoute = require('./routes/docs/main'); const app = express(); - const PORT = 3000; app.use(express.static(path.join(__dirname, '../www'))); - app.use(bodyParser.json()); -app.use('/api/sort', sortRoute); -app.use('/api/stalinSort', stalinSortRoute); -app.use('/api/isEven', isEvenRoute); -app.use('/api/isOdd', isOddRoute); -app.use('/api/toString', toStringRoute); -app.use('/api/isNumber', isNumberRoute); -app.use('/api/stringSplit', stringSplitRoute); -app.use('/api/add', addRoute); -app.use('/api/subtract', subtractRoute); -app.use('/api/multiply', multiplyRoute); -app.use('/api/divide', divideRoute); - - +app.use('/api', apiRoutes); app.use('/docs', docsRoute); app.listen(PORT, () => {