typed-conversions
Advanced tools
Comparing version 0.9.5 to 0.9.6
@@ -10,17 +10,2 @@ import { IDictionary } from "common-types"; | ||
export declare function keyValueArrayToDictionary<T = any>(input: T[], options?: IDictionary): IDictionary; | ||
/** | ||
* hashToArray | ||
* | ||
* Converts a hash data structure of {key: value, key2: value2} to an | ||
* array of [ {id, value}, {id2, value2} ]. This should happen regardless | ||
* to whether the values are themselves hashes (which they often are) or | ||
* scalar values. | ||
* | ||
* The one edge case is where all the hashes passed in have a value of "true" | ||
* which indicates that this really just a simple value based array encoded as | ||
* a hash (as is often the case in Firebase for FK relationships). | ||
* | ||
* @param hashObj an object of keys that point to some data payload | ||
* @param ___key__ the property name on the converted array-of-hashes which will contain the key value; by default this is "id" | ||
*/ | ||
export declare function hashToArray<T = any>(hashObj: IDictionary<T> | IDictionary<string> | IDictionary<number>, __key__?: keyof (T & { | ||
@@ -31,40 +16,7 @@ id: string; | ||
export declare type FunctionProperty<T> = (obj: T) => string; | ||
/** | ||
* arrayToHash | ||
* | ||
* Converts an array of things into a hash/dictionary where "things" is a consistent | ||
* type of data structure (can be either object or primitive) | ||
* | ||
* @param arr an array of a particular type | ||
* @param keyProperty the property that will be used as the dictionaries key; if false then will assign a firebase pushkey | ||
*/ | ||
export declare function arrayToHash<T = any>(arr: T[], keyProperty?: keyof T | FunctionProperty<T>): IDictionary<T>; | ||
/** | ||
* Snapshot to Array (unordered) | ||
* | ||
* converts snapshot directly to JS and then converts hash to an | ||
* array structure but any sorting that came from the server query | ||
* will be ignored. | ||
*/ | ||
export declare function snapshotToArray<T = IDictionary>(snap: ISnapShot, idProp?: string): T[]; | ||
/** | ||
* Converts a Firebase snapshot to JS object with both val() and key | ||
* represented in the JS object | ||
* | ||
* @param snap the Firebase Snapshot | ||
* @param idProp the property used to store the "id/key" of the record | ||
*/ | ||
export declare function snapshotToHash<T = IDictionary>(snap: ISnapShot, idProp?: string): T; | ||
/** | ||
* Snapshot to Array (ordered) | ||
* | ||
* uses Firebase forEach() iterator to gain the appropriate sorting from the query. | ||
*/ | ||
export declare function snapshotToOrderedArray<T = IDictionary>(snap: ISnapShot, idProp?: string): T[]; | ||
export declare function snapshotToOrderedHash<T = IDictionary>(snap: ISnapShot, idProp?: string): IDictionary<T>; | ||
/** | ||
* | ||
* @param dictionary A dictionary of a structured type | ||
* @param property Which property in each dictionary item are we getting | ||
*/ | ||
export declare function getPropertyAcrossDictionaryItems<T>(dictionary: IDictionary, property: string): T[]; |
@@ -1,212 +0,161 @@ | ||
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
exports.__esModule = true; | ||
var lodash_es_1 = require("lodash-es"); | ||
function removeIdPropertyFromHash(hash, idProp) { | ||
if (idProp === void 0) { idProp = "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]; | ||
} | ||
}); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var lodash = require('lodash'); | ||
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; | ||
} | ||
function keyValueDictionaryToArray(dict, options = {}) { | ||
const __key__ = options.key || "id"; | ||
const __value__ = options.value || "value"; | ||
return Object.keys(dict).reduce((result, key) => { | ||
return result.concat({ | ||
[__key__]: key, | ||
[__value__]: dict[key] | ||
}); | ||
}, []); | ||
} | ||
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.removeIdPropertyFromHash = removeIdPropertyFromHash; | ||
function keyValueDictionaryToArray(dict, options) { | ||
if (options === void 0) { options = {}; } | ||
var __key__ = options.key || "id"; | ||
var __value__ = options.value || "value"; | ||
return Object.keys(dict).reduce(function (result, key) { | ||
var _a; | ||
return result.concat((_a = {}, _a[__key__] = key, _a[__value__] = dict[key], _a)); | ||
}, []); | ||
function hashToArray(hashObj, __key__ = "id") { | ||
if (hashObj && typeof hashObj !== "object") { | ||
throw new Error("Cant convert hash-to-array because hash was not passed in: " + hashObj); | ||
} | ||
const hash = Object.assign({}, hashObj); | ||
const results = []; | ||
const isHashArray = Object.keys(hash).every(i => hash[i] === true); | ||
const isHashValue = Object.keys(hash).every(i => typeof hash[i] !== "object"); | ||
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); | ||
}); | ||
return results; | ||
} | ||
exports.keyValueDictionaryToArray = keyValueDictionaryToArray; | ||
function keyValueArrayToDictionary(input, options) { | ||
if (options === void 0) { options = {}; } | ||
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; | ||
}, {}); | ||
function flatten(list) { | ||
return list.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []); | ||
} | ||
exports.keyValueArrayToDictionary = keyValueArrayToDictionary; | ||
/** | ||
* hashToArray | ||
* | ||
* Converts a hash data structure of {key: value, key2: value2} to an | ||
* array of [ {id, value}, {id2, value2} ]. This should happen regardless | ||
* to whether the values are themselves hashes (which they often are) or | ||
* scalar values. | ||
* | ||
* The one edge case is where all the hashes passed in have a value of "true" | ||
* which indicates that this really just a simple value based array encoded as | ||
* a hash (as is often the case in Firebase for FK relationships). | ||
* | ||
* @param hashObj an object of keys that point to some data payload | ||
* @param ___key__ the property name on the converted array-of-hashes which will contain the key value; by default this is "id" | ||
*/ | ||
function hashToArray(hashObj, __key__) { | ||
if (__key__ === void 0) { __key__ = "id"; } | ||
if (hashObj && typeof hashObj !== "object") { | ||
throw new Error("Cant convert hash-to-array because hash was not passed in: " + hashObj); | ||
function arrayToHash(arr, keyProperty) { | ||
if (arr.length === 0) { | ||
return {}; | ||
} | ||
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; | ||
} | ||
var hash = __assign({}, hashObj); | ||
var results = []; | ||
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"; }); | ||
Object.keys(hash).map(function (id) { | ||
var _a, _b; | ||
var obj = typeof hash[id] === "object" | ||
? __assign({}, hash[id], (_a = {}, _a[__key__] = id, _a)) : isHashArray | ||
? id | ||
: (_b = {}, _b[__key__] = id, _b.value = hash[id], _b); | ||
results.push(obj); | ||
} | ||
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 : typeof keyProperty === "function" ? keyProperty(curr) : curr[keyProperty]; | ||
return isScalar ? Object.assign({}, prev, { | ||
[key]: true | ||
}) : Object.assign({}, prev, { | ||
[key]: curr | ||
}); | ||
return results; | ||
}, {}); | ||
return output; | ||
} | ||
exports.hashToArray = hashToArray; | ||
function flatten(list) { | ||
return list.reduce(function (a, b) { return a.concat(Array.isArray(b) ? flatten(b) : b); }, []); | ||
function snapshotToArray(snap, idProp = "id") { | ||
const hash = snap.val() || {}; | ||
return hashToArray(hash, idProp); | ||
} | ||
exports.flatten = flatten; | ||
/** | ||
* arrayToHash | ||
* | ||
* Converts an array of things into a hash/dictionary where "things" is a consistent | ||
* type of data structure (can be either object or primitive) | ||
* | ||
* @param arr an array of a particular type | ||
* @param keyProperty the property that will be used as the dictionaries key; if false then will assign a firebase pushkey | ||
*/ | ||
function arrayToHash(arr, keyProperty) { | ||
if (arr.length === 0) { | ||
return {}; | ||
function snapshotToHash(snap, idProp = "id") { | ||
const hash = snap.val() || {}; | ||
Object.keys(hash).forEach(key => typeof hash[key] === "object" ? hash[key][idProp] = key : hash[key] = { | ||
[idProp]: key, | ||
value: hash[key] | ||
}); | ||
return hash; | ||
} | ||
function snapshotToOrderedArray(snap, idProp = "id") { | ||
const output = []; | ||
snap.forEach(child => { | ||
const obj = child.val(); | ||
const key = child.key; | ||
if (typeof obj !== "object") { | ||
throw new Error(`Can't create a list from scalar values: "${obj}" | "${key}"`); | ||
} | ||
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; | ||
output.push(Object.assign({ | ||
[idProp]: key | ||
}, obj)); | ||
return true; | ||
}); | ||
return output; | ||
} | ||
function snapshotToOrderedHash(snap, idProp = "id") { | ||
const orderedArray = this.snapshotToOrderedArray(snap, idProp); | ||
return this.arrayToHash(orderedArray); | ||
} | ||
function getPropertyAcrossDictionaryItems(dictionary, property) { | ||
const output = []; | ||
Object.keys(dictionary).map(item => { | ||
const value = lodash.get(dictionary[item], property, undefined); | ||
if (value !== undefined) { | ||
output.push(value); | ||
} | ||
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 e = new Error("arrayToHash: input was not an array!"); | ||
e.name = "NotAllowed"; | ||
throw e; | ||
} | ||
var output = arr.reduce(function (prev, curr) { | ||
var _a, _b; | ||
var key = isScalar | ||
? curr | ||
: typeof keyProperty === "function" | ||
? keyProperty(curr) | ||
: curr[keyProperty]; | ||
return isScalar | ||
? __assign({}, prev, (_a = {}, _a[key] = true, _a)) : __assign({}, prev, (_b = {}, _b[key] = curr, _b)); | ||
}, {}); | ||
return output; | ||
}); | ||
return output; | ||
} | ||
exports.removeIdPropertyFromHash = removeIdPropertyFromHash; | ||
exports.keyValueDictionaryToArray = keyValueDictionaryToArray; | ||
exports.keyValueArrayToDictionary = keyValueArrayToDictionary; | ||
exports.hashToArray = hashToArray; | ||
exports.flatten = flatten; | ||
exports.arrayToHash = arrayToHash; | ||
/** | ||
* Snapshot to Array (unordered) | ||
* | ||
* converts snapshot directly to JS and then converts hash to an | ||
* array structure but any sorting that came from the server query | ||
* will be ignored. | ||
*/ | ||
function snapshotToArray(snap, idProp) { | ||
if (idProp === void 0) { idProp = "id"; } | ||
var hash = snap.val() || {}; | ||
return hashToArray(hash, idProp); | ||
} | ||
exports.snapshotToArray = snapshotToArray; | ||
/** | ||
* Converts a Firebase snapshot to JS object with both val() and key | ||
* represented in the JS object | ||
* | ||
* @param snap the Firebase Snapshot | ||
* @param idProp the property used to store the "id/key" of the record | ||
*/ | ||
function snapshotToHash(snap, idProp) { | ||
if (idProp === void 0) { idProp = "id"; } | ||
var hash = snap.val() || {}; | ||
Object.keys(hash).forEach(function (key) { | ||
var _a; | ||
return typeof hash[key] === "object" | ||
? (hash[key][idProp] = key) | ||
: (hash[key] = (_a = {}, _a[idProp] = key, _a.value = hash[key], _a)); | ||
}); | ||
return hash; | ||
} | ||
exports.snapshotToHash = snapshotToHash; | ||
/** | ||
* Snapshot to Array (ordered) | ||
* | ||
* uses Firebase forEach() iterator to gain the appropriate sorting from the query. | ||
*/ | ||
function snapshotToOrderedArray(snap, idProp) { | ||
if (idProp === void 0) { idProp = "id"; } | ||
var output = []; | ||
snap.forEach(function (child) { | ||
var _a; | ||
var obj = child.val(); | ||
var key = child.key; | ||
if (typeof obj !== "object") { | ||
throw new Error("Can't create a list from scalar values: \"" + obj + "\" | \"" + key + "\""); | ||
} | ||
output.push(__assign((_a = {}, _a[idProp] = key, _a), obj)); | ||
return true; | ||
}); | ||
return output; | ||
} | ||
exports.snapshotToOrderedArray = snapshotToOrderedArray; | ||
function snapshotToOrderedHash(snap, idProp) { | ||
if (idProp === void 0) { idProp = "id"; } | ||
var orderedArray = this.snapshotToOrderedArray(snap, idProp); | ||
return this.arrayToHash(orderedArray); | ||
} | ||
exports.snapshotToOrderedHash = snapshotToOrderedHash; | ||
/** | ||
* | ||
* @param dictionary A dictionary of a structured type | ||
* @param property Which property in each dictionary item are we getting | ||
*/ | ||
function getPropertyAcrossDictionaryItems(dictionary, property) { | ||
var output = []; | ||
Object.keys(dictionary).map(function (item) { | ||
var value = lodash_es_1.get(dictionary[item], property, undefined); | ||
if (value !== undefined) { | ||
output.push(value); | ||
} | ||
}); | ||
return output; | ||
} | ||
exports.getPropertyAcrossDictionaryItems = getPropertyAcrossDictionaryItems; |
{ | ||
"name": "typed-conversions", | ||
"version": "0.9.5", | ||
"version": "0.9.6", | ||
"description": "typed-conversions", | ||
@@ -26,3 +26,3 @@ "license": "MIT", | ||
"lint": "tslint --force --format verbose \"src/**/*.ts\"", | ||
"build": "npm run clean && bili src/index.ts --format es && tsc src/index.ts --declaration -outDir dist", | ||
"build": "npm run clean && bili src/index.ts --format es,cjs && tsc src/index.ts --declaration -outDir dist --noEmit", | ||
"test": "npm run build && mocha --compilers ts:ts-node/register --recursive 'test/**/*-spec.ts'", | ||
@@ -35,3 +35,3 @@ "coverage": "nyc --include='src/**/*.ts' --reporter=text --reporter=html --reporter=lcov mocha --compilers ts:ts-node/register --recursive 'test/**/*.test.ts'", | ||
"common-types": "^1.7.36", | ||
"lodash-es": "^4.17.11" | ||
"lodash": "^4.17.11" | ||
}, | ||
@@ -41,4 +41,4 @@ "devDependencies": { | ||
"@types/js-yaml": "^3.12.0", | ||
"@types/lodash-es": "^4.17.3", | ||
"@types/mocha": "^2.0.0", | ||
"@types/lodash": "^4.14.123", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^8.10.0", | ||
@@ -51,3 +51,2 @@ "@types/shelljs": "^0.8.3", | ||
"firebase-admin": "7.0.0", | ||
"lodash": "^4.17.11", | ||
"mocha": "^6.0.2", | ||
@@ -54,0 +53,0 @@ "nyc": "^13.3.0", |
Sorry, the diff of this file is not supported yet
19
14069
296
+ Addedlodash@^4.17.11
+ Addedlodash@4.17.21(transitive)
- Removedlodash-es@^4.17.11
- Removedlodash-es@4.17.21(transitive)