ptn-helpers 0.0.13 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,24 +1,4 @@
1
- declare const FEED_LINK_KEYS: readonly ["title", "_ptr_open_graph_description", "_ptr_open_graph_site_name", "_ptr_open_graph_type"];
2
- declare type FeedLinkKey = typeof FEED_LINK_KEYS[number];
3
- declare type FeedAttachment = {
4
- [feedLinkKey in FeedLinkKey]: string;
5
- } & {
6
- _ptr_media_type: string;
7
- _ptr_open_graph_image_url?: string;
8
- url?: string;
9
- };
10
- export declare type FeedItem = {
11
- id: string;
12
- date_published: string;
13
- url: string;
14
- content_text: string;
15
- attachments?: FeedAttachment[];
16
- _ptr_sender_type: string;
17
- };
18
- export declare type PtnNode = {
19
- text: string;
20
- children: PtnNode[];
21
- uid?: string;
22
- };
1
+ import { FeedItem, PtnNode } from "./types";
2
+ export declare const FEED_LINK_KEYS: string[];
23
3
  export declare const itemToNode: (feedItem: FeedItem, hashtag: string) => PtnNode;
24
- export {};
4
+ export declare const organizeFeedItems: (feedItems: FeedItem[], dateFormat?: string) => Record<string, Record<string, FeedItem[]>>;
package/lib/index.js CHANGED
@@ -1,20 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.itemToNode = void 0;
4
- var FEED_LINK_KEYS = [
3
+ exports.organizeFeedItems = exports.itemToNode = exports.FEED_LINK_KEYS = void 0;
4
+ var date_fns_1 = require("date-fns");
5
+ exports.FEED_LINK_KEYS = [
5
6
  "title",
6
7
  "_ptr_open_graph_description",
7
8
  "_ptr_open_graph_site_name",
8
9
  "_ptr_open_graph_type",
9
10
  ];
10
11
  var itemToNode = function (feedItem, hashtag) {
11
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
12
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
12
13
  var children = [];
13
14
  var text = (_a = feedItem === null || feedItem === void 0 ? void 0 : feedItem.content_text) !== null && _a !== void 0 ? _a : "";
14
15
  if (((_c = (_b = feedItem === null || feedItem === void 0 ? void 0 : feedItem.attachments) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 1 && ((_d = feedItem === null || feedItem === void 0 ? void 0 : feedItem.attachments) === null || _d === void 0 ? void 0 : _d[0]._ptr_media_type) === "text") {
15
16
  (_e = feedItem === null || feedItem === void 0 ? void 0 : feedItem.attachments) === null || _e === void 0 ? void 0 : _e.forEach(function (element) {
17
+ var _a;
16
18
  children.push({
17
- text: element.title,
19
+ text: (_a = element.title) !== null && _a !== void 0 ? _a : "",
18
20
  children: [],
19
21
  });
20
22
  });
@@ -28,11 +30,12 @@ var itemToNode = function (feedItem, hashtag) {
28
30
  children: [],
29
31
  });
30
32
  }
31
- FEED_LINK_KEYS.forEach(function (k) {
33
+ exports.FEED_LINK_KEYS.forEach(function (k) {
34
+ var _a;
32
35
  var v = attachment_1[k];
33
- if ((v === null || v === void 0 ? void 0 : v.length) > 0) {
36
+ if ((_a = v === null || v === void 0 ? void 0 : v.length) !== null && _a !== void 0 ? _a : 0 > 0) {
34
37
  children.push({
35
- text: "".concat(k.replace("_ptr_", "").replace(/_/g, " "), ":: ").concat(v.trim()),
38
+ text: "".concat(k.replace("_ptr_", "").replace(/_/g, " "), ":: ").concat(v === null || v === void 0 ? void 0 : v.trim()),
36
39
  children: [],
37
40
  });
38
41
  }
@@ -48,16 +51,26 @@ var itemToNode = function (feedItem, hashtag) {
48
51
  }
49
52
  else if ((attachment_1 === null || attachment_1 === void 0 ? void 0 : attachment_1._ptr_media_type) === "image") {
50
53
  text = "![](".concat(attachment_1.url, ")");
54
+ // this is captions
55
+ if ((_m = (_l = attachment_1 === null || attachment_1 === void 0 ? void 0 : attachment_1.title) === null || _l === void 0 ? void 0 : _l.length) !== null && _m !== void 0 ? _m : 0 > 0) {
56
+ children.push({
57
+ text: "".concat(attachment_1.title),
58
+ children: [],
59
+ });
60
+ }
51
61
  }
52
62
  else if ((attachment_1 === null || attachment_1 === void 0 ? void 0 : attachment_1._ptr_media_type) === "audio") {
53
- var title = ((_m = (_l = feedItem.content_text) === null || _l === void 0 ? void 0 : _l.trim()) === null || _m === void 0 ? void 0 : _m.length) > 0 ? feedItem.content_text : "Audio Recording";
63
+ var title = ((_p = (_o = feedItem.content_text) === null || _o === void 0 ? void 0 : _o.trim()) === null || _p === void 0 ? void 0 : _p.length) > 0 ? feedItem.content_text : "Audio Recording";
54
64
  text = "[".concat(title, "](").concat(attachment_1.url, ")");
55
65
  }
66
+ else if ((attachment_1 === null || attachment_1 === void 0 ? void 0 : attachment_1._ptr_media_type) === "document" && attachment_1.url) {
67
+ text = "[".concat((attachment_1 === null || attachment_1 === void 0 ? void 0 : attachment_1.title) || "document attachment", "](").concat(attachment_1.url, ")");
68
+ }
56
69
  }
57
70
  text = "".concat(text.trim());
58
71
  var validHashtag = hashtag && typeof hashtag === "string" && hashtag.length > 0;
59
72
  var existingTags = /#\w+to(roam|note)/;
60
- var needsNewTag = !!!((_o = text.match(existingTags)) === null || _o === void 0 ? void 0 : _o.length);
73
+ var needsNewTag = !!!((_q = text.match(existingTags)) === null || _q === void 0 ? void 0 : _q.length);
61
74
  if (validHashtag && needsNewTag) {
62
75
  text = "".concat(text, " #").concat(hashtag);
63
76
  }
@@ -68,3 +81,19 @@ var itemToNode = function (feedItem, hashtag) {
68
81
  };
69
82
  };
70
83
  exports.itemToNode = itemToNode;
84
+ var organizeFeedItems = function (feedItems, dateFormat) {
85
+ if (dateFormat === void 0) { dateFormat = "MMMM do, yyyy"; }
86
+ var reduceFeedItems = function (obj, feedItem) {
87
+ var date = new Date(feedItem.date_published), pageName = (0, date_fns_1.format)(date, dateFormat), senderType = feedItem._ptr_sender_type;
88
+ if (!obj.hasOwnProperty(pageName)) {
89
+ obj[pageName] = {};
90
+ }
91
+ if (!obj[pageName][senderType]) {
92
+ obj[pageName][senderType] = [];
93
+ }
94
+ obj[pageName][senderType].push(feedItem);
95
+ return obj;
96
+ };
97
+ return feedItems.reduce(reduceFeedItems, {});
98
+ };
99
+ exports.organizeFeedItems = organizeFeedItems;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ptn-helpers",
3
- "version": "0.0.13",
3
+ "version": "0.0.16",
4
4
  "description": "helper functions to fetch and display ptn data",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -42,5 +42,8 @@
42
42
  "prettier": "^2.5.1",
43
43
  "ts-jest": "^27.1.2",
44
44
  "typescript": "^4.5.4"
45
+ },
46
+ "dependencies": {
47
+ "date-fns": "^2.28.0"
45
48
  }
46
49
  }