typed-conversions
Advanced tools
Comparing version 0.8.11 to 0.8.12
@@ -152,3 +152,3 @@ function _typeof(obj) { | ||
var output = arr.reduce(function (prev, curr) { | ||
var key = isScalar ? curr : curr[keyProperty]; | ||
var key = isScalar ? curr : typeof keyProperty === "function" ? curr[keyProperty(curr)] : curr[keyProperty]; | ||
return isScalar ? Object.assign({}, prev, _defineProperty({}, key, true)) : Object.assign({}, prev, _defineProperty({}, key, curr)); | ||
@@ -155,0 +155,0 @@ }, {}); |
@@ -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=[],i=Object.keys(n).every(function(r){return!0===n[r]});Object.keys(n).every(function(e){return"object"!==r(n[e])});return 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}}); | ||
!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]});Object.keys(n).every(function(e){return"object"!==r(n[e])});return 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:"function"==typeof o?t[o(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 |
@@ -14,3 +14,4 @@ import { IDictionary } from "common-types"; | ||
export declare function flatten<T = any>(list: any): T[]; | ||
export declare function arrayToHash<T = any>(arr: T[], keyProperty?: keyof T): IDictionary<T>; | ||
export declare type FunctionProperty<T> = (obj: T) => string; | ||
export declare function arrayToHash<T = any>(arr: T[], keyProperty?: keyof T | FunctionProperty<T>): IDictionary<T>; | ||
export declare function snapshotToArray<T = IDictionary>(snap: ISnapShot, idProp?: string): T[]; | ||
@@ -17,0 +18,0 @@ export declare function snapshotToHash<T = IDictionary>(snap: ISnapShot, idProp?: string): T; |
@@ -85,3 +85,7 @@ "use strict"; | ||
const output = arr.reduce((prev, curr) => { | ||
const key = isScalar ? curr : curr[keyProperty]; | ||
const key = isScalar | ||
? curr | ||
: typeof keyProperty === "function" | ||
? curr[keyProperty(curr)] | ||
: curr[keyProperty]; | ||
return isScalar | ||
@@ -88,0 +92,0 @@ ? Object.assign({}, prev, { [key]: true }) : Object.assign({}, prev, { [key]: curr }); |
{ | ||
"name": "typed-conversions", | ||
"version": "0.8.11", | ||
"version": "0.8.12", | ||
"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
33763
345