Skip to content

Commit

Permalink
Merge pull request #20 from leeoniya/drop-lodash
Browse files Browse the repository at this point in the history
refactor: drop lodash dependency
  • Loading branch information
nareshbhatia authored Feb 15, 2019
2 parents a255d5e + 9a0c352 commit 3f1088c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
"semantic-release": "semantic-release",
"docs": "jsdoc src -d apidocs"
},
"dependencies": {
"lodash": "^4.17.11"
},
"dependencies": {},
"devDependencies": {
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
Expand Down
45 changes: 26 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from 'lodash';

/** Thrown when mapOne does not find an object in the resultSet and "isRequired" is passed in as true */
function NotFoundError(message = 'Not Found') {
this.name = 'NotFoundError';
Expand All @@ -23,7 +21,7 @@ function map(resultSet, maps, mapId, columnPrefix) {

let mappedCollection = [];

_.each(resultSet, (result) => {
resultSet.forEach(result => {
injectResultInCollection(result, mappedCollection, maps, mapId, columnPrefix);
});

Expand Down Expand Up @@ -72,15 +70,24 @@ function mapOne(resultSet, maps, mapId, columnPrefix, isRequired = true) {
function injectResultInCollection(result, mappedCollection, maps, mapId, columnPrefix = '') {

// Check if the object is already in mappedCollection
let resultMap = _.find(maps, ['mapId', mapId]);
let resultMap = maps.find(map => map.mapId === mapId);
let idProperty = getIdProperty(resultMap);
let predicate = _.transform(idProperty, (accumulator, field) => {
let predicate = idProperty.reduce((accumulator, field) => {
accumulator[field.name] = result[columnPrefix + field.column];
return accumulator;
}, {});
let mappedObject = _.find(mappedCollection, predicate);

let mappedObject = mappedCollection.find(item => {
for (let k in predicate) {
if (item[k] !== predicate[k]) {
return false;
}
}
return true;
});

// Inject only if the value of idProperty is not null (ignore joins to null records)
let isIdPropertyNotNull = _.every(idProperty, field => !_.isNull(result[columnPrefix + field.column]));
let isIdPropertyNotNull = idProperty.every(field => result[columnPrefix + field.column] !== null);

if (isIdPropertyNotNull) {
// Create mappedObject if it does not exist in mappedCollection
Expand All @@ -106,20 +113,21 @@ function injectResultInCollection(result, mappedCollection, maps, mapId, columnP
function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix = '') {

// Get the resultMap for this object
let resultMap = _.find(maps, ['mapId', mapId]);
let resultMap = maps.find(map => map.mapId === mapId);

// Copy id property
let idProperty = getIdProperty(resultMap);

_.each(idProperty, field => {
idProperty.forEach(field => {
if (!mappedObject[field.name]) {
mappedObject[field.name] = result[columnPrefix + field.column];
}
});

const {properties, associations, collections} = resultMap;

// Copy other properties
_.each(resultMap.properties, (property) => {
properties && properties.forEach(property => {
// If property is a string, convert it to an object
if (typeof property === 'string') {
// eslint-disable-next-line
Expand All @@ -137,20 +145,19 @@ function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix =
});

// Copy associations
_.each(resultMap.associations, (association) => {
associations && associations.forEach(association => {

let associatedObject = mappedObject[association.name];

if (!associatedObject) {
let associatedResultMap = _.find(maps, ['mapId', association.mapId]);
let associatedResultMap = maps.find(map => map.mapId === association.mapId);
let associatedObjectIdProperty = getIdProperty(associatedResultMap);

mappedObject[association.name] = null;

// Don't create associated object if it's key value is null
let isAssociatedObjectIdPropertyNotNull = _.every(
associatedObjectIdProperty,
field => !_.isNull(result[association.columnPrefix + field.column])
let isAssociatedObjectIdPropertyNotNull = associatedObjectIdProperty.every(field =>
result[association.columnPrefix + field.column] !== null
);

if (isAssociatedObjectIdPropertyNotNull) {
Expand All @@ -165,7 +172,7 @@ function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix =
});

// Copy collections
_.each(resultMap.collections, (collection) => {
collections && collections.forEach(collection => {

let mappedCollection = mappedObject[collection.name];

Expand All @@ -190,14 +197,14 @@ function getIdProperty(resultMap) {

let idProperties = resultMap.idProperty;

if (!_.isArray(idProperties)) {
if (!Array.isArray(idProperties)) {
idProperties = [idProperties];
}

return _.map(idProperties, idProperty => {
return idProperties.map(idProperty => {

// If property is a string, convert it to an object
if (_.isString(idProperty)) {
if (typeof idProperty === 'string') {
return {name: idProperty, column: idProperty};
}

Expand Down

0 comments on commit 3f1088c

Please sign in to comment.