@subiz/widget-v4 4.1.83 → 4.1.84

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.
@@ -1,5 +1,133 @@
1
1
  var op = require('./op.js')
2
2
 
3
+
4
+ export function extractEmojis (text) {
5
+ var out = []
6
+ for (var i = 0; i < text.length; i++) {
7
+ for (var e = 0; e < emojis.length; e++) {
8
+ if (text.indexOf(emojis[e], i) !== i) continue
9
+ out.push({ index: i, length: emojis[e].length })
10
+ i += emojis[e].length - 1
11
+ break
12
+ }
13
+ }
14
+
15
+ return out
16
+ }
17
+
18
+ export function extractLinks (text) {
19
+ text = text || ''
20
+ var reg = /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.,~#?&//=]*)/gim
21
+ var out = []
22
+ var match
23
+ while ((match = reg.exec(text))) {
24
+ out.push({ index: match.index, length: reg.lastIndex - match.index })
25
+ }
26
+
27
+ return out
28
+ }
29
+
30
+ export function extractEmails (text) {
31
+ text = text || ''
32
+ var reg = /(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/gim
33
+
34
+ var out = []
35
+ var match
36
+ while ((match = reg.exec(text))) {
37
+ out.push({ index: match.index, length: reg.lastIndex - match.index })
38
+ }
39
+
40
+ return out
41
+ }
42
+
43
+ export function extractPhones (msg) {
44
+ msg = msg || ''
45
+ var fgmsg = ''
46
+ // fine-grained msg
47
+ // remove '-' in number-number
48
+ // remove ' ' in number number
49
+ // remove '.' in number.number
50
+
51
+ var realchmap = {}
52
+ for (var i = 0; i < msg.length; i++) {
53
+ var ch = msg.charAt(i)
54
+
55
+ if (ch !== '.' && ch !== '-' && ch !== ' ') {
56
+ fgmsg += ch
57
+ realchmap[fgmsg.length - 1] = i
58
+ continue
59
+ }
60
+
61
+ var prevCh = i > 0 ? msg.charAt(i - 1) : '-'
62
+ var nextCh = i < msg.length - 1 ? msg.charAt(i + 1) : '-'
63
+
64
+ if ('0123456789'.indexOf(prevCh) !== -1 && '0123456789'.indexOf(nextCh) !== -1) continue
65
+ fgmsg += ch
66
+ realchmap[fgmsg.length - 1] = i
67
+ }
68
+
69
+ var reg = /([0-9]{8,13})/gim
70
+
71
+ var out = []
72
+ var match
73
+ while ((match = reg.exec(fgmsg))) {
74
+ out.push({ index: realchmap[match.index], length: realchmap[reg.lastIndex - 1] - realchmap[match.index] + 1 })
75
+ }
76
+
77
+ return out
78
+ }
79
+
80
+ export function extract (text, extractor, type) {
81
+ if (!text) return []
82
+ var out = []
83
+ var lastindex = 0
84
+ var tags = extractor(text)
85
+ if (size(tags) === 0) return [{ type: 'text', text }]
86
+ each(tags, tag => {
87
+ out.push({ type: 'text', text: text.substr(lastindex, tag.index - lastindex) })
88
+ out.push({ type, text: text.substr(tag.index, tag.length) })
89
+ lastindex = tag.index + tag.length
90
+ })
91
+ var lastTag = last(tags)
92
+ out.push({
93
+ type: 'text',
94
+ text: text.substr(lastTag.index + lastTag.length, text.length - lastTag.index + lastTag.length),
95
+ })
96
+
97
+ return filter(out, o => o.text !== '')
98
+ }
99
+
100
+ export function pipeExtracts (text, extractors) {
101
+ var out = []
102
+ if (size(extractors) === 0) return [{ type: 'text', text }]
103
+ var ext = extractors.shift()
104
+
105
+ var tags = extract(text, ext, ext.type)
106
+ each(tags, t => {
107
+ if (t.type !== 'text') return out.push(t)
108
+ out = out.concat(pipeExtracts(t.text, extractors.slice()))
109
+ })
110
+
111
+ return out
112
+ }
113
+
114
+ export function extractText (text) {
115
+ var link = extractLinks
116
+ link.type = 'link'
117
+
118
+ var email = extractEmails
119
+ email.type = 'email'
120
+
121
+ var phone = extractPhones
122
+ phone.type = 'phone'
123
+
124
+ var emoji = extractEmojis
125
+ emoji.type = 'emoji'
126
+
127
+ return pipeExtracts(text, [email, link, phone, emoji])
128
+ }
129
+
130
+
3
131
  export function parseJSON(str) {
4
132
  try {
5
133
  return parse(str)
@@ -1143,3 +1271,99 @@ const ALL_CURRENCIES = [
1143
1271
 
1144
1272
  //}
1145
1273
  //}
1274
+
1275
+ export function blockToText(block) {
1276
+ return _blockToText(block).trim()
1277
+ }
1278
+
1279
+ function _blockToText(block) {
1280
+ if (!block || !block.type) return ''
1281
+ let out = ''
1282
+ if (block.type == 'bullet_list' || block.type == 'ordered_list') {
1283
+ map(block.content, (item) => {
1284
+ preifx = '\n* '
1285
+ if (block.type == 'ordered_list') {
1286
+ prefix = i + '\n. '
1287
+ }
1288
+ out += prefix + blockToText(item).trim()
1289
+ })
1290
+ return out
1291
+ }
1292
+
1293
+ if (block.type == 'heading' || block.type == 'paragraph') {
1294
+ out += '\n'
1295
+ }
1296
+
1297
+ if (
1298
+ block.type == '' ||
1299
+ block.type == 'text' ||
1300
+ block.type == 'link' ||
1301
+ block.type == 'mention-agent' ||
1302
+ block.type == 'dynamic-field'
1303
+ ) {
1304
+ return out + block.text
1305
+ }
1306
+
1307
+ if (block.type == 'horizontal_rule') return out + '\n---\n'
1308
+ if (block.type == 'emoji') {
1309
+ let code
1310
+ if (size(block.attrs) > 0) code = block.attrs['code']
1311
+ if (code == '') code = block.text
1312
+
1313
+ if (code != '') {
1314
+ let emoji = find(LEXICAL_EMOJI_LIST, (emoji) => emoji.code === code)
1315
+ out + op.get(emoji, 'text', code)
1316
+ }
1317
+ return out
1318
+ }
1319
+
1320
+ map(block.content, (block) => {
1321
+ out += blockToText(block)
1322
+ })
1323
+ return out
1324
+ }
1325
+
1326
+
1327
+ let emojis = [
1328
+ 'like',
1329
+ 'unlike',
1330
+ 'wink',
1331
+ 'tongue-out',
1332
+ 'tired',
1333
+ 'surprised',
1334
+ 'smiling',
1335
+ 'sleepy',
1336
+ 'sad',
1337
+ 'neutral',
1338
+ 'heart-eyes',
1339
+ 'grinning',
1340
+ 'crying',
1341
+ 'confused',
1342
+ 'angry',
1343
+
1344
+ 'cry',
1345
+ 'heart_eyes',
1346
+ 'neutral_face',
1347
+ 'emoji--disappointed',
1348
+ 'smile',
1349
+ 'open_mouth',
1350
+ 'tired_face',
1351
+ 'stuck_out_tongue',
1352
+ 'moyai',
1353
+ 'thumbsdown',
1354
+ 'dislike',
1355
+ 'thumbsup',
1356
+ 'ok_hand',
1357
+ 'octopus',
1358
+ 'pig2',
1359
+ 'wave',
1360
+ 'mouse',
1361
+ 'mouse2',
1362
+ 'rat',
1363
+ ]
1364
+ emojis = map(emojis, e => ':' + e + ':')
1365
+
1366
+ function absLink(url) {
1367
+ if (!url.startsWith('http://') && !url.startsWith('https://') && !url.startsWith('//')) return 'http://' + url
1368
+ return url
1369
+ }
package/src/util/index.js CHANGED
@@ -26,9 +26,6 @@ sb.set = op.set
26
26
 
27
27
  sb.Pubsub = require('./pubsub.js').default
28
28
 
29
- import * as extractor from './extractor.js'
30
- sb = Object.assign(sb, extractor)
31
-
32
29
  import * as campaign from './campaign.js'
33
30
  sb = Object.assign(sb, campaign)
34
31
 
@@ -1,136 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.extract = extract;
7
- exports.extractEmails = extractEmails;
8
- exports.extractEmojis = extractEmojis;
9
- exports.extractLinks = extractLinks;
10
- exports.extractPhones = extractPhones;
11
- exports.extractText = extractText;
12
- exports.pipeExtracts = pipeExtracts;
13
- var common = require('./common.js');
14
- let emojis = ['like', 'unlike', 'wink', 'tongue-out', 'tired', 'surprised', 'smiling', 'sleepy', 'sad', 'neutral', 'heart-eyes', 'grinning', 'crying', 'confused', 'angry', 'cry', 'heart_eyes', 'neutral_face', 'emoji--disappointed', 'smile', 'open_mouth', 'tired_face', 'stuck_out_tongue', 'moyai', 'thumbsdown', 'dislike', 'thumbsup', 'ok_hand', 'octopus', 'pig2', 'wave', 'mouse', 'mouse2', 'rat'];
15
- emojis = common.map(emojis, e => ':' + e + ':');
16
- function extractEmojis(text) {
17
- var out = [];
18
- for (var i = 0; i < text.length; i++) {
19
- for (var e = 0; e < emojis.length; e++) {
20
- if (text.indexOf(emojis[e], i) !== i) continue;
21
- out.push({
22
- index: i,
23
- length: emojis[e].length
24
- });
25
- i += emojis[e].length - 1;
26
- break;
27
- }
28
- }
29
- return out;
30
- }
31
- function extractLinks(text) {
32
- text = text || '';
33
- var reg = /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.,~#?&//=]*)/gim;
34
- var out = [];
35
- var match;
36
- while (match = reg.exec(text)) {
37
- out.push({
38
- index: match.index,
39
- length: reg.lastIndex - match.index
40
- });
41
- }
42
- return out;
43
- }
44
- function extractEmails(text) {
45
- text = text || '';
46
- var reg = /(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/gim;
47
- var out = [];
48
- var match;
49
- while (match = reg.exec(text)) {
50
- out.push({
51
- index: match.index,
52
- length: reg.lastIndex - match.index
53
- });
54
- }
55
- return out;
56
- }
57
- function extractPhones(msg) {
58
- msg = msg || '';
59
- var fgmsg = '';
60
- var realchmap = {};
61
- for (var i = 0; i < msg.length; i++) {
62
- var ch = msg.charAt(i);
63
- if (ch !== '.' && ch !== '-' && ch !== ' ') {
64
- fgmsg += ch;
65
- realchmap[fgmsg.length - 1] = i;
66
- continue;
67
- }
68
- var prevCh = i > 0 ? msg.charAt(i - 1) : '-';
69
- var nextCh = i < msg.length - 1 ? msg.charAt(i + 1) : '-';
70
- if ('0123456789'.indexOf(prevCh) !== -1 && '0123456789'.indexOf(nextCh) !== -1) continue;
71
- fgmsg += ch;
72
- realchmap[fgmsg.length - 1] = i;
73
- }
74
- var reg = /([0-9]{8,13})/gim;
75
- var out = [];
76
- var match;
77
- while (match = reg.exec(fgmsg)) {
78
- out.push({
79
- index: realchmap[match.index],
80
- length: realchmap[reg.lastIndex - 1] - realchmap[match.index] + 1
81
- });
82
- }
83
- return out;
84
- }
85
- function extract(text, extractor, type) {
86
- if (!text) return [];
87
- var out = [];
88
- var lastindex = 0;
89
- var tags = extractor(text);
90
- if (common.size(tags) === 0) return [{
91
- type: 'text',
92
- text
93
- }];
94
- common.each(tags, tag => {
95
- out.push({
96
- type: 'text',
97
- text: text.substr(lastindex, tag.index - lastindex)
98
- });
99
- out.push({
100
- type,
101
- text: text.substr(tag.index, tag.length)
102
- });
103
- lastindex = tag.index + tag.length;
104
- });
105
- var lastTag = common.last(tags);
106
- out.push({
107
- type: 'text',
108
- text: text.substr(lastTag.index + lastTag.length, text.length - lastTag.index + lastTag.length)
109
- });
110
- return common.filter(out, o => o.text !== '');
111
- }
112
- function pipeExtracts(text, extractors) {
113
- var out = [];
114
- if (common.size(extractors) === 0) return [{
115
- type: 'text',
116
- text
117
- }];
118
- var ext = extractors.shift();
119
- var tags = extract(text, ext, ext.type);
120
- common.each(tags, t => {
121
- if (t.type !== 'text') return out.push(t);
122
- out = out.concat(pipeExtracts(t.text, extractors.slice()));
123
- });
124
- return out;
125
- }
126
- function extractText(text) {
127
- var link = extractLinks;
128
- link.type = 'link';
129
- var email = extractEmails;
130
- email.type = 'email';
131
- var phone = extractPhones;
132
- phone.type = 'phone';
133
- var emoji = extractEmojis;
134
- emoji.type = 'emoji';
135
- return pipeExtracts(text, [email, link, phone, emoji]);
136
- }
@@ -1,166 +0,0 @@
1
- var common = require('./common.js')
2
-
3
- let emojis = [
4
- 'like',
5
- 'unlike',
6
- 'wink',
7
- 'tongue-out',
8
- 'tired',
9
- 'surprised',
10
- 'smiling',
11
- 'sleepy',
12
- 'sad',
13
- 'neutral',
14
- 'heart-eyes',
15
- 'grinning',
16
- 'crying',
17
- 'confused',
18
- 'angry',
19
-
20
- 'cry',
21
- 'heart_eyes',
22
- 'neutral_face',
23
- 'emoji--disappointed',
24
- 'smile',
25
- 'open_mouth',
26
- 'tired_face',
27
- 'stuck_out_tongue',
28
- 'moyai',
29
- 'thumbsdown',
30
- 'dislike',
31
- 'thumbsup',
32
- 'ok_hand',
33
- 'octopus',
34
- 'pig2',
35
- 'wave',
36
- 'mouse',
37
- 'mouse2',
38
- 'rat',
39
- ]
40
- emojis = common.map(emojis, e => ':' + e + ':')
41
-
42
- export function extractEmojis (text) {
43
- var out = []
44
- for (var i = 0; i < text.length; i++) {
45
- for (var e = 0; e < emojis.length; e++) {
46
- if (text.indexOf(emojis[e], i) !== i) continue
47
- out.push({ index: i, length: emojis[e].length })
48
- i += emojis[e].length - 1
49
- break
50
- }
51
- }
52
-
53
- return out
54
- }
55
-
56
- export function extractLinks (text) {
57
- text = text || ''
58
- var reg = /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.,~#?&//=]*)/gim
59
- var out = []
60
- var match
61
- while ((match = reg.exec(text))) {
62
- out.push({ index: match.index, length: reg.lastIndex - match.index })
63
- }
64
-
65
- return out
66
- }
67
-
68
- export function extractEmails (text) {
69
- text = text || ''
70
- var reg = /(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/gim
71
-
72
- var out = []
73
- var match
74
- while ((match = reg.exec(text))) {
75
- out.push({ index: match.index, length: reg.lastIndex - match.index })
76
- }
77
-
78
- return out
79
- }
80
-
81
- export function extractPhones (msg) {
82
- msg = msg || ''
83
- var fgmsg = ''
84
- // fine-grained msg
85
- // remove '-' in number-number
86
- // remove ' ' in number number
87
- // remove '.' in number.number
88
-
89
- var realchmap = {}
90
- for (var i = 0; i < msg.length; i++) {
91
- var ch = msg.charAt(i)
92
-
93
- if (ch !== '.' && ch !== '-' && ch !== ' ') {
94
- fgmsg += ch
95
- realchmap[fgmsg.length - 1] = i
96
- continue
97
- }
98
-
99
- var prevCh = i > 0 ? msg.charAt(i - 1) : '-'
100
- var nextCh = i < msg.length - 1 ? msg.charAt(i + 1) : '-'
101
-
102
- if ('0123456789'.indexOf(prevCh) !== -1 && '0123456789'.indexOf(nextCh) !== -1) continue
103
- fgmsg += ch
104
- realchmap[fgmsg.length - 1] = i
105
- }
106
-
107
- var reg = /([0-9]{8,13})/gim
108
-
109
- var out = []
110
- var match
111
- while ((match = reg.exec(fgmsg))) {
112
- out.push({ index: realchmap[match.index], length: realchmap[reg.lastIndex - 1] - realchmap[match.index] + 1 })
113
- }
114
-
115
- return out
116
- }
117
-
118
- export function extract (text, extractor, type) {
119
- if (!text) return []
120
- var out = []
121
- var lastindex = 0
122
- var tags = extractor(text)
123
- if (common.size(tags) === 0) return [{ type: 'text', text }]
124
- common.each(tags, tag => {
125
- out.push({ type: 'text', text: text.substr(lastindex, tag.index - lastindex) })
126
- out.push({ type, text: text.substr(tag.index, tag.length) })
127
- lastindex = tag.index + tag.length
128
- })
129
- var lastTag = common.last(tags)
130
- out.push({
131
- type: 'text',
132
- text: text.substr(lastTag.index + lastTag.length, text.length - lastTag.index + lastTag.length),
133
- })
134
-
135
- return common.filter(out, o => o.text !== '')
136
- }
137
-
138
- export function pipeExtracts (text, extractors) {
139
- var out = []
140
- if (common.size(extractors) === 0) return [{ type: 'text', text }]
141
- var ext = extractors.shift()
142
-
143
- var tags = extract(text, ext, ext.type)
144
- common.each(tags, t => {
145
- if (t.type !== 'text') return out.push(t)
146
- out = out.concat(pipeExtracts(t.text, extractors.slice()))
147
- })
148
-
149
- return out
150
- }
151
-
152
- export function extractText (text) {
153
- var link = extractLinks
154
- link.type = 'link'
155
-
156
- var email = extractEmails
157
- email.type = 'email'
158
-
159
- var phone = extractPhones
160
- phone.type = 'phone'
161
-
162
- var emoji = extractEmojis
163
- emoji.type = 'emoji'
164
-
165
- return pipeExtracts(text, [email, link, phone, emoji])
166
- }