stamina-api
Version:
Npm module can make jokes generator and other. This module is for Stamina website and game. - Only Stamina's developpers: Can manage Stamina's infrastructure.
57 lines (46 loc) • 2.66 kB
JavaScript
const MessageErrors = require("../errors/MessageErrors.js")
module.exports = {
byType(type, options) {
if (!type) throw new Error(MessageErrors.jokesByType_Missing)
if (typeof type !== "string") throw new Error(MessageErrors.jokesByType_Invalid)
var blagues = require("../jokes/jokes.json")
if (type.toLowerCase() === "random") {
if (!options) return blagues[Math.floor(Math.random() * blagues.length)]
if (typeof options !== 'object') throw new TypeError(MessageErrors.MUST_BE_TYPE_OBJECT);
if (typeof options.disallow !== 'string' && !Array.isArray(options.disallow)) throw new TypeError(MessageErrors.MUST_BE_TYPE_STRING_ARRAY)
var disallow = typeof options.disallow === 'string' ? [options.disallow] : options.disallow;
for (var i of disallow) {
blagues = blagues.filter(b => b.type !== i)
}
return blagues[Math.floor(Math.random() * blagues.length)]
} else if (type.toLowerCase() === "dev") {
var dev = blagues.filter(t => t.type === "dev")
return dev[Math.floor(Math.random() * dev.length)]
} else if (type.toLowerCase() === "beauf") {
var beauf = blagues.filter(t => t.type === "beauf")
return beauf[Math.floor(Math.random() * beauf.length)]
} else if (type.toLowerCase() === "blondes") {
var blondes = blagues.filter(t => t.type === "blondes")
return blondes[Math.floor(Math.random() * blondes.length)]
} else if (type.toLowerCase() === "limit") {
var limit = blagues.filter(t => t.type === "limit")
return limit[Math.floor(Math.random() * limit.length)]
} else if (type.toLowerCase() === "dark") {
var dark = blagues.filter(t => t.type === "dark")
return dark[Math.floor(Math.random() * dark.length)]
} else if (type.toLowerCase() === "global") {
var global = blagues.filter(t => t.type === "global")
return global[Math.floor(Math.random() * global.length)]
} else throw new Error(MessageErrors.jokesByType_Invalid)
},
byId(id) {
if (!id) throw new Error(MessageErrors.jokesById_Missing)
id = Number(id)
if (typeof id !== "number") throw new Error(MessageErrors.jokesById_Invalid)
var blagues = require("../jokes/jokes.json")
blagues = blagues.filter(b => b.id === id)
if (blagues.length > 0) {
return blagues[0]
} else throw new Error(MessageErrors.jokesById_Invalid)
}
}