From 7b4ae45d4859cb6da7fc8b5c32dab706eda6efd5 Mon Sep 17 00:00:00 2001 From: arnorhs Date: Wed, 1 Jan 2014 03:08:56 +0000 Subject: [PATCH] moved helpers to a stand alone module --- endpoints/bus/index.js | 2 +- endpoints/bus/realtime.js | 2 +- endpoints/car/index.js | 2 +- endpoints/company/index.js | 2 +- endpoints/currency/arion.js | 2 +- endpoints/currency/m5.js | 2 +- endpoints/declension/index.js | 2 +- endpoints/earthquake/index.js | 2 +- endpoints/flight/index.js | 2 +- endpoints/tv/index.js | 2 +- endpoints/weather/index.js | 2 +- lib/helpers.js | 130 ---------------------------------- package.json | 3 +- test/unit/helpers.js | 71 ------------------- 14 files changed, 13 insertions(+), 213 deletions(-) delete mode 100644 lib/helpers.js delete mode 100644 test/unit/helpers.js diff --git a/endpoints/bus/index.js b/endpoints/bus/index.js index 2f94c505..60e49495 100644 --- a/endpoints/bus/index.js +++ b/endpoints/bus/index.js @@ -1,5 +1,5 @@ var request = require('request'), - h = require('../../lib/helpers.js'), + h = require('apis-helpers'), app = require('../../server'); app.post('/bus/search', function(req, res) { diff --git a/endpoints/bus/realtime.js b/endpoints/bus/realtime.js index ee29d38a..7b8a0955 100644 --- a/endpoints/bus/realtime.js +++ b/endpoints/bus/realtime.js @@ -1,4 +1,4 @@ -var h = require('../../lib/helpers.js'), +var h = require('apis-helpers'), request = require('request'), app = require('../../server'), isn2wgs = require('isn2wgs'); diff --git a/endpoints/car/index.js b/endpoints/car/index.js index 76f61c5b..dbfbf548 100644 --- a/endpoints/car/index.js +++ b/endpoints/car/index.js @@ -1,6 +1,6 @@ var request = require('request'), $ = require('jquery'), - h = require('../../lib/helpers.js'), + h = require('apis-helpers'), app = require('../../server'); diff --git a/endpoints/company/index.js b/endpoints/company/index.js index 26dbec6f..3624d73b 100644 --- a/endpoints/company/index.js +++ b/endpoints/company/index.js @@ -1,6 +1,6 @@ var request = require('request'), $ = require('jquery'), - h = require('../../lib/helpers.js'), + h = require('apis-helpers'), app = require('../../server'); $.fn.cleanHtml = function() { diff --git a/endpoints/currency/arion.js b/endpoints/currency/arion.js index 3e9511e0..82fcf18c 100644 --- a/endpoints/currency/arion.js +++ b/endpoints/currency/arion.js @@ -1,6 +1,6 @@ var request = require('request'), moment = require('moment'), - h = require('../../lib/helpers.js'), + h = require('apis-helpers'), app = require('../../server'); app.get('/currency/arion', function(req, res){ diff --git a/endpoints/currency/m5.js b/endpoints/currency/m5.js index 52de292a..fd4e0427 100644 --- a/endpoints/currency/m5.js +++ b/endpoints/currency/m5.js @@ -1,6 +1,6 @@ var request = require('request'), $ = require('jquery'), - h = require('../../lib/helpers.js'), + h = require('apis-helpers'), app = require('../../server'); app.get('/currency/m5', function (req, res) { diff --git a/endpoints/declension/index.js b/endpoints/declension/index.js index b20ab2b7..7d13987a 100644 --- a/endpoints/declension/index.js +++ b/endpoints/declension/index.js @@ -2,7 +2,7 @@ // dependencies var app = require('../../server'), request = require('request'), - helper = require('../../lib/helpers.js'), + helper = require('apis-helpers'), cheerio = require('cheerio'), _ = require('underscore'); diff --git a/endpoints/earthquake/index.js b/endpoints/earthquake/index.js index 53c4299c..ee9d80d1 100644 --- a/endpoints/earthquake/index.js +++ b/endpoints/earthquake/index.js @@ -1,6 +1,6 @@ var request = require('request'), cheerio = require('cheerio'), - browser = require('../../lib/helpers.js').browser, + browser = require('apis-helpers').browser, app = require('../../server'); app.get('/earthquake/is', function (req, res, next) { diff --git a/endpoints/flight/index.js b/endpoints/flight/index.js index 3cb2c7db..687c58c4 100644 --- a/endpoints/flight/index.js +++ b/endpoints/flight/index.js @@ -1,6 +1,6 @@ var request = require('request'), $ = require('jquery'), - h = require('../../lib/helpers.js'), + h = require('apis-helpers'), app = require('../../server'); app.get('/flight', function(req, res){ diff --git a/endpoints/tv/index.js b/endpoints/tv/index.js index 887563bc..3f83cdcd 100644 --- a/endpoints/tv/index.js +++ b/endpoints/tv/index.js @@ -1,7 +1,7 @@ var request = require('request'), moment = require('moment'), parseString = require('xml2js').parseString, - h = require('../../lib/helpers.js'), + h = require('apis-helpers'), app = require('../../server'); var schedStruct = { diff --git a/endpoints/weather/index.js b/endpoints/weather/index.js index 17be84b4..4cfba462 100644 --- a/endpoints/weather/index.js +++ b/endpoints/weather/index.js @@ -9,7 +9,7 @@ var request, parseString, h, app, cheerio, Q, xregexp, validTypes, measurements, /** Requires **/ request = require('request'), parseString = require('xml2js').parseString, -h = require('../../lib/helpers.js'), +h = require('apis-helpers'), app = require('../../server'), cheerio = require('cheerio'), xregexp = require('xregexp').XRegExp; diff --git a/lib/helpers.js b/lib/helpers.js deleted file mode 100644 index f8b5a094..00000000 --- a/lib/helpers.js +++ /dev/null @@ -1,130 +0,0 @@ -/** - * Maps currency letters to names - * @return Obj - */ -exports.currency = { - USD : { - short: 'USD', - long: 'Bandarískur dalur' - }, - DKK: { - short: 'DKK', - long: 'Dönsk króna' - }, - EUR: { - short: 'EUR', - long: 'Evra' - }, - CAD: { - short: 'CAD', - long: 'Kanadískur dalur' - }, - NOK: { - short: 'NOK', - long: 'Norsk króna' - }, - GBP: { - short: 'GBP', - long: 'Sterlingspund' - }, - CHF: { - short: 'CHF', - long: 'Svissneskur franki' - }, - SEK: { - short: 'SEK', - long: 'Sænsk króna' - }, - TWI: { - short: 'TWI', - long: 'Trade Weighted Index' - }, - XDR: { - short: 'XDR', - long: 'Gengisvísitala' - }, - ISK: { - short: 'ISK', - long: 'Íslensk króna' - }, - JPY: { - short: 'JPY', - long: 'Japanskt Yen' - } -} - -/** - * Combines two objects into one - * @return Obj - */ -exports.mergeObjects = function(obj1, obj2) { - var obj = {}; - - for (var x in obj1) - if (obj1.hasOwnProperty(x)) - obj[x] = obj1[x]; - - for (var x in obj2) - if (obj2.hasOwnProperty(x)) - obj[x] = obj2[x]; - - return obj; -} - -/** - * Returns random valid browser string - * @return String - */ -exports.browser = function(){ - var browsers = [ - 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.66 Safari/535.11', - 'Mozilla/5.0 (X11; Linux amd64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1', - 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1', - 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1', - 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:14.0) Gecko/20120405 Firefox/14.0a1', - 'Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20120405 Firefox/14.0a1', - 'Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120405 Firefox/14.0a1', - 'Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1', - 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1', - 'Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1' - ]; - - return browsers[Math.floor(Math.random()*browsers.length)]; -} - -/** - * Iterates through all members of an object and changes it in place. - * Changes all members holding arrays of size 1 and sets them to the single item the array holds. - * @return undefined - */ -exports.deArrayfy = function(obj){ - var self = this; - if (Array.isArray(obj)) { - obj.forEach(function(child){ - self.deArrayfy(child); - }); - } - else if (obj instanceof Object){ - var keys = Object.keys(obj); - keys.forEach(function(key){ - var member = obj[key]; - // if the member is an array of size 1, set the member as the single item - if (Array.isArray(member) && member.length === 1) { - obj[key] = member[0]; - } - self.deArrayfy(member); - }) - } -} diff --git a/package.json b/package.json index 813c7eef..95e8211e 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,8 @@ "xregexp": "~2.0.0", "cheerio": "~0.12.4", "mocha": "~1.15.1", - "express": "~3.4.7" + "express": "~3.4.7", + "apis-helpers": "0.0.1" }, "scripts": { "test": "node_modules/mocha/bin/mocha test/integration", diff --git a/test/unit/helpers.js b/test/unit/helpers.js deleted file mode 100644 index f9eb17c9..00000000 --- a/test/unit/helpers.js +++ /dev/null @@ -1,71 +0,0 @@ -var helpers = require('../../lib/helpers.js'); -var assert = require('assert'); - -describe('deArrayfy', function () { - it("should not change single item variables in objects", function () { - - var object = {foo: "foo"}; - - helpers.deArrayfy(object); - assert.equal("foo", object.foo); - }); - it("should change 1 item array member to single item variables", function () { - - var object = {foo: [ 1 ]}; - - helpers.deArrayfy(object); - assert.equal(false, Array.isArray(object.foo)); - }); - it("should not change two item array members", function () { - - var object = {foo: [ 1, 2 ]}; - - helpers.deArrayfy(object); - assert.equal(true, Array.isArray(object.foo)); - }); - it("should change 1 item array member in sub-objects to single item variables", function () { - - var object = {foo: {bar: [ 1 ]}}; - - helpers.deArrayfy(object); - assert.equal(false, Array.isArray(object.foo.bar)); - }); - it("should not change two item array members in sub-objects", function () { - - var object = {foo: {bar: [ 1, 2 ]}}; - - helpers.deArrayfy(object); - assert.equal(true, Array.isArray(object.foo.bar)); - }); - it("should change 1 item array member in sub-arrays to single item variables", function () { - - var object = {foo: {bar: [ { baz: [ 1 ] } ]}}; - - helpers.deArrayfy(object); - assert.equal(false, Array.isArray(object.foo.bar)); - assert.equal(false, Array.isArray(object.foo.bar.baz)); - }); - it("should not change two item array members in sub-arrays", function () { - - var object = {foo: {bar: [ { baz: [ 1, 2 ] }, { baz : [ 1, 2 ]} ]}}; - - helpers.deArrayfy(object); - assert.equal(true, Array.isArray(object.foo.bar)); - assert.equal(true, Array.isArray(object.foo.bar[0].baz)); - assert.equal(true, Array.isArray(object.foo.bar[1].baz)); - }); - it("should change all 1 item arrays in the object to single item variables and leave single item variables unchanged", function () { - - var object = {foo: "foo", bar: [ 1 ], baz: [ 1, 2 ], qux: { foo : "foo", bar : [ [ 1, 2 ], [ 1 ] ], baz: [ 1 ] }}; - - helpers.deArrayfy(object); - assert.equal(false, Array.isArray(object.bar)); - assert.equal(true, Array.isArray(object.baz)); - - assert.equal(true, Array.isArray(object.qux.bar)); - assert.equal(false, Array.isArray(object.qux.baz)); - - assert.equal("foo", object.qux.foo); - }); - -});