summaryrefslogtreecommitdiff
path: root/transformation.jsx
blob: 417a7df4b17222bd89c89abe8367b948fed68a7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const { splitSegments } = require('./util');
const { React } = require('powercord/webpack');
const { open } = require('powercord/modal');
const { getLabel } = require('./popup.jsx');
const {
  timezones,
  lookup
} = require('./tz');

const timezonePattern = new RegExp('(\\d+):(\\d+) *(' + timezones.map(it => `(?:${it.code})`).join('|') + ')', 'g');

function transformMessage (userTimezoneProvider, msgConstruct) {
  if (!('content' in msgConstruct) || !('flatMap' in msgConstruct.content)) {
    return msgConstruct;
  }
  return Object.assign({}, msgConstruct, {
    content: msgConstruct.content.flatMap(it => transformPart(userTimezoneProvider, it))
  });
}

function transformPart (userTimezoneProvider, part) {
  if (typeof part === 'string') {
    return splitSegments(part, timezonePattern).map(it => {
      if (typeof it === 'string') {
        return it;
      }
      let [ _, hour, minute, tz ] = it.match;
      let timezone = lookup(tz);
      let time = hour * 60 + minute;
      let userTimezone = userTimezoneProvider();
      return getLabel(time, timezone, userTimezone);
    });
  }
  return [ part ];
}

exports.transformMessageArray = userTimezoneProvider => args =>
  args.map(it => transformMessage(userTimezoneProvider, it));