2023-12-24 09:14:46 -06:00
function deep _copy ( from ) { return json . decode ( json . encode ( from ) ) ; }
2023-04-22 16:44:26 -05:00
2023-10-26 11:48:02 -05:00
function valdiff ( from , to )
{
if ( typeof from !== typeof to ) return from ;
if ( typeof from === 'function' ) return undefined ;
if ( typeof from === 'undefined' ) return undefined ;
if ( typeof from === 'number' ) {
return to ;
return undefined ;
}
if ( typeof from === 'object' )
return ediff ( from , to ) ;
if ( from !== to ) return to ;
return undefined ;
}
function ediff ( from , to )
{
var ret = { } ;
if ( ! to )
// return ediff(from, {});
return deep _copy ( from ) ;
Object . entries ( from ) . forEach ( function ( [ key , v ] ) {
if ( typeof v === 'function' ) return ;
if ( typeof v === 'undefined' ) return ;
2024-02-29 13:54:33 -06:00
2023-10-26 11:48:02 -05:00
if ( Array . isArray ( v ) ) {
2023-11-06 07:05:27 -06:00
if ( ! Array . isArray ( to [ key ] ) || v . length !== to [ key ] . length ) {
var r = ediff ( v , [ ] ) ;
if ( r ) ret [ key ] = Object . values ( r ) ;
return ;
}
2023-10-26 11:48:02 -05:00
var diff = ediff ( from [ key ] , to [ key ] ) ;
2024-02-19 20:31:26 -06:00
if ( diff && ! Object . empty ( diff ) )
2023-10-26 11:48:02 -05:00
ret [ key ] = Object . values ( ediff ( v , [ ] ) ) ;
return ;
}
if ( typeof v === 'object' ) {
var diff = ediff ( v , to [ key ] ) ;
2024-02-19 20:31:26 -06:00
if ( diff && ! Object . empty ( diff ) )
2023-10-26 11:48:02 -05:00
ret [ key ] = diff ;
return ;
}
if ( typeof v === 'number' ) {
2024-02-29 13:54:33 -06:00
if ( ! isFinite ( v ) ) v = null ; // Squash infinity to null
if ( v !== to [ key ] )
2023-12-24 09:14:46 -06:00
ret [ key ] = v ;
2023-10-26 11:48:02 -05:00
return ;
}
if ( ! to || v !== to [ key ] )
ret [ key ] = v ;
} ) ;
2024-02-19 20:31:26 -06:00
if ( Object . empty ( ret ) ) return undefined ;
2023-10-26 11:48:02 -05:00
return ret ;
}
2024-02-29 13:54:33 -06:00
ediff . doc = "Given a from and to object, returns an object that, if applied to from, will make it the same as to. Does not include deletion; it is only additive. If one element in an array is different, the entire array is copied. Squashes infinite numbers to null for use in JSON." ;
2023-10-26 11:48:02 -05:00
function samediff ( from , to )
{
var same = [ ] ;
if ( ! to ) return same ;
if ( typeof to !== 'object' ) {
2024-02-25 17:31:48 -06:00
console . warn ( "'To' must be an object. Got " + to ) ;
2023-10-26 11:48:02 -05:00
return same ;
}
Object . keys ( from ) . forEach ( function ( k ) {
if ( Object . isObject ( from [ k ] ) ) {
samediff ( from [ k ] , to [ k ] ) ;
return ;
}
// if (Array.isArray(from[k])) {
// var d = valdiff(from[k], to[k]);
// if (!d)
// }
var d = valdiff ( from [ k ] , to [ k ] ) ;
if ( ! d )
delete from [ k ] ;
} ) ;
return same ;
}
samediff . doc = "Given a from and to object, returns an array of keys that are the same on from as on to." ;
2024-02-27 10:09:15 -06:00
return {
deep _copy ,
ediff ,
samediff ,
2023-10-26 11:48:02 -05:00
}