Convert Errors to DataError

This commit is contained in:
Faris Ansari 2017-12-02 00:20:41 +05:30
parent 44cc809da4
commit ea99a02ad0
6 changed files with 125 additions and 42 deletions

View File

@ -1,4 +1,9 @@
{ {
"presets": ["env"], "presets": ["env"],
"plugins": ["babel-plugin-add-module-exports"] "plugins": [
"babel-plugin-add-module-exports",
["babel-plugin-transform-builtin-extend", {
"globals": ["TypeError", "RangeError", "Error"]
}]
]
} }

View File

@ -7,7 +7,7 @@
exports["DataTable"] = factory(require("clusterize.js"), require("sortablejs")); exports["DataTable"] = factory(require("clusterize.js"), require("sortablejs"));
else else
root["DataTable"] = factory(root["Clusterize"], root["Sortable"]); root["DataTable"] = factory(root["Clusterize"], root["Sortable"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_7__, __WEBPACK_EXTERNAL_MODULE_10__) { })(typeof self !== 'undefined' ? self : this, function(__WEBPACK_EXTERNAL_MODULE_7__, __WEBPACK_EXTERNAL_MODULE_10__) {
return /******/ (function(modules) { // webpackBootstrap return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache /******/ // The module cache
/******/ var installedModules = {}; /******/ var installedModules = {};
@ -2498,11 +2498,43 @@ module.exports = __WEBPACK_EXTERNAL_MODULE_7__;
Object.defineProperty(exports, "__esModule", { Object.defineProperty(exports, "__esModule", {
value: true value: true
}); });
exports.DataError = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _utils = __webpack_require__(1); var _utils = __webpack_require__(1);
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _extendableBuiltin(cls) {
function ExtendableBuiltin() {
var instance = Reflect.construct(cls, Array.from(arguments));
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true
}
});
if (Object.setPrototypeOf) {
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}
return ExtendableBuiltin;
}
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var DataManager = function () { var DataManager = function () {
@ -2510,10 +2542,6 @@ var DataManager = function () {
_classCallCheck(this, DataManager); _classCallCheck(this, DataManager);
this.options = options; this.options = options;
this.currentSort = {
colIndex: -1,
sortOrder: 'none' // asc, desc, none
};
this.sortRows = (0, _utils.promisify)(this.sortRows, this); this.sortRows = (0, _utils.promisify)(this.sortRows, this);
this.switchColumn = (0, _utils.promisify)(this.switchColumn, this); this.switchColumn = (0, _utils.promisify)(this.switchColumn, this);
this.removeColumn = (0, _utils.promisify)(this.removeColumn, this); this.removeColumn = (0, _utils.promisify)(this.removeColumn, this);
@ -2522,8 +2550,13 @@ var DataManager = function () {
_createClass(DataManager, [{ _createClass(DataManager, [{
key: 'init', key: 'init',
value: function init(data) { value: function init(data) {
var columns = data.columns, if (!data) {
rows = data.rows; data = this.options.data;
}
var _data = data,
columns = _data.columns,
rows = _data.rows;
this.rowCount = 0; this.rowCount = 0;
@ -2532,6 +2565,12 @@ var DataManager = function () {
this._serialNoColumnAdded = false; this._serialNoColumnAdded = false;
this._checkboxColumnAdded = false; this._checkboxColumnAdded = false;
// initialize sort state
this.currentSort = {
colIndex: -1,
sortOrder: 'none' // asc, desc, none
};
this.columns = this.prepareColumns(columns); this.columns = this.prepareColumns(columns);
this.rows = this.prepareRows(rows); this.rows = this.prepareRows(rows);
@ -2541,7 +2580,33 @@ var DataManager = function () {
key: 'prepareColumns', key: 'prepareColumns',
value: function prepareColumns(columns) { value: function prepareColumns(columns) {
if (!Array.isArray(columns)) { if (!Array.isArray(columns)) {
throw new TypeError('`columns` must be an array'); throw new DataError('`columns` must be an array');
}
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = columns[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var column = _step.value;
if (typeof column !== 'string' && (typeof column === 'undefined' ? 'undefined' : _typeof(column)) !== 'object') {
throw new DataError('`column` must be a string or an object');
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
} }
if (this.options.addSerialNoColumn && !this._serialNoColumnAdded) { if (this.options.addSerialNoColumn && !this._serialNoColumnAdded) {
@ -2598,10 +2663,20 @@ var DataManager = function () {
value: function prepareRows(rows) { value: function prepareRows(rows) {
var _this = this; var _this = this;
if (!Array.isArray(rows) || !Array.isArray(rows[0])) { if (!Array.isArray(rows)) {
throw new TypeError('`rows` must be an array of arrays'); throw new DataError('`rows` must be an array');
} }
rows.forEach(function (row, i) {
if (!Array.isArray(row)) {
throw new DataError('`row` must be an array');
}
if (row.length !== _this.getColumnCount()) {
throw new DataError('Row index "' + i + '" doesn\'t match column length');
}
});
rows = rows.map(function (row, i) { rows = rows.map(function (row, i) {
var index = _this._getNextRowCount(); var index = _this._getNextRowCount();
@ -2854,7 +2929,22 @@ function prepareCell(col, i) {
colIndex: i colIndex: i
}); });
} }
module.exports = exports['default'];
var DataError = function (_extendableBuiltin2) {
_inherits(DataError, _extendableBuiltin2);
function DataError() {
_classCallCheck(this, DataError);
return _possibleConstructorReturn(this, (DataError.__proto__ || Object.getPrototypeOf(DataError)).apply(this, arguments));
}
return DataError;
}(_extendableBuiltin(TypeError));
;
exports.DataError = DataError;
/***/ }), /***/ }),
/* 9 */ /* 9 */
@ -3591,7 +3681,7 @@ module.exports = function (css) {
/* 17 */ /* 17 */
/***/ (function(module, exports) { /***/ (function(module, exports) {
module.exports = {"name":"frappe-datatable","version":"0.0.1","description":"A modern datatable library for the web","main":"lib/frappe-datatable.js","scripts":{"build":"webpack --env build","dev":"webpack --progress --colors --watch --env dev","test":"mocha --compilers js:babel-core/register --colors ./test/*.spec.js","test:watch":"mocha --compilers js:babel-core/register --colors -w ./test/*.spec.js"},"devDependencies":{"babel-cli":"6.24.1","babel-core":"6.24.1","babel-eslint":"7.2.3","babel-loader":"7.0.0","babel-plugin-add-module-exports":"0.2.1","babel-preset-env":"^1.6.1","chai":"3.5.0","css-loader":"^0.28.7","eslint":"3.19.0","eslint-loader":"1.7.1","mocha":"3.3.0","node-sass":"^4.5.3","sass-loader":"^6.0.6","style-loader":"^0.18.2","webpack":"^3.1.0","yargs":"7.1.0"},"repository":{"type":"git","url":"https://github.com/frappe/datatable.git"},"keywords":["webpack","es6","starter","library","universal","umd","commonjs"],"author":"Faris Ansari","license":"MIT","bugs":{"url":"https://github.com/frappe/datatable/issues"},"homepage":"https://frappe.github.io/datatable","dependencies":{"clusterize.js":"^0.18.0","sortablejs":"^1.7.0"}} module.exports = {"name":"frappe-datatable","version":"0.0.1","description":"A modern datatable library for the web","main":"lib/frappe-datatable.js","scripts":{"build":"webpack --env build","dev":"webpack --progress --colors --watch --env dev","test":"mocha --compilers js:babel-core/register --colors ./test/*.spec.js","test:watch":"mocha --compilers js:babel-core/register --colors -w ./test/*.spec.js"},"devDependencies":{"babel-cli":"6.24.1","babel-core":"6.24.1","babel-eslint":"7.2.3","babel-loader":"7.0.0","babel-plugin-add-module-exports":"0.2.1","babel-plugin-transform-builtin-extend":"^1.1.2","babel-preset-env":"^1.6.1","chai":"3.5.0","css-loader":"^0.28.7","eslint":"3.19.0","eslint-loader":"1.7.1","mocha":"3.3.0","node-sass":"^4.5.3","sass-loader":"^6.0.6","style-loader":"^0.18.2","webpack":"^3.1.0","yargs":"7.1.0"},"repository":{"type":"git","url":"https://github.com/frappe/datatable.git"},"keywords":["webpack","es6","starter","library","universal","umd","commonjs"],"author":"Faris Ansari","license":"MIT","bugs":{"url":"https://github.com/frappe/datatable/issues"},"homepage":"https://frappe.github.io/datatable","dependencies":{"clusterize.js":"^0.18.0","sortablejs":"^1.7.0"}}
/***/ }) /***/ })
/******/ ]); /******/ ]);

File diff suppressed because one or more lines are too long

View File

@ -15,6 +15,7 @@
"babel-eslint": "7.2.3", "babel-eslint": "7.2.3",
"babel-loader": "7.0.0", "babel-loader": "7.0.0",
"babel-plugin-add-module-exports": "0.2.1", "babel-plugin-add-module-exports": "0.2.1",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-preset-env": "^1.6.1", "babel-preset-env": "^1.6.1",
"chai": "3.5.0", "chai": "3.5.0",
"css-loader": "^0.28.7", "css-loader": "^0.28.7",

View File

@ -35,11 +35,11 @@ export default class DataManager {
prepareColumns(columns) { prepareColumns(columns) {
if (!Array.isArray(columns)) { if (!Array.isArray(columns)) {
throw ColumnsTypeError; throw new DataError('`columns` must be an array');
} }
for (const column of columns) { for (const column of columns) {
if (typeof column !== 'string' && typeof column !== 'object') { if (typeof column !== 'string' && typeof column !== 'object') {
throw ColumnTypeError; throw new DataError('`column` must be a string or an object');
} }
} }
@ -92,19 +92,18 @@ export default class DataManager {
prepareRows(rows) { prepareRows(rows) {
if (!Array.isArray(rows)) { if (!Array.isArray(rows)) {
throw RowsTypeError; throw new DataError('`rows` must be an array');
} }
for (const row of rows) { rows.forEach((row, i) => {
if (!Array.isArray(row)) { if (!Array.isArray(row)) {
throw RowTypeError; throw new DataError('`row` must be an array');
} }
if (row.length !== this.getColumnCount()) { if (row.length !== this.getColumnCount()) {
throw RowLengthError; throw new DataError(`Row index "${i}" doesn't match column length`);
} }
} });
rows = rows.map((row, i) => { rows = rows.map((row, i) => {
const index = this._getNextRowCount(); const index = this._getNextRowCount();
@ -329,16 +328,8 @@ function prepareCell(col, i) {
}); });
} }
const ColumnsTypeError = new TypeError('`columns` must be an array'); class DataError extends TypeError {};
const RowsTypeError = new TypeError('`rows` must be an array');
const RowTypeError = new TypeError('`row` must be an array');
const ColumnTypeError = new TypeError('`column` must be a string or an object');
const RowLengthError = new RangeError('A Row length doesn\'t match column length');
export { export {
ColumnsTypeError, DataError
RowsTypeError,
ColumnTypeError,
RowTypeError,
RowLengthError
}; };

View File

@ -2,11 +2,7 @@
import chai from 'chai'; import chai from 'chai';
import DataManager, { import DataManager, {
ColumnTypeError, DataError
RowTypeError,
ColumnsTypeError,
RowsTypeError,
RowLengthError
} from '../src/datamanager'; } from '../src/datamanager';
chai.expect(); chai.expect();
@ -51,21 +47,21 @@ describe.only('DataManager instance', () => {
expect(() => datamanager.init({ expect(() => datamanager.init({
columns: ['Name'], columns: ['Name'],
rows: 2 rows: 2
})).to.throw(RowsTypeError); })).to.throw(DataError, '`rows` must be an array');
}); });
it('should throw when any of the row is not an Array', () => { it('should throw when any of the row is not an Array', () => {
expect(() => datamanager.init({ expect(() => datamanager.init({
columns: ['Name'], columns: ['Name'],
rows: [2] rows: [2]
})).to.throw(RowTypeError); })).to.throw(DataError, '`row` must be an array');
}); });
it('should throw when any of the row\'s length doesn\'t match column length', () => { it('should throw when any of the row\'s length doesn\'t match column length', () => {
expect(() => datamanager.init({ expect(() => datamanager.init({
columns: ['Name'], columns: ['Name'],
rows: [[]] rows: [[]]
})).to.throw(RowLengthError); })).to.throw(DataError, 'column length');
}); });
it('should not throw given valid data', () => { it('should not throw given valid data', () => {
@ -90,13 +86,13 @@ describe.only('DataManager instance', () => {
it('should throw when columns parameter is not an Array', () => { it('should throw when columns parameter is not an Array', () => {
expect(() => datamanager.init({ expect(() => datamanager.init({
columns: 2 columns: 2
})).to.throw(ColumnsTypeError); })).to.throw(DataError, 'must be an array');
}); });
it('should throw when any of the column is not a string or object', () => { it('should throw when any of the column is not a string or object', () => {
expect(() => datamanager.init({ expect(() => datamanager.init({
columns: [2] columns: [2]
})).to.throw(ColumnTypeError); })).to.throw(DataError, 'must be a string or an object');
}); });
it('should not throw given valid params', () => { it('should not throw given valid params', () => {