Skip to content

Commit

Permalink
[New Exercise] Ledger (#2398)
Browse files Browse the repository at this point in the history
* Adding autogenerated files

* Adding scaffolding, tests and solution

* Accidentally deleted a key in `config.json`

* Update proof.ci.js
  • Loading branch information
Cool-Katt authored Mar 23, 2024
1 parent 2447138 commit 0c91c84
Show file tree
Hide file tree
Showing 13 changed files with 569 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2598,6 +2598,16 @@
"strings"
],
"difficulty": 2
},
{
"slug": "ledger",
"name": "Ledger",
"uuid": "8716b347-e18f-48a6-b373-426cc4ca98cb",
"practices": [],
"prerequisites": [
"string-formatting"
],
"difficulty": 5
}
]
},
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/ledger/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Instructions

Refactor a ledger printer.

The ledger exercise is a refactoring exercise.
There is code that prints a nicely formatted ledger, given a locale (American or Dutch) and a currency (US dollar or euro).
The code however is rather badly written, though (somewhat surprisingly) it consistently passes the test suite.

Rewrite this code.
Remember that in refactoring the trick is to make small steps that keep the tests passing.
That way you can always quickly go back to a working version.
Version control tools like git can help here as well.

Please keep a log of what changes you've made and make a comment on the exercise containing that log, this will help reviewers.
14 changes: 14 additions & 0 deletions exercises/practice/ledger/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
"excludedFiles": ["custom.spec.js"],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
5 changes: 5 additions & 0 deletions exercises/practice/ledger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/node_modules
/bin/configlet
/bin/configlet.exe
/pnpm-lock.yaml
/yarn.lock
17 changes: 17 additions & 0 deletions exercises/practice/ledger/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"Cool-Katt"
],
"files": {
"solution": [
"ledger.js"
],
"test": [
"ledger.spec.js"
],
"example": [
".meta/proof.ci.js"
]
},
"blurb": "Refactor a ledger printer."
}
99 changes: 99 additions & 0 deletions exercises/practice/ledger/.meta/proof.ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
class LedgerEntry {
constructor(date, description, change) {
this.date = new Date(date);
this.description = description;
this.change = change;
}
}

class FormattedLedgerEntry {
constructor(entry, locale, dateFormat, currencyFormat) {
this.entry = entry;
this.locale = locale;
this.dateFormat = dateFormat;
this.currencyFormat = currencyFormat;
}

date() {
return this.entry.date.toLocaleDateString(this.locale, this.dateFormat);
}

description(length = 25) {
if (this.entry.description.length > length) {
return `${this.entry.description.substring(0, length - 3)}...`;
}

return this.entry.description.padEnd(length, ' ');
}

change(offset = 13) {
const formatted = (this.entry.change / 100).toLocaleString(
this.locale,
this.currencyFormat,
);

const trailingSpace = formatted.includes(')') ? '' : ' ';
return `${formatted}${trailingSpace}`.padStart(offset, ' ');
}

toTableRow() {
return [this.date(), this.description(), this.change()].join(' | ');
}
}

const OPTIONS = {
HEADERS: {
'en-US': ['Date', 'Description', 'Change'],
'nl-NL': ['Datum', 'Omschrijving', 'Verandering'],
},
headerRow: function (locale) {
const [date, description, change] = this.HEADERS[locale];
return [
date.padEnd(10, ' '),
description.padEnd(25, ' '),
change.padEnd(13, ' '),
].join(' | ');
},
dateFormatOptions: function () {
return {
day: '2-digit',
month: '2-digit',
year: 'numeric',
};
},
currencyFormatOptions: function (currency, locale) {
return {
style: 'currency',
currency: currency,
currencySign: locale === 'en-US' ? 'accounting' : 'standard',
currencyDisplay: locale === 'en-US' ? 'symbol' : 'narrowSymbol',
};
},
};

export const createEntry = (date, description, change) =>
new LedgerEntry(date, description, change);

export function formatEntries(currency, locale, entries) {
let dateFormat = OPTIONS.dateFormatOptions();
let currencyFormat = OPTIONS.currencyFormatOptions(currency, locale);

let rows = entries
.sort(
(a, b) =>
a.date - b.date ||
a.change - b.change ||
a.description.localeCompare(b.description),
)
.map((entry) => {
let formattedEntry = new FormattedLedgerEntry(
entry,
locale,
dateFormat,
currencyFormat,
);
return formattedEntry.toTableRow();
});

return [OPTIONS.headerRow(locale), ...rows].join('\n');
}
48 changes: 48 additions & 0 deletions exercises/practice/ledger/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[d131ecae-a30e-436c-b8f3-858039a27234]
description = "empty ledger"

[ce4618d2-9379-4eca-b207-9df1c4ec8aaa]
description = "one entry"

[8d02e9cb-e6ee-4b77-9ce4-e5aec8eb5ccb]
description = "credit and debit"

[502c4106-0371-4e7c-a7d8-9ce33f16ccb1]
description = "multiple entries on same date ordered by description"
include = false

[29dd3659-6c2d-4380-94a8-6d96086e28e1]
description = "final order tie breaker is change"

[9b9712a6-f779-4f5c-a759-af65615fcbb9]
description = "overlong description is truncated"

[67318aad-af53-4f3d-aa19-1293b4d4c924]
description = "euros"

[bdc499b6-51f5-4117-95f2-43cb6737208e]
description = "Dutch locale"

[86591cd4-1379-4208-ae54-0ee2652b4670]
description = "Dutch locale and euros"

[876bcec8-d7d7-4ba4-82bd-b836ac87c5d2]
description = "Dutch negative number with 3 digits before decimal point"

[29670d1c-56be-492a-9c5e-427e4b766309]
description = "American negative number with 3 digits before decimal point"

[9c70709f-cbbd-4b3b-b367-81d7c6101de4]
description = "multiple entries on same date ordered by description"
reimplements = "502c4106-0371-4e7c-a7d8-9ce33f16ccb1"
1 change: 1 addition & 0 deletions exercises/practice/ledger/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/ledger/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions exercises/practice/ledger/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
presets: ['@exercism/babel-preset-javascript'],
plugins: [],
};
Loading

0 comments on commit 0c91c84

Please sign in to comment.