Skip to content

Commit 1569b76

Browse files
committed
use objectUtils helpers everywhere
1 parent 012dc3e commit 1569b76

29 files changed

+87
-76
lines changed

‎lib/model/Model.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { clone } = require('./modelClone');
2-
const { asArray } = require('../utils/objectUtils');
32
const { bindKnex } = require('./modelBindKnex');
43
const { validate } = require('./modelValidate');
54
const { isMsSql } = require('../utils/knexUtils');
@@ -9,6 +8,7 @@ const { hasId, getSetId } = require('./modelId');
98
const { toJson, toDatabaseJson } = require('./modelToJson');
109
const { values, propKey, hasProps } = require('./modelValues');
1110
const { defineNonEnumerableProperty } = require('./modelUtils');
11+
const { asArray, isFunction, isString } = require('../utils/objectUtils');
1212
const { parseRelationsIntoModelInstances } = require('./modelParseRelations');
1313
const { fetchTableMetadata, tableMetadata } = require('./modelTableMetadata');
1414
const { setJson, setFast, setRelated, appendRelated, setDatabaseJson } = require('./modelSet');
@@ -337,7 +337,7 @@ class Model {
337337
});
338338
}
339339

340-
static createNotFoundError(ctx) {
340+
static createNotFoundError() {
341341
return new this.NotFoundError();
342342
}
343343

@@ -348,11 +348,11 @@ class Model {
348348
static getTableName() {
349349
let tableName = this.tableName;
350350

351-
if (typeof tableName === 'function') {
351+
if (isFunction(tableName)) {
352352
tableName = this.tableName();
353353
}
354354

355-
if (typeof tableName !== 'string') {
355+
if (!isString(tableName)) {
356356
throw new Error(`Model ${this.name} must have a static property tableName`);
357357
}
358358

@@ -362,7 +362,7 @@ class Model {
362362
static getIdColumn() {
363363
let idColumn = this.idColumn;
364364

365-
if (typeof idColumn === 'function') {
365+
if (isFunction(idColumn)) {
366366
idColumn = this.idColumn();
367367
}
368368

@@ -394,7 +394,7 @@ class Model {
394394
return 4;
395395
}
396396
} else {
397-
if (typeof this.concurrency === 'function') {
397+
if (isFunction(this.concurrency)) {
398398
return this.concurrency();
399399
} else {
400400
return this.concurrency;
@@ -586,7 +586,7 @@ class Model {
586586
filterConstructor = null;
587587
}
588588

589-
if (typeof traverser !== 'function') {
589+
if (!isFunction(traverser)) {
590590
throw new Error('traverser must be a function');
591591
}
592592

@@ -701,14 +701,14 @@ function getReadOnlyVirtualAttributes(modelClass) {
701701
return false;
702702
}
703703

704-
return (desc.get && !desc.set) || desc.writable === false || typeof desc.value === 'function';
704+
return (desc.get && !desc.set) || desc.writable === false || isFunction(desc.value);
705705
});
706706
}
707707

708708
function getRelations(modelClass) {
709709
let relationMappings = modelClass.relationMappings;
710710

711-
if (typeof relationMappings === 'function') {
711+
if (isFunction(relationMappings)) {
712712
relationMappings = relationMappings.call(modelClass);
713713
}
714714

‎lib/model/ValidationError.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { asArray } = require('../utils/objectUtils');
1+
const { asArray, isString } = require('../utils/objectUtils');
22

33
const Type = {
44
ModelValidation: 'ModelValidation',
@@ -36,7 +36,7 @@ function errorsToMessage(data) {
3636
}
3737

3838
function message(it) {
39-
if (typeof it === 'string') {
39+
if (isString(it)) {
4040
return it;
4141
} else {
4242
return it.message;

‎lib/model/modelJsonAttributes.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { asArray, isObject, flatten } = require('../utils/objectUtils');
1+
const { asArray, isObject, flatten, isString } = require('../utils/objectUtils');
22

33
function parseJsonAttributes(json, modelClass) {
44
const jsonAttr = modelClass.getJsonAttributes();
@@ -10,7 +10,7 @@ function parseJsonAttributes(json, modelClass) {
1010
const attr = jsonAttr[i];
1111
const value = json[attr];
1212

13-
if (typeof value === 'string') {
13+
if (isString(value)) {
1414
const parsed = tryParseJson(value);
1515

1616
// tryParseJson returns undefined if parsing failed.

‎lib/model/modelQueryProps.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { isObject } = require('../utils/objectUtils');
1+
const { isObject, isFunction } = require('../utils/objectUtils');
22
const { defineNonEnumerableProperty } = require('./modelUtils');
33
const { isKnexRaw, isKnexQueryBuilder } = require('../utils/knexUtils');
44

@@ -50,8 +50,8 @@ function isQueryProp(value) {
5050
return (
5151
isKnexQueryBuilder(value) ||
5252
isKnexRaw(value) ||
53-
value.isObjectionQueryBuilderBase ||
54-
typeof value.toKnexRaw === 'function'
53+
isKnexRawConvertable(value) ||
54+
value.isObjectionQueryBuilderBase
5555
);
5656
}
5757

@@ -134,7 +134,7 @@ function buildKnexRawConvertable(converable, builder) {
134134
}
135135

136136
function isKnexRawConvertable(queryProp) {
137-
return typeof queryProp.toKnexRaw === 'function';
137+
return isFunction(queryProp.toKnexRaw);
138138
}
139139

140140
module.exports = {

‎lib/model/modelSet.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { isObject } = require('../utils/objectUtils');
21
const { splitQueryProps } = require('./modelQueryProps');
2+
const { isFunction, isString } = require('../utils/objectUtils');
33
const { parseRelationsIntoModelInstances } = require('./modelParseRelations');
44

55
function setJson(model, json, options) {
@@ -62,7 +62,7 @@ function setFast(model, obj) {
6262

6363
if (
6464
key.charAt(0) !== '$' &&
65-
typeof value !== 'function' &&
65+
!isFunction(value) &&
6666
(readOnlyVirtuals === null || readOnlyVirtuals.indexOf(key) === -1)
6767
) {
6868
model[key] = value;
@@ -116,7 +116,7 @@ function appendRelated(model, relation, models) {
116116
}
117117

118118
function ensureRelation(model, relation) {
119-
if (typeof relation === 'string') {
119+
if (isString(relation)) {
120120
return model.constructor.getRelation(relation);
121121
} else {
122122
return relation;

‎lib/model/modelToJson.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { mergeQueryProps } = require('./modelQueryProps');
2-
const { isObject, cloneDeep } = require('../utils/objectUtils');
2+
const { isObject, cloneDeep, isFunction } = require('../utils/objectUtils');
33

44
function toJson(model, opt) {
55
const modelClass = model.constructor;
@@ -24,7 +24,8 @@ function toJson(model, opt) {
2424
opt.omit = modelClass.getRelations();
2525
}
2626

27-
let json = toExternalJsonImpl(model, opt);
27+
const json = toExternalJsonImpl(model, opt);
28+
2829
return model.$formatJson(json);
2930
}
3031

@@ -103,7 +104,7 @@ function assignVirtualAttributes(json, model, vAttr, opt) {
103104
const key = vAttr[i];
104105
let value = model[key];
105106

106-
if (typeof value === 'function') {
107+
if (isFunction(value)) {
107108
value = value.call(model);
108109
}
109110

‎lib/model/modelValues.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { isObject } = require('../utils/objectUtils');
2+
13
// Property keys needs to be prefixed with a non-numeric character so that
24
// they are not considered indexes when used as object keys.
35
const PROP_KEY_PREFIX = 'k_';
@@ -101,7 +103,7 @@ function propToStr(value) {
101103
return 'null';
102104
} else if (value === undefined) {
103105
return 'undefined';
104-
} else if (typeof value === 'object') {
106+
} else if (isObject(value)) {
105107
return JSON.stringify(value);
106108
} else {
107109
return `${value}`;

‎lib/queryBuilder/QueryBuilderOperationSupport.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class QueryBuilderOperationSupport {
211211
let check;
212212
if (operationSelector instanceof RegExp) {
213213
check = op => operationSelector.test(op.name);
214-
} else if (typeof operationSelector === 'string') {
214+
} else if (isString(operationSelector)) {
215215
check = op => op.name === operationSelector;
216216
} else {
217217
check = op => op.is(operationSelector);

‎lib/queryBuilder/RelationExpression.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const parser = require('./parsers/relationExpressionParser');
2-
const { isObject, cloneDeep, values, union } = require('../utils/objectUtils');
2+
const { isObject, cloneDeep, isNumber, isString, union } = require('../utils/objectUtils');
33
const RelationDoesNotExistError = require('../model/RelationDoesNotExistError');
44

55
class RelationExpressionParseError extends Error {}
@@ -53,7 +53,7 @@ class RelationExpression {
5353
} else {
5454
return new RelationExpression(normalizeNode(expr));
5555
}
56-
} else if (typeof expr === 'string') {
56+
} else if (isString(expr)) {
5757
if (expr.trim().length === 0) {
5858
return new RelationExpression();
5959
} else {
@@ -86,7 +86,7 @@ class RelationExpression {
8686
}
8787

8888
get maxRecursionDepth() {
89-
if (typeof this.$recursive === 'number') {
89+
if (isNumber(this.$recursive)) {
9090
return this.$recursive;
9191
} else {
9292
return this.$recursive ? Number.MAX_SAFE_INTEGER : 0;
@@ -252,7 +252,7 @@ function toString(node) {
252252
let str = node.$relation;
253253

254254
if (node.$recursive) {
255-
if (typeof node.$recursive === 'number') {
255+
if (isNumber(node.$recursive)) {
256256
str += '.^' + node.$recursive;
257257
} else {
258258
str += '.^';
@@ -446,7 +446,7 @@ function mergeNodes(node1, node2) {
446446
function mergeRecursion(rec1, rec2) {
447447
if (rec1 === true || rec2 === true) {
448448
return true;
449-
} else if (typeof rec1 === 'number' && typeof rec2 === 'number') {
449+
} else if (isNumber(rec1) && isNumber(rec2)) {
450450
return Math.max(rec1, rec2);
451451
} else {
452452
return rec1 || rec2;

‎lib/queryBuilder/graphInserter/DependencyGraph.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { appendDataPath } = require('../../utils/dataPath');
2-
const { RelationExpression } = require('../RelationExpression');
32
const { Type: ValidationErrorType } = require('../../model/ValidationError');
4-
const { isObject, last } = require('../../utils/objectUtils');
3+
const { isObject, last, isString } = require('../../utils/objectUtils');
54

65
const HasManyRelation = require('../../relations/hasMany/HasManyRelation');
76
const ManyToManyRelation = require('../../relations/manyToMany/ManyToManyRelation');
@@ -303,7 +302,7 @@ class DependencyGraph {
303302

304303
path.push(key);
305304

306-
if (typeof value === 'string') {
305+
if (isString(value)) {
307306
allMatches(propRefRegex, value, matchResult => {
308307
const [match, refId, refProp] = matchResult;
309308
const refNode = this.nodesById.get(refId);

0 commit comments

Comments
 (0)