@gooddata/js-utils
Advanced tools
Comparing version 3.3.0 to 3.4.0-tomsatgd-cleanup-2019-08-26T10-04-54-358Z
{ | ||
"name": "@gooddata/js-utils", | ||
"version": "3.3.0", | ||
"version": "3.4.0-tomsatgd-cleanup-2019-08-26T10-04-54-358Z", | ||
"description": "Various utils shared on GoodData frontend", | ||
@@ -28,5 +28,3 @@ "repository": { | ||
"scripts": { | ||
"build:es": "rm -rf es && tsc -p tsconfig.build.json", | ||
"build:commonjs": "rm -rf lib && BABEL_ENV=commonjs babel es --out-dir lib", | ||
"build": "yarn build:es && yarn build:commonjs", | ||
"build": "rm -rf dist && tsc -p tsconfig.build.json", | ||
"prepublishOnly": "yarn build", | ||
@@ -44,7 +42,2 @@ "test": "jest --watch", | ||
"@types/js-cookie": "2.1.0", | ||
"@types/lodash-es": "4.17.1", | ||
"babel-cli": "6.26.0", | ||
"babel-plugin-lodash": "3.3.2", | ||
"babel-preset-es2015": "6.24.1", | ||
"babel-preset-stage-2": "6.24.1", | ||
"jest": "22.4.3", | ||
@@ -56,5 +49,3 @@ "jest-junit": "3.0.0", | ||
"dependencies": { | ||
"js-cookie": "^2.1.4", | ||
"lodash": "4.17.11", | ||
"lodash-es": "4.17.11" | ||
"js-cookie": "^2.1.4" | ||
}, | ||
@@ -64,8 +55,4 @@ "license": "BSD-3-Clause", | ||
"transform": { | ||
"^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest", | ||
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js" | ||
}, | ||
"transformIgnorePatterns": [ | ||
"<rootDir>/node_modules/(?!lodash-es/.*)" | ||
], | ||
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.ts$", | ||
@@ -79,9 +66,4 @@ "moduleFileExtensions": [ | ||
"!src/index.ts" | ||
], | ||
"globals": { | ||
"ts-jest": { | ||
"useBabelrc": true | ||
} | ||
} | ||
] | ||
} | ||
} |
@@ -25,9 +25,1 @@ GDC JavaScript utilities | ||
Default version increment is patch. | ||
## Build info | ||
This repo has 2 builds: | ||
1. ES modules (`/es`) - primary transpiled TypeScript build which uses `lodash-es` (useful for webpack 2+ and tree-shaking) | ||
2. CommonJS modules (`/lib`) - it's transpiled ES build with babel and `lodash-es` is replaced by `lodash` |
// (C) 2007-2018 GoodData Corporation | ||
import * as Cookies from 'js-cookie'; | ||
import { get } from 'lodash-es'; | ||
@@ -11,3 +10,3 @@ const LOCALE_COOKIE_NAME = 'locale'; | ||
*/ | ||
function areCookiesEnabled() { | ||
function areCookiesEnabled(): boolean { | ||
const TEST_COOKIE_NAME = 'cookiename'; | ||
@@ -29,5 +28,5 @@ const TEST_COOKIE_VALUE = 'cookievalue'; | ||
* Gets locale from cookies. | ||
* @returns {String} | ||
* @returns {String|undefined} | ||
*/ | ||
function getLocale() { | ||
function getLocale(): string | undefined { | ||
return Cookies.get(LOCALE_COOKIE_NAME); | ||
@@ -41,3 +40,3 @@ } | ||
*/ | ||
function setLocale(value: string, days = 60) { | ||
function setLocale(value: string, days: number = 60): void { | ||
Cookies.set(LOCALE_COOKIE_NAME, value, { expires: days }); | ||
@@ -48,8 +47,10 @@ } | ||
* Set/override locale stored in cookies to whatever locale arrives in bootstrapResource. | ||
* @param {Object} data Response from bootstrapResource with user preffered language. | ||
* @param {Object} data Response from bootstrapResource with user preferred language. | ||
* @param {Array} [localeKeys] Array of supported locale (xx-XX, e.g. de-DE) | ||
* @param {String} [localeDefault=en-US] Default locale when user preffered language is not supported. | ||
* @param {Object} [deps] Dependecies for testing purposes. | ||
* @param {String} [localeDefault=en-US] Default locale when user preferred language is not supported. | ||
* @param {Object} [deps] Dependencies for testing purposes. | ||
*/ | ||
export function syncCookieWithBootstrap(data: any, localeKeys: string[] = [], localeDefault = 'en-US', deps: any = {}) { | ||
export function syncCookieWithBootstrap( | ||
data: any, localeKeys: string[] = [], localeDefault: string = 'en-US', deps: any = {} | ||
): void { | ||
const { | ||
@@ -61,3 +62,9 @@ getLocaleCookie = getLocale, | ||
const localeBootstrap = get(data, ['bootstrapResource', 'accountSetting', 'language']); | ||
let localeBootstrap; | ||
if ( | ||
data !== undefined && | ||
data.bootstrapResource !== undefined && | ||
data.bootstrapResource.accountSetting !== undefined) { | ||
localeBootstrap = data.bootstrapResource.accountSetting.language; | ||
} | ||
const localeCookie = getLocaleCookie(); | ||
@@ -64,0 +71,0 @@ const locale = localeKeys.includes(localeBootstrap) ? localeBootstrap : localeDefault; |
// (C) 2007-2018 GoodData Corporation | ||
/** | ||
* Checks if hostname belongs to production server | ||
* Checks if hostname belongs to production server. | ||
* @param {String} hostname hostname to check | ||
@@ -5,0 +5,0 @@ * @returns {Boolean} whether hostname is production or not |
@@ -11,3 +11,5 @@ // (C) 2007-2018 GoodData Corporation | ||
export function createScriptTag(url: string, onLoad: any, onError: any): HTMLElement { | ||
export function createScriptTag( | ||
url: string, onLoad: HTMLScriptElement['onload'], onError: HTMLScriptElement['onerror'] | ||
): HTMLScriptElement { | ||
const script = document.createElement('script'); | ||
@@ -14,0 +16,0 @@ script.charset = 'utf-8'; |
// (C) 2007-2018 GoodData Corporation | ||
import { get } from 'lodash-es'; | ||
let host: any; | ||
export interface IGdcMessageEvent extends MessageEvent { | ||
data: { | ||
gdc: { | ||
product: string; | ||
event: { | ||
name: string; | ||
data: any; | ||
} | ||
} | ||
}; | ||
} | ||
export type GdcMessageEventListener = (event: IGdcMessageEvent) => boolean; | ||
export interface IHost { | ||
postMessage?: Window['postMessage']; | ||
parent?: IHost; | ||
} | ||
let host: IHost; | ||
try { | ||
@@ -13,7 +30,10 @@ // eslint-disable-next-line no-restricted-globals | ||
// enable unit testing | ||
export const setHost = (h: any) => { | ||
export const setHost = (h: IHost): void => { | ||
host = h; | ||
}; | ||
export const postEvent = (product: any, name: any, data: any) => { | ||
export const postEvent = (product: string, name: string, data: any): void => { | ||
if (!host.postMessage) { | ||
return; | ||
} | ||
host.postMessage({ | ||
@@ -27,4 +47,12 @@ gdc: { | ||
const receivers: any = []; | ||
let config: any = { | ||
interface IReceiverEntry { | ||
listener: GdcMessageEventListener; | ||
receiver: GdcMessageEventListener; | ||
} | ||
const receivers: IReceiverEntry[] = []; | ||
interface IConfig { | ||
product: string; | ||
validReceivedPostEvents: string[]; | ||
} | ||
let config: IConfig = { | ||
product: '', | ||
@@ -34,16 +62,18 @@ validReceivedPostEvents: [] | ||
const receiveListener = (listener: any) => (event: any) => { | ||
return ( | ||
get(event, 'data.gdc.product') === config.product && | ||
config.validReceivedPostEvents.includes(get(event, 'data.gdc.event.name')) ? // check for valid incoming command | ||
listener(event) : | ||
false | ||
); | ||
const receiveListener = (listener: GdcMessageEventListener): GdcMessageEventListener => (event: IGdcMessageEvent) => { | ||
if (event.data !== undefined && event.data.gdc !== undefined && event.data.gdc.event !== undefined) { | ||
const product = event.data.gdc.product; | ||
const eventName = event.data.gdc.event.name; | ||
if (product === config.product && config.validReceivedPostEvents.includes(eventName)) { | ||
return listener(event); | ||
} | ||
} | ||
return false; | ||
}; | ||
export function setConfig(product: any, validReceivedPostEvents: any) { | ||
export function setConfig(product: string, validReceivedPostEvents: string[]): void { | ||
config = { product, validReceivedPostEvents }; | ||
} | ||
export function addListener(listener: any, target = window) { | ||
export function addListener(listener: GdcMessageEventListener, target = window) { | ||
const receiver = receiveListener(listener); | ||
@@ -54,4 +84,4 @@ receivers.push({ listener, receiver }); | ||
export function removeListener(listener: any, target = window) { | ||
const receiverObj = receivers.find((r: any) => r.listener === listener); | ||
export function removeListener(listener: GdcMessageEventListener, target = window) { | ||
const receiverObj = receivers.find(r => r.listener === listener); | ||
if (receiverObj) { | ||
@@ -58,0 +88,0 @@ receivers.splice(receivers.indexOf(receiverObj), 1); |
// (C) 2007-2018 GoodData Corporation | ||
export interface IShortenTextOptions { | ||
maxLength?: number; | ||
} | ||
/** | ||
@@ -7,15 +11,7 @@ * @param {String} value string to be shortened | ||
*/ | ||
export interface IShortenTextOptions { | ||
maxLength?: number; | ||
} | ||
export function shortenText(value: string, options?: IShortenTextOptions) { | ||
// tslint:disable-next-line:no-parameter-reassignment | ||
options = options || {}; | ||
export function shortenText(value: string, options: IShortenTextOptions = {}): string { | ||
const { maxLength } = options; | ||
if (value && maxLength && value.length > maxLength) { | ||
// tslint:disable-next-line:no-parameter-reassignment | ||
value = `${value.substr(0, maxLength)}…`; | ||
return `${value.substr(0, maxLength)}…`; | ||
} | ||
@@ -51,6 +47,6 @@ | ||
* Replaces non-alphanumerical characters with underscore. | ||
* @param {String} value string to perform replacement on | ||
* @param {String|Number|null} value string to perform replacement on | ||
* @returns {String} | ||
*/ | ||
export function simplifyText(value: string | number): string { | ||
export function simplifyText(value: string | number | null): string { | ||
const s = value && value.toString ? value.toString() : ''; | ||
@@ -57,0 +53,0 @@ return s.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase(); |
@@ -1,2 +0,2 @@ | ||
// (C) 2007-2018 GoodData Corporatio | ||
// (C) 2007-2018 GoodData Corporation | ||
import { syncCookieWithBootstrap } from '../cookies'; | ||
@@ -3,0 +3,0 @@ |
// (C) 2007-2018 GoodData Corporation | ||
import { setHost, postEvent, setConfig, addListener, removeListener } from '../post-events'; | ||
import { setHost, postEvent, setConfig, addListener, removeListener, IGdcMessageEvent } from '../post-events'; | ||
@@ -34,3 +34,3 @@ describe('Post events', () => { | ||
beforeEach(() => { | ||
listener = (e: any) => e.data.gdc.event.data; | ||
listener = (e: IGdcMessageEvent) => e.data.gdc.event.data; | ||
target = { | ||
@@ -70,3 +70,62 @@ addEventListener: jest.fn(), | ||
}); | ||
it('should not accept an event without data property', () => { | ||
addListener(listener, target); | ||
const receiver = target.addEventListener.mock.calls[0][1]; | ||
expect(receiver({})).toEqual(false); | ||
}); | ||
it('should not accept an event without data.gdc', () => { | ||
addListener(listener, target); | ||
const receiver = target.addEventListener.mock.calls[0][1]; | ||
expect(receiver({ data: {}})).toEqual(false); | ||
}); | ||
it('should not accept an event without data.gdc.product', () => { | ||
addListener(listener, target); | ||
const receiver = target.addEventListener.mock.calls[0][1]; | ||
expect(receiver({ | ||
data: { | ||
gdc: { | ||
event: { | ||
name: 'postEventName', | ||
data: { foo: 'bar' } | ||
} | ||
} | ||
} | ||
})).toEqual(false); | ||
}); | ||
it('should not accept an event without data.gdc.event', () => { | ||
addListener(listener, target); | ||
const receiver = target.addEventListener.mock.calls[0][1]; | ||
expect(receiver({ | ||
data: { | ||
gdc: { | ||
product: 'productName', | ||
} | ||
} | ||
})).toEqual(false); | ||
}); | ||
it('should not accept an event without data.gdc.event.name', () => { | ||
addListener(listener, target); | ||
const receiver = target.addEventListener.mock.calls[0][1]; | ||
expect(receiver({ | ||
data: { | ||
gdc: { | ||
product: 'productName', | ||
event: { | ||
data: { foo: 'bar' } | ||
} | ||
} | ||
} | ||
})).toEqual(false); | ||
}); | ||
}); | ||
}); |
@@ -20,3 +20,3 @@ // (C) 2007-2018 GoodData Corporation | ||
it('should not load trialSnippet when env is not defined', () => { | ||
const env: string = undefined; | ||
const env: string | undefined = undefined; | ||
@@ -28,3 +28,3 @@ load(env); | ||
it('should not load trialSnippet when env is deferent than prod or dev', () => { | ||
const env: string = 'fakeEnv'; | ||
const env: string = 'fakeEnv'; | ||
@@ -36,3 +36,3 @@ load(env); | ||
it('should load dev trialSnippet when env is dev', () => { | ||
const env: string = 'dev'; | ||
const env: string = 'dev'; | ||
@@ -47,3 +47,3 @@ load(env); | ||
it('should load prod trialSnippet when env is prod', () => { | ||
const env: string = 'prod'; | ||
const env: string = 'prod'; | ||
@@ -50,0 +50,0 @@ load(env); |
// (C) 2007-2018 GoodData Corporation | ||
export function delay(timeout = 0) { | ||
export function delay(timeout = 0): Promise<void> { | ||
return new Promise((resolve) => { | ||
@@ -4,0 +4,0 @@ setTimeout(() => { |
@@ -8,3 +8,3 @@ // (C) 2007-2018 GoodData Corporation | ||
function generateFinalUrl(environment: string): string { | ||
function generateFinalUrl(environment: string): string | null { | ||
if (environment === 'dev') { | ||
@@ -19,3 +19,3 @@ return DEVELOPMENT_SNIPPET; | ||
export function load(environment: string): Promise<any> { | ||
export function load(environment: string | undefined): Promise<any> { | ||
if (!environment) { return Promise.resolve(); } | ||
@@ -22,0 +22,0 @@ |
@@ -8,11 +8,14 @@ // (C) 2007-2018 GoodData Corporation | ||
export function generateFinalUrl(walkmeHashFromWhiteLabel: string, environment?: string): string { | ||
let env = ''; | ||
function getEnvironment(walkmeHashFromWhiteLabel: string, environment?: string) { | ||
if (environment) { | ||
env = environment; | ||
} else if (walkmeHashFromWhiteLabel === GDC_WALKME_HASH) { | ||
env = isProductionHostname(window.location.hostname) ? '' : 'test'; | ||
return environment; | ||
} else if (walkmeHashFromWhiteLabel === GDC_WALKME_HASH && !isProductionHostname(window.location.hostname)) { | ||
return 'test'; | ||
} | ||
return ''; | ||
} | ||
export function generateFinalUrl(walkmeHashFromWhiteLabel: string, environment?: string): string { | ||
const env = getEnvironment(walkmeHashFromWhiteLabel, environment); | ||
return [ | ||
@@ -35,6 +38,6 @@ GDC_WALKME_URL, | ||
return loadScript(url) // load walkme script | ||
.catch(() => { | ||
// tslint:disable-next-line:no-console | ||
console.log(`Error loading walkme library. Please check the presence of ${url}`); | ||
}); | ||
.catch(() => { | ||
// tslint:disable-next-line:no-console | ||
console.log(`Error loading walkme library. Please check the presence of ${url}`); | ||
}); | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
7
32072
21
731
2
25
- Removedlodash@4.17.11
- Removedlodash-es@4.17.11
- Removedlodash@4.17.11(transitive)
- Removedlodash-es@4.17.11(transitive)