Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
added debug output and nicer error
Browse files Browse the repository at this point in the history
  • Loading branch information
mschipperheyn committed Jun 14, 2016
1 parent e4fc3ee commit 07281aa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "normalizr-immutable",
"version": "0.0.4-beta",
"version": "0.0.4-beta1",
"description": "Normalizes JSON to immutable Records according to schema for Redux applications and provide proxied access to properties",
"main": "lib/index.js",
"private": false,
Expand Down
58 changes: 38 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,46 @@ function proxy(id, schema, bag, options){
if(name === 'id')
return target.id;

const state = options.getState();

if(state[schema.getReducerKey()].entities){
try{
const state = options.getState();

if(typeof state[schema.getReducerKey()] === 'undefined')
throw new Error(`No such reducer: ${schema.getReducerKey()}`);

if(state[schema.getReducerKey()].entities){

if(options.debug){
if(typeof state[schema.getReducerKey()].entities[schema.getKey()] === 'undefined'){
console.info(`Normalizr: ${schema.getKey()} not found on reducer ${schema.getReducerKey()}`);
}else if(options.useMapsForEntityObjects){
if(state[schema.getReducerKey()].entities[schema.getKey()].findKey(ky => ky === target.id + '') === null)
console.info(`Normalizr: ${schema.getKey()}-${target.id} not found on reducer ${schema.getReducerKey()}`);
}else{
if(Object.keys(state[schema.getReducerKey()].entities[schema.getKey()]).indexOf(target.id) === -1)
console.info(`Normalizr: ${schema.getKey()}-${target.id} not found on reducer ${schema.getReducerKey()}`);
}
}

if(options.debug){
if(typeof state[schema.getReducerKey()].entities[schema.getKey()] === 'undefined'){
console.debug(`Normalizr: ${schema.getKey()} not found on reducer ${schema.getReducerKey()}`);
}else if(options.useMapsForEntityObjects){
if(state[schema.getReducerKey()].entities[schema.getKey()].findKey(target.id + '') === null)
console.debug(`Normalizr: ${schema.getKey()}-${target.id} not found on reducer ${schema.getReducerKey()}`);
if(options.useMapsForEntityObjects){
return state[schema.getReducerKey()].entities[schema.getKey()].get(target.id + '')[name];
}else{
if(Object.keys(state[schema.getReducerKey()].entities[schema.getKey()]).indexOf(target.id) === -1)
console.debug(`Normalizr: ${schema.getKey()}-${target.id} not found on reducer ${schema.getReducerKey()}`);
return state[schema.getReducerKey()].entities[schema.getKey()][target.id][name];
}
}else if(options.debug){
console.info(`Normalizr: reducer ${schema.getReducerKey()} doesn't have entities key. Are you sure you configured the correct reducer?`);
}

if(options.useMapsForEntityObjects){
return state[schema.getReducerKey()].entities[schema.getKey()].get(target.id + '')[name];
}else{
return state[schema.getReducerKey()].entities[schema.getKey()][target.id][name];
}
}else if(options.debug){
console.debug(`Normalizr: reducer ${schema.getReducerKey()} doesn't have entities key. Are you sure you configured the correct reducer?`);
}catch(err){

console.error({
message:'Error processing Proxy',
id:target.id,
entity:target.key,
key:name,
reducer:schema.getReducerKey()
});

throw err;
}
return undefined;
},
Expand Down Expand Up @@ -264,7 +281,7 @@ function normalize(obj, schema, options = {
}) {

if(options.debug)
console.debug(`Normalizr: getState ${options.getState}, useMapsForEntityObjects:${options.useMapsForEntityObjects}, useProxyForResults:${options.useProxyForResults}, debug:${options.debug}`);
console.info(`Normalizr: getState ${options.getState}, useMapsForEntityObjects:${options.useMapsForEntityObjects}, useProxyForResults:${options.useProxyForResults}, debug:${options.debug}`);

if (!lodashIsObject(obj) && !Array.isArray(obj)) {
throw new Error('Normalize accepts an object or an array as its input.');
Expand All @@ -275,7 +292,7 @@ function normalize(obj, schema, options = {
}

if(options.getState && typeof Proxy === 'undefined'){
console.warn('Proxies not supported in this environment');
console.info('Proxies not supported in this environment');
}

let bag = {};
Expand Down Expand Up @@ -312,6 +329,7 @@ function normalize(obj, schema, options = {
}

const EntityStructure = new Record(keyStructure);

entities = new EntityStructure(entityStructure);

return new NormalizedRecord({
Expand Down
41 changes: 23 additions & 18 deletions test/normalizerTest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ function myReducer(state = initialState, action) {
return state;
};

function inboxReducer(state = initialState, action) {
return state;
};

const store = createStore(combineReducers({
myReducer
myReducer,
inboxReducer
}),{},applyMiddleware(
// loggerMiddleware()
));
Expand Down Expand Up @@ -301,31 +306,31 @@ describe("test normalizr", () => {
});

it("should produce test output", () => {
let normalized = normalize(json.articles.items, arrayOf(schemas.article),{
getState:store.getState,
useMapsForEntityObjects:true,
debug:true
});
const altSchemas = {
article : new Schema('articles', Article, { idAttribute: 'id', reducerKey: reducerKey }),
user : new Schema('users', User, { idAttribute: 'id', reducerKey: 'inboxReducer' }),
tag : new Schema('tags', Tag, { idAttribute: 'id', reducerKey: reducerKey })
};

normalized = normalize(json.articles.items, arrayOf(schemas.article),{
getState:store.getState,
useMapsForEntityObjects:false,
debug:true
altSchemas.article.define({
user: altSchemas.user,
tags: arrayOf(altSchemas.tag),
comments:arrayOf(altSchemas.article)
});

normalized = normalize(json.articles.items, arrayOf(schemas.article),{
let normalized = normalize(json.articles.items, arrayOf(altSchemas.article),{
getState:store.getState,
useMapsForEntityObjects:true,
useProxyForResults:false
useProxyForResults:true,
debug:true
});

normalized = normalize(json.articles.items, arrayOf(schemas.article),{
getState:store.getState,
useMapsForEntityObjects:false,
useProxyForResults:true
debug:true
});
store.dispatch({
type:'articles',
payload:normalized
})

console.log(normalized.result.get(0).user.nickName);

});

Expand Down

0 comments on commit 07281aa

Please sign in to comment.