/**
 * pick.js
 *
 * @Author: jruif
 * @Date: 2018/1/19 下午2:11
 */
import { isObject, isFunction, isUndefined } from './isType';

var base = function base(obj, path, predicate) {
    var result = {};
    if (Array.isArray(path)) {
        for (var i = 0; i < path.length; i++) {
            var key = path[i];
            if (isFunction(predicate) && predicate(obj[key]) || isUndefined(predicate)) {
                result[key] = obj[key];
            }
        }
    }
    return result;
};

/*
 * let object = { 'a': 1, 'b': '2', 'c': 3 };
 *
 * pick(object, ['a', 'c']);
 * // => { 'a': 1, 'c': 3 }
 *
 */
var pick = function pick(obj, path) {
    return isObject(obj) ? base(obj, path) : {};
};

/*
 * import { isNumber } from './isType';
 * let object = { 'a': 1, 'b': '2', 'c': 3 };
 *
 * pickBy(object, isString);
 * // => { 'a': 1, 'c': 3 }
 *
 */
export var pickBy = function pickBy(obj, predicate) {
    var path = Object.keys(obj);
    return isObject(obj) ? base(obj, path, predicate) : {};
};

export default pick;