'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

exports.default = toFlat;

/**
 * take an object and create a new FLAT object
 * { foo: { bar: 'baz' } } -> { "$['foo']['bar']": 'baz' }
 * @param  {any} data   [description]
 * @param  {Object} buffer because it's recursive
 * @param  {String} path   to keep the json path
 * @return {Object}        flatten
 */
function toFlat(data) {
	var buffer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
	var path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '$';

	if (Array.isArray(data)) {
		data.forEach(function (value, index) {
			toFlat(value, buffer, path + '[' + index + ']');
		});
	} else if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) === 'object') {
		Object.keys(data).forEach(function (key) {
			toFlat(data[key], buffer, path + '[\'' + key + '\']');
		});
	} else {
		buffer[path] = data; // eslint-disable-line no-param-reassign
	}
	return buffer;
}