Comparing version 1.0.7 to 1.0.9
{ | ||
"name": "pageviews", | ||
"main": "pageviews", | ||
"version": "1.0.7", | ||
"version": "1.0.9", | ||
"homepage": "https://github.com/tomayac/pageviews.js", | ||
@@ -6,0 +6,0 @@ "authors": [ |
{ | ||
"name": "pageviews", | ||
"version": "1.0.7", | ||
"version": "1.0.9", | ||
"description": "A lightweight JavaScript client library for the Wikimedia Pageviews API for Wikipedia and various of its sister projects for Node.js and the browser.", | ||
"main": "pageviews.js", | ||
"scripts": { | ||
"test": "node tests/tests.js", | ||
"lint": "jshint pageviews.js tests/tests.js; jscs --preset=google pageviews.js tests/tests.js", | ||
"test": "mocha", | ||
"lint": "jshint pageviews.js test/pageviewsTest.js; jscs --preset=google pageviews.js test/pageviewsTest.js", | ||
"build": "uglifyjs pageviews.js --compress sequences,properties,dead_code,conditionals,comparisons,evaluate,booleans,loops,unused,if_return,join_vars,warnings --mangle --comments --output pageviews.min.js" | ||
@@ -10,0 +10,0 @@ }, |
@@ -76,3 +76,3 @@ /** | ||
var pad = function(d) { | ||
return d < 10 ? '0' + d : d; | ||
return d < 10 ? '0' + d : d.toString(); | ||
}; | ||
@@ -93,2 +93,3 @@ | ||
if ((params.project !== 'all-projects') && | ||
(params.project !== 'wikidata') && | ||
(params.project.indexOf('.') === -1)) { | ||
@@ -103,3 +104,5 @@ return new Error('Required parameter "project" invalid.'); | ||
(params.projects.filter(function(project) { | ||
return project.indexOf('.') === -1 && project !== 'all-projects'; | ||
return project.indexOf('.') === -1 && | ||
project !== 'all-projects' && | ||
project !== 'wikidata'; | ||
}).length) | ||
@@ -128,4 +131,5 @@ ) { | ||
params.start = typeof params.start === 'object' ? | ||
(params.start.getFullYear() + (pad(params.start.getMonth() + 1)) + | ||
pad(params.start.getDate())) : | ||
(params.start.getUTCFullYear() + | ||
(pad(params.start.getUTCMonth() + 1)) + | ||
(pad(params.start.getUTCDate()))) : | ||
params.start; | ||
@@ -140,4 +144,4 @@ if (!/^(?:19|20)\d\d[- /.]?(?:0[1-9]|1[012])[- /.]?(?:0[1-9]|[12][0-9]|3[01])$/.test(params.start)) { | ||
params.end = typeof params.end === 'object' ? | ||
(params.end.getFullYear() + (pad(params.end.getMonth() + 1)) + | ||
pad(params.end.getDate())) : | ||
(params.end.getUTCFullYear() + (pad(params.end.getUTCMonth() + 1)) + | ||
pad(params.end.getUTCDate())) : | ||
params.end; | ||
@@ -153,4 +157,5 @@ if (!/^(19|20)\d\d[- /.]?(0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])$/.test(params.end)) { | ||
params.start = typeof params.start === 'object' ? | ||
(params.start.getFullYear() + (pad(params.start.getMonth() + 1)) + | ||
pad(params.start.getDate()) + pad(params.start.getHours())) : | ||
(params.start.getUTCFullYear() + | ||
(pad(params.start.getUTCMonth() + 1)) + | ||
(pad(params.start.getUTCDate()) + pad(params.start.getUTCHours()))) : | ||
params.start; | ||
@@ -165,4 +170,4 @@ if (!/^(?:19|20)\d\d[- /.]?(?:0[1-9]|1[012])[- /.]?(?:0[1-9]|[12][0-9]|3[01])[- /.]?(?:[012][0-9])$/.test(params.start)) { | ||
params.end = typeof params.end === 'object' ? | ||
(params.end.getFullYear() + (pad(params.end.getMonth() + 1)) + | ||
pad(params.end.getDate()) + pad(params.end.getHours())) : | ||
(params.end.getUTCFullYear() + (pad(params.end.getUTCMonth() + 1)) + | ||
pad(params.end.getUTCDate()) + pad(params.end.getUTCHours())) : | ||
params.end; | ||
@@ -181,5 +186,5 @@ if (!/^(19|20)\d\d[- /.]?(0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])[- /.]?(?:[012][0-9])$/.test(params.end)) { | ||
params.date.substr(6, 2)); | ||
params.year = params.date.getFullYear(); | ||
params.month = pad(params.date.getMonth() + 1); | ||
params.day = pad(params.date.getDate()); | ||
params.year = params.date.getUTCFullYear(); | ||
params.month = pad(params.date.getUTCMonth() + 1); | ||
params.day = pad(params.date.getUTCDate()); | ||
} | ||
@@ -195,3 +200,4 @@ // Required: year | ||
// Required: day | ||
if ((!params.day) || (!/^(?:0?[1-9]|[12][0-9]|3[01])$/.test(params.day))) { | ||
if ((!params.day) || | ||
(!/^(?:0?[1-9]|[12][0-9]|3[01])$/.test(params.day))) { | ||
return new Error('Required parameter "day" missing or invalid.'); | ||
@@ -198,0 +204,0 @@ } |
@@ -17,2 +17,2 @@ /** | ||
*/ | ||
var request,USER_AGENT="pageviews.js",environment="undefined"==typeof window?"node":"browser";if("node"===environment){request=require("request");var packageJson=require("./package.json");USER_AGENT="pageviews.js–v"+packageJson.version+" ("+packageJson.repository.url+")"}else request=function(e,r){var t=new XMLHttpRequest;t.addEventListener("load",function(){return r(null,{statusCode:this.status},this.responseText)}),t.addEventListener("error",function(e){return r(e)}),t.open("GET",e.url),t.send()};var pageviews=function(){var e="https://wikimedia.org/api/rest_v1",r={"default":"all-access",allowed:["all-access","desktop","mobile-web","mobile-app"]},t={"default":"all-agents",allowed:["all-agents","user","spider","bot"]},a={"default":"hourly",allowed:["daily","hourly","monthly"]},n={"default":"daily",allowed:["daily"]},i=function(e,i){var s=function(e){return 10>e?"0"+e:e};if(!e)return new Error("Required parameters missing.");if(!e.project&&!e.projects)return"getAggregatedPageviews"===i||"getTopPageviews"===i?new Error('Required parameter "project" or "projects" missing.'):new Error('Required parameter "project" missing.');if(e.project&&"all-projects"!==e.project&&-1===e.project.indexOf("."))return new Error('Required parameter "project" invalid.');if(("getAggregatedPageviews"===i||"getTopPageviews"===i)&&e.projects&&"all-projects"!=e.projects&&(!Array.isArray(e.projects)||!e.projects.length||e.projects.filter(function(e){return-1===e.indexOf(".")&&"all-projects"!==e}).length))return new Error('Required parameter "projects" invalid.');if("getPerArticlePageviews"===i){if(!e.article&&!e.articles)return new Error('Required parameter "article" or "articles" missing.');if(e.articles&&(!Array.isArray(e.articles)||!e.articles.length))return new Error('Required parameter "articles" invalid.')}if("getPerArticlePageviews"===i){if(!e.start)return new Error('Required parameter "start" missing.');if(e.start="object"==typeof e.start?e.start.getFullYear()+s(e.start.getMonth()+1)+s(e.start.getDate()):e.start,!/^(?:19|20)\d\d[- \/.]?(?:0[1-9]|1[012])[- \/.]?(?:0[1-9]|[12][0-9]|3[01])$/.test(e.start))return new Error('Required parameter "start" invalid.');if(!e.end)return new Error('Required parameter "end" missing.');if(e.end="object"==typeof e.end?e.end.getFullYear()+s(e.end.getMonth()+1)+s(e.end.getDate()):e.end,!/^(19|20)\d\d[- \/.]?(0[1-9]|1[012])[- \/.]?(0[1-9]|[12][0-9]|3[01])$/.test(e.end))return new Error('Required parameter "end" invalid.')}else if("getAggregatedPageviews"===i){if(!e.start)return new Error('Required parameter "end" missing.');if(e.start="object"==typeof e.start?e.start.getFullYear()+s(e.start.getMonth()+1)+s(e.start.getDate())+s(e.start.getHours()):e.start,!/^(?:19|20)\d\d[- \/.]?(?:0[1-9]|1[012])[- \/.]?(?:0[1-9]|[12][0-9]|3[01])[- \/.]?(?:[012][0-9])$/.test(e.start))return new Error('Required parameter "start" missing or invalid.');if(!e.end)return new Error('Required parameter "end" missing.');if(e.end="object"==typeof e.end?e.end.getFullYear()+s(e.end.getMonth()+1)+s(e.end.getDate())+s(e.end.getHours()):e.end,!/^(19|20)\d\d[- \/.]?(0[1-9]|1[012])[- \/.]?(0[1-9]|[12][0-9]|3[01])[- \/.]?(?:[012][0-9])$/.test(e.end))return new Error('Required parameter "end" missing or invalid.')}if("getTopPageviews"===i){if(e.date&&(e.date="object"==typeof e.date?e.date:new Date(e.date.substr(0,4)+"-"+e.date.substr(4,2)+"-"+e.date.substr(6,2)),e.year=e.date.getFullYear(),e.month=s(e.date.getMonth()+1),e.day=s(e.date.getDate())),!e.year||!/^(?:19|20)\d\d$/.test(e.year))return new Error('Required parameter "year" missing or invalid.');if(!e.month||!/^(?:0?[1-9]|1[012])$/.test(e.month))return new Error('Required parameter "month" missing or invalid.');if(!e.day||!/^(?:0?[1-9]|[12][0-9]|3[01])$/.test(e.day))return new Error('Required parameter "day" missing or invalid.');if(e.limit&&!/^\d+$/.test(e.limit)&&0<e.limit&&e.limit<=1e3)return new Error('Invalid optional parameter "limit".')}if(e.access&&-1===r.allowed.indexOf(e.access))return new Error('Invalid optional parameter "access".');if(e.agent&&-1===t.allowed.indexOf(e.agent))return new Error('Invalid optional parameter "agent".');if(e.granularity)if("getAggregatedPageviews"===i){if(-1===a.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".')}else if("getPerArticlePageviews"===i&&-1===n.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".');return e},s=function(e,r,t){var a;if(e||200!==r.statusCode){if(e)return e;if(404===r.statusCode)try{return a=JSON.parse(t),new Error(a.detail)}catch(n){return new Error(n)}return new Error("Status code "+r.statusCode)}try{a=JSON.parse(t)}catch(n){return new Error(n)}return a},o=function(a){return new Promise(function(u,d){if(a=i(a,"getPerArticlePageviews"),a.stack)return d(a);if(a.articles){var l=[];return a.articles.map(function(e,r){var t=a;delete t.articles,t.article=e,l[r]=o(t)}),u(Promise.all(l))}var c=a.project,g=encodeURIComponent(a.article.replace(/\s/g,"_")),p=a.start,f=a.end,m=a.access?a.access:r["default"],w=a.agent?a.agent:t["default"],v=a.granularity?a.granularity:n["default"],y={url:e+"/metrics/pageviews/per-article/"+c+"/"+m+"/"+w+"/"+g+"/"+v+"/"+p+"/"+f,headers:{"User-Agent":USER_AGENT}};request(y,function(e,r,t){var a=s(e,r,t);return a.stack?d(a):u(a)})})},u=function(n){return new Promise(function(o,d){if(n=i(n,"getAggregatedPageviews"),n.stack)return d(n);if("all-projects"===n.projects&&(n.projects=null,n.project="all-projects"),n.projects){var l=[];return n.projects.map(function(e,r){var t=n;delete t.projects,t.project=e,l[r]=u(t)}),o(Promise.all(l))}var c=n.project,g=n.start,p=n.end,f=n.access?n.access:r["default"],m=n.agent?n.agent:t["default"],w=n.granularity?n.granularity:a["default"],v={url:e+"/metrics/pageviews/aggregate/"+c+"/"+f+"/"+m+"/"+w+"/"+g+"/"+p,headers:{"User-Agent":USER_AGENT}};request(v,function(e,r,t){var a=s(e,r,t);return a.stack?d(a):o(a)})})},d=function(t){return new Promise(function(a,n){if(t=i(t,"getTopPageviews"),t.stack)return n(t);if(t.projects){var o=[];return t.projects.map(function(e,r){var a=t;delete a.projects,a.project=e,o[r]=d(a)}),a(Promise.all(o))}var u=t.project,l=t.year,c="number"==typeof t.month&&t.month<10?"0"+t.month:t.month,g="number"==typeof t.day&&t.day<10?"0"+t.day:t.day,p=t.limit||!1,f=t.access?t.access:r["default"],m={url:e+"/metrics/pageviews/top/"+u+"/"+f+"/"+l+"/"+c+"/"+g,headers:{"User-Agent":USER_AGENT}};request(m,function(e,r,t){var i=s(e,r,t);return i.stack?n(i):(p&&(i.items[0].articles=i.items[0].articles.slice(0,p)),a(i))})})},l=function(){return new Promise(function(r,t){var a={url:e+"/metrics/pageviews/",headers:{"User-Agent":USER_AGENT}};request(a,function(e,a,n){var i=s(e,a,n);return i.stack?t(i):r(i)})})};return{getPageviewsDimensions:l,getPerArticlePageviews:o,getAggregatedPageviews:u,getTopPageviews:d}}();"node"===environment&&(module.exports=pageviews); | ||
var request,USER_AGENT="pageviews.js",environment="undefined"==typeof window?"node":"browser";if("node"===environment){request=require("request");var packageJson=require("./package.json");USER_AGENT="pageviews.js–v"+packageJson.version+" ("+packageJson.repository.url+")"}else request=function(e,r){var t=new XMLHttpRequest;t.addEventListener("load",function(){return r(null,{statusCode:this.status},this.responseText)}),t.addEventListener("error",function(e){return r(e)}),t.open("GET",e.url),t.send()};var pageviews=function(){var e="https://wikimedia.org/api/rest_v1",r={"default":"all-access",allowed:["all-access","desktop","mobile-web","mobile-app"]},t={"default":"all-agents",allowed:["all-agents","user","spider","bot"]},a={"default":"hourly",allowed:["daily","hourly","monthly"]},n={"default":"daily",allowed:["daily"]},i=function(e,i){var s=function(e){return 10>e?"0"+e:e.toString()};if(!e)return new Error("Required parameters missing.");if(!e.project&&!e.projects)return"getAggregatedPageviews"===i||"getTopPageviews"===i?new Error('Required parameter "project" or "projects" missing.'):new Error('Required parameter "project" missing.');if(e.project&&"all-projects"!==e.project&&"wikidata"!==e.project&&-1===e.project.indexOf("."))return new Error('Required parameter "project" invalid.');if(("getAggregatedPageviews"===i||"getTopPageviews"===i)&&e.projects&&"all-projects"!=e.projects&&(!Array.isArray(e.projects)||!e.projects.length||e.projects.filter(function(e){return-1===e.indexOf(".")&&"all-projects"!==e&&"wikidata"!==e}).length))return new Error('Required parameter "projects" invalid.');if("getPerArticlePageviews"===i){if(!e.article&&!e.articles)return new Error('Required parameter "article" or "articles" missing.');if(e.articles&&(!Array.isArray(e.articles)||!e.articles.length))return new Error('Required parameter "articles" invalid.')}if("getPerArticlePageviews"===i){if(!e.start)return new Error('Required parameter "start" missing.');if(e.start="object"==typeof e.start?e.start.getUTCFullYear()+s(e.start.getUTCMonth()+1)+s(e.start.getUTCDate()):e.start,!/^(?:19|20)\d\d[- \/.]?(?:0[1-9]|1[012])[- \/.]?(?:0[1-9]|[12][0-9]|3[01])$/.test(e.start))return new Error('Required parameter "start" invalid.');if(!e.end)return new Error('Required parameter "end" missing.');if(e.end="object"==typeof e.end?e.end.getUTCFullYear()+s(e.end.getUTCMonth()+1)+s(e.end.getUTCDate()):e.end,!/^(19|20)\d\d[- \/.]?(0[1-9]|1[012])[- \/.]?(0[1-9]|[12][0-9]|3[01])$/.test(e.end))return new Error('Required parameter "end" invalid.')}else if("getAggregatedPageviews"===i){if(!e.start)return new Error('Required parameter "end" missing.');if(e.start="object"==typeof e.start?e.start.getUTCFullYear()+s(e.start.getUTCMonth()+1)+(s(e.start.getUTCDate())+s(e.start.getUTCHours())):e.start,!/^(?:19|20)\d\d[- \/.]?(?:0[1-9]|1[012])[- \/.]?(?:0[1-9]|[12][0-9]|3[01])[- \/.]?(?:[012][0-9])$/.test(e.start))return new Error('Required parameter "start" missing or invalid.');if(!e.end)return new Error('Required parameter "end" missing.');if(e.end="object"==typeof e.end?e.end.getUTCFullYear()+s(e.end.getUTCMonth()+1)+s(e.end.getUTCDate())+s(e.end.getUTCHours()):e.end,!/^(19|20)\d\d[- \/.]?(0[1-9]|1[012])[- \/.]?(0[1-9]|[12][0-9]|3[01])[- \/.]?(?:[012][0-9])$/.test(e.end))return new Error('Required parameter "end" missing or invalid.')}if("getTopPageviews"===i){if(e.date&&(e.date="object"==typeof e.date?e.date:new Date(e.date.substr(0,4)+"-"+e.date.substr(4,2)+"-"+e.date.substr(6,2)),e.year=e.date.getUTCFullYear(),e.month=s(e.date.getUTCMonth()+1),e.day=s(e.date.getUTCDate())),!e.year||!/^(?:19|20)\d\d$/.test(e.year))return new Error('Required parameter "year" missing or invalid.');if(!e.month||!/^(?:0?[1-9]|1[012])$/.test(e.month))return new Error('Required parameter "month" missing or invalid.');if(!e.day||!/^(?:0?[1-9]|[12][0-9]|3[01])$/.test(e.day))return new Error('Required parameter "day" missing or invalid.');if(e.limit&&!/^\d+$/.test(e.limit)&&0<e.limit&&e.limit<=1e3)return new Error('Invalid optional parameter "limit".')}if(e.access&&-1===r.allowed.indexOf(e.access))return new Error('Invalid optional parameter "access".');if(e.agent&&-1===t.allowed.indexOf(e.agent))return new Error('Invalid optional parameter "agent".');if(e.granularity)if("getAggregatedPageviews"===i){if(-1===a.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".')}else if("getPerArticlePageviews"===i&&-1===n.allowed.indexOf(e.granularity))return new Error('Invalid optional parameter "granularity".');return e},s=function(e,r,t){var a;if(e||200!==r.statusCode){if(e)return e;if(404===r.statusCode)try{return a=JSON.parse(t),new Error(a.detail)}catch(n){return new Error(n)}return new Error("Status code "+r.statusCode)}try{a=JSON.parse(t)}catch(n){return new Error(n)}return a},o=function(a){return new Promise(function(d,u){if(a=i(a,"getPerArticlePageviews"),a.stack)return u(a);if(a.articles){var l=[];return a.articles.map(function(e,r){var t=a;delete t.articles,t.article=e,l[r]=o(t)}),d(Promise.all(l))}var c=a.project,g=encodeURIComponent(a.article.replace(/\s/g,"_")),p=a.start,f=a.end,m=a.access?a.access:r["default"],w=a.agent?a.agent:t["default"],v=a.granularity?a.granularity:n["default"],j={url:e+"/metrics/pageviews/per-article/"+c+"/"+m+"/"+w+"/"+g+"/"+v+"/"+p+"/"+f,headers:{"User-Agent":USER_AGENT}};request(j,function(e,r,t){var a=s(e,r,t);return a.stack?u(a):d(a)})})},d=function(n){return new Promise(function(o,u){if(n=i(n,"getAggregatedPageviews"),n.stack)return u(n);if("all-projects"===n.projects&&(n.projects=null,n.project="all-projects"),n.projects){var l=[];return n.projects.map(function(e,r){var t=n;delete t.projects,t.project=e,l[r]=d(t)}),o(Promise.all(l))}var c=n.project,g=n.start,p=n.end,f=n.access?n.access:r["default"],m=n.agent?n.agent:t["default"],w=n.granularity?n.granularity:a["default"],v={url:e+"/metrics/pageviews/aggregate/"+c+"/"+f+"/"+m+"/"+w+"/"+g+"/"+p,headers:{"User-Agent":USER_AGENT}};request(v,function(e,r,t){var a=s(e,r,t);return a.stack?u(a):o(a)})})},u=function(t){return new Promise(function(a,n){if(t=i(t,"getTopPageviews"),t.stack)return n(t);if(t.projects){var o=[];return t.projects.map(function(e,r){var a=t;delete a.projects,a.project=e,o[r]=u(a)}),a(Promise.all(o))}var d=t.project,l=t.year,c="number"==typeof t.month&&t.month<10?"0"+t.month:t.month,g="number"==typeof t.day&&t.day<10?"0"+t.day:t.day,p=t.limit||!1,f=t.access?t.access:r["default"],m={url:e+"/metrics/pageviews/top/"+d+"/"+f+"/"+l+"/"+c+"/"+g,headers:{"User-Agent":USER_AGENT}};request(m,function(e,r,t){var i=s(e,r,t);return i.stack?n(i):(p&&(i.items[0].articles=i.items[0].articles.slice(0,p)),a(i))})})},l=function(){return new Promise(function(r,t){var a={url:e+"/metrics/pageviews/",headers:{"User-Agent":USER_AGENT}};request(a,function(e,a,n){var i=s(e,a,n);return i.stack?t(i):r(i)})})};return{getPageviewsDimensions:l,getPerArticlePageviews:o,getAggregatedPageviews:d,getTopPageviews:u}}();"node"===environment&&(module.exports=pageviews); |
@@ -49,2 +49,13 @@ var environment = typeof window === 'undefined' ? 'node' : 'browser'; | ||
it('Returns pageviews for a single article (wikidata).', function() { | ||
return pageviews.getPerArticlePageviews({ | ||
article: 'Q42', | ||
project: 'wikidata', | ||
start: '20151201', | ||
end: '20151202' | ||
}).then(function(result) { | ||
assert.isNumber(result.items[0].views); | ||
}); | ||
}); | ||
it('Returns pageviews for a single article (date object).', function() { | ||
@@ -61,2 +72,15 @@ return pageviews.getPerArticlePageviews({ | ||
it('Returns pageviews for a single article (date object, no padding).', | ||
function() { | ||
return pageviews.getPerArticlePageviews({ | ||
article: 'Berlin', | ||
project: 'en.wikipedia', | ||
start: new Date('2015-12-10'), | ||
end: new Date('2015-12-20') | ||
}).then(function(result) { | ||
assert.isNumber(result.items[0].views); | ||
assert.equal(result.items[0].timestamp, '2015121000'); | ||
}); | ||
}); | ||
it('Returns pageviews for multiple articles (date string).', function() { | ||
@@ -106,2 +130,15 @@ return pageviews.getPerArticlePageviews({ | ||
it('Returns aggregated pageviews for a single project ' + | ||
'(date object, no padding).', function() { | ||
return pageviews.getAggregatedPageviews({ | ||
project: 'en.wikipedia', | ||
start: new Date('2015-12-10'), | ||
end: new Date('2015-12-20'), | ||
granularity: 'daily' | ||
}).then(function(result) { | ||
assert(result.items[0].views >= 0 && result.items[1].views >= 0); | ||
assert.equal(result.items[0].timestamp, '2015121000'); | ||
}); | ||
}); | ||
it('Returns aggregated pageviews for multiple projects (date string).', | ||
@@ -140,4 +177,4 @@ function() { | ||
it('Returns aggregated pageviews for all projects (plural) and a particular project (date object).', | ||
function() { | ||
it('Returns aggregated pageviews for all projects (plural) ' + | ||
'and a particular project (date object).', function() { | ||
return pageviews.getAggregatedPageviews({ | ||
@@ -144,0 +181,0 @@ projects: ['all-projects', 'en.wikipedia'], |
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
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
53134
793