typed-conversions
Advanced tools
Comparing version 0.8.7 to 0.8.8
@@ -69,2 +69,19 @@ function _typeof(obj) { | ||
function keyValueArrayToDictionary(input) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var __key__ = options.key || "key"; | ||
var __value__ = options.value || "value"; | ||
return input.reduce(function (output, curr) { | ||
var key = curr[__key__]; | ||
var value = curr[__value__]; | ||
output[key] = value; | ||
return output; | ||
}, {}); | ||
} | ||
exports.keyValueArrayToDictionary = keyValueArrayToDictionary; | ||
function hashToArray(hashObj) { | ||
@@ -71,0 +88,0 @@ var __key__ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "id"; |
@@ -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),s=Object.keys(t).every(function(r){return!0===n[r]});i?s?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.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 s=new Error("arrayToHash: input was not an array!");throw s.name="NotAllowed",s}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=[];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}}); | ||
//# sourceMappingURL=typed-conversions.min.js.map |
@@ -9,2 +9,3 @@ import { IDictionary } from "common-types"; | ||
export declare function keyValueDictionaryToArray<T = any>(dict: IDictionary<T>, options?: IDictionary): any[]; | ||
export declare function keyValueArrayToDictionary<T = any>(input: T[], options?: IDictionary): IDictionary; | ||
export declare function hashToArray<T = any>(hashObj: IDictionary<T> | IDictionary<string> | IDictionary<number>, __key__?: keyof (T & { | ||
@@ -11,0 +12,0 @@ id: string; |
@@ -26,2 +26,13 @@ "use strict"; | ||
exports.keyValueDictionaryToArray = keyValueDictionaryToArray; | ||
function keyValueArrayToDictionary(input, options = {}) { | ||
const __key__ = options.key || "key"; | ||
const __value__ = options.value || "value"; | ||
return input.reduce((output, curr) => { | ||
const key = curr[__key__]; | ||
const value = curr[__value__]; | ||
output[key] = value; | ||
return output; | ||
}, {}); | ||
} | ||
exports.keyValueArrayToDictionary = keyValueArrayToDictionary; | ||
function hashToArray(hashObj, __key__ = "id") { | ||
@@ -28,0 +39,0 @@ if (hashObj && typeof hashObj !== "object") { |
{ | ||
"name": "typed-conversions", | ||
"version": "0.8.7", | ||
"version": "0.8.8", | ||
"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
34082
346