@guardian/libs
Advanced tools
Comparing version 1.4.1 to 1.4.2
import type { CountryCode } from './types/countries'; | ||
export declare const __resetCachedValue: () => void; | ||
/** | ||
@@ -3,0 +4,0 @@ * Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getLocale = void 0; | ||
exports.getLocale = exports.__resetCachedValue = void 0; | ||
const isString_1 = require("./isString"); | ||
@@ -11,2 +11,6 @@ const storage_1 = require("./storage"); | ||
const daysFromNow = (days) => new Date().getTime() + 60 * 60 * 24 * days; | ||
// we'll cache any successful lookups so we only have to do this once | ||
let locale; | ||
// just used for tests | ||
exports.__resetCachedValue = () => (locale = void 0); | ||
/** | ||
@@ -16,2 +20,4 @@ * Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc | ||
exports.getLocale = async () => { | ||
if (locale) | ||
return locale; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- it _is_ any | ||
@@ -21,3 +27,3 @@ const stored = storage_1.storage.local.get(KEY); | ||
if (isValidCountryCode(stored)) | ||
return stored; | ||
return (locale = stored); | ||
// use our API to get one | ||
@@ -31,3 +37,3 @@ try { | ||
// return it | ||
return country; | ||
return (locale = country); | ||
} | ||
@@ -34,0 +40,0 @@ } |
import type { CountryCode } from './types/countries'; | ||
export declare const __resetCachedValue: () => void; | ||
/** | ||
@@ -3,0 +4,0 @@ * Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc |
@@ -8,2 +8,6 @@ import { isString } from './isString'; | ||
const daysFromNow = (days) => new Date().getTime() + 60 * 60 * 24 * days; | ||
// we'll cache any successful lookups so we only have to do this once | ||
let locale; | ||
// just used for tests | ||
export const __resetCachedValue = () => (locale = void 0); | ||
/** | ||
@@ -13,2 +17,4 @@ * Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc | ||
export const getLocale = async () => { | ||
if (locale) | ||
return locale; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- it _is_ any | ||
@@ -18,3 +24,3 @@ const stored = storage.local.get(KEY); | ||
if (isValidCountryCode(stored)) | ||
return stored; | ||
return (locale = stored); | ||
// use our API to get one | ||
@@ -28,3 +34,3 @@ try { | ||
// return it | ||
return country; | ||
return (locale = country); | ||
} | ||
@@ -31,0 +37,0 @@ } |
@@ -15,2 +15,2 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).gu=e.gu||{},e.gu.libs={}))}(this,(function(e){"use strict"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function t(e,t,n,o){return new(n||(n=Promise))((function(r,i){function s(e){try{c(o.next(e))}catch(e){i(e)}}function a(e){try{c(o.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))}function n(e,t){if(!t.has(e))throw new TypeError("attempted to get private field on non-instance");return t.get(e)}function o(e,t,n){if(!t.has(e))throw new TypeError("attempted to set private field on non-instance");return t.set(e,n),n}const r=e=>"[object String]"===Object.prototype.toString.call(e);var i,s,a,c;class l{constructor(e){i.set(this,void 0);try{const t=(new Date).toString();e.setItem(t,t);const n=e.getItem(t)==t;e.removeItem(t),n&&o(this,i,e)}catch(e){}}isAvailable(){return Boolean(n(this,i))}get(e){var t,o;try{const{value:r,expires:s}=JSON.parse(null!==(o=null===(t=n(this,i))||void 0===t?void 0:t.getItem(e))&&void 0!==o?o:"");return s&&new Date>new Date(s)?(this.remove(e),null):r}catch(e){return null}}set(e,t,o){var r;return null===(r=n(this,i))||void 0===r?void 0:r.setItem(e,JSON.stringify({value:t,expires:o}))}remove(e){var t;return null===(t=n(this,i))||void 0===t?void 0:t.removeItem(e)}clear(){var e;return null===(e=n(this,i))||void 0===e?void 0:e.clear()}getRaw(e){var t,o;return null!==(o=null===(t=n(this,i))||void 0===t?void 0:t.getItem(e))&&void 0!==o?o:null}setRaw(e,t){var o;return null===(o=n(this,i))||void 0===o?void 0:o.setItem(e,t)}}i=new WeakMap;const u=new(c=class{constructor(){s.set(this,void 0),a.set(this,void 0)}get local(){return o(this,s,n(this,s)||new l(localStorage))}get session(){return o(this,a,n(this,a)||new l(sessionStorage))}},s=new WeakMap,a=new WeakMap,c),d="gu.geolocation",v=e=>r(e)&&/^[A-Z]{2}$/.test(e);e.getLocale=()=>t(void 0,void 0,void 0,(function*(){const e=u.local.get(d);if(v(e))return e;try{const{country:e}=yield fetch("https://api.nextgen.guardianapps.co.uk/geolocation").then((e=>e.json()));if(v(e))return u.local.set(d,e,(t=10,(new Date).getTime()+86400*t)),e}catch(e){}var t;return null})),e.isString=r,e.loadScript=(e,t)=>new Promise(((n,o)=>{var r;const i=document.createElement("script");if(i.src=e,Array.from(document.scripts).some((({src:e})=>i.src===e)))return n();Object.assign(i,t),i.onload=n,i.onerror=()=>{o(new Error("Failed to load script "+e))};const s=document.scripts[0];null===(r=s.parentNode)||void 0===r||r.insertBefore(i,s)})),e.storage=u,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
***************************************************************************** */function t(e,t,n,o){return new(n||(n=Promise))((function(r,i){function s(e){try{c(o.next(e))}catch(e){i(e)}}function a(e){try{c(o.throw(e))}catch(e){i(e)}}function c(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}c((o=o.apply(e,t||[])).next())}))}function n(e,t){if(!t.has(e))throw new TypeError("attempted to get private field on non-instance");return t.get(e)}function o(e,t,n){if(!t.has(e))throw new TypeError("attempted to set private field on non-instance");return t.set(e,n),n}const r=e=>"[object String]"===Object.prototype.toString.call(e);var i,s,a,c;class l{constructor(e){i.set(this,void 0);try{const t=(new Date).toString();e.setItem(t,t);const n=e.getItem(t)==t;e.removeItem(t),n&&o(this,i,e)}catch(e){}}isAvailable(){return Boolean(n(this,i))}get(e){var t,o;try{const{value:r,expires:s}=JSON.parse(null!==(o=null===(t=n(this,i))||void 0===t?void 0:t.getItem(e))&&void 0!==o?o:"");return s&&new Date>new Date(s)?(this.remove(e),null):r}catch(e){return null}}set(e,t,o){var r;return null===(r=n(this,i))||void 0===r?void 0:r.setItem(e,JSON.stringify({value:t,expires:o}))}remove(e){var t;return null===(t=n(this,i))||void 0===t?void 0:t.removeItem(e)}clear(){var e;return null===(e=n(this,i))||void 0===e?void 0:e.clear()}getRaw(e){var t,o;return null!==(o=null===(t=n(this,i))||void 0===t?void 0:t.getItem(e))&&void 0!==o?o:null}setRaw(e,t){var o;return null===(o=n(this,i))||void 0===o?void 0:o.setItem(e,t)}}i=new WeakMap;const u=new(c=class{constructor(){s.set(this,void 0),a.set(this,void 0)}get local(){return o(this,s,n(this,s)||new l(localStorage))}get session(){return o(this,a,n(this,a)||new l(sessionStorage))}},s=new WeakMap,a=new WeakMap,c),d="gu.geolocation",v=e=>r(e)&&/^[A-Z]{2}$/.test(e);let f;e.getLocale=()=>t(void 0,void 0,void 0,(function*(){if(f)return f;const e=u.local.get(d);if(v(e))return f=e;try{const{country:e}=yield fetch("https://api.nextgen.guardianapps.co.uk/geolocation").then((e=>e.json()));if(v(e))return u.local.set(d,e,(t=10,(new Date).getTime()+86400*t)),f=e}catch(e){}var t;return null})),e.isString=r,e.loadScript=(e,t)=>new Promise(((n,o)=>{var r;const i=document.createElement("script");if(i.src=e,Array.from(document.scripts).some((({src:e})=>i.src===e)))return n();Object.assign(i,t),i.onload=n,i.onerror=()=>{o(new Error("Failed to load script "+e))};const s=document.scripts[0];null===(r=s.parentNode)||void 0===r||r.insertBefore(i,s)})),e.storage=u,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@guardian/libs", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"private": false, | ||
@@ -40,28 +40,29 @@ "description": "A collection of JavaScript libraries for Guardian projects", | ||
"devDependencies": { | ||
"@guardian/eslint-config": "^0.4.0", | ||
"@guardian/eslint-config-typescript": "^0.4.1", | ||
"@guardian/prettier": "^0.4.1", | ||
"@rollup/plugin-typescript": "^6.1.0", | ||
"@semantic-release/github": "^7.1.1", | ||
"@types/jest": "^26.0.15", | ||
"@typescript-eslint/eslint-plugin": "^4.6.0", | ||
"@typescript-eslint/parser": "^4.6.0", | ||
"eslint": "^7.12.1", | ||
"eslint-config-prettier": "^6.14.0", | ||
"eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jest": "^24.1.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"husky": "^4.3.0", | ||
"jest": "^26.6.1", | ||
"jest-environment-jsdom-sixteen": "^1.0.3", | ||
"jest-fetch-mock": "^3.0.3", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.1.2", | ||
"rollup": "^2.32.1", | ||
"rollup-plugin-size": "^0.2.2", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"semantic-release": "^17.2.1", | ||
"ts-jest": "^26.4.3", | ||
"typescript": "^4.0.5" | ||
"@guardian/eslint-config": "0.4.0", | ||
"@guardian/eslint-config-typescript": "0.4.1", | ||
"@guardian/prettier": "0.4.1", | ||
"@rollup/plugin-typescript": "6.1.0", | ||
"@semantic-release/github": "7.1.1", | ||
"@types/jest": "26.0.15", | ||
"@typescript-eslint/eslint-plugin": "4.7.0", | ||
"@typescript-eslint/parser": "4.7.0", | ||
"eslint": "7.13.0", | ||
"eslint-config-prettier": "6.15.0", | ||
"eslint-plugin-eslint-comments": "3.2.0", | ||
"eslint-plugin-import": "2.22.1", | ||
"eslint-plugin-jest": "24.1.0", | ||
"eslint-plugin-prettier": "3.1.4", | ||
"husky": "4.3.0", | ||
"jest": "26.6.3", | ||
"jest-environment-jsdom-sixteen": "1.0.3", | ||
"jest-fetch-mock": "3.0.3", | ||
"npm-run-all": "4.1.5", | ||
"prettier": "2.1.2", | ||
"rollup": "2.33.1", | ||
"rollup-plugin-size": "0.2.2", | ||
"rollup-plugin-terser": "7.0.2", | ||
"semantic-release": "17.2.2", | ||
"ts-jest": "26.4.4", | ||
"tslib": "2.0.3", | ||
"typescript": "4.0.5" | ||
}, | ||
@@ -68,0 +69,0 @@ "publishConfig": { |
import fetchMock from 'jest-fetch-mock'; | ||
import { getLocale } from './getLocale'; | ||
import { __resetCachedValue, getLocale } from './getLocale'; | ||
import { storage } from './storage'; | ||
@@ -12,2 +12,3 @@ | ||
storage.local.clear(); | ||
__resetCachedValue(); | ||
}); | ||
@@ -25,2 +26,3 @@ | ||
expect(locale).toBe('CZ'); | ||
expect(storage.local.get(KEY)).toBe('CZ'); | ||
}); | ||
@@ -49,2 +51,14 @@ | ||
}); | ||
it('uses the cached value if available', async () => { | ||
const spy = jest.spyOn(storage.local, 'get'); | ||
storage.local.set(KEY, 'CY'); | ||
const locale = await getLocale(); | ||
const locale2 = await getLocale(); | ||
expect(locale).toBe(locale2); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
expect(fetchMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
@@ -15,2 +15,8 @@ import { isString } from './isString'; | ||
// we'll cache any successful lookups so we only have to do this once | ||
let locale: CountryCode | undefined; | ||
// just used for tests | ||
export const __resetCachedValue = (): void => (locale = void 0); | ||
/** | ||
@@ -21,2 +27,4 @@ * Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc | ||
export const getLocale = async (): Promise<CountryCode | null> => { | ||
if (locale) return locale; | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- it _is_ any | ||
@@ -26,3 +34,3 @@ const stored = storage.local.get(KEY); | ||
// if we've got a locale, return it | ||
if (isValidCountryCode(stored)) return stored as CountryCode; | ||
if (isValidCountryCode(stored)) return (locale = stored as CountryCode); | ||
@@ -41,3 +49,3 @@ // use our API to get one | ||
// return it | ||
return country as CountryCode; | ||
return (locale = country as CountryCode); | ||
} | ||
@@ -44,0 +52,0 @@ } catch (e) { |
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
100779
2382
27