@sendbird/uikit-utils 2.4.0 → 2.4.1
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/commonjs/index.js +40 -117
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/sendbird/message.js +2 -2
- package/lib/commonjs/sendbird/message.js.map +1 -1
- package/lib/commonjs/shared/file.js +225 -0
- package/lib/commonjs/shared/file.js.map +1 -0
- package/lib/commonjs/shared/index.js +10 -43
- package/lib/commonjs/shared/index.js.map +1 -1
- package/lib/commonjs/shared/regex.js +1 -46
- package/lib/commonjs/shared/regex.js.map +1 -1
- package/lib/module/index.js +3 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/sendbird/message.js +1 -1
- package/lib/module/sendbird/message.js.map +1 -1
- package/lib/module/shared/file.js +192 -0
- package/lib/module/shared/file.js.map +1 -0
- package/lib/module/shared/index.js +10 -37
- package/lib/module/shared/index.js.map +1 -1
- package/lib/module/shared/regex.js +0 -33
- package/lib/module/shared/regex.js.map +1 -1
- package/lib/typescript/src/index.d.ts +3 -3
- package/lib/typescript/src/shared/file.d.ts +68 -0
- package/lib/typescript/src/shared/index.d.ts +10 -16
- package/lib/typescript/src/shared/regex.d.ts +0 -6
- package/package.json +2 -2
- package/src/index.ts +3 -19
- package/src/sendbird/message.ts +1 -1
- package/src/shared/file.ts +193 -0
- package/src/shared/index.ts +10 -37
- package/src/shared/regex.ts +0 -34
- package/lib/commonjs/shared/pubsub.js +0 -54
- package/lib/commonjs/shared/pubsub.js.map +0 -1
- package/lib/module/shared/pubsub.js +0 -45
- package/lib/module/shared/pubsub.js.map +0 -1
- package/lib/typescript/src/shared/pubsub.d.ts +0 -8
- package/src/shared/pubsub.ts +0 -42
package/lib/commonjs/index.js
CHANGED
|
@@ -14,31 +14,9 @@ var _exportNames = {
|
|
|
14
14
|
SBErrorMessage: true,
|
|
15
15
|
Logger: true,
|
|
16
16
|
arrayToMap: true,
|
|
17
|
-
arrayToMapWithGetter: true
|
|
18
|
-
createPubSub: true,
|
|
19
|
-
urlRegexStrict: true,
|
|
20
|
-
urlRegexRough: true,
|
|
21
|
-
newLineRegex: true,
|
|
22
|
-
audioExtRegex: true,
|
|
23
|
-
videoExtRegex: true,
|
|
24
|
-
imageExtRegex: true,
|
|
25
|
-
getFileType: true,
|
|
26
|
-
getFileExtension: true,
|
|
27
|
-
replaceUrlAsComponents: true,
|
|
28
|
-
normalizeFileName: true,
|
|
29
|
-
emailRegex: true,
|
|
30
|
-
phoneRegex: true,
|
|
31
|
-
replaceWithRegex: true,
|
|
32
|
-
createMentionTemplateRegex: true,
|
|
33
|
-
BufferedRequest: true
|
|
17
|
+
arrayToMapWithGetter: true
|
|
34
18
|
};
|
|
35
19
|
exports.ASYNC_NOOP = void 0;
|
|
36
|
-
Object.defineProperty(exports, "BufferedRequest", {
|
|
37
|
-
enumerable: true,
|
|
38
|
-
get: function () {
|
|
39
|
-
return _bufferedRequest.BufferedRequest;
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
20
|
Object.defineProperty(exports, "Logger", {
|
|
43
21
|
enumerable: true,
|
|
44
22
|
get: function () {
|
|
@@ -58,110 +36,55 @@ Object.defineProperty(exports, "arrayToMapWithGetter", {
|
|
|
58
36
|
return _arrayToMap.arrayToMapWithGetter;
|
|
59
37
|
}
|
|
60
38
|
});
|
|
61
|
-
Object.defineProperty(exports, "audioExtRegex", {
|
|
62
|
-
enumerable: true,
|
|
63
|
-
get: function () {
|
|
64
|
-
return _regex.audioExtRegex;
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
Object.defineProperty(exports, "createMentionTemplateRegex", {
|
|
68
|
-
enumerable: true,
|
|
69
|
-
get: function () {
|
|
70
|
-
return _regex.createMentionTemplateRegex;
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
Object.defineProperty(exports, "createPubSub", {
|
|
74
|
-
enumerable: true,
|
|
75
|
-
get: function () {
|
|
76
|
-
return _pubsub.createPubSub;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
Object.defineProperty(exports, "emailRegex", {
|
|
80
|
-
enumerable: true,
|
|
81
|
-
get: function () {
|
|
82
|
-
return _regex.emailRegex;
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
Object.defineProperty(exports, "getFileExtension", {
|
|
86
|
-
enumerable: true,
|
|
87
|
-
get: function () {
|
|
88
|
-
return _regex.getFileExtension;
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
Object.defineProperty(exports, "getFileType", {
|
|
92
|
-
enumerable: true,
|
|
93
|
-
get: function () {
|
|
94
|
-
return _regex.getFileType;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
39
|
exports.ifThenOr = ifThenOr;
|
|
98
|
-
|
|
99
|
-
enumerable: true,
|
|
100
|
-
get: function () {
|
|
101
|
-
return _regex.imageExtRegex;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
exports.isFunction = void 0;
|
|
105
|
-
Object.defineProperty(exports, "newLineRegex", {
|
|
106
|
-
enumerable: true,
|
|
107
|
-
get: function () {
|
|
108
|
-
return _regex.newLineRegex;
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
Object.defineProperty(exports, "normalizeFileName", {
|
|
112
|
-
enumerable: true,
|
|
113
|
-
get: function () {
|
|
114
|
-
return _regex.normalizeFileName;
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
Object.defineProperty(exports, "phoneRegex", {
|
|
118
|
-
enumerable: true,
|
|
119
|
-
get: function () {
|
|
120
|
-
return _regex.phoneRegex;
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
Object.defineProperty(exports, "replaceUrlAsComponents", {
|
|
124
|
-
enumerable: true,
|
|
125
|
-
get: function () {
|
|
126
|
-
return _regex.replaceUrlAsComponents;
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
Object.defineProperty(exports, "replaceWithRegex", {
|
|
130
|
-
enumerable: true,
|
|
131
|
-
get: function () {
|
|
132
|
-
return _regex.replaceWithRegex;
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
exports.toMegabyte = void 0;
|
|
136
|
-
Object.defineProperty(exports, "urlRegexRough", {
|
|
137
|
-
enumerable: true,
|
|
138
|
-
get: function () {
|
|
139
|
-
return _regex.urlRegexRough;
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
Object.defineProperty(exports, "urlRegexStrict", {
|
|
143
|
-
enumerable: true,
|
|
144
|
-
get: function () {
|
|
145
|
-
return _regex.urlRegexStrict;
|
|
146
|
-
}
|
|
147
|
-
});
|
|
148
|
-
Object.defineProperty(exports, "videoExtRegex", {
|
|
149
|
-
enumerable: true,
|
|
150
|
-
get: function () {
|
|
151
|
-
return _regex.videoExtRegex;
|
|
152
|
-
}
|
|
153
|
-
});
|
|
40
|
+
exports.toMegabyte = exports.isFunction = void 0;
|
|
154
41
|
|
|
155
42
|
var _logger = require("./shared/logger");
|
|
156
43
|
|
|
157
44
|
var _arrayToMap = _interopRequireWildcard(require("./shared/arrayToMap"));
|
|
158
45
|
|
|
159
|
-
var _pubsub = require("./shared/pubsub");
|
|
160
|
-
|
|
161
46
|
var _regex = require("./shared/regex");
|
|
162
47
|
|
|
48
|
+
Object.keys(_regex).forEach(function (key) {
|
|
49
|
+
if (key === "default" || key === "__esModule") return;
|
|
50
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
51
|
+
if (key in exports && exports[key] === _regex[key]) return;
|
|
52
|
+
Object.defineProperty(exports, key, {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () {
|
|
55
|
+
return _regex[key];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
163
60
|
var _bufferedRequest = require("./shared/bufferedRequest");
|
|
164
61
|
|
|
62
|
+
Object.keys(_bufferedRequest).forEach(function (key) {
|
|
63
|
+
if (key === "default" || key === "__esModule") return;
|
|
64
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
65
|
+
if (key in exports && exports[key] === _bufferedRequest[key]) return;
|
|
66
|
+
Object.defineProperty(exports, key, {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
get: function () {
|
|
69
|
+
return _bufferedRequest[key];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
var _file = require("./shared/file");
|
|
75
|
+
|
|
76
|
+
Object.keys(_file).forEach(function (key) {
|
|
77
|
+
if (key === "default" || key === "__esModule") return;
|
|
78
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
79
|
+
if (key in exports && exports[key] === _file[key]) return;
|
|
80
|
+
Object.defineProperty(exports, key, {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
get: function () {
|
|
83
|
+
return _file[key];
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
165
88
|
var _shared = require("./shared");
|
|
166
89
|
|
|
167
90
|
Object.keys(_shared).forEach(function (key) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NOOP","ASYNC_NOOP","PASS","val","toMegabyte","byte","isFunction","param","ifThenOr","cond","then","or","SBErrorCode","NON_AUTHORIZED","CHANNEL_NOT_FOUND_SERVER","CHANNEL_NOT_FOUND_SDK","SBErrorMessage","ACL"],"sources":["index.ts"],"sourcesContent":["export { Logger } from './shared/logger';\nexport { default as arrayToMap, arrayToMapWithGetter } from './shared/arrayToMap';\nexport
|
|
1
|
+
{"version":3,"names":["NOOP","ASYNC_NOOP","PASS","val","toMegabyte","byte","isFunction","param","ifThenOr","cond","then","or","SBErrorCode","NON_AUTHORIZED","CHANNEL_NOT_FOUND_SERVER","CHANNEL_NOT_FOUND_SDK","SBErrorMessage","ACL"],"sources":["index.ts"],"sourcesContent":["export { Logger } from './shared/logger';\nexport { default as arrayToMap, arrayToMapWithGetter } from './shared/arrayToMap';\nexport * from './shared/regex';\nexport * from './shared/bufferedRequest';\nexport * from './shared/file';\nexport * from './shared';\n\nexport * from './hooks';\nexport * from './hooks/react-native';\nexport * from './ui-format/groupChannel';\nexport * from './ui-format/openChannel';\nexport * from './ui-format/common';\nexport * from './sendbird/channel';\nexport * from './sendbird/message';\nexport * from './sendbird/attrs';\nexport * from './sendbird/user';\n\nexport const NOOP: () => void = () => void 0;\nexport const ASYNC_NOOP = async () => void 0;\nexport const PASS = <T>(val: T) => val;\nexport const toMegabyte = (byte: number) => byte / 1024 / 1024;\nexport const isFunction = (param?: unknown): param is Function => typeof param === 'function';\n\nexport function ifThenOr<T>(cond: boolean, then: T): T | undefined;\nexport function ifThenOr<T, V>(cond: boolean, then: T, or: V): T | V;\nexport function ifThenOr(cond: boolean, then: unknown, or?: unknown) {\n if (cond) return then;\n return or;\n}\n\nexport const SBErrorCode = {\n NON_AUTHORIZED: 400108,\n CHANNEL_NOT_FOUND_SERVER: 400201,\n CHANNEL_NOT_FOUND_SDK: 900500,\n};\nexport const SBErrorMessage = {\n ACL:\n 'Sendbird provides various access control options when using the Chat SDK.\\n' +\n 'By default, the Allow retrieving user list attribute is turned on to facilitate creating sample apps.\\n' +\n 'However, this may grant access to unwanted data or operations, leading to potential security concerns.\\n' +\n 'To manage your access control settings, you can turn on or off each setting on Sendbird Dashboard.',\n};\n\nexport type {\n UserStruct,\n FilterByValueType,\n UnionToIntersection,\n OmittedValues,\n PartialDeep,\n Optional,\n ContextValue,\n OnBeforeHandler,\n SendbirdMessage,\n SendbirdChatSDK,\n SendbirdChannel,\n SendbirdBaseChannel,\n SendbirdGroupChannel,\n SendbirdOpenChannel,\n SendbirdUser,\n SendbirdMember,\n SendbirdBaseMessage,\n SendbirdUserMessage,\n SendbirdFileMessage,\n SendbirdAdminMessage,\n SendbirdGroupChannelCreateParams,\n SendbirdFileMessageCreateParams,\n SendbirdUserMessageCreateParams,\n SendbirdError,\n SendbirdMessageCollection,\n SendbirdGroupChannelCollection,\n NotificationFiles,\n NotificationMentionedUsers,\n NotificationTranslations,\n PartialNullable,\n SendbirdDataPayload,\n SendbirdPreviousMessageListQuery,\n SendbirdSendableMessage,\n SendbirdOpenChannelListQuery,\n SendbirdGroupChannelListQuery,\n SendbirdGroupChannelUpdateParams,\n SendbirdUserMessageUpdateParams,\n SendbirdFileMessageUpdateParams,\n SendbirdUserUpdateParams,\n SendbirdRestrictedUser,\n SendbirdReaction,\n SendbirdEmojiContainer,\n SendbirdEmoji,\n SendbirdEmojiCategory,\n SendbirdOpenChannelUpdateParams,\n SendbirdOpenChannelCreateParams,\n SendbirdParticipant,\n} from './types';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AAEA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;;;;;AAEO,MAAMA,IAAgB,GAAG,MAAM,KAAK,CAApC;;;;AACA,MAAMC,UAAU,GAAG,YAAY,KAAK,CAApC;;;;AACA,MAAMC,IAAI,GAAOC,GAAJ,IAAeA,GAA5B;;;;AACA,MAAMC,UAAU,GAAIC,IAAD,IAAkBA,IAAI,GAAG,IAAP,GAAc,IAAnD;;;;AACA,MAAMC,UAAU,GAAIC,KAAD,IAAwC,OAAOA,KAAP,KAAiB,UAA5E;;;;AAIA,SAASC,QAAT,CAAkBC,IAAlB,EAAiCC,IAAjC,EAAgDC,EAAhD,EAA8D;EACnE,IAAIF,IAAJ,EAAU,OAAOC,IAAP;EACV,OAAOC,EAAP;AACD;;AAEM,MAAMC,WAAW,GAAG;EACzBC,cAAc,EAAE,MADS;EAEzBC,wBAAwB,EAAE,MAFD;EAGzBC,qBAAqB,EAAE;AAHE,CAApB;;AAKA,MAAMC,cAAc,GAAG;EAC5BC,GAAG,EACD,gFACA,yGADA,GAEA,0GAFA,GAGA;AAL0B,CAAvB"}
|
|
@@ -19,7 +19,7 @@ exports.messageKeyExtractor = messageKeyExtractor;
|
|
|
19
19
|
exports.parseSendbirdNotification = parseSendbirdNotification;
|
|
20
20
|
exports.shouldRenderReaction = shouldRenderReaction;
|
|
21
21
|
|
|
22
|
-
var
|
|
22
|
+
var _file = require("../shared/file");
|
|
23
23
|
|
|
24
24
|
var _common = require("../ui-format/common");
|
|
25
25
|
|
|
@@ -139,7 +139,7 @@ function getReactionCount(reaction) {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
function getFileTypeFromMessage(message) {
|
|
142
|
-
return (0,
|
|
142
|
+
return (0, _file.getFileType)(message.type || (0, _file.getFileExtension)(message.name));
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
function getMessageType(message) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["isNewMessage","msg","currentUserId","myMessage","isMyMessage","isAdminMessage","updatedAt","isSendableMessage","undefined","sender","userId","sendingStatus","messageKeyExtractor","message","getMessageUniqId","messageComparator","a","b","aStatusOffset","bStatusOffset","createdAt","hasSameSender","calcMessageGrouping","groupEnabled","curr","prev","next","getPrev","getMessageTimeFormat","Date","getNext","groupWithPrev","groupWithNext","isUserMessage","isFileMessage","String","messageId","reqId","getAvailableUriFromFileMessage","url","messageParams","file","uri","isSendbirdNotification","dataPayload","Boolean","parseSendbirdNotification","sendbird","JSON","parse","shouldRenderReaction","channel","reactionEnabled","isOpenChannel","isGroupChannel","isBroadcast","isSuper","getReactionCount","reaction","userIds","length","getFileTypeFromMessage","getFileType","type","getFileExtension","name","getMessageType","ogMetaData","fileType"],"sources":["message.ts"],"sourcesContent":["import { getFileExtension, getFileType } from '../shared/regex';\nimport type {\n SendbirdBaseChannel,\n SendbirdBaseMessage,\n SendbirdDataPayload,\n SendbirdFileMessage,\n SendbirdMessage,\n SendbirdReaction,\n SendbirdSendableMessage,\n} from '../types';\nimport { getMessageTimeFormat } from '../ui-format/common';\n\nexport function isNewMessage(msg: SendbirdMessage, currentUserId?: string) {\n const myMessage = isMyMessage(msg, currentUserId);\n if (myMessage) return false;\n if (msg.isAdminMessage()) return false;\n return msg.updatedAt === 0;\n}\n\nexport function isSendableMessage(msg?: SendbirdMessage | null): msg is SendbirdSendableMessage {\n return msg !== undefined && msg !== null && 'sendingStatus' in msg;\n}\n\nexport function isMyMessage(msg?: SendbirdMessage | null, currentUserId = '##__USER_ID_IS_NOT_PROVIDED__##') {\n if (!isSendableMessage(msg)) return false;\n return (\n ('sender' in msg && msg.sender?.userId === currentUserId) ||\n msg.sendingStatus === 'pending' ||\n msg.sendingStatus === 'failed' ||\n msg.sendingStatus === 'canceled'\n );\n}\n\nexport function messageKeyExtractor(message: SendbirdMessage): string {\n return getMessageUniqId(message);\n}\n\n// |-------------------|-------------------|-----------------|-------------------|\n// | sending status | reqId | messageId | createdAt |\n// |-------------------|-------------------|-----------------|-------------------|\n// | pending | timestamp(A) | 0 | timestamp(B) |\n// | canceled | timestamp(A) | 0 | ? |\n// | failed | timestamp(A) | 0 | ? |\n// | succeeded | timestamp(A) / '' | id from DB | timestamp(C) |\n// |-------------------|-------------------|-----------------|-------------------|\nexport function messageComparator(a: SendbirdMessage, b: SendbirdMessage) {\n let aStatusOffset = 0;\n let bStatusOffset = 0;\n\n if (isSendableMessage(a) && a.sendingStatus !== 'succeeded') aStatusOffset = 999999;\n if (isSendableMessage(b) && b.sendingStatus !== 'succeeded') bStatusOffset = 999999;\n\n return b.createdAt + bStatusOffset - (a.createdAt + aStatusOffset);\n}\n\nexport function hasSameSender(a?: SendbirdMessage, b?: SendbirdMessage) {\n if (!a || !b) return false;\n if ('sender' in a && 'sender' in b) return a.sender?.userId === b.sender?.userId;\n return false;\n}\n\nexport function calcMessageGrouping(\n groupEnabled: boolean,\n curr: SendbirdMessage,\n prev?: SendbirdMessage,\n next?: SendbirdMessage,\n) {\n const getPrev = () => {\n if (!groupEnabled) return false;\n if (!prev) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, prev)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(prev.createdAt))) return false;\n return true;\n };\n\n const getNext = () => {\n if (!groupEnabled) return false;\n if (!next) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, next)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(next.createdAt))) return false;\n return true;\n };\n\n return { groupWithPrev: getPrev(), groupWithNext: getNext() };\n}\n\nexport function getMessageUniqId(msg: SendbirdBaseMessage) {\n if (msg.isUserMessage() || msg.isFileMessage()) {\n if (msg.sendingStatus === 'succeeded') return String(msg.messageId);\n return msg.reqId;\n }\n\n return String(msg.messageId);\n}\n\nexport function getAvailableUriFromFileMessage(message: SendbirdFileMessage) {\n if (!message.url && message.messageParams.file && 'uri' in message.messageParams.file) {\n return message.messageParams.file.uri;\n }\n return message.url;\n}\n\ntype RawSendbirdDataPayload = { sendbird: string | object };\nexport function isSendbirdNotification(dataPayload?: {\n [key: string]: string | object;\n}): dataPayload is RawSendbirdDataPayload {\n if (!dataPayload) return false;\n return Boolean(dataPayload['sendbird']);\n}\n\nexport function parseSendbirdNotification(dataPayload: RawSendbirdDataPayload): SendbirdDataPayload {\n return typeof dataPayload.sendbird === 'string' ? JSON.parse(dataPayload.sendbird) : dataPayload.sendbird;\n}\n\nexport function shouldRenderReaction(channel: SendbirdBaseChannel, reactionEnabled: boolean) {\n if (channel.isOpenChannel()) {\n return false;\n }\n\n if (channel.isGroupChannel()) {\n if (channel.isBroadcast) return false;\n if (channel.isSuper) return false;\n }\n\n return reactionEnabled;\n}\n\nexport function getReactionCount(reaction: SendbirdReaction) {\n return reaction.userIds.length;\n}\n\ntype MessageType =\n | 'user'\n | 'admin'\n | 'file'\n | 'unknown'\n | `user.${'opengraph'}`\n | `file.${'image' | 'video' | 'audio'}`;\n\nexport function getFileTypeFromMessage(message: SendbirdFileMessage) {\n return getFileType(message.type || getFileExtension(message.name));\n}\n\nexport function getMessageType(message: SendbirdMessage): MessageType {\n if (message.isAdminMessage()) {\n return 'admin';\n }\n\n if (message.isUserMessage()) {\n if (message.ogMetaData) {\n return 'user.opengraph';\n }\n return 'user';\n }\n\n if (message.isFileMessage()) {\n const fileType = getFileTypeFromMessage(message);\n switch (fileType) {\n case 'image':\n case 'video': {\n return `file.${fileType}`;\n }\n case 'audio': {\n return `file.${fileType}`;\n }\n default: {\n return 'file';\n }\n }\n }\n\n return 'unknown';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;AAUA;;AAEO,SAASA,YAAT,CAAsBC,GAAtB,EAA4CC,aAA5C,EAAoE;EACzE,MAAMC,SAAS,GAAGC,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O,CAACE,aAAR,EAAJ,EAA6B;IAC3B,OAAO,KAAP;EACD;;EAED,IAAIF,OAAO,CAACG,cAAR,EAAJ,EAA8B;IAC5B,IAAIH,OAAO,CAACI,WAAZ,EAAyB,OAAO,KAAP;IACzB,IAAIJ,OAAO,CAACK,OAAZ,EAAqB,OAAO,KAAP;EACtB;;EAED,OAAOJ,eAAP;AACD;;AAEM,SAASK,gBAAT,CAA0BC,QAA1B,EAAsD;EAC3D,OAAOA,QAAQ,CAACC,OAAT,CAAiBC,MAAxB;AACD;;AAUM,SAASC,sBAAT,CAAgChD,OAAhC,EAA8D;EACnE,OAAO,IAAAiD,kBAAA,EAAYjD,OAAO,CAACkD,IAAR,IAAgB,IAAAC,uBAAA,EAAiBnD,OAAO,CAACoD,IAAzB,CAA5B,CAAP;AACD;;AAEM,SAASC,cAAT,CAAwBrD,OAAxB,EAA+D;EACpE,IAAIA,OAAO,CAACR,cAAR,EAAJ,EAA8B;IAC5B,OAAO,OAAP;EACD;;EAED,IAAIQ,OAAO,CAACoB,aAAR,EAAJ,EAA6B;IAC3B,IAAIpB,OAAO,CAACsD,UAAZ,EAAwB;MACtB,OAAO,gBAAP;IACD;;IACD,OAAO,MAAP;EACD;;EAED,IAAItD,OAAO,CAACqB,aAAR,EAAJ,EAA6B;IAC3B,MAAMkC,QAAQ,GAAGP,sBAAsB,CAAChD,OAAD,CAAvC;;IACA,QAAQuD,QAAR;MACE,KAAK,OAAL;MACA,KAAK,OAAL;QAAc;UACZ,OAAQ,QAAOA,QAAS,EAAxB;QACD;;MACD,KAAK,OAAL;QAAc;UACZ,OAAQ,QAAOA,QAAS,EAAxB;QACD;;MACD;QAAS;UACP,OAAO,MAAP;QACD;IAVH;EAYD;;EAED,OAAO,SAAP;AACD"}
|
|
1
|
+
{"version":3,"names":["isNewMessage","msg","currentUserId","myMessage","isMyMessage","isAdminMessage","updatedAt","isSendableMessage","undefined","sender","userId","sendingStatus","messageKeyExtractor","message","getMessageUniqId","messageComparator","a","b","aStatusOffset","bStatusOffset","createdAt","hasSameSender","calcMessageGrouping","groupEnabled","curr","prev","next","getPrev","getMessageTimeFormat","Date","getNext","groupWithPrev","groupWithNext","isUserMessage","isFileMessage","String","messageId","reqId","getAvailableUriFromFileMessage","url","messageParams","file","uri","isSendbirdNotification","dataPayload","Boolean","parseSendbirdNotification","sendbird","JSON","parse","shouldRenderReaction","channel","reactionEnabled","isOpenChannel","isGroupChannel","isBroadcast","isSuper","getReactionCount","reaction","userIds","length","getFileTypeFromMessage","getFileType","type","getFileExtension","name","getMessageType","ogMetaData","fileType"],"sources":["message.ts"],"sourcesContent":["import { getFileExtension, getFileType } from '../shared/file';\nimport type {\n SendbirdBaseChannel,\n SendbirdBaseMessage,\n SendbirdDataPayload,\n SendbirdFileMessage,\n SendbirdMessage,\n SendbirdReaction,\n SendbirdSendableMessage,\n} from '../types';\nimport { getMessageTimeFormat } from '../ui-format/common';\n\nexport function isNewMessage(msg: SendbirdMessage, currentUserId?: string) {\n const myMessage = isMyMessage(msg, currentUserId);\n if (myMessage) return false;\n if (msg.isAdminMessage()) return false;\n return msg.updatedAt === 0;\n}\n\nexport function isSendableMessage(msg?: SendbirdMessage | null): msg is SendbirdSendableMessage {\n return msg !== undefined && msg !== null && 'sendingStatus' in msg;\n}\n\nexport function isMyMessage(msg?: SendbirdMessage | null, currentUserId = '##__USER_ID_IS_NOT_PROVIDED__##') {\n if (!isSendableMessage(msg)) return false;\n return (\n ('sender' in msg && msg.sender?.userId === currentUserId) ||\n msg.sendingStatus === 'pending' ||\n msg.sendingStatus === 'failed' ||\n msg.sendingStatus === 'canceled'\n );\n}\n\nexport function messageKeyExtractor(message: SendbirdMessage): string {\n return getMessageUniqId(message);\n}\n\n// |-------------------|-------------------|-----------------|-------------------|\n// | sending status | reqId | messageId | createdAt |\n// |-------------------|-------------------|-----------------|-------------------|\n// | pending | timestamp(A) | 0 | timestamp(B) |\n// | canceled | timestamp(A) | 0 | ? |\n// | failed | timestamp(A) | 0 | ? |\n// | succeeded | timestamp(A) / '' | id from DB | timestamp(C) |\n// |-------------------|-------------------|-----------------|-------------------|\nexport function messageComparator(a: SendbirdMessage, b: SendbirdMessage) {\n let aStatusOffset = 0;\n let bStatusOffset = 0;\n\n if (isSendableMessage(a) && a.sendingStatus !== 'succeeded') aStatusOffset = 999999;\n if (isSendableMessage(b) && b.sendingStatus !== 'succeeded') bStatusOffset = 999999;\n\n return b.createdAt + bStatusOffset - (a.createdAt + aStatusOffset);\n}\n\nexport function hasSameSender(a?: SendbirdMessage, b?: SendbirdMessage) {\n if (!a || !b) return false;\n if ('sender' in a && 'sender' in b) return a.sender?.userId === b.sender?.userId;\n return false;\n}\n\nexport function calcMessageGrouping(\n groupEnabled: boolean,\n curr: SendbirdMessage,\n prev?: SendbirdMessage,\n next?: SendbirdMessage,\n) {\n const getPrev = () => {\n if (!groupEnabled) return false;\n if (!prev) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, prev)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(prev.createdAt))) return false;\n return true;\n };\n\n const getNext = () => {\n if (!groupEnabled) return false;\n if (!next) return false;\n if (curr.isAdminMessage()) return false;\n if (!hasSameSender(curr, next)) return false;\n if (getMessageTimeFormat(new Date(curr.createdAt)) !== getMessageTimeFormat(new Date(next.createdAt))) return false;\n return true;\n };\n\n return { groupWithPrev: getPrev(), groupWithNext: getNext() };\n}\n\nexport function getMessageUniqId(msg: SendbirdBaseMessage) {\n if (msg.isUserMessage() || msg.isFileMessage()) {\n if (msg.sendingStatus === 'succeeded') return String(msg.messageId);\n return msg.reqId;\n }\n\n return String(msg.messageId);\n}\n\nexport function getAvailableUriFromFileMessage(message: SendbirdFileMessage) {\n if (!message.url && message.messageParams.file && 'uri' in message.messageParams.file) {\n return message.messageParams.file.uri;\n }\n return message.url;\n}\n\ntype RawSendbirdDataPayload = { sendbird: string | object };\nexport function isSendbirdNotification(dataPayload?: {\n [key: string]: string | object;\n}): dataPayload is RawSendbirdDataPayload {\n if (!dataPayload) return false;\n return Boolean(dataPayload['sendbird']);\n}\n\nexport function parseSendbirdNotification(dataPayload: RawSendbirdDataPayload): SendbirdDataPayload {\n return typeof dataPayload.sendbird === 'string' ? JSON.parse(dataPayload.sendbird) : dataPayload.sendbird;\n}\n\nexport function shouldRenderReaction(channel: SendbirdBaseChannel, reactionEnabled: boolean) {\n if (channel.isOpenChannel()) {\n return false;\n }\n\n if (channel.isGroupChannel()) {\n if (channel.isBroadcast) return false;\n if (channel.isSuper) return false;\n }\n\n return reactionEnabled;\n}\n\nexport function getReactionCount(reaction: SendbirdReaction) {\n return reaction.userIds.length;\n}\n\ntype MessageType =\n | 'user'\n | 'admin'\n | 'file'\n | 'unknown'\n | `user.${'opengraph'}`\n | `file.${'image' | 'video' | 'audio'}`;\n\nexport function getFileTypeFromMessage(message: SendbirdFileMessage) {\n return getFileType(message.type || getFileExtension(message.name));\n}\n\nexport function getMessageType(message: SendbirdMessage): MessageType {\n if (message.isAdminMessage()) {\n return 'admin';\n }\n\n if (message.isUserMessage()) {\n if (message.ogMetaData) {\n return 'user.opengraph';\n }\n return 'user';\n }\n\n if (message.isFileMessage()) {\n const fileType = getFileTypeFromMessage(message);\n switch (fileType) {\n case 'image':\n case 'video': {\n return `file.${fileType}`;\n }\n case 'audio': {\n return `file.${fileType}`;\n }\n default: {\n return 'file';\n }\n }\n }\n\n return 'unknown';\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;AAUA;;AAEO,SAASA,YAAT,CAAsBC,GAAtB,EAA4CC,aAA5C,EAAoE;EACzE,MAAMC,SAAS,GAAGC,WAAW,CAACH,GAAD,EAAMC,aAAN,CAA7B;EACA,IAAIC,SAAJ,EAAe,OAAO,KAAP;EACf,IAAIF,GAAG,CAACI,cAAJ,EAAJ,EAA0B,OAAO,KAAP;EAC1B,OAAOJ,GAAG,CAACK,SAAJ,KAAkB,CAAzB;AACD;;AAEM,SAASC,iBAAT,C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;;EAED,OAAOJ,eAAP;AACD;;AAEM,SAASK,gBAAT,CAA0BC,QAA1B,EAAsD;EAC3D,OAAOA,QAAQ,CAACC,OAAT,CAAiBC,MAAxB;AACD;;AAUM,SAASC,sBAAT,CAAgChD,OAAhC,EAA8D;EACnE,OAAO,IAAAiD,iBAAA,EAAYjD,OAAO,CAACkD,IAAR,IAAgB,IAAAC,sBAAA,EAAiBnD,OAAO,CAACoD,IAAzB,CAA5B,CAAP;AACD;;AAEM,SAASC,cAAT,CAAwBrD,OAAxB,EAA+D;EACpE,IAAIA,OAAO,CAACR,cAAR,EAAJ,EAA8B;IAC5B,OAAO,OAAP;EACD;;EAED,IAAIQ,OAAO,CAACoB,aAAR,EAAJ,EAA6B;IAC3B,IAAIpB,OAAO,CAACsD,UAAZ,EAAwB;MACtB,OAAO,gBAAP;IACD;;IACD,OAAO,MAAP;EACD;;EAED,IAAItD,OAAO,CAACqB,aAAR,EAAJ,EAA6B;IAC3B,MAAMkC,QAAQ,GAAGP,sBAAsB,CAAChD,OAAD,CAAvC;;IACA,QAAQuD,QAAR;MACE,KAAK,OAAL;MACA,KAAK,OAAL;QAAc;UACZ,OAAQ,QAAOA,QAAS,EAAxB;QACD;;MACD,KAAK,OAAL;QAAc;UACZ,OAAQ,QAAOA,QAAS,EAAxB;QACD;;MACD;QAAS;UACP,OAAO,MAAP;QACD;IAVH;EAYD;;EAED,OAAO,SAAP;AACD"}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.audioExtRegex = void 0;
|
|
7
|
+
exports.getDownscaleSize = getDownscaleSize;
|
|
8
|
+
exports.getFileExtension = getFileExtension;
|
|
9
|
+
exports.getFileExtensionFromMime = getFileExtensionFromMime;
|
|
10
|
+
exports.getFileExtensionFromUri = getFileExtensionFromUri;
|
|
11
|
+
exports.getFileType = void 0;
|
|
12
|
+
exports.getMimeFromFileExtension = getMimeFromFileExtension;
|
|
13
|
+
exports.imageExtRegex = void 0;
|
|
14
|
+
exports.isImage = isImage;
|
|
15
|
+
exports.normalizeFileName = normalizeFileName;
|
|
16
|
+
exports.parseMimeType = parseMimeType;
|
|
17
|
+
exports.shouldCompressImage = shouldCompressImage;
|
|
18
|
+
exports.videoExtRegex = void 0;
|
|
19
|
+
const EXTENSION_MIME_MAP = {
|
|
20
|
+
// Image
|
|
21
|
+
'jpeg': 'image/jpeg',
|
|
22
|
+
'jpg': 'image/jpeg',
|
|
23
|
+
'png': 'image/png',
|
|
24
|
+
'gif': 'image/gif',
|
|
25
|
+
'webp': 'image/webp',
|
|
26
|
+
'svg': 'image/svg+xml',
|
|
27
|
+
// Video
|
|
28
|
+
'3gp': 'video/3gpp',
|
|
29
|
+
'mp4': 'video/mp4',
|
|
30
|
+
'mpeg': 'video/mpeg',
|
|
31
|
+
'ogv': 'video/ogg',
|
|
32
|
+
'video/quicktime': 'mov',
|
|
33
|
+
'webm': 'video/webm',
|
|
34
|
+
'avi': 'video/x-msvideo',
|
|
35
|
+
// Audio
|
|
36
|
+
'aac': 'audio/aac',
|
|
37
|
+
'mid': 'audio/midi',
|
|
38
|
+
'mp3': 'audio/mpeg',
|
|
39
|
+
'ogg': 'audio/ogg',
|
|
40
|
+
'wav': 'audio/wav',
|
|
41
|
+
'weba': 'audio/webm',
|
|
42
|
+
// Files
|
|
43
|
+
'txt': 'text/plain',
|
|
44
|
+
'pdf': 'application/pdf',
|
|
45
|
+
'doc': 'application/msword',
|
|
46
|
+
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
47
|
+
'xls': 'application/vnd.ms-excel',
|
|
48
|
+
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
49
|
+
'ppt': 'application/vnd.ms-powerpoint',
|
|
50
|
+
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
51
|
+
'zip': 'application/zip',
|
|
52
|
+
'json': 'application/json',
|
|
53
|
+
'gzip': 'application/x-gzip'
|
|
54
|
+
};
|
|
55
|
+
const imageExtRegex = /jpeg|jpg|png|webp|gif/i;
|
|
56
|
+
exports.imageExtRegex = imageExtRegex;
|
|
57
|
+
const audioExtRegex = /3gp|aac|aax|act|aiff|flac|gsm|m4a|m4b|m4p|tta|wma|mp3|webm|wav/i;
|
|
58
|
+
exports.audioExtRegex = audioExtRegex;
|
|
59
|
+
const videoExtRegex = /mov|vod|mp4|avi/i;
|
|
60
|
+
exports.videoExtRegex = videoExtRegex;
|
|
61
|
+
|
|
62
|
+
const getFileType = extOrType => {
|
|
63
|
+
if (extOrType.indexOf('/') > -1) {
|
|
64
|
+
const type = extOrType.split('/')[0];
|
|
65
|
+
if (type === 'video') return 'video';
|
|
66
|
+
if (type === 'audio') return 'audio';
|
|
67
|
+
if (type === 'image') return 'image';
|
|
68
|
+
return 'file';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (extOrType.match(imageExtRegex)) return 'image';
|
|
72
|
+
if (extOrType.match(audioExtRegex)) return 'audio';
|
|
73
|
+
if (extOrType.match(videoExtRegex)) return 'video';
|
|
74
|
+
return 'file';
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Calculates the downscaled size of an image while preserving its aspect ratio.
|
|
78
|
+
*
|
|
79
|
+
* @param {Object} origin - The original size object with `width` and `height` properties.
|
|
80
|
+
* @param {Object} resizing - The resizing object with optional `width` and `height` properties.
|
|
81
|
+
* @returns {Object} - A new size object with the downscaled `width` and `height` properties.
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* getDownscaleSize({ width: 1200, height: 800 }, { width: 600 }); // returns { width: 600, height: 400 }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
exports.getFileType = getFileType;
|
|
90
|
+
|
|
91
|
+
function getDownscaleSize(origin, resizing) {
|
|
92
|
+
let ratio;
|
|
93
|
+
const maxWidth = resizing.width || origin.width,
|
|
94
|
+
maxHeight = resizing.height || origin.height;
|
|
95
|
+
|
|
96
|
+
if (origin.width <= maxWidth && origin.height <= maxHeight) {
|
|
97
|
+
ratio = 1;
|
|
98
|
+
} else if (origin.width > maxWidth && origin.height <= maxHeight) {
|
|
99
|
+
ratio = maxWidth / origin.width;
|
|
100
|
+
} else if (origin.width <= maxWidth && origin.height > maxHeight) {
|
|
101
|
+
ratio = maxHeight / origin.height;
|
|
102
|
+
} else {
|
|
103
|
+
ratio = Math.min(maxWidth / origin.width, maxHeight / origin.height);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
width: origin.width * ratio,
|
|
108
|
+
height: origin.height * ratio
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Normalize a file name by ensuring it has the given extension, if it doesn't already.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} fileName - The file name to normalize.
|
|
115
|
+
* @param {string} extension - The desired extension, without a leading period.
|
|
116
|
+
* @returns {string} - The normalized file name, with the extension.
|
|
117
|
+
*/
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
function normalizeFileName(fileName, extension) {
|
|
121
|
+
if (!extension) return fileName; // .extension
|
|
122
|
+
|
|
123
|
+
let _extension = extension.toLowerCase();
|
|
124
|
+
|
|
125
|
+
if (_extension.indexOf('.') !== 0) {
|
|
126
|
+
_extension = '.' + _extension;
|
|
127
|
+
} // filename.extension | filename
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
const _filename = fileName.toLowerCase();
|
|
131
|
+
|
|
132
|
+
const hasExtension = _filename.lastIndexOf(_extension) === _filename.length - _extension.length;
|
|
133
|
+
|
|
134
|
+
if (!hasExtension) {
|
|
135
|
+
// filename.extension
|
|
136
|
+
return fileName + _extension;
|
|
137
|
+
} else {
|
|
138
|
+
// filename.extension
|
|
139
|
+
return fileName;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Parses a MIME type string into its components.
|
|
144
|
+
*
|
|
145
|
+
* @param mimeType - The MIME type string to parse.
|
|
146
|
+
* @returns An object containing the type, subtype, and parameters of the MIME type.
|
|
147
|
+
*/
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
function parseMimeType(mimeType) {
|
|
151
|
+
const [fullType, ...parts] = mimeType.split(';');
|
|
152
|
+
const [type, subtype] = fullType.split('/');
|
|
153
|
+
const parameters = {};
|
|
154
|
+
|
|
155
|
+
for (const part of parts) {
|
|
156
|
+
const [name, value] = part.trim().split('=');
|
|
157
|
+
parameters[name] = value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
type,
|
|
162
|
+
subtype,
|
|
163
|
+
parameters
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Returns the file extension based on the MIME type.
|
|
168
|
+
*
|
|
169
|
+
* @param {string | null | undefined} mimeType - The MIME type to look up.
|
|
170
|
+
* @returns {string} - The file extension for the given MIME type, or an empty string if no matching file extension was found.
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
function getFileExtensionFromMime(mimeType) {
|
|
175
|
+
if (!mimeType) return '';
|
|
176
|
+
const MIME_EXTENSION_MAP = Object.entries(EXTENSION_MIME_MAP).reduce((acc, _ref) => {
|
|
177
|
+
let [key, value] = _ref;
|
|
178
|
+
acc[value] = key;
|
|
179
|
+
return acc;
|
|
180
|
+
}, {});
|
|
181
|
+
return MIME_EXTENSION_MAP[mimeType.toLowerCase()] || '';
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Returns the MIME type based on the file extension.
|
|
185
|
+
*
|
|
186
|
+
* @param {string | null | undefined} ext - The file extension to look up.
|
|
187
|
+
* @returns {string} - The MIME type for the given file extension, or an empty string if no matching MIME type was found.
|
|
188
|
+
*/
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
function getMimeFromFileExtension(ext) {
|
|
192
|
+
if (!ext) return '';
|
|
193
|
+
return EXTENSION_MIME_MAP[ext.toLowerCase()] || '';
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Returns the file extension of a file path.
|
|
197
|
+
*
|
|
198
|
+
* @param {string} filePath - The file path to extract the extension from.
|
|
199
|
+
* @returns {string} - The file extension, or an empty string if the file path does not have an extension.
|
|
200
|
+
*/
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
function getFileExtension(filePath) {
|
|
204
|
+
const pathWithoutParams = filePath.split('?')[0];
|
|
205
|
+
const idx = pathWithoutParams.lastIndexOf('.');
|
|
206
|
+
if (idx === -1) return '';
|
|
207
|
+
const result = pathWithoutParams.slice(idx - pathWithoutParams.length).toLowerCase();
|
|
208
|
+
if (result === '.') return '';else return result;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function getFileExtensionFromUri(uri) {
|
|
212
|
+
return fetch(uri).then(response => response.headers.get('content-type'));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function isImage(filePath, mimeType) {
|
|
216
|
+
const type = getFileType(mimeType || getFileExtension(filePath));
|
|
217
|
+
return type === 'image';
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function shouldCompressImage(mime) {
|
|
221
|
+
let compressionEnabled = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
222
|
+
const extension = getFileExtensionFromMime(mime);
|
|
223
|
+
return Boolean(extension.match(/jpg|jpeg|png/i) && compressionEnabled);
|
|
224
|
+
}
|
|
225
|
+
//# sourceMappingURL=file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["EXTENSION_MIME_MAP","imageExtRegex","audioExtRegex","videoExtRegex","getFileType","extOrType","indexOf","type","split","match","getDownscaleSize","origin","resizing","ratio","maxWidth","width","maxHeight","height","Math","min","normalizeFileName","fileName","extension","_extension","toLowerCase","_filename","hasExtension","lastIndexOf","length","parseMimeType","mimeType","fullType","parts","subtype","parameters","part","name","value","trim","getFileExtensionFromMime","MIME_EXTENSION_MAP","Object","entries","reduce","acc","key","getMimeFromFileExtension","ext","getFileExtension","filePath","pathWithoutParams","idx","result","slice","getFileExtensionFromUri","uri","fetch","then","response","headers","get","isImage","shouldCompressImage","mime","compressionEnabled","Boolean"],"sources":["file.ts"],"sourcesContent":["const EXTENSION_MIME_MAP = {\n // Image\n 'jpeg': 'image/jpeg',\n 'jpg': 'image/jpeg',\n 'png': 'image/png',\n 'gif': 'image/gif',\n 'webp': 'image/webp',\n 'svg': 'image/svg+xml',\n\n // Video\n '3gp': 'video/3gpp',\n 'mp4': 'video/mp4',\n 'mpeg': 'video/mpeg',\n 'ogv': 'video/ogg',\n 'video/quicktime': 'mov',\n 'webm': 'video/webm',\n 'avi': 'video/x-msvideo',\n\n // Audio\n 'aac': 'audio/aac',\n 'mid': 'audio/midi',\n 'mp3': 'audio/mpeg',\n 'ogg': 'audio/ogg',\n 'wav': 'audio/wav',\n 'weba': 'audio/webm',\n\n // Files\n 'txt': 'text/plain',\n 'pdf': 'application/pdf',\n 'doc': 'application/msword',\n 'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',\n 'xls': 'application/vnd.ms-excel',\n 'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',\n 'ppt': 'application/vnd.ms-powerpoint',\n 'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',\n 'zip': 'application/zip',\n 'json': 'application/json',\n 'gzip': 'application/x-gzip',\n} as Record<string, string>;\n\nexport const imageExtRegex = /jpeg|jpg|png|webp|gif/i;\nexport const audioExtRegex = /3gp|aac|aax|act|aiff|flac|gsm|m4a|m4b|m4p|tta|wma|mp3|webm|wav/i;\nexport const videoExtRegex = /mov|vod|mp4|avi/i;\nexport const getFileType = (extOrType: string) => {\n if (extOrType.indexOf('/') > -1) {\n const type = extOrType.split('/')[0];\n if (type === 'video') return 'video';\n if (type === 'audio') return 'audio';\n if (type === 'image') return 'image';\n return 'file';\n }\n\n if (extOrType.match(imageExtRegex)) return 'image';\n if (extOrType.match(audioExtRegex)) return 'audio';\n if (extOrType.match(videoExtRegex)) return 'video';\n return 'file';\n};\n\n/**\n * Calculates the downscaled size of an image while preserving its aspect ratio.\n *\n * @param {Object} origin - The original size object with `width` and `height` properties.\n * @param {Object} resizing - The resizing object with optional `width` and `height` properties.\n * @returns {Object} - A new size object with the downscaled `width` and `height` properties.\n * @example\n * ```ts\n * getDownscaleSize({ width: 1200, height: 800 }, { width: 600 }); // returns { width: 600, height: 400 }\n * ```\n */\ntype Size = { width: number; height: number };\nexport function getDownscaleSize(origin: Size, resizing: Partial<Size>) {\n let ratio: number;\n\n const maxWidth = resizing.width || origin.width,\n maxHeight = resizing.height || origin.height;\n\n if (origin.width <= maxWidth && origin.height <= maxHeight) {\n ratio = 1;\n } else if (origin.width > maxWidth && origin.height <= maxHeight) {\n ratio = maxWidth / origin.width;\n } else if (origin.width <= maxWidth && origin.height > maxHeight) {\n ratio = maxHeight / origin.height;\n } else {\n ratio = Math.min(maxWidth / origin.width, maxHeight / origin.height);\n }\n\n return { width: origin.width * ratio, height: origin.height * ratio };\n}\n\n/**\n * Normalize a file name by ensuring it has the given extension, if it doesn't already.\n *\n * @param {string} fileName - The file name to normalize.\n * @param {string} extension - The desired extension, without a leading period.\n * @returns {string} - The normalized file name, with the extension.\n */\nexport function normalizeFileName(fileName: string, extension: string) {\n if (!extension) return fileName;\n\n // .extension\n let _extension = extension.toLowerCase();\n if (_extension.indexOf('.') !== 0) {\n _extension = '.' + _extension;\n }\n\n // filename.extension | filename\n const _filename = fileName.toLowerCase();\n const hasExtension = _filename.lastIndexOf(_extension) === _filename.length - _extension.length;\n if (!hasExtension) {\n // filename.extension\n return fileName + _extension;\n } else {\n // filename.extension\n return fileName;\n }\n}\n\n/**\n * Parses a MIME type string into its components.\n *\n * @param mimeType - The MIME type string to parse.\n * @returns An object containing the type, subtype, and parameters of the MIME type.\n */\ntype MimeType = { type: string; subtype: string; parameters: Record<string, string> };\nexport function parseMimeType(mimeType: string): MimeType {\n const [fullType, ...parts] = mimeType.split(';');\n const [type, subtype] = fullType.split('/');\n const parameters: MimeType['parameters'] = {};\n\n for (const part of parts) {\n const [name, value] = part.trim().split('=');\n parameters[name] = value;\n }\n\n return { type, subtype, parameters };\n}\n\n/**\n * Returns the file extension based on the MIME type.\n *\n * @param {string | null | undefined} mimeType - The MIME type to look up.\n * @returns {string} - The file extension for the given MIME type, or an empty string if no matching file extension was found.\n */\nexport function getFileExtensionFromMime(mimeType?: string | null): string {\n if (!mimeType) return '';\n const MIME_EXTENSION_MAP = Object.entries(EXTENSION_MIME_MAP).reduce((acc, [key, value]) => {\n acc[value] = key;\n return acc;\n }, {} as Record<string, string>);\n return MIME_EXTENSION_MAP[mimeType.toLowerCase()] || '';\n}\n\n/**\n * Returns the MIME type based on the file extension.\n *\n * @param {string | null | undefined} ext - The file extension to look up.\n * @returns {string} - The MIME type for the given file extension, or an empty string if no matching MIME type was found.\n */\nexport function getMimeFromFileExtension(ext?: string | null) {\n if (!ext) return '';\n return EXTENSION_MIME_MAP[ext.toLowerCase()] || '';\n}\n\n/**\n * Returns the file extension of a file path.\n *\n * @param {string} filePath - The file path to extract the extension from.\n * @returns {string} - The file extension, or an empty string if the file path does not have an extension.\n */\nexport function getFileExtension(filePath: string) {\n const pathWithoutParams = filePath.split('?')[0];\n\n const idx = pathWithoutParams.lastIndexOf('.');\n if (idx === -1) return '';\n\n const result = pathWithoutParams.slice(idx - pathWithoutParams.length).toLowerCase();\n if (result === '.') return '';\n else return result;\n}\n\nexport function getFileExtensionFromUri(uri: string) {\n return fetch(uri).then((response) => response.headers.get('content-type'));\n}\n\nexport function isImage(filePath: string, mimeType?: string) {\n const type = getFileType(mimeType || getFileExtension(filePath));\n return type === 'image';\n}\n\nexport function shouldCompressImage(mime: string, compressionEnabled = true) {\n const extension = getFileExtensionFromMime(mime);\n return Boolean(extension.match(/jpg|jpeg|png/i) && compressionEnabled);\n}\n"],"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A9B,CAAP;AACD;;AAEM,SAASC,OAAT,CAAiBZ,QAAjB,EAAmCnB,QAAnC,EAAsD;EAC3D,MAAMvB,IAAI,GAAGH,WAAW,CAAC0B,QAAQ,IAAIkB,gBAAgB,CAACC,QAAD,CAA7B,CAAxB;EACA,OAAO1C,IAAI,KAAK,OAAhB;AACD;;AAEM,SAASuD,mBAAT,CAA6BC,IAA7B,EAAsE;EAAA,IAA3BC,kBAA2B,uEAAN,IAAM;EAC3E,MAAM1C,SAAS,GAAGiB,wBAAwB,CAACwB,IAAD,CAA1C;EACA,OAAOE,OAAO,CAAC3C,SAAS,CAACb,KAAV,CAAgB,eAAhB,KAAoCuD,kBAArC,CAAd;AACD"}
|
|
@@ -4,14 +4,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.conditionChaining = conditionChaining;
|
|
7
|
-
exports.getDownscaleSize = getDownscaleSize;
|
|
8
7
|
exports.hash = hash;
|
|
9
|
-
exports.isImage = isImage;
|
|
10
8
|
exports.pick = pick;
|
|
11
9
|
exports.replace = replace;
|
|
12
|
-
exports.shouldCompressImage = shouldCompressImage;
|
|
13
|
-
|
|
14
|
-
var _regex = require("./regex");
|
|
15
10
|
|
|
16
11
|
/**
|
|
17
12
|
* Converts a given string to a hashed string.
|
|
@@ -37,44 +32,16 @@ function conditionChaining(conditions, values) {
|
|
|
37
32
|
return idx > -1 ? values[idx] : values[values.length - 1];
|
|
38
33
|
}
|
|
39
34
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
ratio = 1;
|
|
51
|
-
} else if (origin.width > maxWidth && origin.height <= maxHeight) {
|
|
52
|
-
ratio = maxWidth / origin.width;
|
|
53
|
-
} else if (origin.width <= maxWidth && origin.height > maxHeight) {
|
|
54
|
-
ratio = maxHeight / origin.height;
|
|
55
|
-
} else {
|
|
56
|
-
ratio = Math.max(maxWidth / origin.width, maxHeight / origin.height);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return {
|
|
60
|
-
width: origin.width * ratio,
|
|
61
|
-
height: origin.height * ratio
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function isImage(filePath, mimeType) {
|
|
66
|
-
const type = (0, _regex.getFileType)(mimeType || (0, _regex.getFileExtension)(filePath));
|
|
67
|
-
return type === 'image';
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function shouldCompressImage(filePath) {
|
|
71
|
-
let compressionEnabled = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
72
|
-
const extension = (0, _regex.getFileExtension)(filePath);
|
|
73
|
-
return Boolean(extension.match(/jpg|jpeg|png/i) && compressionEnabled);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Returns the picked data from object.
|
|
77
|
-
* */
|
|
35
|
+
* Returns a new object with only the specified keys from the input object.
|
|
36
|
+
*
|
|
37
|
+
* @param {Object} obj - The input object to pick keys from.
|
|
38
|
+
* @param {Array<string>} keys - An array of keys to pick from the input object.
|
|
39
|
+
* @returns {Object} - A new object containing only the specified keys from the input object.
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* pick({ a: 1, b: '2', c: true }, ['a', 'c']); // returns { a: 1, c: true }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
78
45
|
|
|
79
46
|
|
|
80
47
|
function pick(obj, keys) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["hash","str","String","Math","abs","split","reduce","a","c","charCodeAt","replace","start","end","text","slice","conditionChaining","conditions","values","idx","findIndex","state","Boolean","length","
|
|
1
|
+
{"version":3,"names":["hash","str","String","Math","abs","split","reduce","a","c","charCodeAt","replace","start","end","text","slice","conditionChaining","conditions","values","idx","findIndex","state","Boolean","length","pick","obj","keys","pickedObj","key"],"sources":["index.ts"],"sourcesContent":["/**\n * Converts a given string to a hashed string.\n * */\nexport function hash(str: string) {\n return String(Math.abs(str.split('').reduce((a, c) => ((a << 5) - a + c.charCodeAt(0)) | 0, 0)));\n}\n\n/**\n * Replace a specific range of text in the string with another text.\n * */\nexport function replace(str: string, start: number, end: number, text: string) {\n return str.slice(0, start) + text + str.slice(end);\n}\n\n/**\n * Returns the value corresponding to the first true index of a given condition array.\n * */\nexport function conditionChaining<T, V>(conditions: T[], values: V[]) {\n const idx = conditions.findIndex((state) => Boolean(state));\n return idx > -1 ? values[idx] : values[values.length - 1];\n}\n\n/**\n * Returns a new object with only the specified keys from the input object.\n *\n * @param {Object} obj - The input object to pick keys from.\n * @param {Array<string>} keys - An array of keys to pick from the input object.\n * @returns {Object} - A new object containing only the specified keys from the input object.\n * @example\n * ```ts\n * pick({ a: 1, b: '2', c: true }, ['a', 'c']); // returns { a: 1, c: true }\n * ```\n */\nexport function pick<T extends { [key: string]: unknown }, Keys extends keyof T>(obj: T, keys: Keys[]) {\n return keys.reduce((pickedObj, key) => {\n pickedObj[key] = obj[key];\n return pickedObj;\n }, {} as { [key in Keys]: T[key] });\n}\n"],"mappings":";;;;;;;;;;AAAA;AACA;AACA;AACO,SAASA,IAAT,CAAcC,GAAd,EAA2B;EAChC,OAAOC,MAAM,CAACC,IAAI,CAACC,GAAL,CAASH,GAAG,CAACI,KAAJ,CAAU,EAAV,EAAcC,MAAd,CAAqB,CAACC,CAAD,EAAIC,CAAJ,KAAW,CAACD,CAAC,IAAI,CAAN,IAAWA,CAAX,GAAeC,CAAC,CAACC,UAAF,CAAa,CAAb,CAAhB,GAAmC,CAAlE,EAAqE,CAArE,CAAT,CAAD,CAAb;AACD;AAED;AACA;AACA;;;AACO,SAASC,OAAT,CAAiBT,GAAjB,EAA8BU,KAA9B,EAA6CC,GAA7C,EAA0DC,IAA1D,EAAwE;EAC7E,OAAOZ,GAAG,CAACa,KAAJ,CAAU,CAAV,EAAaH,KA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