Refactor, simplify, improve readability, remove redundant closures.

This commit is contained in:
Mikael Sand
2019-01-06 01:31:34 +02:00
parent fe85f9e66d
commit 741509763c
12 changed files with 39 additions and 71 deletions
+1 -8
View File
@@ -25,14 +25,7 @@ export default class extends Component {
children,
} = props;
let extractedTransform;
if (maskTransform) {
extractedTransform = extractTransform(maskTransform);
} else if (transform) {
extractedTransform = extractTransform(transform);
} else {
extractedTransform = extractTransform(props);
}
const extractedTransform = extractTransform(maskTransform || transform || props);
return (
<RNSVGMask
+1 -8
View File
@@ -28,14 +28,7 @@ export default class extends Component {
preserveAspectRatio,
} = props;
let extractedTransform;
if (patternTransform) {
extractedTransform = extractTransform(patternTransform);
} else if (transform) {
extractedTransform = extractTransform(transform);
} else {
extractedTransform = extractTransform(props);
}
const extractedTransform = extractTransform(patternTransform || transform || props);
return (
<RNSVGPattern
+3 -11
View File
@@ -11,24 +11,16 @@ export default class extends Shape {
};
setNativeProps = props => {
let { points } = props;
const { points } = props;
if (points) {
if (Array.isArray(points)) {
points = points.join(",");
}
props.d = `M${extractPolyPoints(points)}`;
props.d = `M${extractPolyPoints(points)}z`;
}
this.root.setNativeProps(props);
};
render() {
const { props } = this;
let { points } = props;
if (Array.isArray(points)) {
points = points.join(",");
}
const { points } = props;
return (
<Path
ref={ele => {
+2 -10
View File
@@ -11,11 +11,8 @@ export default class extends Shape {
};
setNativeProps = props => {
let { points } = props;
const { points } = props;
if (points) {
if (Array.isArray(points)) {
points = points.join(",");
}
props.d = `M${extractPolyPoints(points)}`;
}
this.root.setNativeProps(props);
@@ -23,12 +20,7 @@ export default class extends Shape {
render() {
const { props } = this;
let { points } = props;
if (Array.isArray(points)) {
points = points.join(",");
}
const { points } = props;
return (
<Path
ref={ele => {
+6 -5
View File
@@ -1,15 +1,16 @@
import { Component } from "react";
import SvgTouchableMixin from "../lib/SvgTouchableMixin";
const touch = Object.entries(SvgTouchableMixin);
class Shape extends Component {
constructor() {
super(...arguments);
for (let key of Object.keys(SvgTouchableMixin)) {
const method = SvgTouchableMixin[key];
if (typeof method === "function") {
this[key] = method.bind(this);
for (let [key, val] of touch) {
if (typeof val === "function") {
this[key] = val.bind(this);
} else {
this[key] = method;
this[key] = val;
}
}
//noinspection JSUnusedGlobalSymbols
+5 -4
View File
@@ -65,11 +65,12 @@ class Svg extends Shape {
};
setNativeProps = props => {
if (props.width) {
props.bbWidth = `${props.width}`;
const { width, height } = props;
if (width) {
props.bbWidth = `${width}`;
}
if (props.height) {
props.bbHeight = `${props.height}`;
if (height) {
props.bbHeight = `${height}`;
}
this.root.setNativeProps(props);
};
+2 -2
View File
@@ -9,11 +9,11 @@ const fillRules = {
const fillProps = ["fill", "fillOpacity", "fillRule"];
export default function(props, styleProperties) {
fillProps.forEach(name => {
for (let name of fillProps) {
if (props.hasOwnProperty(name)) {
styleProperties.push(name);
}
});
}
return {
// default fill is black
+1 -1
View File
@@ -10,7 +10,7 @@ export default function(lengthList) {
} else if (typeof lengthList === "number") {
return [`${lengthList}`];
} else if (lengthList && typeof lengthList.map === "function") {
return lengthList.map(d => `${d}`);
return lengthList.map(String);
} else {
return [];
}
+2 -1
View File
@@ -1,4 +1,5 @@
export default function(polyPoints) {
export default function(points) {
const polyPoints = Array.isArray(points) ? points.join(",") : points;
return polyPoints
.replace(/[^e]-/, " -")
.split(/(?:\s+|\s*,\s*)/g)
+7 -11
View File
@@ -12,9 +12,14 @@ export default function(prop, ref) {
const styleProperties = [];
const extractedProps = {
opacity: extractOpacity(opacity),
propList: styleProperties,
onLayout,
propList: styleProperties,
opacity: extractOpacity(opacity),
matrix: extractTransform(props),
...props2transform(props),
...extractResponder(props, ref),
...extractFill(props, styleProperties),
...extractStroke(props, styleProperties),
};
if (id) {
@@ -39,14 +44,5 @@ export default function(prop, ref) {
}
}
Object.assign(extractedProps, extractStroke(props, styleProperties));
Object.assign(extractedProps, extractFill(props, styleProperties));
extractedProps.matrix = extractTransform(props);
Object.assign(extractedProps, props2transform(props));
Object.assign(extractedProps, extractResponder(props, ref));
return extractedProps;
}
+2 -2
View File
@@ -26,11 +26,11 @@ const strokeProps = [
];
export default function(props, styleProperties) {
strokeProps.forEach(name => {
for (let name of strokeProps) {
if (props.hasOwnProperty(name)) {
styleProperties.push(name);
}
});
}
const { stroke } = props;
let { strokeWidth, strokeDasharray } = props;
+7 -8
View File
@@ -1,20 +1,19 @@
// eslint-disable-next-line eqeqeq
export const notNil = a => a != null;
export function pick(object, props) {
export function pick(object, keys) {
const result = {};
for (let prop of props) {
result[prop] = object[prop];
for (let key of keys) {
result[key] = object[key];
}
return result;
}
export function pickBy(object, pred) {
export function pickBy(object, predicate) {
const result = {};
for (let prop of Object.keys(object)) {
const val = object[prop];
if (pred(val)) {
result[prop] = val;
for (let [key, value] of Object.entries(object)) {
if (predicate(value)) {
result[key] = value;
}
}
return result;