Skip to content

Commit

Permalink
chapter 02: worsen the genericEncoder adding the method prepareData t…
Browse files Browse the repository at this point in the history
…hat also violates the open/closed principle
  • Loading branch information
devcorpio committed Mar 30, 2019
1 parent 0ac5c23 commit c4ee242
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions chapter-02-the-open-closed-principle/genericEncoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ function genericEncoder() {
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

0 comments on commit c4ee242

Please sign in to comment.