json-api-denormalizr
Advanced tools
Comparing version 0.1.1 to 0.1.3
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.1.3"></a> | ||
## [0.1.3](https://github.com/willsoto/json-api-denormalizr/compare/v0.1.2...v0.1.3) (2017-07-03) | ||
<a name="0.1.2"></a> | ||
## [0.1.2](https://github.com/willsoto/json-api-denormalizr/compare/v0.1.1...v0.1.2) (2017-07-03) | ||
<a name="0.1.1"></a> | ||
@@ -7,0 +17,0 @@ ## [0.1.1](https://github.com/willsoto/json-api-denormalizr/compare/v0.1.0...v0.1.1) (2017-07-03) |
@@ -1,43 +0,2 @@ | ||
!(function(e, t) { | ||
'object' == typeof exports && 'undefined' != typeof module | ||
? t(exports, require('lodash')) | ||
: 'function' == typeof define && define.amd | ||
? define(['exports', 'lodash'], t) | ||
: t((e['json-api-denormalizr'] = e['json-api-denormalizr'] || {}), e._); | ||
})(this, function(e, t) { | ||
'use strict'; | ||
t = t && 'default' in t ? t.default : t; | ||
var i = | ||
Object.assign || | ||
function(e) { | ||
for (var t = 1; t < arguments.length; t++) { | ||
var i = arguments[t]; | ||
for (var n in i) Object.prototype.hasOwnProperty.call(i, n) && (e[n] = i[n]); | ||
} | ||
return e; | ||
}, | ||
n = function(e, i, n) { | ||
for (var o in i) { | ||
var a = i[o].data; | ||
a && | ||
(t.isArray(a) | ||
? (e[o] = t.map(a, function(e) { | ||
return r(e, n); | ||
})) | ||
: (e[o] = r(a, n))); | ||
} | ||
return e; | ||
}, | ||
r = function(e, t) { | ||
var r = t[e.type][e.id], | ||
o = i({ id: r.id }, r.attributes); | ||
return r.relationships && (o = n(o, r.relationships, t)), o; | ||
}; | ||
(e.denormalize = function(e, r) { | ||
var o = (arguments.length > 2 && void 0 !== arguments[2] ? arguments[2] : {}).entities || 'entities', | ||
a = t.get(e, o), | ||
s = i({ id: r.id }, r.attributes); | ||
return r.relationships && n(s, r.relationships, a), s; | ||
}), Object.defineProperty(e, '__esModule', { value: !0 }); | ||
}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("lodash")):"function"==typeof define&&define.amd?define(["exports","lodash"],t):t(e["json-api-denormalizr"]=e["json-api-denormalizr"]||{},e._)}(this,function(e,t){"use strict";t=t&&"default"in t?t.default:t;var i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var i=arguments[t];for(var r in i)Object.prototype.hasOwnProperty.call(i,r)&&(e[r]=i[r])}return e},r=function(e,i,r){for(var o in i){var a=i[o].data;a&&(t.isArray(a)?e[o]=t.map(a,function(e){return n(e,r)}):e[o]=n(a,r))}return e},n=function(e,t){var n=t[e.type][e.id],o=i({id:n.id},n.attributes);return n.relationships&&(o=r(o,n.relationships,t)),o};e.denormalize=function(e,n){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};o.pathToEntities&&(e=t.get(e,o.pathToEntities));var a=i({id:n.id},n.attributes);return n.relationships&&(a=r(a,n.relationships,e)),a},Object.defineProperty(e,"__esModule",{value:!0})}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "json-api-denormalizr", | ||
"version": "0.1.1", | ||
"version": "0.1.3", | ||
"description": "", | ||
@@ -14,3 +14,3 @@ "main": "./dist/index.js", | ||
"lint": "eslint src spec --fix", | ||
"prerelease": "cross-env NODE_ENV=test nyc npm test", | ||
"prerelease": "cross-env NODE_ENV=test nyc npm test && npm run build", | ||
"release": "standard-version --sign && git push --follow-tags origin master", | ||
@@ -17,0 +17,0 @@ "precommit": "lint-staged && npm test", |
# json-api-denormalizr | ||
[![CircleCI](https://circleci.com/gh/willsoto/json-api-denormalizr.svg?style=svg&circle-token=0b410de6b9ef748768a5d2cd9fc7f3eec658a27c)](https://circleci.com/gh/willsoto/json-api-denormalizr) | ||
## Getting Started | ||
@@ -4,0 +6,0 @@ |
import _ from 'lodash'; | ||
export const denormalize = (state, entity, options = {}) => { | ||
let allEntities = state; | ||
if (options.pathToEntities) { | ||
allEntities = _.get(state, options.pathToEntities); | ||
state = _.get(state, options.pathToEntities); | ||
} | ||
@@ -16,3 +14,3 @@ | ||
if (entity.relationships) { | ||
processRelationships(result, entity.relationships, allEntities); | ||
result = processRelationships(result, entity.relationships, state); | ||
} | ||
@@ -23,3 +21,3 @@ | ||
const processRelationships = (entity, relationships, allEntities) => { | ||
const processRelationships = (entity, relationships, state) => { | ||
for (let type in relationships) { | ||
@@ -34,5 +32,5 @@ const relationship = relationships[type]; | ||
if (_.isArray(data)) { | ||
entity[type] = _.map(data, item => createRelationship(item, allEntities)); | ||
entity[type] = _.map(data, item => createRelationship(item, state)); | ||
} else { | ||
entity[type] = createRelationship(data, allEntities); | ||
entity[type] = createRelationship(data, state); | ||
} | ||
@@ -44,4 +42,4 @@ } | ||
const createRelationship = (data, allEntities) => { | ||
let entity = allEntities[data.type][data.id]; | ||
const createRelationship = (data, state) => { | ||
let entity = state[data.type][data.id]; | ||
let result = { | ||
@@ -53,3 +51,3 @@ id: entity.id, | ||
if (entity.relationships) { | ||
result = processRelationships(result, entity.relationships, allEntities); | ||
result = processRelationships(result, entity.relationships, state); | ||
} | ||
@@ -56,0 +54,0 @@ |
Sorry, the diff of this file is not supported yet
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
128
20824
45
3