typed-conversions
Advanced tools
Comparing version 0.8.8 to 0.8.9
@@ -56,3 +56,3 @@ function _typeof(obj) { | ||
var __key__ = options.key || "key"; | ||
var __key__ = options.key || "id"; | ||
@@ -96,16 +96,14 @@ var __value__ = options.value || "value"; | ||
var results = []; | ||
Object.keys(hash).forEach(function (id) { | ||
var obj = hash[id]; | ||
var isHashArray = Object.keys(hash).every(function (i) { | ||
return hash[i] === true; | ||
}); | ||
var isHashValue = Object.keys(hash).every(function (i) { | ||
return _typeof(hash[i]) !== "object"; | ||
}); | ||
console.log(isHashArray, isHashValue); | ||
Object.keys(hash).map(function (id) { | ||
var _ref; | ||
var allEqualTrue = function allEqualTrue(prev, curr) { | ||
return obj[curr] !== true ? false : prev; | ||
}; | ||
var isScalar = Object.keys(obj).reduce(allEqualTrue, true) ? true : false; | ||
var isSimpleArray = Object.keys(obj).every(function (i) { | ||
return hash[i] === true; | ||
}); | ||
var key = isScalar ? isSimpleArray ? results.push(id) : results.push(Object.assign(_defineProperty({}, __key__, id), { | ||
value: hash[id] | ||
})) : results.push(isScalar ? id : Object.assign({}, obj, _defineProperty({}, __key__, id))); | ||
var obj = _typeof(hash[id]) === "object" ? Object.assign({}, hash[id], _defineProperty({}, __key__, id)) : isHashArray ? id : (_ref = {}, _defineProperty(_ref, __key__, id), _defineProperty(_ref, "value", hash[id]), _ref); | ||
results.push(obj); | ||
}); | ||
@@ -112,0 +110,0 @@ return results; |
@@ -1,2 +0,2 @@ | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(e)}function e(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("lodash.get");function o(t){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id";if(t&&"object"!==r(t))throw new Error("Cant convert hash-to-array because hash was not passed in: "+t);var n=Object.assign({},t),a=[];return Object.keys(n).forEach(function(r){var t=n[r],i=!!Object.keys(t).reduce(function(r,e){return!0===t[e]&&r},!0),u=Object.keys(t).every(function(r){return!0===n[r]});i?u?a.push(r):a.push(Object.assign(e({},o,r),{value:n[r]})):a.push(i?r:Object.assign({},t,e({},o,r)))}),a}exports.removeIdPropertyFromHash=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",t={};return Object.keys(r).map(function(o){var n=r[o];t[o]={},Object.keys(n).map(function(r){r!==e&&(t[o][r]=n[r])})}),t},exports.keyValueDictionaryToArray=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=t.key||"key",n=t.value||"value";return Object.keys(r).reduce(function(t,a){var i;return t.concat((e(i={},o,a),e(i,n,r[a]),i))},[])},exports.keyValueArrayToDictionary=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=e.key||"key",o=e.value||"value";return r.reduce(function(r,e){var n=e[t],a=e[o];return r[n]=a,r},{})},exports.hashToArray=o,exports.flatten=function r(e){return e.reduce(function(e,t){return e.concat(Array.isArray(t)?r(t):t)},[])},exports.arrayToHash=function(t,o){if(0===t.length)return{};var n="object"!==r(t[0]);if(n&&o){var a=new Error("You can not have an array of primitive values AND set a keyProperty!");throw a.name="NotAllowed",a}if(!o&&!n){if(!t[0].hasOwnProperty("id")){var i=new Error('Tried to default to a keyProperty of "id" but that property does not appear to be in the array passed in');throw i.name="NotAllowed",i}o="id"}if(!Array.isArray(t)){var u=new Error("arrayToHash: input was not an array!");throw u.name="NotAllowed",u}return t.reduce(function(r,t){var a=n?t:t[o];return n?Object.assign({},r,e({},a,!0)):Object.assign({},r,e({},a,t))},{})},exports.snapshotToArray=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id";return o(r.val()||{},e)},exports.snapshotToHash=function(t){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",n=t.val()||{};return Object.keys(n).forEach(function(t){var a;return"object"===r(n[t])?n[t][o]=t:n[t]=(e(a={},o,t),e(a,"value",n[t]),a)}),n},exports.snapshotToOrderedArray=function(t){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",n=[];return t.forEach(function(t){var a=t.val(),i=t.key;if("object"!==r(a))throw new Error("Can't create a list from scalar values: \"".concat(a,'" | "').concat(i,'"'));return n.push(Object.assign(e({},o,i),a)),!0}),n},exports.snapshotToOrderedHash=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",t=this.snapshotToOrderedArray(r,e);return this.arrayToHash(t)},exports.getPropertyAcrossDictionaryItems=function(r,e){var o=[];return Object.keys(r).map(function(n){var a=t(r[n],e,void 0);void 0!==a&&o.push(a)}),o}}); | ||
!function(r,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(r){return typeof r}:function(r){return r&&"function"==typeof Symbol&&r.constructor===Symbol&&r!==Symbol.prototype?"symbol":typeof r})(e)}function e(r,e,t){return e in r?Object.defineProperty(r,e,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[e]=t,r}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("lodash.get");function o(t){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id";if(t&&"object"!==r(t))throw new Error("Cant convert hash-to-array because hash was not passed in: "+t);var n=Object.assign({},t),a=[],i=Object.keys(n).every(function(r){return!0===n[r]}),u=Object.keys(n).every(function(e){return"object"!==r(n[e])});return console.log(i,u),Object.keys(n).map(function(t){var u,c="object"===r(n[t])?Object.assign({},n[t],e({},o,t)):i?t:(e(u={},o,t),e(u,"value",n[t]),u);a.push(c)}),a}exports.removeIdPropertyFromHash=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",t={};return Object.keys(r).map(function(o){var n=r[o];t[o]={},Object.keys(n).map(function(r){r!==e&&(t[o][r]=n[r])})}),t},exports.keyValueDictionaryToArray=function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=t.key||"id",n=t.value||"value";return Object.keys(r).reduce(function(t,a){var i;return t.concat((e(i={},o,a),e(i,n,r[a]),i))},[])},exports.keyValueArrayToDictionary=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},t=e.key||"key",o=e.value||"value";return r.reduce(function(r,e){var n=e[t],a=e[o];return r[n]=a,r},{})},exports.hashToArray=o,exports.flatten=function r(e){return e.reduce(function(e,t){return e.concat(Array.isArray(t)?r(t):t)},[])},exports.arrayToHash=function(t,o){if(0===t.length)return{};var n="object"!==r(t[0]);if(n&&o){var a=new Error("You can not have an array of primitive values AND set a keyProperty!");throw a.name="NotAllowed",a}if(!o&&!n){if(!t[0].hasOwnProperty("id")){var i=new Error('Tried to default to a keyProperty of "id" but that property does not appear to be in the array passed in');throw i.name="NotAllowed",i}o="id"}if(!Array.isArray(t)){var u=new Error("arrayToHash: input was not an array!");throw u.name="NotAllowed",u}return t.reduce(function(r,t){var a=n?t:t[o];return n?Object.assign({},r,e({},a,!0)):Object.assign({},r,e({},a,t))},{})},exports.snapshotToArray=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id";return o(r.val()||{},e)},exports.snapshotToHash=function(t){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",n=t.val()||{};return Object.keys(n).forEach(function(t){var a;return"object"===r(n[t])?n[t][o]=t:n[t]=(e(a={},o,t),e(a,"value",n[t]),a)}),n},exports.snapshotToOrderedArray=function(t){var o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",n=[];return t.forEach(function(t){var a=t.val(),i=t.key;if("object"!==r(a))throw new Error("Can't create a list from scalar values: \"".concat(a,'" | "').concat(i,'"'));return n.push(Object.assign(e({},o,i),a)),!0}),n},exports.snapshotToOrderedHash=function(r){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",t=this.snapshotToOrderedArray(r,e);return this.arrayToHash(t)},exports.getPropertyAcrossDictionaryItems=function(r,e){var o=[];return Object.keys(r).map(function(n){var a=t(r[n],e,void 0);void 0!==a&&o.push(a)}),o}}); | ||
//# sourceMappingURL=typed-conversions.min.js.map |
@@ -19,3 +19,3 @@ "use strict"; | ||
function keyValueDictionaryToArray(dict, options = {}) { | ||
const __key__ = options.key || "key"; | ||
const __key__ = options.key || "id"; | ||
const __value__ = options.value || "value"; | ||
@@ -44,14 +44,11 @@ return Object.keys(dict).reduce((result, key) => { | ||
const results = []; | ||
Object.keys(hash).forEach(id => { | ||
const obj = hash[id]; | ||
const allEqualTrue = (prev, curr) => { | ||
return obj[curr] !== true ? false : prev; | ||
}; | ||
const isScalar = Object.keys(obj).reduce(allEqualTrue, true) ? true : false; | ||
const isSimpleArray = Object.keys(obj).every(i => hash[i] === true); | ||
const key = isScalar | ||
? isSimpleArray | ||
? results.push(id) | ||
: results.push(Object.assign({ [__key__]: id }, { value: hash[id] })) | ||
: results.push(isScalar ? id : Object.assign({}, obj, { [__key__]: id })); | ||
const isHashArray = Object.keys(hash).every(i => hash[i] === true); | ||
const isHashValue = Object.keys(hash).every(i => typeof hash[i] !== "object"); | ||
console.log(isHashArray, isHashValue); | ||
Object.keys(hash).map(id => { | ||
const obj = typeof hash[id] === "object" | ||
? Object.assign({}, hash[id], { [__key__]: id }) : isHashArray | ||
? id | ||
: { [__key__]: id, value: hash[id] }; | ||
results.push(obj); | ||
}); | ||
@@ -58,0 +55,0 @@ return results; |
{ | ||
"name": "typed-conversions", | ||
"version": "0.8.8", | ||
"version": "0.8.9", | ||
"description": "typed-conversions", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33470
342