utils/object.js

import _ from 'lodash';

/**
 * Utilities for working with objects.
 * @module ObjectUtils
 */
const ObjectUtils = {
    /**
     * Creates a query string representation of an object.
     * @static
     * @function querify
     * @param {Object} ob - The object to querify.
     * @example
     * ObjectUtils.querify({foo: 'hello', baz: 'there'});
     * @returns {String} - The query string output.
     */
    querify(ob) {
        return _.map(ob, (value, key) => {
            return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
        }).join('&');
    },

    /**
     * Returns the properties of an object whose keys partially match the provided string.
     * @static
     * @function getPartialMatchedProps
     * @param {Object} ob - The object to get properties from.
     * @param {String} match - The string to match keys against.
     * @example
     * ReactUtils.getPartialMatchedProps({foo: 'foo', baz: 'baz', fool: 'fool'}, 'foo');
     * @returns {Object} - The object containing only the properties whose keys partiallly matched.
     */
    getPartialMatchedProps(ob, match) {
        const matched = {};

        _.forEach(ob, (value, key) => {
            if (_.includes(key, match)) {
                matched[key] = value;
            }
        });

        return matched;
    }
};

export default ObjectUtils;