mirror of
				https://github.com/flynx/diff.js.git
				synced 2025-10-30 19:40:10 +00:00 
			
		
		
		
	refactored type detection (seems a bit over complicated)...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
		
							parent
							
								
									6d17023cf7
								
							
						
					
					
						commit
						f6d04cd54b
					
				
							
								
								
									
										66
									
								
								diff.js
									
									
									
									
									
								
							
							
						
						
									
										66
									
								
								diff.js
									
									
									
									
									
								
							| @ -417,12 +417,39 @@ var Types = Object.assign( | |||||||
| 		 | 		 | ||||||
| 		// Custom types...
 | 		// Custom types...
 | ||||||
| 		['Text',  | 		['Text',  | ||||||
| 			function(diff, A, B, options){ | 			{ | ||||||
| 				return Types.handle(Array, this, A.split(/\n/), B.split(/\n/), options) }], | 				check: function(A, B, options){ | ||||||
|  | 					options = options || {} | ||||||
|  | 					min = options.min_text_length || 1000 | ||||||
|  | 					return typeof(A) == 'string' && typeof(B) == 'string' | ||||||
|  | 						&& A.length > min && B.length > min | ||||||
|  | 				}, | ||||||
|  | 				handle: function(diff, A, B, options){ | ||||||
|  | 					return Types.handle(Array, this, A.split(/\n/), B.split(/\n/), options) }, | ||||||
|  | 			}], | ||||||
| 	]),  | 	]),  | ||||||
| 	{ | 	{ | ||||||
| 		detect: function(A, B){ | 		// NOTE: if A and B types mismatch we treat them as Object...
 | ||||||
| 			var type = Object | 		// XXX this may have issues with type (key) ordering, for example 
 | ||||||
|  | 		// 		if Object is not last it will match any set of items...
 | ||||||
|  | 		// XXX add support for checker predicates...
 | ||||||
|  | 		// 		...the main question here is how do we structure the predicate???
 | ||||||
|  | 		// XXX should .handle(..)/.check(..) be coupled here or in _diff(..)???
 | ||||||
|  | 		detect: function(A, B, options){ | ||||||
|  | 			var type | ||||||
|  | 
 | ||||||
|  | 			// predicates have priority...
 | ||||||
|  | 			for(var t of Types.keys()){ | ||||||
|  | 				if(Types.get(t).check | ||||||
|  | 						&& Types.get(t).check(A, B, options)){ | ||||||
|  | 					type = t | ||||||
|  | 					break | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			// search instances...
 | ||||||
|  | 			if(!type){ | ||||||
|  | 				type = Object | ||||||
| 				for(var t of Types.keys()){ | 				for(var t of Types.keys()){ | ||||||
| 					// leave pure objects for last...
 | 					// leave pure objects for last...
 | ||||||
| 					if(t === Object  | 					if(t === Object  | ||||||
| @ -442,6 +469,7 @@ var Types = Object.assign( | |||||||
| 						break | 						break | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|  | 			} | ||||||
| 			return type | 			return type | ||||||
| 		}, | 		}, | ||||||
| 		handle: function(type, obj, ...args){ | 		handle: function(type, obj, ...args){ | ||||||
| @ -456,10 +484,12 @@ var Types = Object.assign( | |||||||
| 				if(handler == null){ | 				if(handler == null){ | ||||||
| 					throw new TypeError('Diff: can\'t handle: ' + type) | 					throw new TypeError('Diff: can\'t handle: ' + type) | ||||||
| 				} | 				} | ||||||
| 			} while(!(handler instanceof Function)) | 			} while(!(handler instanceof Function) && !handler.handle) | ||||||
| 
 | 
 | ||||||
| 			// call the handler...
 | 			// call the handler...
 | ||||||
| 			handler.call(obj, ...args) | 			handler.handle ? | ||||||
|  | 				handle.handle.call(obj, ...args) | ||||||
|  | 				: handler.call(obj, ...args) | ||||||
| 
 | 
 | ||||||
| 			return obj | 			return obj | ||||||
| 		} | 		} | ||||||
| @ -515,30 +545,8 @@ function(A, B, options, cache){ | |||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| 	// find the matching type...
 | 	// find the matching type...
 | ||||||
| 	// NOTE: if A and B types mismatch we treat them as Object...
 | 	var type = Types.detect(A, B, options) | ||||||
| 	// XXX this may have issues with type (key) ordering, for example 
 |  | ||||||
| 	// 		if Object is not last it will match any set of items...
 |  | ||||||
| 	// XXX should type detection be here or in types?
 |  | ||||||
| 	var type = Object |  | ||||||
| 	for(var t of Types.keys()){ |  | ||||||
| 		// leave pure objects for last...
 |  | ||||||
| 		if(t === Object  |  | ||||||
| 				// skip non-conctructor stuff...
 |  | ||||||
| 				|| !(t instanceof Function)){ |  | ||||||
| 			continue |  | ||||||
| 		} |  | ||||||
| 
 | 
 | ||||||
| 		// full hit -- type match...
 |  | ||||||
| 		if(A instanceof t && B instanceof t){ |  | ||||||
| 			type = t |  | ||||||
| 			break |  | ||||||
| 		} |  | ||||||
| 		// partial hit -- type mismatch...
 |  | ||||||
| 		if(A instanceof t || B instanceof t){ |  | ||||||
| 			type = 'Basic' |  | ||||||
| 			break |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	// handle type...
 | 	// handle type...
 | ||||||
| 	var res = Types.handle(type, {}, diff, A, B, options) | 	var res = Types.handle(type, {}, diff, A, B, options) | ||||||
| 	// handle things we treat as objects (skipping object itself)...
 | 	// handle things we treat as objects (skipping object itself)...
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user