Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove lodash dependencies (fixes build too) #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@
"@angular/router": "^6.1.0",
"@mdi/font": "^3.0.39",
"@types/d3": "^5.7.1",
"@types/lodash": "^4.14.121",
"@types/node": "^11.13.7",
"core-js": "^2.5.4",
"d3": "^5.9.1",
"hammerjs": "^2.0.8",
"lodash": "^4.17.11",
"ngx-clipboard": "^12.0.0",
"rxjs": "~6.2.0",
"zone.js": "~0.8.26"
Expand Down
44 changes: 22 additions & 22 deletions src/app/models/avro-schema.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as _ from 'lodash';
import { Util } from '../shared/util';

export class AvroUtil {
public static parseAvsc(json): AvroSchema {
Expand Down Expand Up @@ -83,7 +83,7 @@ export class AvroUtil {
names = new Names();
}

if (!_.isNil(jsonData['type'])) {
if (!Util.isNullOrUndefined(jsonData['type'])) {
let type = jsonData['type'];
let otherProperties = AvroUtil.getOtherProperties(jsonData, SCHEMA_RESERVED_PROPS);

Expand Down Expand Up @@ -211,7 +211,7 @@ export class AvroSchema {
schema.fullpath = traversalPath;
AvroSchema.assignFullpaths(schema.type, traversalPath);
} else if (schema instanceof RecordSchema) {
traversalPath = _.isEmpty(traversalPath) ? schema.name : traversalPath;
traversalPath = traversalPath.length === 0 ? schema.name : traversalPath;
schema.fullpath = traversalPath;

if (schema.fields) {
Expand Down Expand Up @@ -267,7 +267,7 @@ class Names {
if (!(test in this.names)) {
return null;
}
return _.cloneDeep(this.names[test]);
return Util.copyString(this.names[test]);
}

addName(nameAttr, spaceAttr, newSchema): Name {
Expand All @@ -288,15 +288,15 @@ class Name {
fullname: string;

constructor(nameAttr: string, spaceAttr: string, defaultSpace: string) {
if (_.isNil(nameAttr) || nameAttr === '') {
if (Util.isNullOrUndefined(nameAttr) || nameAttr === '') {
return;
}

if (nameAttr.indexOf('.') < 0) {
if (!_.isNil(spaceAttr) && spaceAttr !== '') {
if (!Util.isNullOrUndefined(spaceAttr) && spaceAttr !== '') {
this.fullname = `${spaceAttr}.${nameAttr}`;
} else {
if (!_.isNil(defaultSpace) && defaultSpace !== '') {
if (!Util.isNullOrUndefined(defaultSpace) && defaultSpace !== '') {
this.fullname = `${defaultSpace}.${nameAttr}`;
} else {
this.fullname = nameAttr;
Expand All @@ -308,7 +308,7 @@ class Name {
}

getSpace() {
if (_.isNil(this.fullname)) {
if (Util.isNullOrUndefined(this.fullname)) {
return null;
}

Expand All @@ -330,12 +330,12 @@ export class NamedSchema extends AvroSchema {
constructor(type, name = null, namespace = null, names: Names = null, otherProperties: any = null) {
super(type, otherProperties);

if (!_.isNil(name)) {
if (!Util.isNullOrUndefined(name)) {
let newName = names.addName(name, namespace, this);

this.properties['name'] = name;

if (!_.isNil(namespace)) {
if (!Util.isNullOrUndefined(namespace)) {
this.properties['namespace'] = newName.getSpace();
}

Expand All @@ -362,12 +362,12 @@ export class Field {

let typeSchema;

if (typeof type === 'string' && !_.isNil(names) && names.hasName(type, null)) {
if (typeof type === 'string' && !Util.isNullOrUndefined(names) && names.hasName(type, null)) {
typeSchema = names.getName(type, null);
} else {
try {
typeSchema = AvroUtil.makeAvscObject(type, names);
} catch (e) {
} catch(e) {
throw new SchemaParseException(`Type property "${type}" is not a valid Avro schema: ${e}`);
}
}
Expand All @@ -381,11 +381,11 @@ export class Field {
this.properties['defaultValue'] = defaultValue;
this.defaultValue = defaultValue;
}
if (!_.isNil(order)) {
if (!Util.isNullOrUndefined(order)) {
this.properties['order'] = order;
this.order = order;
}
if (!_.isNil(doc)) {
if (!Util.isNullOrUndefined(doc)) {
this.properties['doc'] = doc;
this.doc = doc;
}
Expand Down Expand Up @@ -433,7 +433,7 @@ export class EnumSchema extends NamedSchema {
this.properties['symbols'] = symbols;
this.symbols = symbols;

if (!_.isNil(doc)) {
if (!Util.isNullOrUndefined(doc)) {
this.properties['doc'] = doc;
this.doc = doc;
}
Expand All @@ -456,7 +456,7 @@ export class ArraySchema extends AvroSchema {
} else {
try {
itemsSchema = AvroUtil.makeAvscObject(items, names);
} catch (e) {
} catch(e) {
throw new SchemaParseException(`Items schema (${items}) not a valid Avro schema: ${e} (known names: ${Object.keys(names.names)})`)
}
}
Expand All @@ -479,7 +479,7 @@ export class MapSchema extends AvroSchema {
} else {
try {
valuesSchema = AvroUtil.makeAvscObject(values, names);
} catch (e) {
} catch(e) {
throw new SchemaParseException(`Values schema not a valid Avro schema.`)
}
}
Expand Down Expand Up @@ -509,7 +509,7 @@ export class UnionSchema extends AvroSchema {
} else {
try {
newSchema = AvroUtil.makeAvscObject(schema, names);
} catch (e) {
} catch(e) {
throw new SchemaParseException(`Union item must be a valid Avro schema: ${e}`);
}
}
Expand Down Expand Up @@ -558,7 +558,7 @@ export class RecordSchema extends NamedSchema {
this.fields = fieldObjects;
this.name = name;

if (!_.isNil(doc)) {
if (!Util.isNullOrUndefined(doc)) {
this.properties['doc'] = doc;
this.doc = doc;
}
Expand All @@ -575,20 +575,20 @@ export class RecordSchema extends NamedSchema {
fieldData.forEach(field => {
let newField;

if (_.isObject(field)) {
if (Util.isObject(field)) {
const type = field['type'];
const name = field['name'];

let hasDefault = Object.keys(field).includes('default');
let defaultValue = undefined;

if (hasDefault && _.isNil(field['default'])) {
if (hasDefault && Util.isNullOrUndefined(field['default'])) {
// Default value of 'null'
defaultValue = 'null';
} else if (hasDefault && Array.isArray(field['default'])) {
// Default value of 'for arrays'
defaultValue = '[ ] - empty array';
} else if (hasDefault && _.isObjectLike(field['default'])) {
} else if (hasDefault && Util.isObject(field['default'])) {
defaultValue = '{ } - empty map';
} else {
defaultValue = field['default'];
Expand Down
22 changes: 22 additions & 0 deletions src/app/shared/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,26 @@ export class Util {
public static replaceDots(s: string) {
return s.replace(new RegExp('\\.', 'g'), '-')
}

public static isNullOrUndefined(o: any): boolean {
return o == null
}
Comment on lines +6 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be tested? (Ergo does it work with undefined)


public static isEmpty(obj: object | Array<any>): boolean {
// @ts-ignore
return [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length;
}

public static isObject(value: any): boolean {
const type = typeof value
return type === 'function' || type === 'object' && !Array.isArray(value) && !!value
}

public static copyString(value: string): string {
return value.slice()
}

public static deepCopy(value: object): object {
return JSON.parse(JSON.stringify(value))
}
Comment on lines +24 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best/fastest of doing this?

}
40 changes: 13 additions & 27 deletions src/app/viewer/visualizer/visualizer.component.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import {Component, ElementRef, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';
import * as d3 from 'd3';
import {HierarchyNode, HierarchyPointNode} from 'd3';
import * as _ from 'lodash';
import {SchemaService} from '../../services/schema.service';
import {
ArraySchema,
AvroSchema,
AvroUtil,
EnumSchema,
ErrorUnionSchema,
Field,
FixedSchema,
MapSchema,
PrimitiveSchema,
RecordSchema,
SchemaType,
UnionSchema
} from '../../models/avro-schema.model';
import {Location} from '@angular/common';
import {ActivatedRoute} from '@angular/router';
import {ViewerProperties} from '../../shared/viewer.properties';
import {Util} from '../../shared/util';
import { HierarchyNode, HierarchyPointNode } from 'd3';
import { SchemaService } from '../../services/schema.service';
import { ArraySchema, AvroSchema, AvroUtil, EnumSchema, ErrorUnionSchema, Field, FixedSchema, MapSchema, PrimitiveSchema, RecordSchema, SchemaType, UnionSchema } from '../../models/avro-schema.model';
import { Location } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { ViewerProperties } from '../../shared/viewer.properties';
import { Util } from '../../shared/util';

@Component({
selector: 'app-visualizer',
Expand Down Expand Up @@ -51,8 +37,8 @@ export class VisualizerComponent implements OnInit {
public loading = true;

constructor(private schemaService: SchemaService,
private location: Location,
private route: ActivatedRoute) {
private location: Location,
private route: ActivatedRoute) {
}

ngOnInit() {
Expand Down Expand Up @@ -89,7 +75,7 @@ export class VisualizerComponent implements OnInit {
this.route.paramMap.subscribe(params => {
let schemaFullPath = params.get('schemaFullPath');

if (_.isNil(schemaFullPath) // no path provided
if (Util.isNullOrUndefined(schemaFullPath) // no path provided
|| schemaFullPath === this.avroSchema['fullpath'] // path matches root path
|| schemaFullPath.split('.')[0] !== this.avroSchema['fullpath'] // path does not start with the root nodes path
) {
Expand Down Expand Up @@ -133,7 +119,7 @@ export class VisualizerComponent implements OnInit {
public selectNodeByFullPath(schemaFullPath) {
let match = AvroUtil.getAvroSchemaNodeByFullPath(schemaFullPath, this.avroSchema);

if (_.isNil(match)) {
if (Util.isNullOrUndefined(match)) {
this.selectRootNode();
} else {
this.location.replaceState(`/viewer/${this.schemaVersion}/${schemaFullPath}`);
Expand Down Expand Up @@ -365,7 +351,7 @@ export class VisualizerComponent implements OnInit {
if (node instanceof Field) {
return this.getChildren(node.type, true);
} else if (node instanceof RecordSchema) {
return _.isObject(node.type) ? node.type['fields'] : node['fields'];
return Util.isObject(node.type) ? node.type['fields'] : node['fields'];
} else if (node instanceof UnionSchema || node instanceof ErrorUnionSchema) {
// When the parent is a union and there's only one child, display it as a direct child
let nonPrimitiveSchemas = AvroUtil.getNonPrimitiveSchemas(node.schemas);
Expand Down
Loading