Skip to content

Commit efa0205

Browse files
feat(drag-in-the-blank): remove lodash where possible to improve bundle size PD-5076
1 parent fa9e913 commit efa0205

5 files changed

Lines changed: 21 additions & 32 deletions

File tree

packages/drag-in-the-blank/configure/src/__tests__/choice.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { shallow } from 'enzyme';
22
import React from 'react';
3-
import Choice, { BlankContent, tileSource } from '../choice';
3+
import Choice, { tileSource } from '../choice';
44

55
describe('Choice', () => {
66
let onClick;

packages/drag-in-the-blank/configure/src/choices.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import ReactDOM from 'react-dom';
33
import PropTypes from 'prop-types';
44
import { EditableHtml } from '@pie-lib/pie-toolbox/editable-html';
55
import { renderMath } from '@pie-lib/pie-toolbox/math-rendering';
6-
import find from 'lodash/find';
6+
import { AlertDialog } from '@pie-lib/pie-toolbox/config-ui';
77
import Button from '@material-ui/core/Button';
8+
import { withStyles } from '@material-ui/core/styles';
9+
810
import Choice from './choice';
911
import { choiceIsEmpty } from './markupUtils';
10-
import { withStyles } from '@material-ui/core/styles';
11-
import { AlertDialog } from '@pie-lib/pie-toolbox/config-ui';
1212

1313
const styles = (theme) => ({
1414
design: {
@@ -201,7 +201,7 @@ export class Choices extends React.Component {
201201
}
202202

203203
// if duplicates not allowed, remove the choices that are used to define the correct response
204-
return choices.filter((choice) => !find(correctResponse, (v) => v === choice.id));
204+
return choices.filter((choice) => !Object.values(correctResponse).includes(choice.id));
205205
};
206206

207207
render() {

packages/drag-in-the-blank/configure/src/markupUtils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import escape from 'lodash/escape';
2-
import isUndefined from 'lodash/isUndefined';
32

43
export const createElementFromHTML = (htmlString) => {
54
const div = document.createElement('div');
@@ -47,7 +46,7 @@ export const processMarkup = (markup) => {
4746
markup: slateMarkup.innerHTML,
4847
choices: choices,
4948
correctResponse: choices.reduce((obj, c, index) => {
50-
obj[index] = (!isUndefined(c.id) && c.id) || '';
49+
obj[index] = (c.id !== undefined && c.id) || '';
5150

5251
return obj;
5352
}, {}),

packages/drag-in-the-blank/controller/src/index.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import reduce from 'lodash/reduce';
21
import isEmpty from 'lodash/isEmpty';
32
import { getAllCorrectResponses, choiceIsEmpty } from './utils';
43
import { lockChoices, getShuffledChoices, partialScoring } from '@pie-lib/pie-toolbox/controller-utils';
@@ -29,9 +28,9 @@ export function model(question, session, env, updateSession) {
2928
const { value } = session || {};
3029

3130
for (let i = 0; i < numberOfPossibleResponses; i++) {
32-
const result = reduce(
33-
allCorrectResponses,
34-
(obj, choices, key) => {
31+
const result = Object.keys(allCorrectResponses).reduce(
32+
(obj, key) => {
33+
const choices = allCorrectResponses[key];
3534
const answer = (value && value[key]) || '';
3635

3736
obj.feedback[key] = choices[i] === answer;
@@ -96,9 +95,9 @@ export const getScore = (config, session) => {
9695
const { value } = session || {};
9796

9897
for (let i = 0; i < numberOfPossibleResponses; i++) {
99-
const result = reduce(
100-
allCorrectResponses,
101-
(total, choices, key) => {
98+
const result = Object.keys(allCorrectResponses).reduce(
99+
(total, key) => {
100+
const choices = allCorrectResponses[key];
102101
const answer = (value && value[key]) || '';
103102

104103
if (choices[i] === answer) {

packages/drag-in-the-blank/controller/src/utils.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
import cloneDeep from 'lodash/cloneDeep';
2-
import reduce from 'lodash/reduce';
32

43
const replaceHtmlRegex = /<(?!img)[^>]*>?/gm;
54

6-
export const getAllCorrectResponses = ({ correctResponse, alternateResponses }) => {
7-
return reduce(
8-
correctResponse || {},
9-
(obj, val, key) => {
10-
obj.possibleResponses[key] = [val];
5+
export const getAllCorrectResponses = ({ correctResponse = {}, alternateResponses = {} }) =>
6+
Object.entries(correctResponse).reduce(
7+
(acc, [key, val]) => {
8+
acc.possibleResponses[key] = [val, ...(alternateResponses[key] ? cloneDeep(alternateResponses[key]) : [])];
119

12-
if (alternateResponses && alternateResponses[key]) {
13-
obj.possibleResponses[key] = [...obj.possibleResponses[key], ...cloneDeep(alternateResponses[key])];
14-
}
10+
const length = acc.possibleResponses[key].length;
11+
acc.numberOfPossibleResponses = acc.numberOfPossibleResponses === undefined
12+
? length
13+
: Math.min(acc.numberOfPossibleResponses, length);
1514

16-
if (
17-
obj.numberOfPossibleResponses === undefined ||
18-
obj.numberOfPossibleResponses > obj.possibleResponses[key].length
19-
) {
20-
obj.numberOfPossibleResponses = obj.possibleResponses[key].length;
21-
}
22-
23-
return obj;
15+
return acc;
2416
},
2517
{
2618
possibleResponses: {},
2719
numberOfPossibleResponses: undefined,
2820
},
2921
);
30-
};
3122

3223
export const choiceIsEmpty = (choice) => {
3324
if (choice) {

0 commit comments

Comments
 (0)