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

Commit

Permalink
ES6ify
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Aug 24, 2015
1 parent 9d064e7 commit 4943572
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 75 deletions.
22 changes: 10 additions & 12 deletions src/ArraySchema.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
'use strict';
import isObject from 'lodash/lang/isObject';

var isObject = require('lodash/lang/isObject');
export default class ArraySchema {
constructor(itemSchema) {
if (!isObject(itemSchema)) {
throw new Error('ArraySchema requires item schema to be an object.');
}

function ArraySchema(itemSchema) {
if (!isObject(itemSchema)) {
throw new Error('ArraySchema requires item schema to be an object.');
this._itemSchema = itemSchema;
}

this._itemSchema = itemSchema;
getItemSchema() {
return this._itemSchema;
}
}

ArraySchema.prototype.getItemSchema = function () {
return this._itemSchema;
};

module.exports = ArraySchema;
42 changes: 19 additions & 23 deletions src/EntitySchema.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
'use strict';
export default class EntitySchema {
constructor(key, options = {}) {
if (!key || typeof key !== 'string') {
throw new Error('A string non-empty key is required');
}

function EntitySchema(key, options) {
if (!key || typeof key !== 'string') {
throw new Error('A string non-empty key is required');
this._idAttribute = options.idAttribute || 'id';
this._key = key;
}

options = options || {};

this._idAttribute = options.idAttribute || 'id';
this._key = key;
}

EntitySchema.prototype.getKey = function () {
return this._key;
};
getKey() {
return this._key;
}

EntitySchema.prototype.getIdAttribute = function () {
return this._idAttribute;
};
getIdAttribute() {
return this._idAttribute;
}

EntitySchema.prototype.define = function (nestedSchema) {
for (var key in nestedSchema) {
if (nestedSchema.hasOwnProperty(key)) {
this[key] = nestedSchema[key];
define(nestedSchema) {
for (let key in nestedSchema) {
if (nestedSchema.hasOwnProperty(key)) {
this[key] = nestedSchema[key];
}
}
}
};

module.exports = EntitySchema;
}
68 changes: 28 additions & 40 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
'use strict';

var EntitySchema = require('./EntitySchema'),
ArraySchema = require('./ArraySchema'),
isObject = require('lodash/lang/isObject'),
isEqual = require('lodash/lang/isEqual');
import EntitySchema from './EntitySchema';
import ArraySchema from './ArraySchema';
import isObject from 'lodash/lang/isObject';
import isEqual from 'lodash/lang/isEqual';

function defaultAssignEntity(normalized, key, entity) {
normalized[key] = entity;
}

function visitObject(obj, schema, bag, options) {
var { assignEntity = defaultAssignEntity } = options;
var normalized = {};
const { assignEntity = defaultAssignEntity } = options;

for (var key in obj) {
let normalized = {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
var entity = visit(obj[key], schema[key], bag, options);
const entity = visit(obj[key], schema[key], bag, options);
assignEntity.call(null, normalized, key, entity);
}
}

return normalized;
}

function visitArray(obj, arraySchema, bag, options) {
var itemSchema = arraySchema.getItemSchema(),
normalized;

normalized = obj.map(function (childObj) {
return visit(childObj, itemSchema, bag, options);
});
const itemSchema = arraySchema.getItemSchema();

const normalized = obj.map(childObj =>
visit(childObj, itemSchema, bag, options)
);
return normalized;
}


function mergeIntoEntity(entityA, entityB, entityKey) {
for (var key in entityB) {
for (let key in entityB) {
if (!entityB.hasOwnProperty(key)) {
continue;
}
Expand All @@ -54,11 +49,9 @@ function mergeIntoEntity(entityA, entityB, entityKey) {
}

function visitEntity(entity, entitySchema, bag, options) {
var entityKey = entitySchema.getKey(),
idAttribute = entitySchema.getIdAttribute(),
id = entity[idAttribute],
stored,
normalized;
const entityKey = entitySchema.getKey();
const idAttribute = entitySchema.getIdAttribute();
const id = entity[idAttribute];

if (!bag[entityKey]) {
bag[entityKey] = {};
Expand All @@ -68,9 +61,8 @@ function visitEntity(entity, entitySchema, bag, options) {
bag[entityKey][id] = {};
}

stored = bag[entityKey][id];
normalized = visitObject(entity, entitySchema, bag, options);

let stored = bag[entityKey][id];
let normalized = visitObject(entity, entitySchema, bag, options);
mergeIntoEntity(stored, normalized, entityKey);

return id;
Expand All @@ -90,7 +82,13 @@ function visit(obj, schema, bag, options) {
}
}

function normalize(obj, schema, options = {}) {
export function arrayOf(schema) {
return new ArraySchema(schema);
}

export { EntitySchema as Schema };

export function normalize(obj, schema, options = {}) {
if (!isObject(obj) && !Array.isArray(obj)) {
throw new Error('Normalize accepts an object or an array as its input.');
}
Expand All @@ -99,21 +97,11 @@ function normalize(obj, schema, options = {}) {
throw new Error('Normalize accepts an object for schema.');
}

var bag = {},
result = visit(obj, schema, bag, options);
let bag = {};
let result = visit(obj, schema, bag, options);

return {
entities: bag,
result: result
result
};
}

function arrayOf(schema) {
return new ArraySchema(schema);
}

module.exports = {
Schema: EntitySchema,
arrayOf: arrayOf,
normalize: normalize
};

0 comments on commit 4943572

Please sign in to comment.