Skip to content

Commit

Permalink
🔀 Merge branch 'release/2.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Pustur committed Jun 9, 2019
2 parents 12db5ea + 6812cab commit b4eb86a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 41 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [2.0.2] - 2019-06-09

### Added

- Ability to parse more date formats, including Finnish, that look something like this: `31.5.2019 klo 16.58 - <author>: <message>`

## [2.0.1] - 2019-05-31

### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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@2.0.1/dist/whatsapp-chat-parser.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/whatsapp-chat-parser@2.0.2/dist/whatsapp-chat-parser.min.js"></script>
```

&nbsp;
Expand Down
28 changes: 14 additions & 14 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": "2.0.1",
"version": "2.0.2",
"description": "A package to parse WhatsApp chats",
"main": "dist/whatsapp-chat-parser.js",
"files": [
Expand All @@ -9,9 +9,9 @@
"scripts": {
"build": "rm -rf dist/ && rollup -c",
"pretest": "npm run build",
"test": "jest --passWithNoTests",
"test:watch": "jest --watchAll",
"test:coverage": "jest --coverage",
"test": "TZ=UTC jest --passWithNoTests",
"test:watch": "TZ=UTC jest --watchAll",
"test:coverage": "TZ=UTC jest --coverage",
"lint": "eslint src/**/*.js",
"format": "pretty-quick",
"format:staged": "pretty-quick --staged",
Expand Down Expand Up @@ -49,11 +49,11 @@
"eslint-config-prettier": "4.3.0",
"eslint-plugin-import": "2.17.3",
"eslint-plugin-prettier": "3.1.0",
"husky": "2.3.0",
"husky": "2.4.0",
"jest": "24.8.0",
"prettier": "1.17.1",
"prettier": "1.18.2",
"pretty-quick": "1.11.0",
"rollup": "1.12.5",
"rollup": "1.14.5",
"rollup-plugin-babel": "4.3.2",
"rollup-plugin-commonjs": "10.0.0",
"rollup-plugin-uglify": "6.0.2"
Expand Down
16 changes: 9 additions & 7 deletions src/parser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { daysBeforeMonths, normalizeDate } = require('./date.js');
const {
regexSplitTime,
convertTime12to24,
normalizeAMPM,
normalizeTime,
} = require('./time.js');

const regexParser = /\[?(\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4}),? (\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?m\.?))?\]?(?: -|:)? (.+?): ([^]*)/i;
const regexParserSystem = /\[?(\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4}),? (\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?m\.?))?\]?(?: -|:)? ([^]+)/i;
const regexStartsWithDateTime = /\[?(\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4}),? (\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?m\.?))?\]?/i;
const regexParser = /\[?(\d{1,2}[-/.] ?\d{1,2}[-/.] ?\d{2,4})[,.]? \D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?m\.?))?\]?(?: -|:)? (.+?): ([^]*)/i;
const regexParserSystem = /\[?(\d{1,2}[-/.] ?\d{1,2}[-/.] ?\d{2,4})[,.]? \D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?m\.?))?\]?(?: -|:)? ([^]+)/i;
const regexStartsWithDateTime = /\[?(\d{1,2}[-/.] ?\d{1,2}[-/.] ?\d{2,4})[,.]? \D*?(\d{1,2}[.:]\d{1,2}(?:[.:]\d{1,2})?)(?: ([ap]\.?m\.?))?\]?/i;
const regexSplitDate = /[-/.] ?/;

/**
* Given an array of lines, detects the lines that are part of a previous
Expand Down Expand Up @@ -74,7 +76,7 @@ function parseMessages(messages, options = { daysFirst: undefined }) {
if (typeof daysFirst !== 'boolean') {
const numericDates = Array.from(
new Set(parsed.map(({ date }) => date)),
date => date.split(/[-/.]/).map(Number),
date => date.split(regexSplitDate).map(Number),
);

daysFirst = daysBeforeMonths(numericDates);
Expand All @@ -87,16 +89,16 @@ function parseMessages(messages, options = { daysFirst: undefined }) {
let year;

if (daysFirst === false) {
[month, day, year] = date.split(/[-/.]/);
[month, day, year] = date.split(regexSplitDate);
} else {
[day, month, year] = date.split(/[-/.]/);
[day, month, year] = date.split(regexSplitDate);
}

[year, month, day] = normalizeDate(year, month, day);

const [hours, minutes, seconds] = normalizeTime(
ampm ? convertTime12to24(time, normalizeAMPM(ampm)) : time,
).split(/[:.]/);
).split(regexSplitTime);

return {
date: new Date(year, month - 1, day, hours, minutes, seconds),
Expand Down
24 changes: 15 additions & 9 deletions src/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,28 @@ describe('parser.js', () => {
const format1 = [{ system: false, msg: '3/6/18, 1:55 p.m. - a: m' }];
const format2 = [{ system: false, msg: '03-06-2018, 01.55 PM - a: m' }];
const format3 = [{ system: false, msg: '13.06.18 21.25.15: a: m' }];
const format4 = [{ system: false, msg: '[03.13.18 21:25:15] a: m' }];
const format4 = [{ system: false, msg: '[06.13.18 21:25:15] a: m' }];
const format5 = [{ system: false, msg: '13.6.2018 klo 21.25.15 - a: m' }];
const format6 = [{ system: false, msg: '13. 6. 2018. 21:25:15 a: m' }];

const parsed1 = parseMessages(format1);
const parsed2 = parseMessages(format2);
const parsed3 = parseMessages(format3);
const parsed4 = parseMessages(format4);
const parsed5 = parseMessages(format5);
const parsed6 = parseMessages(format6);

const expected1 = '2018-06-03T13:55:00.000Z';
const expected2 = '2018-06-13T21:25:15.000Z';

describe('the date', () => {
it('should be parsed correctly in various formats', () => {
/**
* Checking for the year should be enough to know there were no errors
* in parsing a specific format
*/
expect(parsed1[0].date.getFullYear()).toBe(2018);
expect(parsed2[0].date.getFullYear()).toBe(2018);
expect(parsed3[0].date.getFullYear()).toBe(2018);
expect(parsed4[0].date.getFullYear()).toBe(2018);
expect(parsed1[0].date.toISOString()).toBe(expected1);
expect(parsed2[0].date.toISOString()).toBe(expected1);
expect(parsed3[0].date.toISOString()).toBe(expected2);
expect(parsed4[0].date.toISOString()).toBe(expected2);
expect(parsed5[0].date.toISOString()).toBe(expected2);
expect(parsed6[0].date.toISOString()).toBe(expected2);
});
});
});
Expand Down
13 changes: 10 additions & 3 deletions src/time.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const regexSplitTime = /[:.]/;

/**
* Converts time from 12 hour format to 24 hour format
* From: https://stackoverflow.com/a/40197728/5303634
*/
function convertTime12to24(time, ampm) {
// eslint-disable-next-line prefer-const
let [hours, minutes, seconds] = time.split(/[:.]/);
let [hours, minutes, seconds] = time.split(regexSplitTime);

if (hours === '12') {
hours = '00';
Expand All @@ -21,7 +23,7 @@ function convertTime12to24(time, ampm) {
* Normalizes a time string to have the following format: hh:mm:ss
*/
function normalizeTime(time) {
const [hours, minutes, seconds] = time.split(/[:.]/);
const [hours, minutes, seconds] = time.split(regexSplitTime);

return `${hours.length === 1 ? `0${hours}` : hours}:${minutes}:${seconds ||
'00'}`;
Expand All @@ -34,4 +36,9 @@ function normalizeAMPM(ampm) {
return ampm.replace(/[^apm]/gi, '').toUpperCase();
}

module.exports = { convertTime12to24, normalizeTime, normalizeAMPM };
module.exports = {
regexSplitTime,
convertTime12to24,
normalizeTime,
normalizeAMPM,
};

0 comments on commit b4eb86a

Please sign in to comment.