import {format} from 'date-fns'
let overrided = false;

let originalParse: any;
const reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.{0,1}\d*))(?:Z|(\+|-)([\d|:]*))?$/;

export  function overrideParser(){
    if(!overrided){
        originalParse = JSON.parse;
        JSON.parse = function(obj, next){
            return originalParse(obj,function(key: any, value: any){
                if (typeof value === 'string') {
                    if(value.charAt(10) == 'T' && value.charAt(value.length - 1) == 'Z'){
                        var varifyResult = reISO.exec(value);
                        if (varifyResult) {
                            value = new Date(Date.parse(value));
                            value.text = format(value, 'yyyy-MM-dd hh:mm:ss')                            
                        }
                    }
                }
                
                if(next){
                    return next(obj, value);
                }
                else{
                    return value
                }
            })
        }
        overrided = true;
    }
}