Skip to content

Commit

Permalink
[refactor] Move to Array.isArray
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Jul 30, 2024
1 parent 4cf38aa commit 6a5ec27
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 25 deletions.
8 changes: 3 additions & 5 deletions src/ObjectProviderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { isArray } from 'lodash-es';

import {
JSONSchema6forRdf,
JsObject,
Expand All @@ -24,7 +22,7 @@ function combineProperties(oldObj: any, newObj: any, schema: JSONSchema6forRdf):
Object.keys(oldObj).forEach((key) => {
if (schema.properties && oldObj[key] !== newObj[key]) {
if (schema.properties[key] && schema.properties[key].type === 'array') {
newData[key] = isArray(oldObj[key]) ? [...oldObj[key], newObj[key]] : [oldObj[key], newObj[key]];
newData[key] = Array.isArray(oldObj[key]) ? [...oldObj[key], newObj[key]] : [oldObj[key], newObj[key]];
return;
}
}
Expand Down Expand Up @@ -265,10 +263,10 @@ export function propertyShapesToSchemaProperties(
*/
export function addToSchemaParentSchema(schema: JSONSchema6forRdf, parentSchema: JSONSchema6forRdf): JSONSchema6forRdf {
const parentCtx = parentSchema['@context'];
if (parentCtx && typeof parentCtx !== 'string' && !isArray(parentCtx)) {
if (parentCtx && typeof parentCtx !== 'string' && !Array.isArray(parentCtx)) {
if (!schema['@context']) schema['@context'] = {};
const schemaCtx = schema['@context'];
if (typeof schemaCtx !== 'string' && !isArray(schemaCtx)) {
if (typeof schemaCtx !== 'string' && !Array.isArray(schemaCtx)) {
copyObjectProps(schemaCtx, parentCtx);
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/SparqlGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import _isUrl from 'is-url';
import { isArray } from 'lodash-es';

import { Quad, NamedNode, Variable } from '@rdfjs/types/data-model';
import { DataFactory } from 'rdf-data-factory';
Expand Down Expand Up @@ -130,14 +129,14 @@ export function addToBgp(triples: any[]): any[] {
export function toBgp(data: any): BgpPattern {
return {
type: 'bgp',
triples: isArray(data) ? data : [data],
triples: Array.isArray(data) ? data : [data],
};
}

export function toOptional(data: any): OptionalPattern {
return {
type: 'optional',
patterns: isArray(data) ? data : [data],
patterns: Array.isArray(data) ? data : [data],
};
}

Expand Down Expand Up @@ -688,7 +687,7 @@ export function getConditionalTriple(
}

export function getTripleWithPredOrPath(subj: any, propUri: string | string[], value: any, prefixes: JsStrObj): Quad {
if (isArray(propUri)) {
if (Array.isArray(propUri)) {
return factory.quad(
subj,
{
Expand Down Expand Up @@ -817,7 +816,7 @@ export function getDataTriples(entConstr: EntConstrInternal): any[] {
...property,
...(<any>property.items),
};
if (isArray(value)) {
if (Array.isArray(value)) {
value.forEach((v) => {
triple = getConditionalTriple(prop, subj, v, propUri, entConstr.prefixes);
if (triple) triples.push(triple);
Expand Down Expand Up @@ -1167,8 +1166,8 @@ export function getInternalCollConstrs(
addData?: JsObject | JsObject[],
) {
const internalCollConstrs: EntConstrInternal[] = [];
if (addConditions && !isArray(addConditions)) addConditions = [addConditions];
if (addData && !isArray(addData)) addData = [addData];
if (addConditions && !Array.isArray(addConditions)) addConditions = [addConditions];
if (addData && !Array.isArray(addData)) addData = [addData];

for (let index = 0; index < collConstr.entConstrs.length; index++) {
const constr = collConstr.entConstrs[index] as EntConstrData;
Expand Down
10 changes: 5 additions & 5 deletions src/SparqlGenSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import uuid62 from 'uuid62';
import { cloneDeep, isArray } from 'lodash-es';
import { cloneDeep } from 'lodash-es';
import { Quad, Variable } from '@rdfjs/types/data-model';
import { ConstructQuery, Ordering, SelectQuery } from 'sparqljs';
import { compact } from 'jsonld';
Expand Down Expand Up @@ -182,12 +182,12 @@ function analyseProps(entConstrs: EntConstrInternal[]) {
}

function convertOrderBy(orderBy: any, entConstrs: any | any[] | undefined): any {
if (isArray(orderBy)) return orderBy.map((e) => convertOrderBy(e, entConstrs));
if (Array.isArray(orderBy)) return orderBy.map((e) => convertOrderBy(e, entConstrs));
if (orderBy.expression || !orderBy.variable) return orderBy;
let entConstrVariable = orderBy.variable;
if (entConstrs) {
entConstrVariable =
(isArray(entConstrs) ? entConstrs.find((e) => e.props2vars[entConstrVariable]) : entConstrs)?.props2vars[
(Array.isArray(entConstrs) ? entConstrs.find((e) => e.props2vars[entConstrVariable]) : entConstrs)?.props2vars[
entConstrVariable
] || orderBy.variable;
}
Expand Down Expand Up @@ -954,7 +954,7 @@ function convertSimpleTypes(val: string, type?: string) {
function convertAnyTypes(val: any, type?: string, ctxs?: JsStrObjObj[]) {
if (typeof val === 'string') {
val = convertSimpleTypes(val, type);
} else if (isArray(val)) {
} else if (Array.isArray(val)) {
val = val.map((subVal) => convertAnyTypes(subVal, type, ctxs));
} else {
val = convertPropValues(val, ctxs);
Expand Down Expand Up @@ -1016,7 +1016,7 @@ function nestObjs(jsonLdObjs: JsObject[], entConstrs: EntConstrInternal[]) {
const fullPropIri = deAbbreviateIri(propIri as string, entConstr.prefixes);
entsObjs[entConstrFromIndex].forEach((entObjFrom) => {
const entObjFromPropVals = entObjFrom[fullPropIri];
if (isArray(entObjFromPropVals)) {
if (Array.isArray(entObjFromPropVals)) {
entObjFromPropVals.forEach((entObjFromPropVal, opvIndex) => {
const refIri = entObjFromPropVal['@id'];
if (refIri) {
Expand Down
3 changes: 1 addition & 2 deletions src/models/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { isArray } from 'lodash-es';
import { when } from 'mobx';

import { ResourceSchema, ClassSchema } from '../schema/RdfsSchema';
Expand Down Expand Up @@ -127,7 +126,7 @@ export const createModelFromState = (
}
}
// If CollConstr with '@id' existed, adds data to the Coll's dataIntrnl
if (existedColl !== undefined && collState.data && isArray(collState.data)) {
if (existedColl !== undefined && collState.data && Array.isArray(collState.data)) {
existedColl.addElems(collState.data);
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/models/MstColl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import dayjs from 'dayjs';
import { isArray, isEqual } from 'lodash-es';
import { isEqual } from 'lodash-es';
import { reaction } from 'mobx';
import {
types,
Expand Down Expand Up @@ -328,7 +328,7 @@ export const MstColl = types
*/
addElems(elems: JsObject | JsObject[]) {
if (!elems) return;
if (!isArray(elems)) elems = [elems];
if (!Array.isArray(elems)) elems = [elems];
elems = elems.filter((elem: any) => !self.dataIntrnl.find((e: any) => e['@id'] === elem['@id']));
self.dataIntrnl.push(elems);
},
Expand Down
3 changes: 1 addition & 2 deletions src/models/MstRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import { isArray } from 'lodash-es';
import { values } from 'mobx';
import { types, getSnapshot, applySnapshot, getEnv, Instance } from 'mobx-state-tree';

Expand Down Expand Up @@ -94,7 +93,7 @@ export const MstRepository = types
},
],
};
} else if (isArray(data)) {
} else if (Array.isArray(data)) {
collConstr = { entConstrs: [...data] }; // data as IEntityConstr[]
} else {
if (data.$schema === undefined) {
Expand Down
3 changes: 1 addition & 2 deletions src/schema/Formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* SPDX-License-Identifier: GPL-3.0-only
********************************************************************************/
import dayjs from 'dayjs';
import { isArray } from 'lodash-es';
import { JSONSchema6forRdf } from '../ObjectProvider';

interface FormatterAndDefaultAndTitle {
Expand All @@ -24,7 +23,7 @@ export function getPropertyFormatterAndDefault(
const properties = schema.properties;
const contexts = schema['@context'];
const result: FormatterAndDefaultAndTitle = {};
if (properties && contexts && typeof contexts !== 'string' && !isArray(contexts)) {
if (properties && contexts && typeof contexts !== 'string' && !Array.isArray(contexts)) {
const property = properties[propKey];
const context = (contexts as any)[propKey];
if (property && context) {
Expand Down

0 comments on commit 6a5ec27

Please sign in to comment.