diff --git a/CHANGELOG.md b/CHANGELOG.md index 84b243ce..299d62c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,10 +53,27 @@ fixed some issues and configured files for publishing diff-ymd-package v1.0.4 on fixed some issues and configured files for publishing diff-ymd-package v1.0.5 on npm registry +### [v1.1.0] + +#### Added + +- Improved handling of customized formatting. +- Includes method for creating customized differrence formats. +- Included Unit test using Jest for customizing method. +- Included through comments in tests script + +#### Changed + +- Included through comments in tests script + +#### Fixed + +fixed some issues and configured files for publishing diff-ymd-package v1.1.0 on npm registry + ### [Next Release] - Planning release time ### Important changes links: -- [Unreleased](https://github.com/farhan7reza7/diff-ymd-package/compare/v1.0.5...HEAD) -- [v1.0.5](https://github.com/farhan7reza7/diff-ymd-package/releases/tag/v1.0.5) +- [Unreleased](https://github.com/farhan7reza7/diff-ymd-package/compare/v1.1.0...HEAD) +- [v1.1.0](https://github.com/farhan7reza7/diff-ymd-package/releases/tag/v1.1.0) - [Next Release](https://github.com/farhan7reza7/diff-ymd-package/milestone/2) diff --git a/README.md b/README.md index 158b051b..58c8aaec 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,10 @@ const customFormat = Y + 'years ' + M + 'months ' + D + 'days'; console.log(customFormat); // output: 1years 11months 30days */ +// you can use this method for creating format of your choice +const customizedFormat = Formatter.customizeFormat('Ys', 'Ms', 'Ds', '-'); + +console.log(customizedFormat); // Output: "1Ys-2Ms-2Ds" ``` ## API Documentation @@ -67,7 +71,6 @@ Represents a utility class for calculating the formatted and customized differen ```javascript const Formatter = new DatesYMD(firstDate, secondDate); - ``` - **`firstDate`**: The first date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd' or 'yyyy.mm.dd'. @@ -80,7 +83,6 @@ Calculates the difference between two dates and returns an array containing Y(ye ```javascript const result = Formatter.diffArray(); - ``` - **`Returns:`** @@ -91,11 +93,24 @@ Returns the formatted difference between two dates in aY bM cD(aYears bMonths cD ```javascript const result = Formatter.formattedYMD(); - ``` - **`Returns:`** A string in the format 'aY bM cD'. +**`customizeFormat(yearUnit, monthUnit, dayUnit, partSeparator)`** +Customizes the difference using specified units and separators + +```javascript +const result = Formatter.customizeFormat( + yearUnit, + monthUnit, + dayUnit, + partSeparator, +); +``` + +- **`Returns:`** A customized formatted difference string of form (a + yearUnit + partSeparator + b + monthUnit + partSeparator + c + dayUnit), eg. aYs-bMs-cDs etc. + For more informations, [See `diff-ymd-package documentation`](https://farhan7reza7.github.io/diff-ymd-package/DatesYMD.html) ## Contributing diff --git a/docs/DatesYMD.html b/docs/DatesYMD.html index 9af1754b..46104dfa 100644 --- a/docs/DatesYMD.html +++ b/docs/DatesYMD.html @@ -1,470 +1,299 @@ - + - - - JSDoc: Class: DatesYMD + + + diff-ymd-package Documentation + - - + + - - - - - - -
- -

Class: DatesYMD

- - - - - - -
- -
- -

DatesYMD(firstDate, secondDate)

- -
Represents a utility class for calculating the difference between two dates in (aYears bMonths cDays)(aY bM cD) format.
- - -
- -
-
- - - - -

Constructor

- - - -

new DatesYMD(firstDate, secondDate)

- - - - - - -
- Creates an instance of DatesYMD. -
- - - - - - - - - -
Parameters:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameTypeDescription
firstDate - - -string - - - - The first date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd' or yyyy.mm.dd .
secondDate - - -string - - - - The second date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd'or yyyy.mm.dd .
- - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - - - - - - - - - + + + + + +
+

Class: DatesYMD

+ +
+
+

+ DatesYMD(firstDate, secondDate) +

+ +
+ Represents a utility class for calculating the difference between + two dates in (aYears bMonths cDays)(aY bM cD) format. +
+
+ +
+
+

Constructor

+ +

+ new DatesYMD(firstDate, secondDate) +

+ +
Creates an instance of DatesYMD.
+ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
firstDate + string + + The first date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd' or + yyyy.mm.dd . +
secondDate + string + + The second date in the format 'yyyy-mm-dd' or 'yyyy/mm/dd'or + yyyy.mm.dd . +
+ +
+
Source:
+
+ +
+
+
+ +

Methods

+ +

+ customizeFormat(yearUnit, monthUnit, dayUnit, partSeparator) → {string} +

+ +
+ Customizes the difference using specified units and separators like + (a + yearUnit + partSeparator + b + monthUnit + partSeparator + c + + dayUnit), eg. aYs-bMs-cDs etc. +
+ +
Parameters:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
yearUnit + string + The unit for years.
monthUnit + string + The unit for months.
dayUnit + string + The unit for days.
partSeparator + string + + The separator between year, month, and day parts. +
+ +
+
Source:
+
+ +
+
+ +
Returns:
+ +
The customized formatted difference.
+ +
+
Type
+
+ string +
+
+ +

+ diffArray() → {Array} +

+ +
+ Calculates the difference between two dates and returns an array + containing Y, M, D, and a formatted 'aY bM cD' difference string. +
+ +
+
Source:
+
+ +
+
+ +
Returns:
+ +
+ An array containing the calculated years, months, days, and the + formatted difference. +
+ +
+
Type
+
+ Array +
+
+ +

+ formattedYMD() → {string} +

+ +
+ Returns the formatted difference between two dates. +
+ +
+
Source:
+
+ +
+
+ +
Returns:
+ +
+ The formatted difference in the format 'aY bM cD'. +
+ +
+
Type
+
+ string +
+
+
+
- - - - - - - - - - - - - - - -

Methods

- - - - - - - -

diffArray() → {Array}

- - - - - - -
- Calculates the difference between two dates and returns an array containing Y, M, D, and a formatted 'aY bM cD' difference string. -
- - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - - - -
Returns:
- - -
- An array containing the calculated years, months, days, and the formatted difference. -
- - - -
-
- Type -
-
- -Array - - -
-
- - - - - - - - - - - - - -

formattedYMD() → {string}

- - - - - - -
- Returns the formatted difference between two dates. -
- - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
Source:
-
- - - - - - - -
- - - - - - - - - - - - - - - -
Returns:
- - -
- The formatted difference in the format 'aY bM cD'. -
- - - -
-
- Type -
-
- -string - - -
-
- - - - - - - - - - - - - -
- -
- - - - -
- - - -
- - - - - - - \ No newline at end of file + + +
+ + + + + + + diff --git a/docs/diff-ymd.js.html b/docs/diff-ymd.js.html index bf1a9726..0a8655b9 100644 --- a/docs/diff-ymd.js.html +++ b/docs/diff-ymd.js.html @@ -1,32 +1,30 @@ - + - - - JSDoc: Source: diff-ymd.js + + + diff-ymd-package Documentation + - - + + - - - - - - -
- -

Source: diff-ymd.js

- - - - - - -
+ + + + + +
+

Source: diff-ymd.js

+ +
-
/**
+          
/**
  * Represents a utility class for calculating the difference between two dates in (aYears bMonths cDays)(aY bM cD) format.
  *
  * @class
@@ -172,30 +170,60 @@ 

Source: diff-ymd.js

formattedYMD() { return this.diffArray()[3]; } + + /** + * Customizes the difference using specified units and separators like (a + yearUnit + partSeparator + b + monthUnit + partSeparator + c + dayUnit), eg. aYs-bMs-cDs etc. + * + * @method + * @param {string} yearUnit - The unit for years. + * @param {string} monthUnit - The unit for months. + * @param {string} dayUnit - The unit for days. + * @param {string} partSeparator - The separator between year, month, and day parts. + * @returns {string} The customized formatted difference. + */ + customizeFormat(yearUnit, monthUnit, dayUnit, partSeparator) { + let Y, M, D; + Y = this.diffArray()[0]; + M = this.diffArray()[1]; + D = this.diffArray()[2]; + const customized = + Y + + yearUnit + + partSeparator + + M + + monthUnit + + partSeparator + + D + + dayUnit; + return customized; + } } // Export the DatesYMD class for use in other modules module.exports = DatesYMD;
-
- - - - -
- - - -
- -
- Documentation generated by JSDoc 4.0.2 on Mon Jan 15 2024 05:21:23 GMT+0530 (India Standard Time) -
- - - - +
+
+ + + +
+ + + + + + diff --git a/docs/index.html b/docs/index.html index 4aee6d0e..82d349d8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,65 +1,48 @@ - + - - - JSDoc: Home + + + diff-ymd-package Documentation + - - + + - - - - - - -
- -

Home

- - - - - - - - -

- - - - - - - - - - - - - - - - - - - - -
- - - -
- - - - - - - \ No newline at end of file + + + + + +
+

diff-ymd-package Documentation

+ +

+
+ + + +
+ + + + + + + diff --git a/jsdoc.json b/jsdoc.json index e51db277..1f68e774 100644 --- a/jsdoc.json +++ b/jsdoc.json @@ -12,8 +12,8 @@ "dictionaries": ["jsdoc", "closure"] }, "templates": { - "cleverLinks": false, - "monospaceLinks": false + "cleverLinks": true, + "monospaceLinks": true }, "plugins": [], "recurseDepth": 10, diff --git a/package.json b/package.json index 44478746..fe37b0c9 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { "name": "diff-ymd-package", - "version": "1.0.5", + "version": "1.1.0", "description": "Utility class for calculating the difference between two dates in formatted ways like (aY bM cD)(aYears bMonths cDays) or customized formats like aY-bM-cD or aYears-bMonths-cDays etc.", "main": "src/diff-ymd.js", "scripts": { "test": "jest" }, "keywords": [ - "diff-ymd-package", "ymd", "diff-ymd", diff --git a/src/diff-ymd.js b/src/diff-ymd.js index 03adfeb8..bc64ce2a 100644 --- a/src/diff-ymd.js +++ b/src/diff-ymd.js @@ -144,6 +144,33 @@ class DatesYMD { formattedYMD() { return this.diffArray()[3]; } + + /** + * Customizes the difference using specified units and separators like (a + yearUnit + partSeparator + b + monthUnit + partSeparator + c + dayUnit), eg. aYs-bMs-cDs etc. + * + * @method + * @param {string} yearUnit - The unit for years. + * @param {string} monthUnit - The unit for months. + * @param {string} dayUnit - The unit for days. + * @param {string} partSeparator - The separator between year, month, and day parts. + * @returns {string} The customized formatted difference. + */ + customizeFormat(yearUnit, monthUnit, dayUnit, partSeparator) { + let Y, M, D; + Y = this.diffArray()[0]; + M = this.diffArray()[1]; + D = this.diffArray()[2]; + const customized = + Y + + yearUnit + + partSeparator + + M + + monthUnit + + partSeparator + + D + + dayUnit; + return customized; + } } // Export the DatesYMD class for use in other modules diff --git a/test/diff-ymd.test.js b/test/diff-ymd.test.js index a3a74f67..96ca6519 100644 --- a/test/diff-ymd.test.js +++ b/test/diff-ymd.test.js @@ -1,20 +1,30 @@ // dates-ymd.test.js +// Import the DatesYMD class from your source code const DatesYMD = require('../src/diff-ymd'); +// Describe block for the DatesYMD class describe('DatesYMD', () => { + // Test case to check if the difference between two dates is calculated correctly test('should calculate the difference between two dates', () => { + // Input dates for the test case const date1 = '2022-01-01'; const date2 = '2023-02-15'; + // Create an instance of the DatesYMD class with the input dates const datesYMD = new DatesYMD(date1, date2); + // Calculate the difference and get the result array const diffArray = datesYMD.diffArray(); + // Assert that the calculated difference matches the expected result expect(diffArray).toEqual([1, 1, 14, '1Y 1M 14D']); }); + // Test case to check if the class handles empty date inputs test('should handle empty date inputs', () => { + // Create an instance of the DatesYMD class with empty date inputs const datesYMD = new DatesYMD('', ''); + // Calculate the difference and get the result array const diffArray = datesYMD.diffArray(); // Assuming the current date is used for empty inputs @@ -23,16 +33,42 @@ describe('DatesYMD', () => { const currentMonth = currentDate.getMonth() + 1; const currentDay = currentDate.getDate(); + // Assert that the calculated difference matches the expected result for empty inputs expect(diffArray).toEqual([0, 0, 0, `0Y 0M 0D`]); }); + // Test case to check if the formatted difference is generated correctly test('should format the difference between two dates like aY bM cD', () => { + // Input dates for the test case const date1 = '2021-03-20'; const date2 = '2022-05-22'; + // Create an instance of the DatesYMD class with the input dates const datesYMD = new DatesYMD(date1, date2); + // Get the formatted difference const formattedDifference = datesYMD.formattedYMD(); + // Assert that the formatted difference matches the expected result expect(formattedDifference).toBe('1Y 2M 2D'); }); + + // Test case to check if the customized difference is generated correctly + test('should customized the difference between two dates like (a + yearUnit + partSeparator + b + monthUnit + partSeparator + c + dayUnit), eg. aYs-bMs-cDs etc.', () => { + // Input dates for the test case + const date1 = '2021-03-20'; + const date2 = '2022-05-22'; + + // Create an instance of the DatesYMD class with the input dates + const datesYMD = new DatesYMD(date1, date2); + // Get the customized difference + const customizedDifference = datesYMD.customizeFormat( + 'Ys', + 'Ms', + 'Ds', + '-', + ); + + // Assert that the customized difference matches the expected result + expect(customizedDifference).toBe('1Ys-2Ms-2Ds'); + }); });