Skip to content

Commit 065d3dc

Browse files
author
simonThiele
committed
fist try of kicking out lodash
1 parent e328315 commit 065d3dc

File tree

3 files changed

+104
-45
lines changed

3 files changed

+104
-45
lines changed

test/unit/SmartComparer.js

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ function SmartComparer() {
99
// Diagnostic message, when comparison fails.
1010
var message;
1111

12-
// Keys to skip during object comparison.
13-
var omitKeys = [ 'id', 'uuid' ];
14-
1512
return {
1613

1714
areEqual: areEqual,
@@ -47,7 +44,7 @@ function SmartComparer() {
4744
}
4845

4946
// Don't compare functions.
50-
if ( _.isFunction( val1 ) && _.isFunction( val2 ) ) return true;
47+
if ( isFunction( val1 ) && isFunction( val2 ) ) return true;
5148

5249
// Array comparison.
5350
var arrCmp = compareArrays( val1, val2 );
@@ -68,13 +65,30 @@ function SmartComparer() {
6865

6966
// if (JSON.stringify( val1 ) == JSON.stringify( val2 ) ) return true;
7067

71-
// Continue with default comparison.
72-
if ( _.isEqual( val1, val2 ) ) return true;
73-
7468
// Object differs (unknown reason).
7569
return makeFail( 'Values differ', val1, val2 );
7670
}
7771

72+
function isFunction(value) {
73+
74+
// The use of `Object#toString` avoids issues with the `typeof` operator
75+
// in Safari 8 which returns 'object' for typed array constructors, and
76+
// PhantomJS 1.9 which returns 'function' for `NodeList` instances.
77+
var tag = isObject(value) ? Object.prototype.toString.call(value) : '';
78+
79+
return tag == '[object Function]' || tag == '[object GeneratorFunction]';
80+
81+
}
82+
83+
function isObject(value) {
84+
85+
// Avoid a V8 JIT bug in Chrome 19-20.
86+
// See https://code.google.com/p/v8/issues/detail?id=2291 for more details.
87+
var type = typeof value;
88+
89+
return !!value && (type == 'object' || type == 'function');
90+
91+
}
7892

7993
function compareArrays( val1, val2 ) {
8094

@@ -107,8 +121,8 @@ function SmartComparer() {
107121

108122
function compareObjects( val1, val2 ) {
109123

110-
var isObj1 = _.isObject( val1 );
111-
var isObj2 = _.isObject( val2 );
124+
var isObj1 = isObject( val1 );
125+
var isObj2 = isObject( val2 );
112126

113127
// Compare type.
114128
if ( isObj1 !== isObj2 ) return makeFail( 'Values are not both objects' );
@@ -117,38 +131,64 @@ function SmartComparer() {
117131
if ( !isObj1 ) return undefined;
118132

119133
// Compare keys.
120-
var keys1 = _( val1 ).keys().difference( omitKeys ).value();
121-
var keys2 = _( val2 ).keys().difference( omitKeys ).value();
134+
var keys1 = Object.keys( val1 );
135+
var keys2 = Object.keys( val2 );
122136

123-
var missingActual = _.difference( keys1, keys2 );
137+
var keys1T = _( val1 ).keys().value();
138+
var keys2T = _( val2 ).keys().value();
139+
var missingActual = _.difference( keys1T, keys2T );
124140
if ( missingActual.length !== 0 ) {
141+
console.log(missingActual, keys1T, keys2T);
142+
}
143+
144+
for ( var i = 0, l = keys1.length; i < l; i++ ) {
145+
146+
if (keys2.indexOf(keys1[ i ]) < 0) {
147+
148+
return makeFail( 'Property "' + keys1[ i ] + '" is unexpected.' );
125149

126-
return makeFail( 'Property "' + missingActual[0] + '" is unexpected.' );
150+
}
127151

128152
}
129153

130-
var missingExpected = _.difference( keys2, keys1 );
131-
if ( missingExpected.length !== 0 ) {
154+
for ( var i = 0, l = keys2.length; i < l; i++ ) {
155+
156+
if (keys1.indexOf(keys2[ i ]) < 0) {
132157

133-
return makeFail( 'Property "' + missingExpected[0] + '" is missing.' );
158+
return makeFail( 'Property "' + keys2[ i ] + '" is missing.' );
159+
160+
}
134161

135162
}
136163

137164
// Keys are the same. For each key, compare content until a difference is found.
138-
var hadDifference = _.any( keys1, function ( key ) {
165+
var hadDifference = false;
166+
167+
for ( var i = 0, l = keys1.length; i < l; i++ ) {
168+
169+
var key = keys1[ i ];
170+
171+
if (key === "uuid" || key === "id") {
139172

140-
var prop1 = val1[key];
141-
var prop2 = val2[key];
173+
continue;
174+
175+
}
176+
177+
var prop1 = val1[ key ];
178+
var prop2 = val2[ key ];
142179

143180
// Compare property content.
144181
var eq = areEqual( prop1, prop2 );
145182

146183
// In case of failure, an message should already be set.
147184
// Add context to low level message.
148-
if ( !eq ) addContext( 'property "' + key + '"' );
149-
return !eq;
185+
if ( !eq ) {
150186

151-
});
187+
addContext( 'property "' + key + '"' );
188+
hadDifference = true;
189+
190+
}
191+
}
152192

153193
return ! hadDifference;
154194

test/unit/qunit-utils.js

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,31 @@ function checkGeometryJsonWriting( geom, json ) {
7272
QUnit.assert.equalKey( geom, json, 'uuid' );
7373
QUnit.assert.equal( json.id, undefined, "should not persist id" );
7474

75+
var params = geom.parameters;
76+
if ( !params ) {
77+
78+
return;
79+
80+
}
81+
7582
// All parameters from geometry should be persisted.
76-
_.forOwn( geom.parameters, function ( val, key ) {
83+
var keys = Object.keys( params );
84+
for ( var i = 0, l = keys.length; i < l; i++ ) {
7785

78-
QUnit.assert.equalKey( geom.parameters, json, key );
86+
QUnit.assert.equalKey( params, json, keys[ i ] );
7987

80-
});
88+
}
8189

8290
// All parameters from json should be transfered to the geometry.
8391
// json is flat. Ignore first level json properties that are not parameters.
8492
var notParameters = [ "metadata", "uuid", "type" ];
85-
_.forOwn( json, function ( val, key ) {
86-
87-
if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( geom.parameters, json, key );
93+
var keys = Object.keys( json );
94+
for ( var i = 0, l = keys.length; i < l; i++ ) {
8895

89-
});
96+
var key = keys[ i ];
97+
if ( notParameters.indexOf( key) === -1 ) QUnit.assert.equalKey( params, json, key );
9098

99+
}
91100
}
92101

93102
// Check parsing and reconstruction of json geometry
@@ -98,9 +107,8 @@ function checkGeometryJsonReading( json, geom ) {
98107
var loader = new THREE.ObjectLoader();
99108
var output = loader.parseGeometries( wrap );
100109

101-
QUnit.assert.ok( output[geom.uuid], 'geometry matching source uuid not in output' );
102-
QUnit.assert.smartEqual( output[geom.uuid], geom, 'Reconstruct geometry from ObjectLoader' );
103-
110+
QUnit.assert.ok( output[ geom.uuid ], 'geometry matching source uuid not in output' );
111+
QUnit.assert.smartEqual( output[ geom.uuid ], geom, 'Reconstruct geometry from ObjectLoader' );
104112
}
105113

106114
// Verify geom -> json -> geom
@@ -115,22 +123,35 @@ function checkGeometryJsonRoundtrip( geom ) {
115123
// Look for undefined and NaN values in numerical fieds.
116124
function checkFinite( geom ) {
117125

118-
var isNotFinite = _.any( geom.vertices, function ( v ) {
126+
var allVerticesAreFinite = true;
127+
128+
var vertices = geom.vertices || [];
129+
130+
for ( var i = 0, l = vertices.length; i < l; i++ ) {
119131

120-
return ! ( _.isFinite( v.x ) || _.isFinite( v.y ) || _.isFinite( v.z ) );
132+
var v = geom.vertices[ i ];
121133

122-
});
134+
if ( !( isFinite( v.x ) || isFinite( v.y ) || isFinite( v.z ) ) ) {
135+
136+
allVerticesAreFinite = false;
137+
break;
138+
139+
}
140+
141+
}
123142

124143
// TODO Buffers, normal, etc.
125144

126-
QUnit.assert.ok( isNotFinite === false, "contains non-finite coordinates" );
145+
QUnit.assert.ok( allVerticesAreFinite, "contains non-finite coordinates" );
127146

128147
}
129148

130149
// Run common geometry tests.
131150
function runStdGeometryTests( assert, geometries ) {
132151

133-
_.each( geometries, function( geom ) {
152+
for ( var i = 0, l = geometries.length; i < l; i++ ) {
153+
154+
var geom = geometries[ i ];
134155

135156
checkFinite( geom );
136157

@@ -140,7 +161,7 @@ function runStdGeometryTests( assert, geometries ) {
140161
// json round trip
141162
checkGeometryJsonRoundtrip( geom );
142163

143-
});
164+
}
144165

145166
}
146167

@@ -154,15 +175,16 @@ function runStdGeometryTests( assert, geometries ) {
154175
// Run common light tests.
155176
function runStdLightTests( assert, lights ) {
156177

157-
_.each( lights, function( light ) {
178+
for ( var i = 0, l = lights.length; i < l; i++ ) {
179+
180+
const light = lights[i];
158181

159182
// Clone
160183
checkLightClone( light );
161184

162185
// json round trip
163186
checkLightJsonRoundtrip( light );
164-
165-
});
187+
}
166188

167189
}
168190

@@ -209,4 +231,3 @@ function checkLightJsonRoundtrip( light ) {
209231
checkLightJsonReading( json, light );
210232

211233
}
212-

test/unit/unittests_three.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</head>
88
<body>
99
<div id="qunit"></div>
10+
<script src="../../node_modules/lodash/index.js"></script>
1011
<script src="qunit-1.18.0.js"></script>
1112
<script src="qunit-utils.js"></script>
1213
<script src="SmartComparer.js"></script>
@@ -27,9 +28,6 @@
2728
<script src="core/InterleavedBuffer.js"></script>
2829
<script src="core/InterleavedBufferAttribute.js"></script>
2930

30-
<script src="core/Raycaster.js"></script>
31-
<script src="core/Face3.js"></script>
32-
<script src="core/Geometry.js"></script>
3331
<script src="core/BufferAttribute.js"></script>
3432
<script src="core/BufferGeometry.js"></script>
3533
<script src="core/Clock.js"></script>

0 commit comments

Comments
 (0)