diff --git a/package.json b/package.json index 197584094..93d41d8ce 100644 --- a/package.json +++ b/package.json @@ -63,10 +63,7 @@ "karma-sourcemap-loader": "^0.3.7", "karma-webpack": "^2.0.6", "lint-staged": "^5.0.0", - "lodash.clonedeep": "^4.5.0", - "lodash.debounce": "^4.0.8", - "lodash.get": "^4.4.2", - "lodash.throttle": "^4.1.1", + "lodash": "^4.17.5", "mocha": "^4.0.1", "mock-local-storage": "^1.0.2", "mojito-rb-gen": "^0.0.1", diff --git a/src/lib/Controls.js b/src/lib/Controls.js index 4015be3a2..3116c4ed6 100644 --- a/src/lib/Controls.js +++ b/src/lib/Controls.js @@ -1,4 +1,4 @@ -import throttle from 'lodash.throttle'; +import throttle from 'lodash/throttle'; import Browser from './Browser'; import { CLASS_HIDDEN } from './constants'; diff --git a/src/lib/Preview.js b/src/lib/Preview.js index f22e514b0..7a9291853 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -1,9 +1,8 @@ /* eslint-disable import/first */ import './polyfill'; import EventEmitter from 'events'; -import cloneDeep from 'lodash.clonedeep'; -import getProp from 'lodash.get'; -import throttle from 'lodash.throttle'; +import cloneDeep from 'lodash/cloneDeep'; +import throttle from 'lodash/throttle'; /* eslint-enable import/first */ import Browser from './Browser'; import Logger from './Logger'; @@ -14,6 +13,7 @@ import PreviewUI from './PreviewUI'; import getTokens from './tokens'; import { get, + getProp, post, decodeKeydown, openUrlInsideIframe, diff --git a/src/lib/__tests__/util-test.js b/src/lib/__tests__/util-test.js index 77e9a346b..996fc2312 100644 --- a/src/lib/__tests__/util-test.js +++ b/src/lib/__tests__/util-test.js @@ -748,4 +748,35 @@ describe('lib/util', () => { expect(result).to.equal(null); }); }); + + describe('getProp()', () => { + it('should return prop value as specified by path', () => { + const someProp = 'some-prop'; + let a = { + b: { + c: 'value', + b: '' + }, + [someProp]: { + value: 'test' + } + }; + + expect(util.getProp(a, 'b.c')).to.equal('value'); + expect(util.getProp(a, 'b.b')).to.equal(''); + expect(util.getProp(a, `${someProp}.value`)).to.equal('test'); + }); + + it('should return default value if prop does not exist or value is undefined', () => { + let a = { + b: {}, + test: undefined, + foo: null + }; + + expect(util.getProp(a, 'b.c', 'default')).to.equal('default'); + expect(util.getProp(a, 'test', 'default')).to.equal('default'); + expect(util.getProp(a, 'foo.bar', 'default')).to.equal('default'); + }); + }); }); diff --git a/src/lib/file.js b/src/lib/file.js index 1b0887e1f..151af9107 100644 --- a/src/lib/file.js +++ b/src/lib/file.js @@ -1,4 +1,4 @@ -import { appendQueryParams } from './util'; +import { getProp, appendQueryParams } from './util'; import { ORIGINAL_REP_NAME } from './constants'; // List of Box Content API fields that the Preview library requires for every file. Updating this list is most likely @@ -64,7 +64,7 @@ export function getRepresentation(file, repName) { * @return {boolean} Whether or not file is watermarked */ export function isWatermarked(file) { - return !!file && !!file.watermark_info && file.watermark_info.is_watermarked; + return getProp(file, 'watermark_info.is_watermarked', false); } /** @@ -76,7 +76,7 @@ export function isWatermarked(file) { * @return {boolean} Whether or not action is permitted */ export function checkPermission(file, operation) { - return !!file && !!file.permissions && !!file.permissions[operation]; + return getProp(file, `permissions.${operation}`, false); } /** diff --git a/src/lib/util.js b/src/lib/util.js index 53d7e9699..8ae7f62da 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -837,3 +837,28 @@ export function getClosestPageToPinch(x, y, visiblePages) { return closestPage; } + +/** + * Simplified lodash.get, this returns the value of a nested property with string path `propPath`. If that property + * does not exist on the object, then return `defaultValue`. + * + * @param {Object} object - Object to fetch property from + * @param {string} propPath - String path to property, e.g. 'b.c' if you are trying to fetch a.b.c + * @param {*} defaultValue - Default value if property is undefined + * @return {*} Value of prop if defined, defaultValue otherwise + */ +export function getProp(object, propPath, defaultValue) { + let value = object; + const path = propPath.split('.'); + + for (let i = 0; i < path.length; i++) { + if (value == null) { + // Checks against null or undefined + return defaultValue; + } + + value = value[path[i]]; + } + + return value !== undefined ? value : defaultValue; +} diff --git a/src/lib/viewers/BaseViewer.js b/src/lib/viewers/BaseViewer.js index 63a0b57c4..5e5bce873 100644 --- a/src/lib/viewers/BaseViewer.js +++ b/src/lib/viewers/BaseViewer.js @@ -1,9 +1,10 @@ import EventEmitter from 'events'; -import debounce from 'lodash.debounce'; -import cloneDeep from 'lodash.clonedeep'; +import cloneDeep from 'lodash/cloneDeep'; +import debounce from 'lodash/debounce'; import fullscreen from '../Fullscreen'; import RepStatus from '../RepStatus'; import { + getProp, appendQueryParams, appendAuthParams, getHeaders, @@ -577,14 +578,12 @@ class BaseViewer extends EventEmitter { * * @protected * @param {string} option - to get - * @return {Object} Value of a viewer option + * @return {Object|undefined} Value of a viewer option */ getViewerOption(option) { const { viewers, viewer } = this.options; - if (viewers && viewers[viewer.NAME]) { - return viewers[viewer.NAME][option]; - } - return null; + const viewerName = getProp(viewer, 'NAME'); + return getProp(viewers, `${viewerName}.${option}`); } /** diff --git a/src/lib/viewers/__tests__/BaseViewer-test.js b/src/lib/viewers/__tests__/BaseViewer-test.js index 59d3c5744..9f344fec2 100644 --- a/src/lib/viewers/__tests__/BaseViewer-test.js +++ b/src/lib/viewers/__tests__/BaseViewer-test.js @@ -686,6 +686,10 @@ describe('lib/viewers/BaseViewer', () => { expect(base.getViewerOption('fooBar')).to.equal(baz); }); + + it('should return undefined if no matching user-defined viewer option is found', () => { + expect(base.getViewerOption('fooBar')).to.equal(undefined); + }); }); describe('loadAssets()', () => { diff --git a/src/lib/viewers/doc/DocBaseViewer.js b/src/lib/viewers/doc/DocBaseViewer.js index c9bbba934..fe2de7364 100644 --- a/src/lib/viewers/doc/DocBaseViewer.js +++ b/src/lib/viewers/doc/DocBaseViewer.js @@ -1,4 +1,4 @@ -import throttle from 'lodash.throttle'; +import throttle from 'lodash/throttle'; import BaseViewer from '../BaseViewer'; import Browser from '../../Browser'; import Controls from '../../Controls'; diff --git a/src/lib/viewers/doc/PresentationViewer.js b/src/lib/viewers/doc/PresentationViewer.js index c74bb78dc..d8f962f66 100644 --- a/src/lib/viewers/doc/PresentationViewer.js +++ b/src/lib/viewers/doc/PresentationViewer.js @@ -1,4 +1,4 @@ -import throttle from 'lodash.throttle'; +import throttle from 'lodash/throttle'; import DocBaseViewer from './DocBaseViewer'; import PresentationPreloader from './PresentationPreloader'; import { CLASS_INVISIBLE } from '../../constants'; diff --git a/src/lib/viewers/media/MediaBaseViewer.js b/src/lib/viewers/media/MediaBaseViewer.js index cd37974e3..f6a988dee 100644 --- a/src/lib/viewers/media/MediaBaseViewer.js +++ b/src/lib/viewers/media/MediaBaseViewer.js @@ -1,4 +1,4 @@ -import debounce from 'lodash.debounce'; +import debounce from 'lodash/debounce'; import BaseViewer from '../BaseViewer'; import Browser from '../../Browser'; import MediaControls from './MediaControls'; diff --git a/src/lib/viewers/media/VideoBaseViewer.js b/src/lib/viewers/media/VideoBaseViewer.js index 0928925f5..89628ce7d 100644 --- a/src/lib/viewers/media/VideoBaseViewer.js +++ b/src/lib/viewers/media/VideoBaseViewer.js @@ -1,4 +1,4 @@ -import throttle from 'lodash.throttle'; +import throttle from 'lodash/throttle'; import MediaBaseViewer from './MediaBaseViewer'; import { CLASS_HIDDEN, CLASS_IS_BUFFERING, CLASS_DARK } from '../../constants'; import { ICON_PLAY_LARGE } from '../../icons/icons'; diff --git a/yarn.lock b/yarn.lock index dbf4e7d06..06c1824a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4864,7 +4864,7 @@ lodash.camelcase@^4.3.0: version "4.3.0" resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6" -lodash.clonedeep@^4.3.2, lodash.clonedeep@^4.5.0: +lodash.clonedeep@^4.3.2: version "4.5.0" resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef" @@ -4872,10 +4872,6 @@ lodash.cond@^4.3.0: version "4.5.2" resolved "https://registry.yarnpkg.com/lodash.cond/-/lodash.cond-4.5.2.tgz#f471a1da486be60f6ab955d17115523dd1d255d5" -lodash.debounce@^4.0.8: - version "4.0.8" - resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af" - lodash.get@^3.7.0: version "3.7.0" resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-3.7.0.tgz#3ce68ae2c91683b281cc5394128303cbf75e691f" @@ -4920,10 +4916,6 @@ lodash.templatesettings@^4.0.0: dependencies: lodash._reinterpolate "~3.0.0" -lodash.throttle@^4.1.1: - version "4.1.1" - resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4" - lodash.unescape@4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c" @@ -4940,6 +4932,10 @@ lodash@^3.10.1, lodash@^3.8.0: version "3.10.1" resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6" +lodash@^4.17.5: + version "4.17.5" + resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" + log-symbols@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-1.0.2.tgz#376ff7b58ea3086a0f09facc74617eca501e1a18"