"use strict";

/**
 * 检查对象是否为JSON。
 *
 * @see http://stackoverflow.com/questions/11182924/how-to-check-if-javascript-object-is-json
 *
 * @param object
 */
exports.isJsonObject = function(object){
    return {}.constructor === object.constructor;
};

/**
 * 检查对象是否具有指定的属性列表。
 *
 * @param arguments
 * @returns {{pass: boolean, message: string}}
 */
exports.fieldsCheck = function(){
    var ret = {pass: false, message: ''};

    if(arguments.length <= 1){
        ret.message = 'Function arguments not enough.';
        return ret;
    }

    var obj = arguments[0];
    if(!this.isJsonObject(obj)){
        ret.message = 'Function first argument must be a dict.';
        return ret;
    }

    ret.pass = true;
    ret.message = 'Missing field(s): ';
    for(var i = 1; i < arguments.length; ++i){
        if(!obj.hasOwnProperty(arguments[i])){
            ret.pass = false;
            ret.message += arguments[i];

            if(i !== arguments.length - 1){
                ret.message += ", ";
            }
        }
    }

    if(ret.pass) ret.message = null;

    return ret;
};

exports.timestampToLong = function(tm){
    return Date.parse(new Date(tm));
};