Skip to content

Commit 9d7d9ce

Browse files
committed
add type checkers
1 parent bef4cc6 commit 9d7d9ce

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

angular.tableview.js

+45-25
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
var module = angular.module("tableview", []);
2626

27-
module.provider("$tableView", function () {
27+
module.provider("$tableView", function tableViewProvider() {
2828

2929
var that = this;
3030

@@ -34,6 +34,7 @@
3434
};
3535

3636
}).directive("tableviewAutofocus", function tableviewAutofocusDirective($timeout) {
37+
// noinspection JSUnusedGlobalSymbols
3738
return {
3839
restrict: "A",
3940
link: function ($scope, $element, $attributes) {
@@ -48,6 +49,30 @@
4849
};
4950
}).directive("tableview", function tableviewDirective($compile, $http, $templateCache, $tableView) {
5051

52+
var isString = function isString(value) {
53+
return ('' + value) === value;
54+
};
55+
56+
var isUndefined = function isUndefined(value) {
57+
return void 0 === value;
58+
};
59+
60+
var isObject = function isObject(value) {
61+
return null !== value && 'object' === typeof value;
62+
};
63+
64+
var isBoolean = function isBoolean(value) {
65+
return true === value || false === value;
66+
};
67+
68+
var isFunction = function isFunction(value) {
69+
return 'function' === typeof value;
70+
};
71+
72+
var isNumber = function isNumber(value) {
73+
return (value - 0) === value;
74+
};
75+
5176
var MODULE_NAME = "angular.tableview";
5277

5378
var getModuleURL = function getModuleURL() {
@@ -215,7 +240,7 @@
215240
}
216241

217242
if (
218-
"string" == typeof $scope.tableview.request.like[field]
243+
isString($scope.tableview.request.like[field])
219244
&&
220245
!$scope.tableview.request.like[field].trim()
221246
) {
@@ -235,21 +260,21 @@
235260
return {message: "", status: true};
236261
};
237262

238-
if (!column.editable || "object" !== typeof column.editable) {
263+
if (!column.editable || !isObject(column.editable)) {
239264
$mode.validation = valid();
240265
return true;
241-
} else if ("function" !== typeof column.editable.validate) {
266+
} else if (!isFunction(column.editable.validate)) {
242267
column.editable.validate = valid;
243268
}
244269

245270
var result = column.editable.validate(column, $row, column.field, $mode.value);
246-
if ("boolean" === typeof result) {
271+
if (isBoolean(result)) {
247272
result = result ? valid() : {message: "", status: false};
248273
}
249274

250-
result = (result && "object" === typeof result) ? result : {};
275+
result = (result && isObject(result) ? result : {});
251276
result.status = !!result.status;
252-
result.message = "string" === typeof result.message ? result.message : "";
277+
result.message = isString(result.message) ? result.message : "";
253278
$mode.validation = result;
254279

255280
return result.status;
@@ -267,13 +292,7 @@
267292
$mode.value = $row[column.field];
268293
}
269294

270-
if (
271-
validation
272-
&& changed
273-
&& column.editable
274-
&& "object" === typeof column.editable
275-
&& "function" === typeof column.editable.change
276-
) {
295+
if (validation && changed && isObject(column.editable) && isFunction(column.editable.change)) {
277296
column.editable.change(column, $row, column.field, $row[column.field]);
278297
}
279298

@@ -302,19 +321,19 @@
302321

303322
$scope.getRowSelectionIndex = function getRowSelectionIndex($row) {
304323
if (
305-
"string" !== typeof $scope.tableview.selectableBy
324+
isString($scope.tableview.selectableBy)
306325
||
307326
!$scope.tableview.selectableBy.trim().length
308327
||
309-
"undefined" === typeof $row[$scope.tableview.selectableBy]
328+
isUndefined($row[$scope.tableview.selectableBy])
310329
) {
311330
return;
312331
}
313332

314333
var key = $scope.tableview.selectableBy;
315334
var val = $row[$scope.tableview.selectableBy];
316335

317-
for (var i = 0; i < $scope.tableview.selection.length; i+=1) {
336+
for (var i = 0; i < $scope.tableview.selection.length; i += 1) {
318337
if ($scope.tableview.selection[i][key] == val) {
319338
return i * 1;
320339
}
@@ -327,11 +346,11 @@
327346
$scope.switchRowSelection = function switchRowSelection($row, sign) {
328347

329348
var index = $scope.getRowSelectionIndex($row);
330-
if ("number" !== typeof index) {
349+
if (!isNumber(index)) {
331350
return;
332351
}
333352

334-
if ("boolean" === typeof sign) {
353+
if (isBoolean(sign)) {
335354
if (index < 0 && sign) {
336355
$scope.tableview.selection.push(angular.copy($row));
337356
} else if (index >= 0 && !sign) {
@@ -349,7 +368,7 @@
349368

350369
$scope.isRowSelected = function isRowSelected($row) {
351370
var i = $scope.getRowSelectionIndex($row);
352-
return !!("number" === typeof i && i >= 0);
371+
return !!(isNumber(i) && 0 <= i);
353372
};
354373

355374
$scope.isRowsSelected = function isRowsSelected() {
@@ -376,7 +395,7 @@
376395
$scope.themeTemplateName = function themeTemplateName(name) {
377396
if ($scope.theme && name) {
378397
name = ["tableview", $scope.theme, name].join(".");
379-
return "string" === typeof $templateCache.get(name) ? name : undefined;
398+
return isString($templateCache.get(name)) ? name : undefined;
380399
}
381400
};
382401

@@ -392,16 +411,17 @@
392411
// theme.template
393412
// default
394413

395-
var $0 = "undefined" !== typeof $index && $scope.tableview.columns[$index] && $scope.tableview.columns[$index].template ? $scope.tableview.columns[$index].template : {};
396-
var $1 = $scope.tableview.template && "object" === typeof $scope.tableview.template ? $scope.tableview.template : {};
397-
var $2 = $scope.$provider.template && "object" === typeof $scope.$provider.template ? $scope.$provider.template : {};
414+
var $0 = (!isUndefined($index) && $scope.tableview.columns[$index] && $scope.tableview.columns[$index].template ? $scope.tableview.columns[$index].template : {});
415+
var $1 = ($scope.tableview.template && isObject($scope.tableview.template) ? $scope.tableview.template : {});
416+
var $2 = ($scope.$provider.template && isObject($scope.$provider.template) ? $scope.$provider.template : {});
398417
var tpl = $0[name] || $1[name] || $2[name] || $scope.themeTemplateName(name) || $scope.defaultTemplateName(name);
399418

400-
return "string" === typeof $templateCache.get(tpl) ? tpl : undefined;
419+
return (isString($templateCache.get(tpl)) ? tpl : undefined);
401420

402421
};
403422

404423
$scope.exec();
424+
405425
};
406426
}
407427
};

0 commit comments

Comments
 (0)