This repository has been archived by the owner on Mar 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
720 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
var isObject = require('lodash-node/modern/objects/isObject'); | ||
|
||
function ArraySchema(itemSchema) { | ||
if (!isObject(itemSchema)) { | ||
throw new Error('ArraySchema requires item schema to be an object.'); | ||
} | ||
|
||
this._itemSchema = itemSchema; | ||
} | ||
|
||
ArraySchema.prototype.getItemSchema = function () { | ||
return this._itemSchema; | ||
}; | ||
|
||
module.exports = ArraySchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
function EntitySchema(key) { | ||
if (!key || typeof key !== 'string') { | ||
throw new Error('A string non-empty key is required'); | ||
} | ||
|
||
this._key = key; | ||
} | ||
|
||
EntitySchema.prototype.getKey = function () { | ||
return this._key; | ||
}; | ||
|
||
EntitySchema.prototype.define = function (nestedSchema) { | ||
for (var prop in nestedSchema) { | ||
if (nestedSchema.hasOwnProperty(prop)) { | ||
this[prop] = nestedSchema[prop]; | ||
} | ||
} | ||
}; | ||
|
||
module.exports = EntitySchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,112 @@ | ||
'use strict'; | ||
|
||
module.exports = 'TODO'; | ||
var EntitySchema = require('./EntitySchema'), | ||
ArraySchema = require('./ArraySchema'), | ||
isObject = require('lodash-node/modern/objects/isObject'), | ||
isEqual = require('lodash-node/modern/objects/isEqual'); | ||
|
||
function visitObject(obj, schema, bag) { | ||
var normalized = {}; | ||
|
||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop)) { | ||
normalized[prop] = visit(obj[prop], schema[prop], bag); | ||
} | ||
} | ||
|
||
return normalized; | ||
} | ||
|
||
function visitArray(obj, arraySchema, bag) { | ||
var itemSchema = arraySchema.getItemSchema(), | ||
normalized; | ||
|
||
normalized = obj.map(function (childObj) { | ||
return visit(childObj, itemSchema, bag); | ||
}); | ||
|
||
return normalized; | ||
} | ||
|
||
|
||
function mergeIntoEntity(entityA, entityB, entityKey) { | ||
for (var prop in entityB) { | ||
if (!entityB.hasOwnProperty(prop)) { | ||
continue; | ||
} | ||
|
||
if (!entityA.hasOwnProperty(prop) || isEqual(entityA[prop], entityB[prop])) { | ||
entityA[prop] = entityB[prop]; | ||
continue; | ||
} | ||
|
||
console.warn( | ||
'When merging two ' + entityKey + ', found shallowly unequal data in their "' + prop + '" values. Using the earlier value.', | ||
entityA[prop], entityB[prop] | ||
); | ||
} | ||
} | ||
|
||
function visitEntity(entity, entitySchema, bag) { | ||
var entityKey = entitySchema.getKey(), | ||
id = entity.id, | ||
stored, | ||
normalized; | ||
|
||
if (!bag[entityKey]) { | ||
bag[entityKey] = {}; | ||
} | ||
|
||
if (!bag[entityKey][id]) { | ||
bag[entityKey][id] = {}; | ||
} | ||
|
||
stored = bag[entityKey][id]; | ||
normalized = visitObject(entity, entitySchema, bag); | ||
|
||
mergeIntoEntity(stored, normalized, entityKey); | ||
|
||
return id; | ||
} | ||
|
||
function visit(obj, schema, bag) { | ||
if (!isObject(obj) || !isObject(schema)) { | ||
return obj; | ||
} | ||
|
||
if (schema instanceof EntitySchema) { | ||
return visitEntity(obj, schema, bag); | ||
} else if (schema instanceof ArraySchema) { | ||
return visitArray(obj, schema, bag); | ||
} else { | ||
return visitObject(obj, schema, bag); | ||
} | ||
} | ||
|
||
function normalize(obj, schema) { | ||
if (!isObject(obj) && !Array.isArray(obj)) { | ||
throw new Error('Normalize accepts an object or an array as its input.'); | ||
} | ||
|
||
if (!isObject(schema) || Array.isArray(schema)) { | ||
throw new Error('Normalize accepts an object for schema.'); | ||
} | ||
|
||
var bag = {}, | ||
result = visit(obj, schema, bag); | ||
|
||
return { | ||
entities: bag, | ||
result: result | ||
}; | ||
} | ||
|
||
function arrayOf(schema) { | ||
return new ArraySchema(schema); | ||
} | ||
|
||
module.exports = { | ||
Schema: EntitySchema, | ||
arrayOf: arrayOf, | ||
normalize: normalize | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,8 @@ | |
"devDependencies": { | ||
"chai": "^1.9.1", | ||
"mocha": "^1.21.4" | ||
}, | ||
"dependencies": { | ||
"lodash-node": "^2.4.1" | ||
} | ||
} |
Oops, something went wrong.