Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Refactor lodash #627

Merged
merged 1 commit into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Controls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import throttle from 'lodash.throttle';
import throttle from 'lodash/throttle';
import Browser from './Browser';
import { CLASS_HIDDEN } from './constants';

Expand Down
6 changes: 3 additions & 3 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -14,6 +13,7 @@ import PreviewUI from './PreviewUI';
import getTokens from './tokens';
import {
get,
getProp,
post,
decodeKeydown,
openUrlInsideIframe,
Expand Down
31 changes: 31 additions & 0 deletions src/lib/__tests__/util-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
6 changes: 3 additions & 3 deletions src/lib/file.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
Copy link
Contributor

@priyajeet priyajeet Feb 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also use getProp(file, [permissions, operation]) without combined string. Might perform a bit better? Unsure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went to custom lodash.get implementation because lodash.get is 4.8KB un-gzipped.

}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

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;
}
13 changes: 6 additions & 7 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance changing from null to undefined here will make a difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked through where we use getViewerOption() and it doesn't look like there will be a problem.

const viewerName = getProp(viewer, 'NAME');
return getProp(viewers, `${viewerName}.${option}`);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/doc/PresentationViewer.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/media/MediaBaseViewer.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/media/VideoBaseViewer.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
14 changes: 5 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4864,18 +4864,14 @@ 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"

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"
Expand Down Expand Up @@ -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"

[email protected]:
version "4.0.1"
resolved "https://registry.yarnpkg.com/lodash.unescape/-/lodash.unescape-4.0.1.tgz#bf2249886ce514cda112fae9218cdc065211fc9c"
Expand All @@ -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"
Expand Down