Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
37 changes: 37 additions & 0 deletions src/formatDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,43 @@
*/
function formatDate(date, fromFormat, toFormat) {
// write code here
const arrDate = date.split(fromFormat.at(-1));
const parts = {};
const resArr = [];

for (let i = 0; i < arrDate.length; i++) {
parts[fromFormat[i]] = arrDate[i];
}

for (let j = 0; j < toFormat.length - 1; j++) {
if (
(fromFormat.includes('YYYY') && toFormat[j] === 'YY') ||
(fromFormat.includes('YY') && toFormat[j] === 'YYYY')
) {
const changedYear = normalizeYear(parts, toFormat[j]);

resArr.push(changedYear);
continue;
}

resArr.push(parts[toFormat[j]]);
}

return resArr.join(toFormat.at(-1));
}

function normalizeYear(obj, targetYearFormat) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normalizeYear has no default return value if targetYearFormat is neither 'YYYY' nor 'YY'. While current calls only use these two, it’s better to ensure this function always returns a string (or explicitly handle unexpected formats) to avoid accidental undefined results later.

if (targetYearFormat !== 'YY' && targetYearFormat !== 'YYYY') {
throw new Error('Unsupported year format');
}

if (targetYearFormat === 'YYYY') {
return obj.YY >= 30 ? '19' + obj.YY : '20' + obj.YY;
}

if (targetYearFormat === 'YY') {
return obj.YYYY.slice(-2);
}
}

module.exports = formatDate;
Loading