├── .travis.yml ├── .gitignore ├── package.json ├── LICENSE ├── examples └── example.js ├── test └── test.js ├── README.md └── index.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 6 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.custom.* 3 | *.log 4 | *.map 5 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "one-liner-joke", 3 | "version": "1.2.2", 4 | "description": "A simple node module which provides one liner joke randomly and from specific category", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha test/test.js" 8 | }, 9 | "engines": { 10 | "node": ">=6.0.0" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git@github.com:faiyaz26/one-liner-joke.git" 15 | }, 16 | "keywords": [ 17 | "joke", 18 | "funny", 19 | "one-liner", 20 | "humor", 21 | "jokes", 22 | "lol" 23 | ], 24 | "author": "Ahmad Faiyaz ", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/faiyaz26/one-liner-joke/issues" 28 | }, 29 | "homepage": "https://github.com/faiyaz26/one-liner-joke#readme", 30 | "devDependencies": { 31 | "chai": "^3.2.0", 32 | "mocha": "^2.3.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Ahmad Faiyaz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /examples/example.js: -------------------------------------------------------------------------------- 1 | var oneLinerJoke = require('../index.js'); // in your code it should be "require('one-liner-joke');" without quote 2 | 3 | 4 | /* 5 | The variable getRandomJoke will contain a random joke with a format: 6 | {"body":"Artificial intelligence is no match for natural stupidity.","tags":["intelligence","stupid"]} 7 | */ 8 | var getRandomJoke = oneLinerJoke.getRandomJoke(); 9 | console.log(getRandomJoke) 10 | 11 | /* 12 | One can add exclusion filter for the jokes tags 13 | default is ['racist', 'dirty', 'sex'] 14 | */ 15 | var getRandomJoke = oneLinerJoke.getRandomJoke({ 16 | 'exclude_tags': ['dirty', 'racist', 'marriage'] 17 | }); 18 | console.log(getRandomJoke) 19 | 20 | 21 | /* 22 | The variable getRandomJoke will contain a random joke with a tag and with a format: 23 | {"body":"Artificial intelligence is no match for natural stupidity.","tags":["intelligence","stupid"]} 24 | */ 25 | 26 | var getRandomJokeWithTag = oneLinerJoke.getRandomJokeWithTag('stupid'); 27 | console.log(getRandomJokeWithTag) 28 | 29 | /* 30 | One can add exclusion filter for the jokes tags 31 | default is ['racist', 'dirty', 'sex'] 32 | */ 33 | var getRandomJoke = oneLinerJoke.getRandomJokeWithTag('stupid', { 34 | 'exclude_tags': ['dirty', 'racist', 'marriage'] 35 | }); 36 | console.log(getRandomJoke) 37 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var should = require('chai').should(); 2 | var expect = require('chai').expect; 3 | var assert = require('chai').assert; 4 | 5 | var oneLinerJoke = require('../index.js'); 6 | 7 | 8 | describe('One Liner Joke Testing', function () { 9 | it('Should return a valid random Joke', function (done) { 10 | this.timeout(10000); 11 | 12 | var randomJoke = oneLinerJoke.getRandomJoke(); 13 | 14 | try{ 15 | expect(randomJoke).to.be.an('object'); 16 | assert.isDefined(randomJoke); 17 | assert.isNotNull(randomJoke); 18 | assert.isObject(randomJoke); 19 | assert.property(randomJoke, 'body'); 20 | assert.property(randomJoke, 'tags'); 21 | expect(randomJoke.tags).to.be.an('array'); 22 | done(); 23 | }catch(e){ 24 | done(e); 25 | } 26 | }); 27 | 28 | it('Should return a valid random Joke with a tag', function (done) { 29 | this.timeout(10000); 30 | 31 | var randomJoke = oneLinerJoke.getRandomJokeWithTag('life'); 32 | try{ 33 | expect(randomJoke).to.be.an('object'); 34 | assert.isDefined(randomJoke); 35 | assert.isNotNull(randomJoke); 36 | assert.isObject(randomJoke); 37 | assert.property(randomJoke, 'body'); 38 | assert.property(randomJoke, 'tags'); 39 | expect(randomJoke.tags).to.be.an('array'); 40 | expect(randomJoke.tags).to.contain('life'); 41 | done(); 42 | }catch(e){ 43 | done(e); 44 | } 45 | }); 46 | 47 | }); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # one-liner-joke 2 | [![Build Status](https://api.travis-ci.org/faiyaz26/one-liner-joke.svg?branch=master)](https://travis-ci.org/faiyaz26/one-liner-joke) 3 | 4 | 5 | A simple Node Module which can provide one line joke randomly or from specific tag 6 | 7 | 8 | This module contains more than 2200 one line jokes. 9 | 10 | 11 | 12 | ### Installation 13 | ``` 14 | npm install one-liner-joke --save 15 | ``` 16 | 17 | 18 | ### Usage 19 | 20 | ``` 21 | var oneLinerJoke = require('one-liner-joke'); 22 | 23 | /* 24 | The variable getRandomJoke will contain a random joke with a format: 25 | {"body":"Artificial intelligence is no match for natural stupidity.","tags":["intelligence","stupid"]} 26 | */ 27 | var getRandomJoke = oneLinerJoke.getRandomJoke(); 28 | console.log(getRandomJoke) 29 | 30 | /* 31 | One can add exclusion filter for the jokes tags 32 | default is ['racist', 'dirty', 'sex'] 33 | */ 34 | var getRandomJoke = oneLinerJoke.getRandomJoke({ 35 | 'exclude_tags': ['dirty', 'racist', 'marriage'] 36 | }); 37 | console.log(getRandomJoke) 38 | 39 | 40 | /* 41 | The variable getRandomJoke will contain a random joke with a tag and with a format: 42 | {"body":"Artificial intelligence is no match for natural stupidity.","tags":["intelligence","stupid"]} 43 | */ 44 | 45 | var getRandomJokeWithTag = oneLinerJoke.getRandomJokeWithTag('stupid'); 46 | console.log(getRandomJokeWithTag) 47 | 48 | /* 49 | One can add exclusion filter for the jokes tags 50 | default is ['racist', 'dirty', 'sex'] 51 | */ 52 | var getRandomJoke = oneLinerJoke.getRandomJokeWithTag('stupid', { 53 | 'exclude_tags': ['dirty', 'racist', 'marriage'] 54 | }); 55 | console.log(getRandomJoke) 56 | 57 | ``` 58 | 59 | ## Tests 60 | ``` 61 | npm test 62 | ``` 63 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var jokes = require('./jokes.json'); 2 | 3 | /** 4 | * Returns a random integer between min (inclusive) and max (inclusive) 5 | * Using Math.round() will give you a non-uniform distribution! 6 | */ 7 | function getRandomInt(min, max) { 8 | return Math.floor(Math.random() * (max - min + 1)) + min; 9 | } 10 | 11 | 12 | exclude_tags_default = ['racist', 'dirty', 'sex'] 13 | 14 | function getRandomJoke(options={ 15 | 'exclude_tags': exclude_tags_default 16 | }){ 17 | const min = 0; 18 | const max = jokes.length - 1; 19 | const exclude_tags = options['exclude_tags']; 20 | while(true){ 21 | const idx = getRandomInt(min, max); 22 | let joke = jokes[idx]; 23 | let flagged = 0; 24 | for(let i = 0; i < exclude_tags.length; i++){ 25 | if(joke.tags.indexOf(exclude_tags[i]) > -1){ 26 | flagged = 1; 27 | } 28 | } 29 | 30 | if(flagged === 0){ 31 | return joke; 32 | } 33 | } 34 | return null; 35 | } 36 | 37 | function getAllJokesWithTag(tag){ 38 | var jokesWithTag = []; 39 | jokes.forEach(function(joke) { 40 | if(joke.tags.indexOf(tag) != -1){ 41 | jokesWithTag.push(joke); 42 | } 43 | }); 44 | 45 | return jokesWithTag; 46 | } 47 | 48 | function getRandomJokeWithTag(tag, options={ 49 | 'exclude_tags': exclude_tags_default 50 | }){ 51 | var jokesWithTag = getAllJokesWithTag(tag); 52 | const exclude_tags = options['exclude_tags']; 53 | 54 | if(jokesWithTag.length == 0){ 55 | return {'body' : '', 'tags' : []}; 56 | } 57 | 58 | const min = 0; 59 | const max = jokesWithTag.length - 1; 60 | while(true){ 61 | const idx = getRandomInt(min, max); 62 | let joke = jokesWithTag[idx]; 63 | let flagged = 0; 64 | for(let i = 0; i < exclude_tags.length; i++){ 65 | if(joke.tags.indexOf(exclude_tags[i]) > -1){ 66 | flagged = 1; 67 | } 68 | } 69 | 70 | if(flagged === 0){ 71 | return joke; 72 | } 73 | } 74 | return null; 75 | } 76 | 77 | module.exports = { 78 | getRandomJoke : getRandomJoke, 79 | getAllJokesWithTag: getAllJokesWithTag, 80 | getRandomJokeWithTag: getRandomJokeWithTag 81 | }; 82 | --------------------------------------------------------------------------------