telegram-transformer 1.0.0
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/constants.d.ts +3 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/constants.js +8 -0
- package/lib/format.d.ts +7 -0
- package/lib/format.d.ts.map +1 -0
- package/lib/format.js +54 -0
- package/lib/formatters.d.ts +4 -0
- package/lib/formatters.d.ts.map +1 -0
- package/lib/formatters.js +30 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +21 -0
- package/lib/types.d.ts +12 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +2 -0
- package/package.json +52 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,eAAO,MAAM,eAAe,EAAE,QAAQ,CAAC,OAAO,CAG7C,CAAC"}
|
package/lib/constants.js
ADDED
package/lib/format.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGzC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,eAAO,MAAM,MAAM,SACX,MAAM,aACF,aAAa,EAAE,YAChB,OAAO;;;CA8DjB,CAAC"}
|
package/lib/format.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.format = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const formatters_1 = require("./formatters");
|
|
6
|
+
const format = (text, entities = [], options = constants_1.DEFAULT_OPTIONS) => {
|
|
7
|
+
if (!(entities === null || entities === void 0 ? void 0 : entities.length)) {
|
|
8
|
+
return {
|
|
9
|
+
text,
|
|
10
|
+
entities,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
const newEntities = [...entities];
|
|
14
|
+
const formatters = {
|
|
15
|
+
...constants_1.DEFAULT_OPTIONS.formatters,
|
|
16
|
+
...options.formatters,
|
|
17
|
+
};
|
|
18
|
+
const startFrom = options.startFrom || constants_1.DEFAULT_OPTIONS.startFrom;
|
|
19
|
+
let newText = text;
|
|
20
|
+
let prevOffsetChange = 0;
|
|
21
|
+
let prevAfterChange = 0;
|
|
22
|
+
for (let i = 0; i < newEntities.length; i++) {
|
|
23
|
+
const entity = { ...newEntities[i] };
|
|
24
|
+
const formatter = formatters[entity.type] || formatters_1.defaultFormatter;
|
|
25
|
+
const isNotInRange = entity.offset < startFrom;
|
|
26
|
+
newEntities[i] = entity;
|
|
27
|
+
entity.offset -= startFrom;
|
|
28
|
+
if (isNotInRange) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
let newOffset = entity.offset + prevOffsetChange;
|
|
32
|
+
let newOffsetWithLength = newOffset + entity.length;
|
|
33
|
+
const prevEntity = newEntities[i - 1];
|
|
34
|
+
if (prevEntity && newOffset >= prevEntity.offset + prevEntity.length) {
|
|
35
|
+
prevOffsetChange += prevAfterChange;
|
|
36
|
+
prevAfterChange = 0;
|
|
37
|
+
}
|
|
38
|
+
newOffset = entity.offset + prevOffsetChange;
|
|
39
|
+
newOffsetWithLength = newOffset + entity.length;
|
|
40
|
+
const beforeEntity = newText.substring(0, newOffset);
|
|
41
|
+
const entityText = newText.substring(newOffset, newOffsetWithLength);
|
|
42
|
+
const afterEntity = newText.substring(newOffsetWithLength, newText.length);
|
|
43
|
+
const formatted = formatter(entityText, entity, text);
|
|
44
|
+
prevOffsetChange += formatted.before.length;
|
|
45
|
+
prevAfterChange += formatted.after.length;
|
|
46
|
+
entity.offset = newOffset;
|
|
47
|
+
newText = `${beforeEntity}${formatted.text}${afterEntity}`;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
text: newText,
|
|
51
|
+
entities: newEntities,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
exports.format = format;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEtD,eAAO,MAAM,gBAAgB,EAAE,WAS9B,CAAC;AAEF,eAAO,MAAM,UAAU,EAAE,cAgBxB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatters = exports.defaultFormatter = void 0;
|
|
4
|
+
const defaultFormatter = (entityText, entity, text) => {
|
|
5
|
+
const before = '';
|
|
6
|
+
const after = '';
|
|
7
|
+
return {
|
|
8
|
+
before,
|
|
9
|
+
after,
|
|
10
|
+
text: `${before}${entityText}${after}`,
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
exports.defaultFormatter = defaultFormatter;
|
|
14
|
+
exports.formatters = {
|
|
15
|
+
mention: exports.defaultFormatter,
|
|
16
|
+
hashtag: exports.defaultFormatter,
|
|
17
|
+
cashtag: exports.defaultFormatter,
|
|
18
|
+
bot_command: exports.defaultFormatter,
|
|
19
|
+
url: exports.defaultFormatter,
|
|
20
|
+
email: exports.defaultFormatter,
|
|
21
|
+
phone_number: exports.defaultFormatter,
|
|
22
|
+
bold: exports.defaultFormatter,
|
|
23
|
+
italic: exports.defaultFormatter,
|
|
24
|
+
underline: exports.defaultFormatter,
|
|
25
|
+
strikethrough: exports.defaultFormatter,
|
|
26
|
+
spoiler: exports.defaultFormatter,
|
|
27
|
+
code: exports.defaultFormatter,
|
|
28
|
+
pre: exports.defaultFormatter,
|
|
29
|
+
text_link: exports.defaultFormatter,
|
|
30
|
+
};
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AAEzB,eAAe,MAAM,CAAC"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
const format_1 = require("./format");
|
|
18
|
+
__exportStar(require("./types"), exports);
|
|
19
|
+
__exportStar(require("./formatters"), exports);
|
|
20
|
+
__exportStar(require("./format"), exports);
|
|
21
|
+
exports.default = format_1.format;
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MessageEntity } from 'typegram';
|
|
2
|
+
export type FormatterFn = (entityText: string, entity: MessageEntity, text: string) => {
|
|
3
|
+
text: string;
|
|
4
|
+
before: string;
|
|
5
|
+
after: string;
|
|
6
|
+
};
|
|
7
|
+
export type FormattersDict = Record<string, FormatterFn>;
|
|
8
|
+
export type Options = {
|
|
9
|
+
formatters?: FormattersDict;
|
|
10
|
+
startFrom?: number;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,MAAM,MAAM,WAAW,GAAG,CACxB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,aAAa,EACrB,IAAI,EAAE,MAAM,KACT;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAErD,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAEzD,MAAM,MAAM,OAAO,GAAG;IAAE,UAAU,CAAC,EAAE,cAAc,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC"}
|
package/lib/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "telegram-transformer",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Telegram message transformer",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "jest --config jestconfig.json --passWithNoTests",
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
11
|
+
"prepare": "npm run build",
|
|
12
|
+
"lint": "tslint -p tsconfig.json",
|
|
13
|
+
"version": "npm run format && git add -A src",
|
|
14
|
+
"prepublishOnly": "npm test && npm run lint",
|
|
15
|
+
"preversion": "npm run lint",
|
|
16
|
+
"postversion": "git push && git push --tags"
|
|
17
|
+
},
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"author": "Ivan Odyntsov <ivan@odintsov.me>",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+ssh://git@github.com/ivanodintsov/telegram-transformer.git"
|
|
24
|
+
},
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/ivanodintsov/telegraf/telegram-transformer"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"lib/**/*"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"typegram": "^4.1.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/jest": "^29.2.4",
|
|
36
|
+
"jest": "^29.3.1",
|
|
37
|
+
"prettier": "^2.8.1",
|
|
38
|
+
"ts-jest": "^29.0.3",
|
|
39
|
+
"tslint": "^6.1.3",
|
|
40
|
+
"tslint-config-prettier": "^1.18.0",
|
|
41
|
+
"typescript": "^4.9.4"
|
|
42
|
+
},
|
|
43
|
+
"keywords": [
|
|
44
|
+
"telegram-transformer",
|
|
45
|
+
"telegram",
|
|
46
|
+
"typegram",
|
|
47
|
+
"telegram parse",
|
|
48
|
+
"bot",
|
|
49
|
+
"telegram entity",
|
|
50
|
+
"telegram formatter"
|
|
51
|
+
]
|
|
52
|
+
}
|