From d9c7fcd1a14163e85ef73437038998066d1c1469 Mon Sep 17 00:00:00 2001 From: Nater Jorde Date: Sun, 9 Sep 2018 15:12:45 -0500 Subject: [PATCH 1/3] reduce package size by installing specific lodash utilities --- package.json | 5 ++++- source/lib/cell/index.js | 4 ++-- source/lib/workbook/dxfCollection.js | 6 +++--- source/lib/workbook/workbook.js | 7 +++---- source/lib/worksheet/cf/cf_rule.js | 9 +++++---- source/lib/worksheet/worksheet.js | 8 ++++---- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index f70b0c2..5a9ef49 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,10 @@ "deepmerge": "2.1.1", "image-size": "0.6.3", "jszip": "3.1.5", - "lodash": "4.17.10", + "lodash.get": "4.4.2", + "lodash.isequal": "4.5.0", + "lodash.isundefined": "3.0.1", + "lodash.reduce": "4.6.0", "mime": "2.3.1", "xmlbuilder": "10.0.0" }, diff --git a/source/lib/cell/index.js b/source/lib/cell/index.js index 3a8a47b..d5cd87c 100644 --- a/source/lib/cell/index.js +++ b/source/lib/cell/index.js @@ -1,4 +1,4 @@ -const _ = require('lodash'); +const deepmerge = require('deepmerge'); const Cell = require('./cell.js'); const Row = require('../row/row.js'); const Column = require('../column/column.js'); @@ -167,7 +167,7 @@ function styleSetter(val) { c.style(thisCellStyle.ids.cellXfs); } else { let curStyle = this.ws.wb.styles[c.s]; - let newStyleOpts = _.merge({}, curStyle.toObject(), thisStyle); + let newStyleOpts = deepmerge(curStyle.toObject(), thisStyle); let mergedStyle = this.ws.wb.createStyle(newStyleOpts); c.style(mergedStyle.ids.cellXfs); } diff --git a/source/lib/workbook/dxfCollection.js b/source/lib/workbook/dxfCollection.js index 21fb109..7ff5549 100644 --- a/source/lib/workbook/dxfCollection.js +++ b/source/lib/workbook/dxfCollection.js @@ -1,4 +1,4 @@ -const _ = require('lodash'); +const _isEqual = require('lodash.isequal'); const Style = require('../style'); const util = require('util'); @@ -30,7 +30,7 @@ class DXFCollection { // §18.8.15 dxfs (Formats) let thisItem; this.items.forEach((item) => { - if (_.isEqual(item.style.toObject(), style.toObject())) { + if (_isEqual(item.style.toObject(), style.toObject())) { return thisItem = item; } }); @@ -57,4 +57,4 @@ class DXFCollection { // §18.8.15 dxfs (Formats) } } -module.exports = DXFCollection; +module.exports = DXFCollection; \ No newline at end of file diff --git a/source/lib/workbook/workbook.js b/source/lib/workbook/workbook.js index f7dbe14..26b3743 100644 --- a/source/lib/workbook/workbook.js +++ b/source/lib/workbook/workbook.js @@ -1,4 +1,4 @@ -const _ = require('lodash'); +const _isUndefined = require('lodash.isundefined'); const deepmerge = require('deepmerge'); const fs = require('fs'); const utils = require('../utils.js'); @@ -71,8 +71,7 @@ class Workbook { * @param {Object} opts.logger Logger that supports warn and error method, defaults to console * @returns {Workbook} */ - constructor(opts) { - opts = opts ? opts : {}; + constructor(opts = {}) { const hasCustomLogger = opts.logger !== undefined; const hasValidCustomLogger = hasCustomLogger && typeof opts.logger.warn === 'function' && typeof opts.logger.error === 'function'; @@ -257,7 +256,7 @@ class Workbook { */ getStringIndex(val) { const target = this.sharedStringLookup[val]; - if (_.isUndefined(target)) { + if (_isUndefined(target)) { const index = this.sharedStrings.push(val) - 1; this.sharedStringLookup[val] = index; return index; diff --git a/source/lib/worksheet/cf/cf_rule.js b/source/lib/worksheet/cf/cf_rule.js index 53e2f6a..174dc70 100644 --- a/source/lib/worksheet/cf/cf_rule.js +++ b/source/lib/worksheet/cf/cf_rule.js @@ -1,4 +1,5 @@ -const _ = require('lodash'); +const _reduce = require('lodash.reduce'); +const _get = require('lodash.get'); const CF_RULE_TYPES = require('./cf_rule_types'); class CfRule { // §18.3.1.10 cfRule (Conditional Formatting Rule) @@ -18,8 +19,8 @@ class CfRule { // §18.3.1.10 cfRule (Conditional Formatting Rule) throw new TypeError('Conditional formatting type "' + this.type + '" is not yet supported'); } - let missingProps = _.reduce(foundType.requiredProps, (list, prop) => { - if (_.get(this, prop, null) === null) { + let missingProps = _reduce(foundType.requiredProps, (list, prop) => { + if (_get(this, prop, null) === null) { list.push(prop); } return list; @@ -51,4 +52,4 @@ class CfRule { // §18.3.1.10 cfRule (Conditional Formatting Rule) } -module.exports = CfRule; +module.exports = CfRule; \ No newline at end of file diff --git a/source/lib/worksheet/worksheet.js b/source/lib/worksheet/worksheet.js index 06a3cac..80300f4 100644 --- a/source/lib/worksheet/worksheet.js +++ b/source/lib/worksheet/worksheet.js @@ -1,4 +1,4 @@ -const _ = require('lodash'); +const deepmerge = require('deepmerge'); const CfRulesCollection = require('./cf/cf_rules_collection'); const cellAccessor = require('../cell'); const rowAccessor = require('../row'); @@ -103,12 +103,12 @@ class Worksheet { * @param {Boolean} opts.hidden Flag indicating whether to not hide the worksheet within the workbook. * @returns {Worksheet} */ - constructor(wb, name, opts) { - + constructor(wb, name, opts = {}) { + this.wb = wb; this.sheetId = this.wb.sheets.length + 1; this.localSheetId = this.wb.sheets.length; - this.opts = _.merge({}, _.cloneDeep(wsDefaultParams), opts); + this.opts = deepmerge(wsDefaultParams, opts); optsValidator(opts); this.opts.sheetView.tabSelected = this.sheetId === 1 ? 1 : 0; From ce6f287a840f2cfa56cae93944388c620d3733f4 Mon Sep 17 00:00:00 2001 From: Nater Jorde Date: Sun, 9 Sep 2018 15:24:28 -0500 Subject: [PATCH 2/3] remove unused imports --- package.json | 1 + source/lib/column/column.js | 8 +- source/lib/column/index.js | 1 - source/lib/drawing/picture.js | 16 ++-- source/lib/row/index.js | 1 - source/lib/row/row.js | 9 +-- source/lib/style/classes/alignment.js | 20 +++-- source/lib/style/classes/border.js | 4 +- source/lib/style/classes/ctColor.js | 2 - source/lib/style/classes/fill.js | 112 +++++++++++++------------- source/lib/style/classes/font.js | 2 - 11 files changed, 81 insertions(+), 95 deletions(-) diff --git a/package.json b/package.json index 5a9ef49..db08a07 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "lodash.isequal": "4.5.0", "lodash.isundefined": "3.0.1", "lodash.reduce": "4.6.0", + "lodash.uniqueid": "4.0.1", "mime": "2.3.1", "xmlbuilder": "10.0.0" }, diff --git a/source/lib/column/column.js b/source/lib/column/column.js index 7887cc3..3a01160 100644 --- a/source/lib/column/column.js +++ b/source/lib/column/column.js @@ -1,6 +1,4 @@ const utils = require('../utils.js'); -const _ = require('lodash'); - class Column { /** @@ -92,7 +90,7 @@ class Column { throw new TypeError('Column group collapse flag must be a boolean'); } - return this; + return this; } /** @@ -108,8 +106,8 @@ class Column { o.state = 'frozen'; o.xSplit = this.min; o.activePane = 'bottomRight'; - o.ySplit === null ? - o.topLeftCell = utils.getExcelCellRef(1, jumpTo) : + o.ySplit === null ? + o.topLeftCell = utils.getExcelCellRef(1, jumpTo) : o.topLeftCell = utils.getExcelCellRef(utils.getExcelRowCol(o.topLeftCell).row, jumpTo); return this; } diff --git a/source/lib/column/index.js b/source/lib/column/index.js index 0ad3bb0..612f5c4 100644 --- a/source/lib/column/index.js +++ b/source/lib/column/index.js @@ -1,4 +1,3 @@ -const _ = require('lodash'); const Cell = require('../cell/cell.js'); const Row = require('../row/row.js'); const Column = require('../column/column.js'); diff --git a/source/lib/drawing/picture.js b/source/lib/drawing/picture.js index d92b270..c4db604 100644 --- a/source/lib/drawing/picture.js +++ b/source/lib/drawing/picture.js @@ -2,7 +2,7 @@ const Drawing = require('./drawing.js'); const path = require('path'); const imgsz = require('image-size'); const mime = require('mime'); -const uniqueId = require('lodash').uniqueId; +const uniqueId = require('lodash.uniqueid'); const EMU = require('../classes/emu.js'); const xmlbuilder = require('xmlbuilder'); @@ -37,18 +37,18 @@ class Picture extends Drawing { this.imagePath = opts.path; this.image = opts.image; - this._name = this.image - ? opts.name || uniqueId('image-') - : opts.name || path.basename(this.imagePath); + this._name = this.image ? + opts.name || uniqueId('image-') : + opts.name || path.basename(this.imagePath); const size = imgsz(this.imagePath || this.image); this._pxWidth = size.width; this._pxHeight = size.height; - this._extension = this.image - ? size.type - : path.extname(this.imagePath).substr(1); + this._extension = this.image ? + size.type : + path.extname(this.imagePath).substr(1); this.contentType = mime.getType(this._extension); @@ -155,7 +155,7 @@ class Picture extends Drawing { atEle.ele('xdr:col').text(at.col); atEle.ele('xdr:colOff').text(at.colOff); atEle.ele('xdr:row').text(at.row); - atEle.ele('xdr:rowOff').text(at.rowOff); + atEle.ele('xdr:rowOff').text(at.rowOff); } if (this.anchorType === 'oneCellAnchor' || this.anchorType === 'absoluteAnchor') { diff --git a/source/lib/row/index.js b/source/lib/row/index.js index ffb7d29..70bb5a2 100644 --- a/source/lib/row/index.js +++ b/source/lib/row/index.js @@ -1,4 +1,3 @@ -const _ = require('lodash'); const Row = require('../row/row.js'); /** diff --git a/source/lib/row/row.js b/source/lib/row/row.js index 2b63483..4330642 100644 --- a/source/lib/row/row.js +++ b/source/lib/row/row.js @@ -1,5 +1,4 @@ const utils = require('../utils.js'); -const _ = require('lodash'); class Row { /** @@ -93,7 +92,7 @@ class Row { return utils.getExcelAlpha(utils.getExcelRowCol(this.cellRefs[0]).col); } else { return 'A'; - } + } } get lastColumn() { @@ -109,7 +108,7 @@ class Row { return utils.getExcelAlpha(utils.getExcelRowCol(this.cellRefs[this.cellRefs.length - 1]).col); } else { return 'A'; - } + } } /** @@ -196,8 +195,8 @@ class Row { o.state = 'frozen'; o.ySplit = this.r; o.activePane = 'bottomRight'; - o.xSplit === null ? - o.topLeftCell = utils.getExcelCellRef(jumpTo, 1) : + o.xSplit === null ? + o.topLeftCell = utils.getExcelCellRef(jumpTo, 1) : o.topLeftCell = utils.getExcelCellRef(jumpTo, utils.getExcelRowCol(o.topLeftCell).col); return this; } diff --git a/source/lib/style/classes/alignment.js b/source/lib/style/classes/alignment.js index f240f0f..b85205f 100644 --- a/source/lib/style/classes/alignment.js +++ b/source/lib/style/classes/alignment.js @@ -1,6 +1,4 @@ -const utils = require('../../utils.js'); const types = require('../../types/index.js'); -const _ = require('lodash'); const xmlbuilder = require('xmlbuilder'); class Alignment { // §18.8.1 alignment (Alignment) @@ -23,15 +21,15 @@ class Alignment { // §18.8.1 alignment (Alignment) if (opts.horizontal !== undefined) { this.horizontal = types.alignment.horizontal.validate(opts.horizontal) === true ? opts.horizontal : null; } - + if (opts.vertical !== undefined) { this.vertical = types.alignment.vertical.validate(opts.vertical) === true ? opts.vertical : null; } - + if (opts.readingOrder !== undefined) { this.readingOrder = types.alignment.readingOrder.validate(opts.readingOrder) === true ? opts.readingOrder : null; } - + if (opts.indent !== undefined) { if (typeof opts.indent === 'number' && parseInt(opts.indent) === opts.indent && opts.indent > 0) { this.indent = opts.indent; @@ -39,7 +37,7 @@ class Alignment { // §18.8.1 alignment (Alignment) throw new TypeError('alignment indent must be a positive integer.'); } } - + if (opts.justifyLastLine !== undefined) { if (typeof opts.justifyLastLine === 'boolean') { this.justifyLastLine = opts.justifyLastLine; @@ -47,7 +45,7 @@ class Alignment { // §18.8.1 alignment (Alignment) throw new TypeError('justifyLastLine alignment option must be of type boolean'); } } - + if (opts.relativeIndent !== undefined) { if (typeof opts.relativeIndent === 'number' && parseInt(opts.relativeIndent) === opts.relativeIndent && opts.relativeIndent > 0) { this.relativeIndent = opts.relativeIndent; @@ -55,7 +53,7 @@ class Alignment { // §18.8.1 alignment (Alignment) throw new TypeError('alignment indent must be a positive integer.'); } } - + if (opts.shrinkToFit !== undefined) { if (typeof opts.shrinkToFit === 'boolean') { this.shrinkToFit = opts.shrinkToFit; @@ -63,7 +61,7 @@ class Alignment { // §18.8.1 alignment (Alignment) throw new TypeError('justifyLastLine alignment option must be of type boolean'); } } - + if (opts.textRotation !== undefined) { if (typeof opts.textRotation === 'number' && parseInt(opts.textRotation) === opts.textRotation) { this.textRotation = opts.textRotation; @@ -71,7 +69,7 @@ class Alignment { // §18.8.1 alignment (Alignment) throw new TypeError('alignment indent must be an integer.'); } } - + if (opts.wrapText !== undefined) { if (typeof opts.wrapText === 'boolean') { this.wrapText = opts.wrapText; @@ -100,7 +98,7 @@ class Alignment { // §18.8.1 alignment (Alignment) this.wrapText !== undefined ? obj.wrapText = this.wrapText : null; return obj; - } + } /** * @alias Alignment.addToXMLele diff --git a/source/lib/style/classes/border.js b/source/lib/style/classes/border.js index a80aa61..7b7dc82 100644 --- a/source/lib/style/classes/border.js +++ b/source/lib/style/classes/border.js @@ -1,6 +1,4 @@ -const utils = require('../../utils.js'); const types = require('../../types/index.js'); -const _ = require('lodash'); const xmlbuilder = require('xmlbuilder'); const CTColor = require('./ctColor.js'); @@ -70,7 +68,7 @@ class Border { } else { throw new TypeError('Border outline option must be of type Boolean'); } - } else if (['left', 'right', 'top', 'bottom', 'diagonal'].indexOf(opt) < 0) { //TODO: move logic to types folder + } else if (['left', 'right', 'top', 'bottom', 'diagonal'].indexOf(opt) < 0) { //TODO: move logic to types folder throw new TypeError(`Invalid key for border declaration ${opt}. Must be one of left, right, top, bottom, diagonal`); } else { this[opt] = new BorderOrdinal(opts[opt]); diff --git a/source/lib/style/classes/ctColor.js b/source/lib/style/classes/ctColor.js index d0f949f..fde8536 100644 --- a/source/lib/style/classes/ctColor.js +++ b/source/lib/style/classes/ctColor.js @@ -1,6 +1,4 @@ -const utils = require('../../utils.js'); const types = require('../../types/index.js'); -const _ = require('lodash'); const xmlbuilder = require('xmlbuilder'); class CTColor { //§18.8.3 && §18.8.19 diff --git a/source/lib/style/classes/fill.js b/source/lib/style/classes/fill.js index 2ec595d..fca7d18 100644 --- a/source/lib/style/classes/fill.js +++ b/source/lib/style/classes/fill.js @@ -1,6 +1,4 @@ -const utils = require('../../utils.js'); const types = require('../../types/index.js'); -const _ = require('lodash'); const xmlbuilder = require('xmlbuilder'); const CTColor = require('./ctColor.js'); @@ -55,77 +53,77 @@ class Fill { //§18.8.20 fill (Fill) } switch (this.type) { - case 'gradient': //§18.8.24 - if (opts.bottom !== undefined) { - if (opts.bottom < 0 || opts.bottom > 1) { - throw new TypeError('Values for gradient fill bottom attribute must be a decimal between 0 and 1'); - } else { - this.bottom = opts.bottom; + case 'gradient': //§18.8.24 + if (opts.bottom !== undefined) { + if (opts.bottom < 0 || opts.bottom > 1) { + throw new TypeError('Values for gradient fill bottom attribute must be a decimal between 0 and 1'); + } else { + this.bottom = opts.bottom; + } } - } - if (opts.degree !== undefined) { - if (typeof opts.degree === 'number') { - this.degree = opts.degree; - } else { - throw new TypeError('Values of gradient fill degree must be of type number.'); + if (opts.degree !== undefined) { + if (typeof opts.degree === 'number') { + this.degree = opts.degree; + } else { + throw new TypeError('Values of gradient fill degree must be of type number.'); + } } - } - if (opts.left !== undefined) { - if (opts.left < 0 || opts.left > 1) { - throw new TypeError('Values for gradient fill left attribute must be a decimal between 0 and 1'); - } else { - this.left = opts.left; + if (opts.left !== undefined) { + if (opts.left < 0 || opts.left > 1) { + throw new TypeError('Values for gradient fill left attribute must be a decimal between 0 and 1'); + } else { + this.left = opts.left; + } } - } - if (opts.right !== undefined) { - if (opts.right < 0 || opts.right > 1) { - throw new TypeError('Values for gradient fill right attribute must be a decimal between 0 and 1'); - } else { - this.right = opts.right; + if (opts.right !== undefined) { + if (opts.right < 0 || opts.right > 1) { + throw new TypeError('Values for gradient fill right attribute must be a decimal between 0 and 1'); + } else { + this.right = opts.right; + } } - } - if (opts.top !== undefined) { - if (opts.top < 0 || opts.top > 1) { - throw new TypeError('Values for gradient fill top attribute must be a decimal between 0 and 1'); - } else { - this.top = opts.top; + if (opts.top !== undefined) { + if (opts.top < 0 || opts.top > 1) { + throw new TypeError('Values for gradient fill top attribute must be a decimal between 0 and 1'); + } else { + this.top = opts.top; + } } - } - - if (opts.stops !== undefined) { - if (opts.stops instanceof Array) { - opts.stops.forEach((s, i) => { - this.stops.push(new Stop(s, i)); - }); - } else { - throw new TypeError('Stops for gradient fills must be sent as an Array'); + + if (opts.stops !== undefined) { + if (opts.stops instanceof Array) { + opts.stops.forEach((s, i) => { + this.stops.push(new Stop(s, i)); + }); + } else { + throw new TypeError('Stops for gradient fills must be sent as an Array'); + } } - } - break; + break; - case 'pattern': //§18.8.32 - if (opts.bgColor !== undefined) { - this.bgColor = new CTColor(opts.bgColor); - } + case 'pattern': //§18.8.32 + if (opts.bgColor !== undefined) { + this.bgColor = new CTColor(opts.bgColor); + } - if (opts.fgColor !== undefined) { - this.fgColor = new CTColor(opts.fgColor); - } + if (opts.fgColor !== undefined) { + this.fgColor = new CTColor(opts.fgColor); + } - if (opts.patternType !== undefined) { - types.fillPattern.validate(opts.patternType) === true ? this.patternType = opts.patternType : null; - } - break; + if (opts.patternType !== undefined) { + types.fillPattern.validate(opts.patternType) === true ? this.patternType = opts.patternType : null; + } + break; - case 'none': - this.patternType = 'none'; - break; + case 'none': + this.patternType = 'none'; + break; } } diff --git a/source/lib/style/classes/font.js b/source/lib/style/classes/font.js index cac2335..ddd673e 100644 --- a/source/lib/style/classes/font.js +++ b/source/lib/style/classes/font.js @@ -1,5 +1,3 @@ -const utils = require('../../utils.js'); -const _ = require('lodash'); const xmlbuilder = require('xmlbuilder'); const types = require('../../types/index.js'); From 4c1b42831cee8e5b33b22344066812e0c939ca41 Mon Sep 17 00:00:00 2001 From: Nater Jorde Date: Sun, 9 Sep 2018 15:37:59 -0500 Subject: [PATCH 3/3] remove lodash usage from tests --- tests/cf_rule.test.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/cf_rule.test.js b/tests/cf_rule.test.js index 9b09ca2..18f74bd 100644 --- a/tests/cf_rule.test.js +++ b/tests/cf_rule.test.js @@ -1,4 +1,4 @@ -var lodash = require('lodash'); +var deepmerge = require('deepmerge'); var test = require('tape'); var CfRule = require('../distribution/lib/worksheet/cf/cf_rule'); @@ -16,7 +16,9 @@ test('CfRule init', function (t) { t.ok(new CfRule(baseConfig), 'init with valid and support type'); try { - var cfr = new CfRule(lodash.extend(baseConfig, { type: 'bogusType' })); + var cfr = new CfRule(deepmerge(baseConfig, { + type: 'bogusType' + })); } catch (err) { t.ok( err instanceof TypeError, @@ -25,7 +27,9 @@ test('CfRule init', function (t) { } try { - var cfr = new CfRule(lodash.extend(baseConfig, { type: 'dataBar' })); + var cfr = new CfRule(deepmerge(baseConfig, { + type: 'dataBar' + })); } catch (err) { t.ok( err instanceof TypeError, @@ -34,7 +38,9 @@ test('CfRule init', function (t) { } try { - var cfr = new CfRule(lodash.extend(baseConfig, { forumla: null })); + var cfr = new CfRule(deepmerge(baseConfig, { + formula: null + })); } catch (err) { t.ok( err instanceof TypeError, @@ -42,4 +48,4 @@ test('CfRule init', function (t) { ); } -}); +}); \ No newline at end of file