Skip to content

Commit

Permalink
🔀 Merge branch 'release/3.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pustur committed Jul 29, 2021
2 parents 5429cb9 + 2a595dd commit 9813f96
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 89 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.2.1] - 2021-07-29

### Changed

- Upgraded dev dependencies

### Fixed

- Issue where date digits whould sometimes get sorted wrong [#237](https://github.com/Pustur/whatsapp-chat-parser/issues/237)

## [3.2.0] - 2021-07-08

### Added
Expand Down Expand Up @@ -237,6 +247,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial release

[3.2.1]: https://github.com/Pustur/whatsapp-chat-parser/compare/3.2.0...3.2.1
[3.2.0]: https://github.com/Pustur/whatsapp-chat-parser/compare/3.1.3...3.2.0
[3.1.3]: https://github.com/Pustur/whatsapp-chat-parser/compare/3.1.2...3.1.3
[3.1.2]: https://github.com/Pustur/whatsapp-chat-parser/compare/3.1.1...3.1.2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ You can also use the [jsDelivr CDN](https://www.jsdelivr.com/package/npm/whatsap
```html
<script src="https://cdn.jsdelivr.net/npm/whatsapp-chat-parser/dist/whatsapp-chat-parser.min.js"></script>
<!-- Or use a specific version -->
<script src="https://cdn.jsdelivr.net/npm/whatsapp-chat-parser@3.2.0/dist/whatsapp-chat-parser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/whatsapp-chat-parser@3.2.1/dist/whatsapp-chat-parser.min.js"></script>
```

&nbsp;
Expand Down
120 changes: 60 additions & 60 deletions package-lock.json

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

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatsapp-chat-parser",
"version": "3.2.0",
"version": "3.2.1",
"description": "A package to parse WhatsApp chats with Node.js or in the browser 💬",
"main": "dist/whatsapp-chat-parser.js",
"types": "types/index.d.ts",
Expand Down Expand Up @@ -49,11 +49,11 @@
},
"license": "MIT",
"devDependencies": {
"@rollup/plugin-typescript": "8.2.1",
"@rollup/plugin-typescript": "8.2.3",
"@types/jest": "26.0.24",
"@typescript-eslint/eslint-plugin": "4.28.2",
"@typescript-eslint/parser": "4.28.2",
"eslint": "7.30.0",
"@typescript-eslint/eslint-plugin": "4.28.5",
"@typescript-eslint/parser": "4.28.5",
"eslint": "7.31.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.23.4",
Expand All @@ -64,10 +64,10 @@
"prettier": "2.3.2",
"pretty-quick": "3.1.1",
"rimraf": "3.0.2",
"rollup": "2.52.8",
"rollup": "2.55.0",
"rollup-plugin-terser": "7.0.2",
"set-tz": "0.2.0",
"ts-jest": "27.0.3",
"ts-jest": "27.0.4",
"typescript": "4.3.5"
}
}
19 changes: 17 additions & 2 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function checkAbove12(numericDates: number[][]): boolean | null {
* before the month or the other way around by checking if a set of numbers
* during the same year decrease at some point.
*
* If it does it's probably the
* days since months can only increase in a given year.
* If it does it's probably the days since months can only increase in a given
* year.
*
* Output is `true` if days are first, `false` if they are second, or `null` if
* it failed to understand the order.
Expand Down Expand Up @@ -131,10 +131,25 @@ function normalizeDate(
];
}

/**
* Pushes the longest number in a date to the end, if there is one. Necessary to
* ensure the year is the last number.
*/
function orderDateComponents(date: string): [string, string, string] {
const regexSplitDate = /[-/.] ?/;
const [a, b, c] = date.split(regexSplitDate);
const maxLength = Math.max(a.length, b.length, c.length);

if (c.length === maxLength) return [a, b, c];
if (b.length === maxLength) return [a, c, b];
return [b, c, a];
}

export {
checkAbove12,
checkDecreasing,
changeFrequencyAnalysis,
daysBeforeMonths,
normalizeDate,
orderDateComponents,
};
8 changes: 3 additions & 5 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { daysBeforeMonths, normalizeDate } from './date';
import { daysBeforeMonths, normalizeDate, orderDateComponents } from './date';
import {
regexSplitTime,
convertTime12to24,
normalizeAMPM,
normalizeTime,
} from './time';
import { Attachment, Message, RawMessage, ParseStringOptions } from './types';
import { sortByLengthAsc } from './utils';

const regexParser =
/^(?:\u200E|\u200F)*\[?(\d{1,4}[-/.] ?\d{1,4}[-/.] ?\d{1,4})[,.]? \D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?\s?m\.?))?\]?(?: -|:)? (.+?): ([^]*)/i;
const regexParserSystem =
/^(?:\u200E|\u200F)*\[?(\d{1,4}[-/.] ?\d{1,4}[-/.] ?\d{1,4})[,.]? \D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?\s?m\.?))?\]?(?: -|:)? ([^]+)/i;
const regexSplitDate = /[-/.] ?/;
const regexAttachment = /<.+:(.+)>|([A-Z\d-]+\.\w+)\s\(.+\)/;

/**
Expand Down Expand Up @@ -100,7 +98,7 @@ function parseMessages(
if (typeof daysFirst !== 'boolean') {
const numericDates = Array.from(
new Set(parsed.map(({ date }) => date)),
date => date.split(regexSplitDate).sort(sortByLengthAsc).map(Number),
date => orderDateComponents(date).map(Number),
);

daysFirst = daysBeforeMonths(numericDates);
Expand All @@ -111,7 +109,7 @@ function parseMessages(
let day: string;
let month: string;
let year: string;
const splitDate = date.split(regexSplitDate).sort(sortByLengthAsc);
const splitDate = orderDateComponents(date);

if (daysFirst === false) {
[month, day, year] = splitDate;
Expand Down
Loading

0 comments on commit 9813f96

Please sign in to comment.