taraskevizer 10.4.8 → 10.4.9

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.
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env node
2
+ import { cpus } from 'node:os';
3
+ import { Worker } from 'node:worker_threads';
4
+ import { pipelines } from '../index.js';
5
+ import { parseArgs } from './parse-args.js';
6
+ const printWithPrefix = (msg) => {
7
+ process.stdout.write("[taraskevizer]" + ' ' + msg + '\n');
8
+ };
9
+ const splitIntoChunks = (text, n) => {
10
+ const size = Math.ceil(text.length / n);
11
+ const chunks = [];
12
+ let start = 0;
13
+ for (let i = 0; i < n; i++) {
14
+ let end = start + size;
15
+ if (end < text.length) {
16
+ const forward = text.indexOf('\n', end);
17
+ const backward = text.lastIndexOf('\n', end);
18
+ if (forward === -1 && backward === -1) {
19
+ // no-op, use raw end
20
+ }
21
+ else if (forward === -1) {
22
+ end = backward;
23
+ }
24
+ else if (backward === -1) {
25
+ end = forward;
26
+ }
27
+ else {
28
+ end = forward - end < end - backward ? forward : backward;
29
+ }
30
+ }
31
+ if (end > text.length)
32
+ end = text.length;
33
+ chunks.push(text.slice(start, end));
34
+ start = end;
35
+ if (start >= text.length)
36
+ break;
37
+ }
38
+ return chunks;
39
+ };
40
+ process.argv.splice(0, 2);
41
+ const firstArg = process.argv[0];
42
+ if (firstArg) {
43
+ if (firstArg === '-v' || firstArg === '--version') {
44
+ printWithPrefix("10.4.9");
45
+ process.exit(0);
46
+ }
47
+ if (firstArg === '-h' || firstArg === '--help') {
48
+ printWithPrefix(`Usage: tarask [options] text
49
+ If text is not passed, interactive mode is enabled
50
+
51
+ EXAMPLES
52
+
53
+ Convert and latinize a word
54
+ tarask --latin 'планета'
55
+ Prints "planeta"
56
+
57
+ Read from one file and write converted text to another
58
+ tarask < ./cyr-text.txt > ./lat-text.txt
59
+
60
+ Enter interactive mode
61
+ tarask
62
+ Prints "[taraskevizer] Enter the text:" and waits until you enter a new line
63
+
64
+ OPTIONS
65
+
66
+ General:
67
+ -h --help
68
+ -v --version
69
+
70
+ Alphabet:
71
+ -l --latin
72
+ -lj --latin-ji
73
+ -a --arabic
74
+
75
+ When to replace і(i) by й(j) after vowels:
76
+ -jr --jrandom
77
+ -ja --jalways
78
+
79
+ Replace ґ(g) by г(h) in cyrillic alphabet:
80
+ --h
81
+
82
+ Variations:
83
+ -nv --no-variations
84
+ -fv --first-variation
85
+
86
+ Mode (only one can be used):
87
+ -html --html
88
+ -abc --alphabet-only
89
+
90
+ Other:
91
+ -nec --not-escape-caps
92
+ -nc --no-color
93
+ -st --single-thread
94
+ `);
95
+ process.exit(0);
96
+ }
97
+ }
98
+ const { mode, cfg, doForceSingleThread } = parseArgs(process.argv);
99
+ let text = '';
100
+ if (process.argv.length) {
101
+ text = process.argv.reverse().join(' ');
102
+ }
103
+ else {
104
+ const chunks = [];
105
+ let length = 0;
106
+ if (process.stdin.isTTY) {
107
+ printWithPrefix('Enter the text');
108
+ for await (const chunk of process.stdin) {
109
+ chunks.push(chunk);
110
+ length += chunk.length;
111
+ if (chunk.includes('\n'))
112
+ break;
113
+ }
114
+ }
115
+ else {
116
+ for await (const chunk of process.stdin) {
117
+ chunks.push(chunk);
118
+ length += chunk.length;
119
+ }
120
+ }
121
+ text = Buffer.concat(chunks, length).toString();
122
+ }
123
+ let result = '';
124
+ if (text.length > 50_000 && !doForceSingleThread) {
125
+ const cpuCount = Math.max(1, cpus()?.length || 1);
126
+ const chunks = splitIntoChunks(text, cpuCount);
127
+ const WORKER_CODE = `
128
+ const { parentPort, workerData } = require('node:worker_threads');
129
+ const { pipelines } = require('./dist');
130
+ const { parseArgs } = require('./dist/bin/parse-args');
131
+ const { argv, chunk } = workerData;
132
+ const { mode, cfg } = parseArgs(argv);
133
+ parentPort.postMessage(pipelines[mode](chunk, cfg));`;
134
+ const results = await Promise.all(chunks.map((chunk) => new Promise((resolve, reject) => {
135
+ const worker = new Worker(WORKER_CODE, {
136
+ eval: true,
137
+ workerData: { argv: process.argv, chunk },
138
+ });
139
+ worker.on('message', resolve);
140
+ worker.on('error', reject);
141
+ worker.on('exit', (code) => {
142
+ if (code !== 0)
143
+ reject(new Error('Worker exit code ' + code));
144
+ });
145
+ })));
146
+ result = results.join('\n') + '\n';
147
+ }
148
+ else {
149
+ result = pipelines[mode](text, cfg) + '\n';
150
+ }
151
+ if (process.stdout.write(result)) {
152
+ process.exit(0);
153
+ }
154
+ else {
155
+ process.stdout.once('drain', () => {
156
+ process.exit(0);
157
+ });
158
+ }
@@ -0,0 +1,6 @@
1
+ import { TaraskConfig } from '..';
2
+ export declare const parseArgs: (argv: string[]) => {
3
+ cfg: Partial<TaraskConfig>;
4
+ mode: "tarask";
5
+ doForceSingleThread: boolean;
6
+ };
@@ -0,0 +1,123 @@
1
+ import { dicts, htmlConfigOptions, TaraskConfig, wrappers, } from '../index.js';
2
+ const toHashTable = (dict) => {
3
+ const result = {};
4
+ for (const { 0: options, 1: callback } of dict)
5
+ for (const option of options)
6
+ result[option] = callback;
7
+ return result;
8
+ };
9
+ export const parseArgs = (argv) => {
10
+ let cfg = {
11
+ g: true,
12
+ variations: 'all',
13
+ wrappers: wrappers.ansiColor,
14
+ };
15
+ let mode = 'tarask';
16
+ let isHtml = false;
17
+ let doForceSingleThread = false;
18
+ const optionDict = toHashTable([
19
+ [
20
+ ['--latin', '-l'],
21
+ () => {
22
+ cfg.abc = dicts.alphabets.latin;
23
+ },
24
+ ],
25
+ [
26
+ ['--latin-ji', '-lj'],
27
+ () => {
28
+ cfg.abc = dicts.alphabets.latinJi;
29
+ },
30
+ ],
31
+ [
32
+ ['--arabic', '-a'],
33
+ () => {
34
+ cfg.abc = dicts.alphabets.arabic;
35
+ },
36
+ ],
37
+ [
38
+ ['--jrandom', '-jr'],
39
+ () => {
40
+ cfg.j = 'random';
41
+ },
42
+ ],
43
+ [
44
+ ['--jalways', '-ja'],
45
+ () => {
46
+ cfg.j = 'always';
47
+ },
48
+ ],
49
+ [
50
+ ['--no-escape-caps', '-nec'],
51
+ () => {
52
+ cfg.doEscapeCapitalized = false;
53
+ },
54
+ ],
55
+ [
56
+ ['--h'],
57
+ () => {
58
+ cfg.g = false;
59
+ },
60
+ ],
61
+ [
62
+ ['--no-variations', '-nv'],
63
+ () => {
64
+ cfg.variations = 'no';
65
+ },
66
+ ],
67
+ [
68
+ ['--first-variation', '-fv'],
69
+ () => {
70
+ cfg.variations = 'first';
71
+ },
72
+ ],
73
+ [
74
+ ['--no-color', '-nc'],
75
+ () => {
76
+ cfg.wrappers = null;
77
+ },
78
+ ],
79
+ [
80
+ ['--html', '-html'],
81
+ () => {
82
+ isHtml = true;
83
+ cfg.wrappers = htmlConfigOptions.wrappers;
84
+ },
85
+ ],
86
+ [
87
+ ['--alphabet-only', '-abc'],
88
+ () => {
89
+ mode = 'alphabetic';
90
+ },
91
+ ],
92
+ [
93
+ ['--phonetic', '-ph'],
94
+ () => {
95
+ mode = 'phonetic';
96
+ },
97
+ ],
98
+ [
99
+ ['--single-thread', '-st'],
100
+ () => {
101
+ doForceSingleThread = true;
102
+ },
103
+ ],
104
+ ]);
105
+ let currOption;
106
+ argv.reverse();
107
+ while ((currOption = argv.pop())) {
108
+ if (currOption in optionDict) {
109
+ optionDict[currOption]();
110
+ }
111
+ else {
112
+ argv.push(currOption);
113
+ break;
114
+ }
115
+ }
116
+ cfg = new TaraskConfig(isHtml
117
+ ? {
118
+ ...htmlConfigOptions,
119
+ ...cfg,
120
+ }
121
+ : cfg);
122
+ return { cfg, mode, doForceSingleThread };
123
+ };
package/dist/config.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { DeepReadonly } from './types';
2
1
  import type { Alphabet } from './dict/alphabets';
2
+ import type { DeepReadonly } from './types';
3
3
  import { type Wrappers } from './wrappers';
4
4
  export type Variation = 'no' | 'first' | 'all';
5
5
  export type OptionJ = 'never' | 'random' | 'always';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taraskevizer",
3
- "version": "10.4.8",
3
+ "version": "10.4.9",
4
4
  "author": "GooseOb",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,7 +22,7 @@
22
22
  "typescript-eslint": "^8.42.0"
23
23
  },
24
24
  "bin": {
25
- "tarask": "dist/bin.js"
25
+ "tarask": "dist/bin/index.js"
26
26
  },
27
27
  "description": "Канвэртацыя акадэмічнага правапісу ў клясычны",
28
28
  "files": [
package/dist/bin.js DELETED
@@ -1,206 +0,0 @@
1
- #!/usr/bin/env node
2
- import { dicts, TaraskConfig, pipelines, htmlConfigOptions, wrappers } from './index.js';
3
- const printWithPrefix = (msg) => {
4
- process.stdout.write("[taraskevizer]" + ' ' + msg + '\n');
5
- };
6
- process.argv.splice(0, 2);
7
- const firstArg = process.argv[0];
8
- if (firstArg) {
9
- if (firstArg === '-v' || firstArg === '--version') {
10
- printWithPrefix("10.4.8");
11
- process.exit(0);
12
- }
13
- if (firstArg === '-h' || firstArg === '--help') {
14
- printWithPrefix(`Usage: tarask [options] text
15
- If text is not passed, interactive mode will be enabled
16
-
17
- EXAMPLES
18
-
19
- Convert and latinize a word
20
- tarask --latin 'планета'
21
- Will print "planeta"
22
-
23
- Read from one file and write converted text to another
24
- tarask < ./cyr-text.txt > ./lat-text.txt
25
-
26
- Enter interactive mode
27
- tarask
28
- Will print "[taraskevizer] Enter the text:" and wait until you press Enter
29
-
30
- OPTIONS
31
-
32
- General:
33
- -h --help
34
- -v --version
35
-
36
- Alphabet:
37
- -l --latin
38
- -lj --latin-ji
39
- -a --arabic
40
-
41
- When to replace і(i) by й(j) after vowels:
42
- -jr --jrandom
43
- -ja --jalways
44
-
45
- Replace ґ(g) by г(h) in cyrillic alphabet:
46
- --h
47
-
48
- Variations:
49
- -nv --no-variations
50
- -fv --first-variation
51
-
52
- Mode (only one can be used):
53
- -html --html
54
- -abc --alphabet-only
55
-
56
- Other:
57
- -nec --not-escape-caps
58
- -nc --no-color
59
- `);
60
- process.exit(0);
61
- }
62
- }
63
- let cfg = {
64
- g: true,
65
- variations: 'all',
66
- wrappers: wrappers.ansiColor,
67
- };
68
- let mode = 'tarask';
69
- const toHashTable = (dict) => {
70
- const result = {};
71
- for (const { 0: options, 1: callback } of dict)
72
- for (const option of options)
73
- result[option] = callback;
74
- return result;
75
- };
76
- let isHtml = false;
77
- const optionDict = toHashTable([
78
- [
79
- ['--latin', '-l'],
80
- () => {
81
- cfg.abc = dicts.alphabets.latin;
82
- },
83
- ],
84
- [
85
- ['--latin-ji', '-lj'],
86
- () => {
87
- cfg.abc = dicts.alphabets.latinJi;
88
- },
89
- ],
90
- [
91
- ['--arabic', '-a'],
92
- () => {
93
- cfg.abc = dicts.alphabets.arabic;
94
- },
95
- ],
96
- [
97
- ['--jrandom', '-jr'],
98
- () => {
99
- cfg.j = 'random';
100
- },
101
- ],
102
- [
103
- ['--jalways', '-ja'],
104
- () => {
105
- cfg.j = 'always';
106
- },
107
- ],
108
- [
109
- ['--no-escape-caps', '-nec'],
110
- () => {
111
- cfg.doEscapeCapitalized = false;
112
- },
113
- ],
114
- [
115
- ['--h'],
116
- () => {
117
- cfg.g = false;
118
- },
119
- ],
120
- [
121
- ['--no-variations', '-nv'],
122
- () => {
123
- cfg.variations = 'no';
124
- },
125
- ],
126
- [
127
- ['--first-variation', '-fv'],
128
- () => {
129
- cfg.variations = 'first';
130
- },
131
- ],
132
- [
133
- ['--no-color', '-nc'],
134
- () => {
135
- cfg.wrappers = null;
136
- },
137
- ],
138
- [
139
- ['--html', '-html'],
140
- () => {
141
- isHtml = true;
142
- cfg.wrappers = htmlConfigOptions.wrappers;
143
- },
144
- ],
145
- [
146
- ['--alphabet-only', '-abc'],
147
- () => {
148
- mode = 'alphabetic';
149
- },
150
- ],
151
- [
152
- ['--phonetic', '-ph'],
153
- () => {
154
- mode = 'phonetic';
155
- },
156
- ],
157
- ]);
158
- let currOption;
159
- process.argv.reverse();
160
- while ((currOption = process.argv.pop())) {
161
- if (currOption in optionDict) {
162
- optionDict[currOption]();
163
- }
164
- else {
165
- process.argv.push(currOption);
166
- break;
167
- }
168
- }
169
- let text = '';
170
- if (process.argv.length) {
171
- text = process.argv.reverse().join(' ');
172
- }
173
- else {
174
- const chunks = [];
175
- let length = 0;
176
- if (process.stdin.isTTY) {
177
- printWithPrefix('Enter the text');
178
- for await (const chunk of process.stdin) {
179
- chunks.push(chunk);
180
- length += chunk.length;
181
- if (chunk.includes('\n'))
182
- break;
183
- }
184
- }
185
- else {
186
- for await (const chunk of process.stdin) {
187
- chunks.push(chunk);
188
- length += chunk.length;
189
- }
190
- }
191
- text = Buffer.concat(chunks, length).toString();
192
- }
193
- cfg = new TaraskConfig(isHtml
194
- ? {
195
- ...htmlConfigOptions,
196
- ...cfg,
197
- }
198
- : cfg);
199
- if (process.stdout.write(pipelines[mode](text, cfg) + '\n')) {
200
- process.exit(0);
201
- }
202
- else {
203
- process.stdout.once('drain', () => {
204
- process.exit(0);
205
- });
206
- }
File without changes