diff --git a/gulpfile.js b/gulpfile.js index 5349626..6e3e6a8 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,7 +3,6 @@ var gutil = require('gulp-util'); var mocha = require('gulp-mocha'); var istanbul = require('gulp-istanbul'); var printExample = require('./lib/print-example'); -var _ = require('lodash'); gulp.task('test',mochaTask); gulp.task('coverage',coverage()); @@ -70,4 +69,4 @@ function logMochaError(err){ } else { gutil.log.apply(gutil,arguments); } -} \ No newline at end of file +} diff --git a/lib/print-example.js b/lib/print-example.js index ae23609..29eb9b6 100644 --- a/lib/print-example.js +++ b/lib/print-example.js @@ -1,7 +1,6 @@ var chai = require('chai'); var expect = chai.expect; var colors = require('colors/safe'); -var _ = require('lodash'); var fs = require('fs'); var git = require('git-rev'); @@ -120,4 +119,4 @@ module.exports = { mdExample:mdExample, logExample:logExample, runTest:runTest -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index c02b369..0851865 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "test": "test" }, "dependencies": { - "lodash": "^3.10.1", + "kind-of": "^3.0.4", + "object-assign": "^4.1.0", "string-width": "^1.0.1" }, "devDependencies": { diff --git a/src/cell.js b/src/cell.js index 1281b97..c65fd7c 100644 --- a/src/cell.js +++ b/src/cell.js @@ -1,4 +1,4 @@ -var _ = require('lodash'); +var kindOf = require('kind-of'); var utils = require('./utils'); /** @@ -13,13 +13,13 @@ function Cell(options){ } Cell.prototype.setOptions = function(options){ - if(_.isString(options) || _.isNumber(options) || _.isBoolean(options)){ + if(['boolean', 'number', 'string'].indexOf(kindOf(options)) !== -1){ options = {content:''+options}; } options = options || {}; this.options = options; var content = options.content; - if (_.isString(content) || _.isNumber(content) || _.isBoolean(content)) { + if (['boolean', 'number', 'string'].indexOf(kindOf(content)) !== -1) { this.content = String(content); } else if (!content) { this.content = ''; @@ -36,7 +36,7 @@ Cell.prototype.mergeTableOptions = function(tableOptions,cells){ var optionsChars = this.options.chars || {}; var tableChars = tableOptions.chars; var chars = this.chars = {}; - _.forEach(CHAR_NAMES,function(name){ + CHAR_NAMES.forEach(function(name){ setOption(optionsChars,tableChars,name,chars); }); @@ -85,8 +85,8 @@ Cell.prototype.init = function(tableOptions){ var y = this.y; this.widths = tableOptions.colWidths.slice(x, x + this.colSpan); this.heights = tableOptions.rowHeights.slice(y, y + this.rowSpan); - this.width = _.reduce(this.widths,sumPlusOne); - this.height = _.reduce(this.heights,sumPlusOne); + this.width = this.widths.reduce(sumPlusOne, -1); + this.height = this.heights.reduce(sumPlusOne, -1); this.hAlign = this.options.hAlign || tableOptions.colAligns[x]; this.vAlign = this.options.vAlign || tableOptions.rowAligns[y]; @@ -132,7 +132,7 @@ Cell.prototype.draw = function(lineNum,spanningCell){ Cell.prototype.drawTop = function(drawRight){ var content = []; if(this.cells){ //TODO: cells should always exist - some tests don't fill it in though - _.forEach(this.widths,function(width,index){ + this.widths.forEach(function(width,index){ content.push(this._topLeftChar(index)); content.push( utils.repeat(this.chars[this.y == 0 ? 'top' : 'mid'],width) @@ -361,4 +361,4 @@ var CHAR_NAMES = [ 'top' ]; module.exports = Cell; module.exports.ColSpanCell = ColSpanCell; -module.exports.RowSpanCell = RowSpanCell; \ No newline at end of file +module.exports.RowSpanCell = RowSpanCell; diff --git a/src/layout-manager.js b/src/layout-manager.js index c17ce34..098df3a 100644 --- a/src/layout-manager.js +++ b/src/layout-manager.js @@ -1,12 +1,13 @@ -var _ = require('lodash'); +var kindOf = require('kind-of'); +var objectAssign = require('object-assign'); var Cell = require('./cell'); var RowSpanCell = Cell.RowSpanCell; var ColSpanCell = Cell.ColSpanCell; (function(){ function layoutTable(table){ - _.forEach(table,function(row,rowIndex){ - _.forEach(row,function(cell,columnIndex){ + table.forEach(function(row,rowIndex){ + row.forEach(function(cell,columnIndex){ cell.y = rowIndex; cell.x = columnIndex; for(var y = rowIndex; y >= 0; y--){ @@ -25,8 +26,8 @@ var ColSpanCell = Cell.ColSpanCell; function maxWidth(table) { var mw = 0; - _.forEach(table, function (row) { - _.forEach(row, function (cell) { + table.forEach(function (row) { + row.forEach(function (cell) { mw = Math.max(mw,cell.x + (cell.colSpan || 1)); }); }); @@ -77,8 +78,8 @@ var ColSpanCell = Cell.ColSpanCell; } function addRowSpanCells(table){ - _.forEach(table,function(row,rowIndex){ - _.forEach(row,function(cell){ + table.forEach(function(row,rowIndex){ + row.forEach(function(cell){ for(var i = 1; i < cell.rowSpan; i++){ var rowSpanCell = new RowSpanCell(cell); rowSpanCell.x = cell.x; @@ -141,11 +142,11 @@ var ColSpanCell = Cell.ColSpanCell; } function generateCells(rows){ - return _.map(rows,function(row){ - if(!_.isArray(row)){ + return rows.map(function(row){ + if(kindOf(row) !== 'array'){ var key = Object.keys(row)[0]; row = row[key]; - if(_.isArray(row)){ + if(kindOf(row) === 'array'){ row = row.slice(); row.unshift(key); } @@ -153,7 +154,7 @@ var ColSpanCell = Cell.ColSpanCell; row = [key,row]; } } - return _.map(row,function(cell){ + return row.map(function(cell){ return new Cell(cell); }); }); @@ -183,8 +184,8 @@ function makeComputeWidths(colSpan,desiredWidth,x,forcedMin){ return function(vals,table){ var result = []; var spanners = []; - _.forEach(table,function(row){ - _.forEach(row,function(cell){ + table.forEach(function(row){ + row.forEach(function(cell){ if((cell[colSpan] || 1) > 1){ spanners.push(cell); } @@ -194,29 +195,29 @@ function makeComputeWidths(colSpan,desiredWidth,x,forcedMin){ }); }); - _.forEach(vals,function(val,index){ - if(_.isNumber(val)){ + vals.forEach(function(val,index){ + if(kindOf(val) === 'number'){ result[index] = val; } }); - //_.forEach(spanners,function(cell){ + //spanners.forEach(function(cell){ for(var k = spanners.length - 1; k >=0; k--){ var cell = spanners[k]; var span = cell[colSpan]; var col = cell[x]; var existingWidth = result[col]; - var editableCols = _.isNumber(vals[col]) ? 0 : 1; + var editableCols = kindOf(vals[col]) === 'number' ? 0 : 1; for(var i = 1; i < span; i ++){ existingWidth += 1 + result[col + i]; - if(!_.isNumber(vals[col + i])){ + if(kindOf(vals[col + i]) !== 'number'){ editableCols++; } } if(cell[desiredWidth] > existingWidth){ i = 0; while(editableCols > 0 && cell[desiredWidth] > existingWidth){ - if(!_.isNumber(vals[col+i])){ + if(kindOf(vals[col+i]) !== 'number'){ var dif = Math.round( (cell[desiredWidth] - existingWidth) / editableCols ); existingWidth += dif; result[col + i] += dif; @@ -227,7 +228,7 @@ function makeComputeWidths(colSpan,desiredWidth,x,forcedMin){ } } - _.extend(vals,result); + objectAssign(vals,result); for(var j = 0; j < vals.length; j++){ vals[j] = Math.max(forcedMin, vals[j] || 0); } diff --git a/src/table.js b/src/table.js index 09af0d7..1df19ae 100644 --- a/src/table.js +++ b/src/table.js @@ -1,7 +1,5 @@ - var utils = require('./utils'); var tableLayout = require('./layout-manager'); -var _ = require('lodash'); function Table(options){ this.options = utils.mergeOptions(options); @@ -24,8 +22,8 @@ Table.prototype.toString = function(){ var cells = tableLayout.makeTableLayout(array); - _.forEach(cells,function(row){ - _.forEach(row,function(cell){ + cells.forEach(function(row){ + row.forEach(function(cell){ cell.mergeTableOptions(this.options,cells); },this); },this); @@ -33,8 +31,8 @@ Table.prototype.toString = function(){ tableLayout.computeWidths(this.options.colWidths,cells); tableLayout.computeHeights(this.options.rowHeights,cells); - _.forEach(cells,function(row,rowIndex){ - _.forEach(row,function(cell,cellIndex){ + cells.forEach(function(row,rowIndex){ + row.forEach(function(cell,cellIndex){ cell.init(this.options); },this); },this); @@ -63,7 +61,7 @@ Table.prototype.toString = function(){ function doDraw(row,lineNum,result){ var line = []; - _.forEach(row,function(cell){ + row.forEach(function(cell){ line.push(cell.draw(lineNum)); }); var str = line.join(''); @@ -75,4 +73,4 @@ Table.prototype.__defineGetter__('width', function (){ return str[0].length; }); -module.exports = Table; \ No newline at end of file +module.exports = Table; diff --git a/src/utils.js b/src/utils.js index 9dae6e5..f6c0ae2 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,4 @@ -var _ = require('lodash'); +var objectAssign = require('object-assign'); var stringWidth = require('string-width'); function codeRegex(capture){ @@ -104,8 +104,8 @@ function unwindState(state,ret){ delete state.lastBackgroundAdded; delete state.lastForegroundAdded; - _.forEach(state,function(value,key){ - if(value){ + Object.keys(state).forEach(function(key){ + if(state[key]){ ret += codeCache[key].off; } }); @@ -127,8 +127,8 @@ function rewindState(state,ret){ delete state.lastBackgroundAdded; delete state.lastForegroundAdded; - _.forEach(state,function(value,key){ - if(value){ + Object.keys(state).forEach(function(key){ + if(state[key]){ ret = codeCache[key].on + ret; } }); @@ -236,9 +236,9 @@ function defaultOptions(){ function mergeOptions(options,defaults){ options = options || {}; defaults = defaults || defaultOptions(); - var ret = _.extend({}, defaults, options); - ret.chars = _.extend({}, defaults.chars, options.chars); - ret.style = _.extend({}, defaults.style, options.style); + var ret = objectAssign({}, defaults, options); + ret.chars = objectAssign({}, defaults.chars, options.chars); + ret.style = objectAssign({}, defaults.style, options.style); return ret; } @@ -287,7 +287,7 @@ function colorizeLines(input){ for(var i = 0; i < input.length; i++){ var line = rewindState(state,input[i]) ; state = readState(line); - var temp = _.extend({},state); + var temp = objectAssign({},state); output.push(unwindState(temp,line)); } return output; diff --git a/test/table-layout-test.js b/test/table-layout-test.js index cebe167..e4b5c59 100644 --- a/test/table-layout-test.js +++ b/test/table-layout-test.js @@ -10,7 +10,7 @@ describe('tableLayout', function () { var computeHeights = layoutManager.computeHeights; var chai = require('chai'); var expect = chai.expect; - var _ = require('lodash'); + var kindOf = require('kind-of'); it('simple 2x2 layout',function(){ var actual = makeTableLayout([ @@ -414,8 +414,8 @@ describe('tableLayout', function () { */ function checkLayout(actualTable,expectedTable){ - _.forEach(expectedTable,function(expectedRow,y){ - _.forEach(expectedRow,function(expectedCell,x){ + expectedTable.forEach(function(expectedRow,y){ + expectedRow.forEach(function(expectedCell,x){ if(expectedCell !== null){ var actualCell = findCell(actualTable,x,y); checkExpectation(actualCell,expectedCell,x,y,actualTable); @@ -437,7 +437,7 @@ describe('tableLayout', function () { } function checkExpectation(actualCell,expectedCell,x,y,actualTable){ - if(_.isString(expectedCell)){ + if(kindOf(expectedCell) === 'string'){ expectedCell = {content:expectedCell}; } var address = '(' + y + ',' + x + ')'; @@ -463,4 +463,4 @@ describe('tableLayout', function () { //TODO: retest here x,y coords } } -}); \ No newline at end of file +});