@guardian/automat-contributions
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -7,3 +7,3 @@ import { WeeklyArticleHistory } from './lib/types'; | ||
export declare const getWeeklyArticleHistory: (localStorage: LocalStorage) => WeeklyArticleHistory | undefined; | ||
export declare const incrementWeeklyArticleCount: (localStorage: LocalStorage, pageId: string) => void; | ||
export declare const incrementWeeklyArticleCount: (localStorage: LocalStorage, pageId: string, tagIds: string[]) => void; | ||
//# sourceMappingURL=history.d.ts.map |
@@ -1,1 +0,1 @@ | ||
"use strict";function e(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function t(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function r(r){for(var n=1;n<arguments.length;n++){var i=null!=arguments[n]?arguments[n]:{};n%2?t(Object(i),!0).forEach((function(t){e(r,t,i[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(i)):t(Object(i)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(i,e))}))}return r}Object.defineProperty(exports,"__esModule",{value:!0});var n,i,o="gu.history.weeklyArticleCount",c=(n=new Date,1!==(i=n.getDay()||7)&&n.setHours(-24*(i-1)),Math.floor(n.getTime()/864e5)),s="gu.contributions.views",u=function(e){return e.get(s)||void 0},a="https://contributions.guardianapis.com/epic/compare-variant-decision",p="https://contributions.guardianapis.com/epic";exports.compareVariantDecision=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:a,r=JSON.stringify(e);fetch(t,{method:"post",headers:{"Content-Type":"application/json"},body:r}).catch((function(){}))},exports.getEpicMeta=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p,r=JSON.stringify(e);return fetch(t,{method:"post",headers:{"Content-Type":"application/json"},body:r})},exports.getViewLog=u,exports.getWeeklyArticleHistory=function(e){return e.get(o)||void 0},exports.incrementWeeklyArticleCount=function(t,n){if(!function(t,n){var i=t.get("gu.history.articleCountsThisWeek");if(i&&i.week===c){var o=i.articles[n]||0;return t.set("gu.history.articleCountsThisWeek",{week:c,articles:r(r({},i.articles),{},e({},n,o+1))}),o>0}return t.set("gu.history.articleCountsThisWeek",{week:c,articles:e({},n,1)}),!1}(t,n)){var i=t.get(o)||[];if(i[0]&&i[0].week&&i[0].week===c)i[0].count+=1,t.set(o,i);else{i.unshift({week:c,count:1});var s=c-365,u=i.filter((function(e){return e.week>=s}));t.set(o,u)}}},exports.logView=function(e,t){var r=u(e)||[];r.push({date:(new Date).getTime(),testId:t}),e.set(s,r.slice(-50))}; | ||
"use strict";function e(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function t(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function r(r){for(var n=1;n<arguments.length;n++){var i=null!=arguments[n]?arguments[n]:{};n%2?t(Object(i),!0).forEach((function(t){e(r,t,i[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(i)):t(Object(i)).forEach((function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(i,e))}))}return r}Object.defineProperty(exports,"__esModule",{value:!0});var n,i,o=(n=new Date,1!==(i=n.getDay()||7)&&n.setHours(-24*(i-1)),Math.floor(n.getTime()/864e5)),s=new Set(["environment/climate-change","environment/environment","science/science","politics/politics","us-news/us-politics","australia-news/australian-politics","world/world","world/europe-news","world/russia","books/books","culture/culture","world/coronavirus-outbreak","world/race","inequality/inequality","technology/technology","business/business"]),c=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return e.forEach((function(e){var r;s.has(e)&&(t[e]=(null!==(r=t[e])&&void 0!==r?r:0)+1)})),t},u="gu.contributions.views",a=function(e){return e.get(u)||void 0},l="https://contributions.guardianapis.com/epic/compare-variant-decision",p="https://contributions.guardianapis.com/epic";exports.compareVariantDecision=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:l,r=JSON.stringify(e);fetch(t,{method:"post",headers:{"Content-Type":"application/json"},body:r}).catch((function(){}))},exports.getEpicMeta=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:p,r=JSON.stringify(e);return fetch(t,{method:"post",headers:{"Content-Type":"application/json"},body:r})},exports.getViewLog=a,exports.getWeeklyArticleHistory=function(e){return e.get("gu.history.weeklyArticleCount")||void 0},exports.incrementWeeklyArticleCount=function(t,n,i){if(!function(t,n){var i=t.get("gu.history.articleCountsThisWeek");if(i&&i.week===o){var s=i.articles[n]||0;return t.set("gu.history.articleCountsThisWeek",{week:o,articles:r(r({},i.articles),{},e({},n,s+1))}),s>0}return t.set("gu.history.articleCountsThisWeek",{week:o,articles:e({},n,1)}),!1}(t,n)){var s=t.get("gu.history.weeklyArticleCount")||[],u=s[0];if(u&&u.week&&u.week===o)u.count+=1,u.tags=c(i,u.tags),t.set("gu.history.weeklyArticleCount",s);else{s.unshift({week:o,count:1,tags:c(i,{})});var a=o-365,l=s.filter((function(e){return e.week>=a}));t.set("gu.history.weeklyArticleCount",l)}}},exports.logView=function(e,t){var r=a(e)||[];r.push({date:(new Date).getTime(),testId:t}),e.set(u,r.slice(-50))}; |
@@ -17,5 +17,9 @@ declare type Tracking = { | ||
export declare type ViewLog = View[]; | ||
export declare type TagCounts = { | ||
[tag: string]: number; | ||
}; | ||
export declare type WeeklyArticleLog = { | ||
week: number; | ||
count: number; | ||
tags?: TagCounts; | ||
}; | ||
@@ -22,0 +26,0 @@ export declare type WeeklyArticleHistory = WeeklyArticleLog[]; |
@@ -36,5 +36,5 @@ import { getMondayFromDate } from "./lib/dates"; | ||
incrementWeeklyArticleCount(storage, pageId); | ||
incrementWeeklyArticleCount(storage, pageId, []); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ week: startOfThisWeek, count: 2 }]); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ week: startOfThisWeek, count: 2, tags: {} }]); | ||
}); | ||
@@ -46,7 +46,5 @@ | ||
incrementWeeklyArticleCount(storage, pageId); | ||
incrementWeeklyArticleCount(storage, pageId, []); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([ | ||
{ week: startOfThisWeek, count: 1 }, | ||
]); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ week: startOfThisWeek, count: 1, tags: {} }]); | ||
}); | ||
@@ -62,9 +60,9 @@ | ||
incrementWeeklyArticleCount(storage, pageId); | ||
incrementWeeklyArticleCount(storage, pageId, []); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ week: startOfThisWeek, count: 2 }]); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ week: startOfThisWeek, count: 2, tags: {} }]); | ||
}); | ||
it('does not increment the weekly article count if duplicate article', () => { | ||
const counts = [{ week: startOfThisWeek, count: 1 }]; | ||
const counts = [{ week: startOfThisWeek, count: 1, tags: {} }]; | ||
const thisWeek = { | ||
@@ -76,6 +74,55 @@ week: startOfThisWeek, | ||
incrementWeeklyArticleCount(storage, pageId); | ||
incrementWeeklyArticleCount(storage, pageId, []); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ week: startOfThisWeek, count: 1 }]); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ week: startOfThisWeek, count: 1, tags: {} }]); | ||
}); | ||
it('adds a new tag', () => { | ||
const counts = [{ week: startOfThisWeek, count: 1 }]; | ||
const storage = new FakeLocalStorage(counts, articleCountsThisWeek); | ||
incrementWeeklyArticleCount(storage, pageId, ['environment/climate-change']); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ | ||
week: startOfThisWeek, | ||
count: 2, | ||
tags: { 'environment/climate-change': 1 }, | ||
}]); | ||
}); | ||
it('increments existing tags', () => { | ||
const counts = [{ | ||
week: startOfThisWeek, | ||
count: 1, | ||
tags: { | ||
'environment/climate-change': 1, | ||
'science/science': 2, | ||
} | ||
}]; | ||
const storage = new FakeLocalStorage(counts, articleCountsThisWeek); | ||
incrementWeeklyArticleCount(storage, pageId, ['environment/climate-change', 'science/science']); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ | ||
week: startOfThisWeek, | ||
count: 2, | ||
tags: { | ||
'environment/climate-change': 2, | ||
'science/science': 3, | ||
} | ||
}]); | ||
}); | ||
it('ignore tags that are not of interest', () => { | ||
const counts = [{ week: startOfThisWeek, count: 1 }]; | ||
const storage = new FakeLocalStorage(counts, articleCountsThisWeek); | ||
incrementWeeklyArticleCount(storage, pageId, ['boring-tag']); | ||
expect(getWeeklyArticleHistory(storage)).toEqual([{ | ||
week: startOfThisWeek, | ||
count: 2, | ||
tags: { }, | ||
}]); | ||
}); | ||
}); |
@@ -1,5 +0,5 @@ | ||
import { WeeklyArticleLog, WeeklyArticleHistory, ArticleCountsThisWeek } from './lib/types'; | ||
import { WeeklyArticleLog, WeeklyArticleHistory, ArticleCountsThisWeek, TagCounts } from './lib/types'; | ||
import { getMondayFromDate } from './lib/dates'; | ||
const historyWeeklyKey = 'gu.history.weeklyArticleCount'; | ||
const weeklyArticleCountKey = 'gu.history.weeklyArticleCount'; | ||
const articleCountsThisWeekKey = 'gu.history.articleCountsThisWeek'; | ||
@@ -21,3 +21,3 @@ const mondayThisWeek = getMondayFromDate(new Date()); | ||
export const getWeeklyArticleHistory = (localStorage: LocalStorage): WeeklyArticleHistory | undefined => { | ||
return localStorage.get(historyWeeklyKey) || undefined; | ||
return localStorage.get(weeklyArticleCountKey) || undefined; | ||
}; | ||
@@ -50,3 +50,35 @@ | ||
const tagsOfInterest = new Set<string>([ | ||
'environment/climate-change', | ||
'environment/environment', | ||
'science/science', | ||
'politics/politics', | ||
'us-news/us-politics', | ||
'australia-news/australian-politics', | ||
'world/world', | ||
'world/europe-news', | ||
'world/russia', | ||
'books/books', | ||
'culture/culture', | ||
'world/coronavirus-outbreak', | ||
'world/race', | ||
'inequality/inequality', | ||
'technology/technology', | ||
'business/business', | ||
]); | ||
/** | ||
* Returns a TagCounts object with incremented counts for all relevant tags in tagIds. | ||
* Actually mutates and returns the given currentTagCounts for efficiency. | ||
*/ | ||
const updateTagsCounts = (tagIds: string[], currentTagCounts: TagCounts = {}): TagCounts => { | ||
tagIds.forEach(tagId => { | ||
if (tagsOfInterest.has(tagId)) { | ||
currentTagCounts[tagId] = (currentTagCounts[tagId] ?? 0) + 1; | ||
} | ||
}); | ||
return currentTagCounts; | ||
}; | ||
/** | ||
* Increment the weekly article counter | ||
@@ -57,16 +89,19 @@ * Checks whether an object already exists for the current week | ||
*/ | ||
export const incrementWeeklyArticleCount = (localStorage: LocalStorage, pageId: string): void => { | ||
export const incrementWeeklyArticleCount = (localStorage: LocalStorage, pageId: string, tagIds: string[]): void => { | ||
const hasBeenViewedThisWeek = articleHasBeenViewedThisWeek(localStorage, pageId); | ||
if (!hasBeenViewedThisWeek) { | ||
const weeklyArticleHistory = localStorage.get(historyWeeklyKey) || []; | ||
const weeklyArticleHistory = localStorage.get(weeklyArticleCountKey) || []; | ||
const currentWeek = weeklyArticleHistory[0]; | ||
if ( | ||
weeklyArticleHistory[0] && | ||
weeklyArticleHistory[0].week && | ||
weeklyArticleHistory[0].week === mondayThisWeek | ||
currentWeek && | ||
currentWeek.week && | ||
currentWeek.week === mondayThisWeek | ||
) { | ||
// Increment this week's counter & save updated array | ||
weeklyArticleHistory[0].count += 1; | ||
localStorage.set(historyWeeklyKey, weeklyArticleHistory); | ||
currentWeek.count += 1; | ||
currentWeek.tags = updateTagsCounts(tagIds, currentWeek.tags); | ||
localStorage.set(weeklyArticleCountKey, weeklyArticleHistory); | ||
} else { | ||
@@ -77,2 +112,3 @@ // Create new counter for this week | ||
count: 1, | ||
tags: updateTagsCounts(tagIds, {}), | ||
}); | ||
@@ -87,5 +123,5 @@ | ||
// Save new array | ||
localStorage.set(historyWeeklyKey, weeksNewerThanOneYear); | ||
localStorage.set(weeklyArticleCountKey, weeksNewerThanOneYear); | ||
} | ||
} | ||
}; |
@@ -21,5 +21,10 @@ type Tracking = { | ||
export type TagCounts = { | ||
[tag: string]: number | ||
}; | ||
export type WeeklyArticleLog = { | ||
week: number; | ||
count: number; | ||
tags?: TagCounts; | ||
}; | ||
@@ -26,0 +31,0 @@ |
{ | ||
"name": "@guardian/automat-contributions", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"author": "Nicolas Long <nicolas.long@theguardian.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
27279
524
2