Skip to content

Commit

Permalink
remove _.has
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Metelkin committed Nov 8, 2023
1 parent 64d21c5 commit d5ffd77
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 37 deletions.
10 changes: 5 additions & 5 deletions src/core/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ class Component {
Object.entries(req)
.forEach(([prop, rule]) => { // iterates through rules
// isReference: true
if(rule.isReference && _.has(this, prop)){
if(rule.isArray){ // iterates through array
if (rule.isReference && this[prop] !== undefined) {
if (rule.isArray) { // iterates through array
this[prop].forEach((item, i) => {
let fullPath = rule.path ? `${prop}[${i}].${rule.path}` : `${prop}[${i}]`;
iterator(item, fullPath, rule);
});
}else{
} else {
let item = this[prop];
let fullPath = rule.path ? `${prop}.${rule.path}` : `${prop}`;
iterator(item, fullPath, rule);
Expand Down Expand Up @@ -189,14 +189,14 @@ class Component {
let req = this.constructor.requirements();
Object.entries(req).forEach(([prop, rule]) => { // iterates through rules
// required: true
if (rule.required && !_.has(this, prop)) {
if (rule.required && this[prop] === undefined) {
logger.error(
`No required "${prop}" property for "${this.index}" of ${this.className}.`,
{type: 'BindingError', space: this.space}
);
}
// isReference: true + className
if (rule.isReference && _.has(this, prop)) {
if (rule.isReference && this[prop] !== undefined) {
if (rule.isArray) { // iterates through array
this[prop].forEach((item, i) => {
let fullPath = rule.path ? `${prop}[${i}].${rule.path}` : `${prop}[${i}]`;
Expand Down
10 changes: 5 additions & 5 deletions src/core/dose.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { _Size } = require('./_size');
const { Const } = require('./const');
const { floor, min } = Math;
const _ = require('lodash');

/*
Dose class
*/
Expand Down Expand Up @@ -84,28 +84,28 @@ class Dose extends _Size {
getStart(){
if (this.start !== undefined) {
return this.start;
} else if (_.has(this, 'startObj.num')) {
} else if (this.startObj?.num !== undefined) {
return this.startObj.num;
}
}
getPeriod(){
if (this.period !== undefined) {
return this.period;
} else if (_.has(this, 'periodObj.num')) {
} else if (this.periodObj?.num !== undefined) {
return this.periodObj.num;
}
}
getStop(){
if (this.stop !== undefined) {
return this.stop;
} else if (_.has(this, 'stopObj.num')) {
} else if (this.stopObj?.num !== undefined) {
return this.stopObj.num;
}
}
getRepeatCount(){
if (this.repeatCount !== undefined) {
return this.repeatCount;
} else if (_.has(this, 'repeatCountObj.num')) {
} else if (this.repeatCountObj?.num !== undefined) {
return this.repeatCountObj.num;
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/core/time-switcher.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { _Switcher } = require('./_switcher');
const { Const } = require('./const');
const _ = require('lodash');
/*
TimeSwitcher class
Expand Down Expand Up @@ -84,21 +83,21 @@ class TimeSwitcher extends _Switcher {
getStart(){
if (this.start !== undefined) {
return this.start;
} else if (_.has(this, 'startObj.num')) {
} else if (this.startObj?.num !== undefined) {
return this.startObj.num;
}
}
getPeriod(){
if (this.period !== undefined) {
return this.period;
} else if (_.has(this, 'periodObj.num')) {
} else if (this.periodObj?.num !== undefined) {
return this.periodObj.num;
}
}
getStop(){
if (this.stop !== undefined) {
return this.stop;
} else if (_.has(this, 'stopObj.num')) {
} else if (this.stopObj?.num !== undefined) {
return this.stopObj.num;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/dbsolve-export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ class DBSolveExport extends AbstractExport{
*/
let initRecords = ns
.sortExpressionsByContext('start_', true)
.filter((x) => x.instanceOf('Record') && (_.has(x, 'assignments.start_') || x.isRule));
.filter((x) => {
return x.instanceOf('Record')
&& (x.assignments?.start_ !== undefined || x.isRule);
});
// create matrix
let matrix = [];
processes.forEach((process, processNum) => {
Expand Down
6 changes: 5 additions & 1 deletion src/julia-export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ class JuliaExport extends AbstractExport {
.selectByInstanceOf('_Switcher')
.map((switcher) => {
let affect = ns.toArray()
.filter((x) => x.instanceOf('Record') && _.has(x, 'assignments.' + switcher.id));
.filter((x) => {
return x.instanceOf('Record')
&& x.assignments !== undefined
&& x.assignments[switcher.id] !== undefined;
});

// find all unique dependencies inside assignments
let affectDeps = [];
Expand Down
10 changes: 7 additions & 3 deletions src/matlab-export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ class MatlabExport extends AbstractExport {
// initialize at start records
let initRecords = ns
.sortExpressionsByContext('start_')
.filter((x) => x.instanceOf('Record') && (_.has(x, 'assignments.start_') || x.isRule));
.filter((x) => {
return x.instanceOf('Record')
&& (x.assignments?.start_ !== undefined || x.isRule);
});
// currently we output all records
let sharedRecords = ns
.sortExpressionsByContext('ode_', true)
Expand Down Expand Up @@ -145,8 +148,9 @@ class MatlabExport extends AbstractExport {
.map((switcher) => {
let affect = switcher.namespace.toArray()
.filter((x) => {
return x.instanceOf('Record')
&& _.has(x, 'assignments.' + switcher.id);
return x.instanceOf('Record')
&& x.assignments !== undefined
&& x.assignments[switcher.id] !== undefined;
});

return {
Expand Down
28 changes: 11 additions & 17 deletions src/module-system/sbml-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ function compartmentToQ(x, unitDict = {}){
let legalUnitIndex = legalUnits.indexOf(unitId); //
if (legalUnitIndex !== -1) { // if id in legal unit list
q.units = Unit.fromQ([{ kind: unitId }]);
} else if (_.has(unitDict, unitId)){
} else if (unitDict[unitId] !== undefined){
q.units = unitDict[unitId].simplify('dimensionless');
} else {
q.units = Unit.fromQ([{ kind: unitId }]);
Expand Down Expand Up @@ -415,7 +415,7 @@ function speciesToQ(x, zeroSpatialDimensions = [], qArr = [], unitDict = {}){
.divide(compartmentUnits)
.simplify();
}
} else if (_.has(unitDict, substanceUnitId)) {
} else if (unitDict[substanceUnitId] !== undefined) {
let amountUnits = unitDict[substanceUnitId];
// set amount or concentration units
if (q.isAmount) {
Expand Down Expand Up @@ -499,7 +499,7 @@ function reactionToQ(x){
// products
let products = x.elements
&& x.elements.find((y) => y.name === 'listOfProducts');
if (_.has(products, 'elements')) {
if (products.elements !== undefined) {
var actors0 = products.elements
.filter((y) => y.name === 'speciesReference')
.map((y) => {
Expand All @@ -523,7 +523,7 @@ function reactionToQ(x){
// reactants
let reactants = x.elements
&& x.elements.find((y) => y.name === 'listOfReactants');
if (_.has(reactants, 'elements')) {
if (reactants.elements !== undefined) {
var actors1 = reactants.elements
.filter((y) => y.name === 'speciesReference')
.map((y) => {
Expand All @@ -545,17 +545,11 @@ function reactionToQ(x){
}

// modifiers
let modifiers = x.elements
&& x.elements.find((y) => y.name === 'listOfModifiers');
if (_.has(modifiers, 'elements')) {
var modifiers1 = modifiers.elements
.filter((y) => y.name === 'modifierSpeciesReference')
.map((y) => {
return { target: y.attributes?.species };
});
} else {
modifiers1 = [];
}
let modifiers1 = (x.elements?.find((y) => y.name === 'listOfModifiers')?.elements || [])
.filter((y) => y.name === 'modifierSpeciesReference')
.map((y) => {
return { target: y.attributes?.species };
});

q.actors = actors0.concat(actors1);
q.modifiers = modifiers1;
Expand Down Expand Up @@ -588,7 +582,7 @@ function parameterToQ(x, unitDict = {}){
let legalUnitIndex = legalUnits.indexOf(unitId); //
if (legalUnitIndex !== -1) { // if id in legal unit list
q.units = Unit.fromQ([{ kind: unitId }]);
} else if (_.has(unitDict, unitId)) { // if id in unitDefinitions
} else if (unitDict[unitId] !== undefined) { // if id in unitDefinitions
// I removed simplify here to support pretty units in IRT
q.units = unitDict[unitId]; //.simplify();
} else {
Expand Down Expand Up @@ -688,7 +682,7 @@ function eventToQ(x){
// assignments
let assignments = x.elements
&& x.elements.find((y) => y.name === 'listOfEventAssignments');
if (_.has(assignments, 'elements')) {
if (assignments.elements !== undefined) {
assignments.elements
.filter((y) => y.name === 'eventAssignment')
.forEach((y) => {
Expand Down
2 changes: 1 addition & 1 deletion src/slv-export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class SLVExport extends AbstractExport{
// create and sort expressions for RHS
let rhs = ns
.sortExpressionsByContext('ode_', false)
.filter((record) => record.instanceOf('Record') && _.has(record, 'assignments.ode_'));
.filter((record) => record.instanceOf('Record') && record.assignments?.ode_ !== undefined);
// check that all record in start are not Expression
let startExpressions = ns
.selectRecordsByContext('start_')
Expand Down

0 comments on commit d5ffd77

Please sign in to comment.