relevant-urban
Advanced tools
Comparing version 1.3.5 to 2.0.0
{ | ||
"name": "relevant-urban", | ||
"version": "1.3.5", | ||
"version": "2.0.0", | ||
"description": "Urban Dictionary API wrapper made for everyone with useful methods using promises and snekfetch", | ||
@@ -17,12 +17,12 @@ "main": "urban.js", | ||
"dependencies": { | ||
"snekfetch": "^2.2.0" | ||
"snekfetch": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^3.19.0", | ||
"ava": "^0.19.1" | ||
"ava": "^0.19.1", | ||
"eslint": "^3.19.0" | ||
}, | ||
"scripts": { | ||
"test": "npm run lint && npm run check", | ||
"lint": "eslint urban.js test/", | ||
"lint:fix": "eslint --fix urban.js test/", | ||
"lint": "eslint urban.js Constants.js test/", | ||
"lint:fix": "eslint --fix urban.js Constants.js test/", | ||
"check": "node_modules/.bin/ava -v" | ||
@@ -37,2 +37,2 @@ }, | ||
} | ||
} | ||
} |
@@ -1,2 +0,3 @@ | ||
# Relevant urban [![Build Status](https://travis-ci.org/SnekLab/relevant-urban.svg?branch=master)](https://travis-ci.org/SnekLab/relevant-urban) [![NPM](https://nodei.co/npm/relevant-urban.png?mini=true)](https://nodei.co/npm/relevant-urban/) | ||
# Relevant urban [![NPM](https://nodei.co/npm/relevant-urban.png?mini=true)](https://nodei.co/npm/relevant-urban/) [![Build Status](https://travis-ci.org/SnekLab/relevant-urban.svg?branch=master)](https://travis-ci.org/SnekLab/relevant-urban) [![Greenkeeper badge](https://badges.greenkeeper.io/SnekLab/relevant-urban.svg)](https://greenkeeper.io/) | ||
<p align="center"> | ||
@@ -3,0 +4,0 @@ <img src="https://github.com/SnekLab/relevant-urban/blob/master/ud.png?raw=true" alt="Urban Dictionary API wrapper logo"/> |
@@ -6,18 +6,18 @@ const urban = require('../urban.js'); | ||
.then(d => t.truthy(d)) | ||
.catch(e => t.falsy(e))); | ||
.catch(e => t.truthy(e))); | ||
test('Fetching \'hello\' urban definition', t => urban('hello') | ||
.then(d => t.truthy(d)) | ||
.catch(e => t.falsy(e))); | ||
.catch(e => t.truthy(e))); | ||
test('Fetching \'fsiojgjsgjsgihsghghjsh\' urban definition (success if not found)', t => urban('fsiojgjsgjsgihsghghjsh') | ||
.then(d => t.falsy(d)) | ||
.catch(e => t.falsy(e))); | ||
.catch(e => t.truthy(e))); | ||
test('Fetching random specific urban definition', t => urban.random('hello') | ||
.then(d => t.truthy(d)) | ||
.catch(e => t.falsy(e))); | ||
.catch(e => t.truthy(e))); | ||
test('Fetching all specific urban definitions', t => urban.all('hello') | ||
.then(d => t.true(typeof d == 'object')) | ||
.catch(e => t.falsy(e))); | ||
.catch(e => t.truthy(e))); |
141
urban.js
const snekfetch = require('snekfetch'); | ||
const api = 'https://api.urbandictionary.com/v0'; | ||
const ArrayRandom = array => array[Math.floor(Math.random() * array.length)]; | ||
const { BASE_URL, DEFINITION, randomArrayItem } = require('./Constants'); | ||
function search(query, page = 1) { | ||
return new snekfetch('GET', `${api}/define?page=${page}&term=${query}`) | ||
.then(res => res.body) | ||
.then(body => body.result_type == 'no_results' ? Promise.reject(null) : body); | ||
} | ||
class Urban { | ||
static first(query) { | ||
return this.search(query) | ||
.then(body => new DEFINITION(body.list[0], body)); | ||
} | ||
static random(query = null) { | ||
if (!query) return this.getRandom(query) | ||
.then(body => new DEFINITION(body.list[0], body)); | ||
else return this.search(query) | ||
.then(body => new DEFINITION(randomArrayItem(body.list), body)); | ||
} | ||
static all(query) { | ||
return this.search(query) | ||
.then(body => body.list.map(d => new DEFINITION(d, body))); | ||
} | ||
static search(query, page = 1) { | ||
return new snekfetch('GET', `${BASE_URL}/define?page=${page}&term=${query}`) | ||
.then(res => res.body && res.body.result_type != 'no_results' ? res.body : Promise.reject(TypeError('No results'))); | ||
} | ||
static getRandom() { | ||
return new snekfetch('GET', `${BASE_URL}/random`) | ||
.then(res => res.body && res.body.result_type != 'no_results' ? res.body : Promise.reject(TypeError('No results'))); | ||
} | ||
static get version() { | ||
return require('./package.json').version; | ||
} | ||
function first(query) { | ||
return search(query) | ||
.then(body => body ? | ||
Object.assign(new Definition(body.list[0]), { | ||
tags: Array.from(new Set(body.tags)), | ||
sounds: body.sounds | ||
}) : Promise.reject(null)); | ||
} | ||
function all(query) { | ||
return search(query) | ||
.then(body => body ? | ||
Object.assign(body.list.map(d => new Definition(d)), { | ||
tags: Array.from(new Set(body.tags)), | ||
sounds: body.sounds | ||
}) : Promise.reject(null)); | ||
} | ||
function random(query = null) { | ||
if (!query) return new snekfetch('GET', `${api}/random`) | ||
.then(res => res.body) | ||
.then(body => body.result_type == 'no_results' ? null : new Definition(body.list[0])); | ||
else return search(query) | ||
.then(body => body ? | ||
Object.assign(new Definition(ArrayRandom(body.list)), { | ||
tags: Array.from(new Set(body.tags)), | ||
sounds: body.sounds | ||
}) : Promise.reject(null)); | ||
} | ||
class Definition { | ||
constructor({ | ||
defid, | ||
word, | ||
definition, | ||
example, | ||
permalink, | ||
thumbs_up, | ||
author, | ||
thumbs_down | ||
}) { | ||
/** | ||
* ID of definition. | ||
* @type {number} | ||
*/ | ||
this.id = defid; | ||
/** | ||
* Word used for definition. | ||
* @type {string} | ||
*/ | ||
this.word = word; | ||
/** | ||
* Definition itself. | ||
* @type {string} | ||
*/ | ||
this.definition = definition; | ||
/** | ||
* Definition example. | ||
* @type {string} | ||
*/ | ||
this.example = example; | ||
/** | ||
* Definition URL. | ||
* @type {string} | ||
*/ | ||
this.urbanURL = permalink; | ||
/** | ||
* Definition author name. | ||
* @type {string} | ||
*/ | ||
this.author = author; | ||
/** | ||
* Definition thumbs up. | ||
* @type {number} | ||
*/ | ||
this.thumbsUp = thumbs_up; | ||
/** | ||
* Definition thumbs down. | ||
* @type {number} | ||
*/ | ||
this.thumbsDown = thumbs_down; | ||
} | ||
} | ||
module.exports = Object.assign(first, { | ||
all, | ||
random, | ||
search, | ||
version: require('./package.json').version | ||
}); | ||
module.exports = Object.assign(Urban.first.bind(Urban), { | ||
search: Urban.search, | ||
definition: DEFINITION, | ||
all: Urban.all.bind(Urban), | ||
random: Urban.random.bind(Urban), | ||
version: Urban.version }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32862
10
162
43
1
+ Addedsnekfetch@3.6.4(transitive)
- Removedsnekfetch@2.3.2(transitive)
Updatedsnekfetch@^3.0.1