metarhia / SummerCamp

Metarhia Summer Camp 2022
12 stars 7 forks source link

Use optional chaining operator #8

Open tshemsedinov opened 2 years ago

tshemsedinov commented 2 years ago

We are going to use optional chaining operator ?.. Here is a list of repositories to be reviewed:

Please don't create pull requests immediately. First step: review all codebase and collect links to the certain places and post all links to this issue. Second step: we will discuss all those cases and assign you to make PRs.

Example: https://github.com/metarhia/metaschema/blob/3de9bea21c35b48e538436f08b683b7e18e72398/lib/util.js#L42-L43

const isInstanceOf = (obj, constrName) =>
  obj && obj.constructor && obj.constructor.name === constrName;

change to

const isInstanceOf = (obj, constrName) => obj?.constructor?.name === constrName;
shatanov commented 2 years ago

Hi , I can try to solve this problem, I have collected all the places where you need to use .? , but maybe I missed something

  1. https://github.com/metarhia/metasql/blob/master/lib/database.js#L257-L259 if (entity && entity.kind === 'registry') { return this.register(table, fields, params, data); } change to if (entity?.kind === 'registry') { return this.register(table, fields, params, data); }

  2. https://github.com/metarhia/metasql/blob/master/lib/pg.js#L178-L180 if (def.length && def.length.max) { pgType = '${pgType}(${def.length.max})'; } change to if (def.length?.max) { pgType = '${pgType}(${def.length.max})'; }

  1. https://github.com/metarhia/metaschema/blob/master/lib/prototypes/collections.js#L49-L51 isInstance(value) { return value && value.constructor && value.constructor.name === 'Map'; }, change to isInstance(value) { return value?.constructor?.name === 'Map'; },

  2. https://github.com/metarhia/metaschema/blob/master/lib/prototypes/collections.js#L93-L95 isInstance(value) { return value && value.constructor && value.constructor.name === 'Set'; }, change to isInstance(value) { return value?.constructor?.name === 'Set'; },

  3. https://github.com/metarhia/metaschema/blob/master/lib/metadata.js#L41-L43 static isInstance(err) { return err && err.errors && Array.isArray(err.errors); }, change to static isInstance(err) { return err?.errors && Array.isArray(err.errors); }

  4. https://github.com/metarhia/metaschema/blob/master/lib/util.js#L42-L43 const isInstanceOf = (obj, constrName) => obj && obj.constructor && obj.constructor.name === constrName; change to const isInstanceOf = (obj, constrName) => obj?.constructor?.name === constrName;

  1. https://github.com/metarhia/metavm/blob/master/metavm.js#L147-L150 const name = options && options.filename ? options.filename : path.basename(filePath, '.js'); change to const name = options?.filename ? options.filename : path.basename(filePath, '.js');

If I missed something, I will be glad if I am corrected

tshemsedinov commented 2 years ago

All places needs refactoring @shatanov !

shatanov commented 2 years ago

added changes in their directories