made with esnextbin
Last active
February 17, 2016 05:02
-
-
Save bfitch/83cb759066b42906988c to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
<!-- put additional styles and scripts here --> | |
</head> | |
<body> | |
<div id="app"></div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash'; | |
let data = new FormData(); | |
data.append('message[author_uid]', '123'); | |
data.append('message[author_type]', 'patient'); | |
data.append('message[body]', 'hello friend'); | |
data.append('message[uploads]', 'an upload'); | |
data.append('message[conversation][uid]', 'abc'); | |
data.append('message[conversation][patient_guid]', 'def124'); | |
data.append('message[conversation][subject]', 'Woot there it is'); | |
data.append('message[conversation][conversation_type]', 'email'); | |
function buildObject([head,...tail], value) { | |
if (head) { | |
return {[head]: buildObject(tail,value)}; | |
} else { | |
return value; | |
} | |
} | |
const json = _. | |
chain([...data.entries()]). | |
map(([key,value],i) => { | |
return { | |
key: _.reject(key.split(/\[|\]/g), _.isEmpty), | |
value: value | |
} | |
}). | |
reduce((acc,{key,value}) => { | |
return _.merge(acc, buildObject(key,value)); | |
},{}).value(); | |
console.log(json) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"lodash": "4.4.0", | |
"babel-runtime": "6.5.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); | |
var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); | |
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); | |
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); | |
var _defineProperty2 = require('babel-runtime/helpers/defineProperty'); | |
var _defineProperty3 = _interopRequireDefault(_defineProperty2); | |
var _toArray2 = require('babel-runtime/helpers/toArray'); | |
var _toArray3 = _interopRequireDefault(_toArray2); | |
var _lodash = require('lodash'); | |
var _lodash2 = _interopRequireDefault(_lodash); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var data = new FormData(); | |
data.append('message[author_uid]', '123'); | |
data.append('message[author_type]', 'patient'); | |
data.append('message[body]', 'hello friend'); | |
data.append('message[uploads]', 'an upload'); | |
data.append('message[conversation][uid]', 'abc'); | |
data.append('message[conversation][patient_guid]', 'def124'); | |
data.append('message[conversation][subject]', 'Woot there it is'); | |
data.append('message[conversation][conversation_type]', 'email'); | |
function buildObject(_ref, value) { | |
var _ref2 = (0, _toArray3.default)(_ref); | |
var head = _ref2[0]; | |
var tail = _ref2.slice(1); | |
if (head) { | |
return (0, _defineProperty3.default)({}, head, buildObject(tail, value)); | |
} else { | |
return value; | |
} | |
} | |
var json = _lodash2.default.chain([].concat((0, _toConsumableArray3.default)(data.entries()))).map(function (_ref4, i) { | |
var _ref5 = (0, _slicedToArray3.default)(_ref4, 2); | |
var key = _ref5[0]; | |
var value = _ref5[1]; | |
return { | |
key: _lodash2.default.reject(key.split(/\[|\]/g), _lodash2.default.isEmpty), | |
value: value | |
}; | |
}).reduce(function (acc, _ref6) { | |
var key = _ref6.key; | |
var value = _ref6.value; | |
return _lodash2.default.merge(acc, buildObject(key, value)); | |
}, {}).value(); | |
console.log(json); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment