typed-conversions
Advanced tools
Comparing version 0.7.4 to 0.8.0
@@ -34,2 +34,19 @@ function _typeof(obj) { | ||
function removeIdPropertyFromHash(hash) { | ||
var idProp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "id"; | ||
var output = {}; | ||
Object.keys(hash).map(function (objId) { | ||
var input = hash[objId]; | ||
output[objId] = {}; | ||
Object.keys(input).map(function (prop) { | ||
if (prop !== idProp) { | ||
output[objId][prop] = input[prop]; | ||
} | ||
}); | ||
}); | ||
return output; | ||
} | ||
exports.removeIdPropertyFromHash = removeIdPropertyFromHash; | ||
function hashToArray(hashObj) { | ||
@@ -44,9 +61,11 @@ var __key__ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "id"; | ||
var results = []; | ||
Object.keys(hash).forEach(function (key) { | ||
var newProps = _typeof(hash[key]) === "object" ? hash[key] : { | ||
value: hash[key] | ||
Object.keys(hash).forEach(function (id) { | ||
var obj = hash[id]; | ||
var allEqualTrue = function allEqualTrue(prev, curr) { | ||
return obj[curr] !== true ? false : prev; | ||
}; | ||
var obj = Object.assign({}, newProps); | ||
obj[__key__] = key; | ||
results.push(obj); | ||
var isScalar = Object.keys(obj).reduce(allEqualTrue, true) ? true : false; | ||
var key = isScalar ? results.push(id) : results.push(isScalar ? id : Object.assign({}, obj, _defineProperty({}, __key__, id))); | ||
}); | ||
@@ -66,14 +85,38 @@ return results; | ||
function arrayToHash(list) { | ||
var __key__ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "id"; | ||
function arrayToHash(arr, keyProperty) { | ||
if (arr.length === 0) { | ||
return {}; | ||
} | ||
if (!Array.isArray(list)) { | ||
throw new Error("arrayToHash: input was not an array!"); | ||
var isScalar = _typeof(arr[0]) === "object" ? false : true; | ||
if (isScalar && keyProperty) { | ||
var e = new Error("You can not have an array of primitive values AND set a keyProperty!"); | ||
e.name = "NotAllowed"; | ||
throw e; | ||
} | ||
return list.reduce(function (acc, record) { | ||
var recordNoId = Object.assign({}, record); | ||
delete recordNoId[__key__]; | ||
return Object.keys(recordNoId).length === 1 && recordNoId.value ? Object.assign({}, acc, _defineProperty({}, record[__key__], recordNoId.value)) : Object.assign({}, acc, _defineProperty({}, record[__key__], recordNoId)); | ||
}, new Object()); | ||
if (!keyProperty && !isScalar) { | ||
if (arr[0].hasOwnProperty("id")) { | ||
keyProperty = "id"; | ||
} else { | ||
var _e = new Error("Tried to default to a keyProperty of \"id\" but that property does not appear to be in the array passed in"); | ||
_e.name = "NotAllowed"; | ||
throw _e; | ||
} | ||
} | ||
if (!Array.isArray(arr)) { | ||
var _e2 = new Error("arrayToHash: input was not an array!"); | ||
_e2.name = "NotAllowed"; | ||
throw _e2; | ||
} | ||
var output = arr.reduce(function (prev, curr) { | ||
var key = isScalar ? curr : curr[keyProperty]; | ||
return isScalar ? Object.assign({}, prev, _defineProperty({}, key, true)) : Object.assign({}, prev, _defineProperty({}, key, curr)); | ||
}, {}); | ||
return output; | ||
} | ||
@@ -80,0 +123,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r():"function"==typeof define&&define.amd?define(r):r()}(0,function(){"use strict";function e(r){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(r)}function r(e,r,t){return r in e?Object.defineProperty(e,r,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[r]=t,e}function t(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id";if(r&&"object"!==e(r))throw new Error("Cant convert hash-to-array because hash was not passed in: "+r);var n=Object.assign({},r),o=[];return Object.keys(n).forEach(function(r){var a="object"===e(n[r])?n[r]:{value:n[r]},s=Object.assign({},a);s[t]=r,o.push(s)}),o}Object.defineProperty(exports,"__esModule",{value:!0}),exports.hashToArray=t,exports.flatten=function e(r){return r.reduce(function(r,t){return r.concat(Array.isArray(t)?e(t):t)},[])},exports.arrayToHash=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id";if(!Array.isArray(e))throw new Error("arrayToHash: input was not an array!");return e.reduce(function(e,n){var o=Object.assign({},n);return delete o[t],1===Object.keys(o).length&&o.value?Object.assign({},e,r({},n[t],o.value)):Object.assign({},e,r({},n[t],o))},new Object)},exports.snapshotToArray=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id";return t(e.val()||{},r)},exports.snapshotToHash=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",o=t.val()||{};return Object.keys(o).forEach(function(t){var a;return"object"===e(o[t])?o[t][n]=t:o[t]=(r(a={},n,t),r(a,"value",o[t]),a)}),o},exports.snapshotToOrderedArray=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",o=[];return t.forEach(function(t){var a=t.val(),s=t.key;if("object"!==e(a))throw new Error("Can't create a list from scalar values: \"".concat(a,'" | "').concat(s,'"'));return o.push(Object.assign(r({},n,s),a)),!0}),o},exports.snapshotToOrderedHash=function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"id",t=this.snapshotToOrderedArray(e,r);return this.arrayToHash(t)}}); | ||
!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}function t(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);i?a.push(r):a.push(i?r:Object.assign({},t,e({},o,r)))}),a}Object.defineProperty(exports,"__esModule",{value:!0}),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.hashToArray=t,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 t(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)}}); | ||
//# sourceMappingURL=typed-conversions.min.js.map |
import { IDictionary } from "common-types"; | ||
export { IDictionary } from "common-types"; | ||
export interface ISnapShot { | ||
@@ -8,5 +7,6 @@ val: () => any; | ||
} | ||
export declare function hashToArray<T = IDictionary>(hashObj: IDictionary<any>, __key__?: string): T[]; | ||
export declare function flatten(list: any[]): any[]; | ||
export declare function arrayToHash<T = IDictionary>(list: any[], __key__?: string): any; | ||
export declare function removeIdPropertyFromHash<T = IDictionary>(hash: IDictionary<T>, idProp?: string): any; | ||
export declare function hashToArray<T = any>(hashObj: IDictionary<T>, __key__?: string): T[]; | ||
export declare function flatten<T = any>(list: any): T[]; | ||
export declare function arrayToHash<T = any>(arr: T[], keyProperty?: keyof T): IDictionary<T>; | ||
export declare function snapshotToArray<T = IDictionary>(snap: ISnapShot, idProp?: string): T[]; | ||
@@ -13,0 +13,0 @@ export declare function snapshotToHash<T = IDictionary>(snap: ISnapShot, idProp?: string): T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function removeIdPropertyFromHash(hash, idProp = "id") { | ||
const output = {}; | ||
Object.keys(hash).map((objId) => { | ||
const input = hash[objId]; | ||
output[objId] = {}; | ||
Object.keys(input).map(prop => { | ||
if (prop !== idProp) { | ||
output[objId][prop] = input[prop]; | ||
} | ||
}); | ||
}); | ||
return output; | ||
} | ||
exports.removeIdPropertyFromHash = removeIdPropertyFromHash; | ||
function hashToArray(hashObj, __key__ = "id") { | ||
@@ -9,7 +23,11 @@ if (hashObj && typeof hashObj !== "object") { | ||
const results = []; | ||
Object.keys(hash).forEach(key => { | ||
const newProps = typeof hash[key] === "object" ? hash[key] : { value: hash[key] }; | ||
const obj = Object.assign({}, newProps); | ||
obj[__key__] = key; | ||
results.push(obj); | ||
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 key = isScalar | ||
? results.push(id) | ||
: results.push(isScalar ? id : Object.assign({}, obj, { [__key__]: id })); | ||
}); | ||
@@ -23,12 +41,33 @@ return results; | ||
exports.flatten = flatten; | ||
function arrayToHash(list, __key__ = "id") { | ||
if (!Array.isArray(list)) { | ||
throw new Error(`arrayToHash: input was not an array!`); | ||
function arrayToHash(arr, keyProperty) { | ||
if (arr.length === 0) { | ||
return {}; | ||
} | ||
return list.reduce((acc, record) => { | ||
const recordNoId = Object.assign({}, record); | ||
delete recordNoId[__key__]; | ||
return Object.keys(recordNoId).length === 1 && recordNoId.value | ||
? Object.assign({}, acc, { [record[__key__]]: recordNoId.value }) : Object.assign({}, acc, { [record[__key__]]: recordNoId }); | ||
}, new Object()); | ||
const isScalar = typeof arr[0] === "object" ? false : true; | ||
if (isScalar && keyProperty) { | ||
const e = new Error(`You can not have an array of primitive values AND set a keyProperty!`); | ||
e.name = "NotAllowed"; | ||
throw e; | ||
} | ||
if (!keyProperty && !isScalar) { | ||
if (arr[0].hasOwnProperty("id")) { | ||
keyProperty = "id"; | ||
} | ||
else { | ||
const e = new Error(`Tried to default to a keyProperty of "id" but that property does not appear to be in the array passed in`); | ||
e.name = "NotAllowed"; | ||
throw e; | ||
} | ||
} | ||
if (!Array.isArray(arr)) { | ||
const e = new Error(`arrayToHash: input was not an array!`); | ||
e.name = "NotAllowed"; | ||
throw e; | ||
} | ||
const output = arr.reduce((prev, curr) => { | ||
const key = isScalar ? curr : curr[keyProperty]; | ||
return isScalar | ||
? Object.assign({}, prev, { [key]: true }) : Object.assign({}, prev, { [key]: curr }); | ||
}, {}); | ||
return output; | ||
} | ||
@@ -35,0 +74,0 @@ exports.arrayToHash = arrayToHash; |
{ | ||
"name": "typed-conversions", | ||
"version": "0.7.4", | ||
"version": "0.8.0", | ||
"description": "typed-conversions", | ||
@@ -8,4 +8,11 @@ "license": "MIT", | ||
"author": "Ken Snyder <ken@ken.net>", | ||
"keywords": ["firebase", "utility", "typescript"], | ||
"files": ["lib", "dist"], | ||
"keywords": [ | ||
"firebase", | ||
"utility", | ||
"typescript" | ||
], | ||
"files": [ | ||
"lib", | ||
"dist" | ||
], | ||
"main": "lib/index.js", | ||
@@ -18,8 +25,5 @@ "browser": "dist/typed-conversions.min.js", | ||
"lint": "tslint --force --format verbose \"src/**/*.ts\"", | ||
"build": | ||
"npm run clean && npm run lint && echo Using TypeScript && tsc --version && tsc --pretty && bili lib/index.js --format umd-min,es", | ||
"test": | ||
"npm run build && mocha --compilers ts:ts-node/register --recursive 'test/**/*-spec.ts'", | ||
"coverage": | ||
"nyc --include='src/**/*.ts' --reporter=text --reporter=html --reporter=lcov mocha --compilers ts:ts-node/register --recursive 'test/**/*.test.ts'", | ||
"build": "npm run clean && npm run lint && echo Using TypeScript && tsc --version && tsc --pretty && bili lib/index.js --format umd-min,es", | ||
"test": "npm run build && mocha --compilers ts:ts-node/register --recursive 'test/**/*-spec.ts'", | ||
"coverage": "nyc --include='src/**/*.ts' --reporter=text --reporter=html --reporter=lcov mocha --compilers ts:ts-node/register --recursive 'test/**/*.test.ts'", | ||
"watch": "npm run build -- --watch", | ||
@@ -55,6 +59,14 @@ "watch:test": "npm run test -- --watch" | ||
"nyc": { | ||
"include": ["src/**/*.ts"], | ||
"exclude": ["lib"], | ||
"extension": [".ts"], | ||
"require": ["ts-node/register"], | ||
"include": [ | ||
"src/**/*.ts" | ||
], | ||
"exclude": [ | ||
"lib" | ||
], | ||
"extension": [ | ||
".ts" | ||
], | ||
"require": [ | ||
"ts-node/register" | ||
], | ||
"reporter": [], | ||
@@ -61,0 +73,0 @@ "all": true |
@@ -6,7 +6,10 @@ # typed-data-conversions | ||
## General Use | ||
- `hashToArray<T>` | ||
- `arrayToHash<T>` | ||
- `flatten` | ||
- **hashToArray**< T = any >(_hash_: IDictionary< T >): T[] | ||
- **arrayToHash**< T = any >(_arr_: T[]): IDictionary< T > | ||
- **removeIdPropertyFromHash**< T = any >(_hash_: IDictionary<T>, _keyProp_: keyof T = "id"): IDictionary<Exclude<T, "id">> | ||
- **flatten**< T = any >(list: T[]): T[] | ||
## Firebase Specific | ||
- `snapshotToArray<T>: T[]` | ||
@@ -13,0 +16,0 @@ - `snapshotToHash<T>: IDictionary<T>` - combines snap.val() and snap.key into a JS Object |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26509
265
18