Skip to content

Commit

Permalink
style: fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 9, 2019
1 parent b0b0e16 commit 3c917e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
55 changes: 50 additions & 5 deletions lib/cast.js
Expand Up @@ -6,7 +6,6 @@

const StrictModeError = require('./error/strict');
const Types = require('./schema/index');
const castFilterPath = require('./helpers/query/castFilterPath');
const castTextSearch = require('./schema/operators/text');
const get = require('./helpers/get');
const util = require('util');
Expand All @@ -30,10 +29,7 @@ module.exports = function cast(schema, obj, options, context) {

const paths = Object.keys(obj);
let i = paths.length;
let _keys;
let any$conditionals;
let schematype;
let nested;
let path;
let type;
let val;
Expand Down Expand Up @@ -245,7 +241,56 @@ module.exports = function cast(schema, obj, options, context) {
} else if (val == null) {
continue;
} else if (val.constructor.name === 'Object') {
obj[path] = castFilterPath(context, schematype, val);
const any$conditionals = Object.keys(val).some(function(k) {
return k.charAt(0) === '$' && k !== '$id' && k !== '$ref';
});

if (!any$conditionals) {
return schematype.castForQueryWrapper({
val: val,
context: context
});
}

const ks = Object.keys(val);

let k = ks.length;

while (k--) {
const $cond = ks[k];
const nested = val[$cond];

if ($cond === '$not') {
if (nested && schematype && !schematype.caster) {
const _keys = Object.keys(nested);
if (_keys.length && _keys[0].charAt(0) === '$') {
for (const key in nested) {
nested[key] = schematype.castForQueryWrapper({
$conditional: key,
val: nested[key],
context: context
});
}
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
val: nested,
context: context
});
}
continue;
}
cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context);
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
val: nested,
context: context
});
}
}

return val;
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
const casted = [];
for (let valIndex = 0; valIndex < val.length; valIndex++) {
Expand Down
3 changes: 1 addition & 2 deletions lib/helpers/query/castFilterPath.js
Expand Up @@ -13,7 +13,6 @@ module.exports = function castFilterPath(query, schematype, val) {
}

const ks = Object.keys(val);
let $cond;

let k = ks.length;

Expand Down Expand Up @@ -41,7 +40,7 @@ module.exports = function castFilterPath(query, schematype, val) {
}
continue;
}
cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context);
// cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context);
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/update/castArrayFilters.js
Expand Up @@ -34,12 +34,12 @@ module.exports = function castArrayFilters(query) {
throw new Error(`Got null array filter in ${arrayFilters}`);
}
const firstKey = Object.keys(filter)[0];

const dot = firstKey.indexOf('.');
const filterPath = dot === -1 ?
updatedPathsByFilter[firstKey] + '.0' :
updatedPathsByFilter[firstKey.substr(0, dot)] + '.0' + firstKey.substr(dot);

if (filterPath == null) {
throw new Error(`Filter path not found for ${firstKey}`);
}
Expand Down

0 comments on commit 3c917e7

Please sign in to comment.