Skip to content

Commit

Permalink
chapter 02: using prepareData of the specific encoder, however we int…
Browse files Browse the repository at this point in the history
…roducing temporal coupling...
  • Loading branch information
devcorpio committed Mar 30, 2019
1 parent bdb6adf commit dc74dde
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
20 changes: 1 addition & 19 deletions chapter-02-the-open-closed-principle/refactor/genericEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@ function genericEncoder(encoderFactory) {
function encodeToFormat(data, format) {
const encoder = encoderFactory.createForFormat(format);

data = prepareData(data, format);
data = encoderFactory.prepareData(data);

return encoder.encode(data);
}

function prepareData(data, format) {
switch (format) {
case 'json': {
data = this.forceArray(data);
data = this.fixKeys(data);
}
case 'xml': {
data = this.fixAttributes(data);
break;
}
default: {
throw new Error('Format not supported');
}
}

return data;
}

return {
encodeToFormat,
};
Expand Down
9 changes: 7 additions & 2 deletions chapter-02-the-open-closed-principle/refactor/jsonEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ function jsonEncoder() {
// something here...
}

function prepareData() {
// something here
function prepareData(data) {
// something here, for example:

data = forceArray(data);
data = fixKeys(data);

return data;
}

return {
Expand Down
8 changes: 6 additions & 2 deletions chapter-02-the-open-closed-principle/refactor/xmlEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ function xmlEncoder() {
// something here...
}

function prepareData() {
// something here
function prepareData(data) {
// something here, for example:

data = fixAttributes(data);

return data;
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function yamlEncoder() {
// something here...
}

function prepareData() {
function prepareData(data) {
// something here
}

Expand Down

0 comments on commit dc74dde

Please sign in to comment.