@vnejs/helpers.tokenize-exec-line 0.1.1 → 0.1.2

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 @@
1
+ export declare const tokenizeExecLine: (text: string) => (string | number | boolean)[];
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ const NUMBER_REGEXP = /^-?\d+(\.\d+)?$/;
2
+ const TOKEN_INSIDE_START_CHARS = ["{", "(", "[", '"', "'", "`"];
3
+ const TOKEN_INSIDE_END_CHARS = { "{": "}", "(": ")", "[": "]" };
4
+ const fromStrToCorrectType = (token) => {
5
+ if (token === "true")
6
+ return true;
7
+ if (token === "false")
8
+ return false;
9
+ if (NUMBER_REGEXP.test(token))
10
+ return token.includes(".") ? parseFloat(token) : parseInt(token, 10);
11
+ return token;
12
+ };
13
+ const toLength = (q) => q.length;
14
+ const reduceTokenize = (acc, char) => {
15
+ if (acc.isInsideToken) {
16
+ if (char !== acc.tokenEndChar)
17
+ acc.array[acc.array.length - 1] += char;
18
+ else {
19
+ acc.array.push("");
20
+ acc.isInsideToken = false;
21
+ }
22
+ return acc;
23
+ }
24
+ if (TOKEN_INSIDE_START_CHARS.includes(char)) {
25
+ acc.array.push("");
26
+ acc.isInsideToken = true;
27
+ acc.tokenEndChar = TOKEN_INSIDE_END_CHARS[char] || char;
28
+ return acc;
29
+ }
30
+ if (char === " ")
31
+ acc.array.push("");
32
+ else
33
+ acc.array[acc.array.length - 1] += char;
34
+ return acc;
35
+ };
36
+ export const tokenizeExecLine = (text) => [...text]
37
+ .reduce(reduceTokenize, { array: [""] })
38
+ .array.filter(toLength)
39
+ .map(fromStrToCorrectType);
package/package.json CHANGED
@@ -1,17 +1,25 @@
1
1
  {
2
2
  "name": "@vnejs/helpers.tokenize-exec-line",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
6
10
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1",
11
+ "test": "npm run build && node tests.js",
12
+ "build": "rm -rf dist && tsc -p tsconfig.json",
8
13
  "publish:major:plugin": "npm run publish:major",
9
14
  "publish:minor:plugin": "npm run publish:minor",
10
15
  "publish:patch:plugin": "npm run publish:patch",
11
- "publish:major": "npm version major && npm publish --access public",
12
- "publish:minor": "npm version minor && npm publish --access public",
13
- "publish:patch": "npm version patch && npm publish --access public"
16
+ "publish:major": "npm run build && npm version major && npm publish --access public",
17
+ "publish:minor": "npm run build && npm version minor && npm publish --access public",
18
+ "publish:patch": "npm run build && npm version patch && npm publish --access public"
14
19
  },
15
20
  "author": "",
16
- "license": "ISC"
21
+ "license": "ISC",
22
+ "devDependencies": {
23
+ "typescript": "^6.0.3"
24
+ }
17
25
  }
package/index.js DELETED
@@ -1,43 +0,0 @@
1
- const NUMBER_REGEXP = /^-?\d+(\.\d+)?$/;
2
-
3
- const TOKEN_INSIDE_START_CHARS = ["{", "(", "[", '"', "'", "`"];
4
- const TOKEN_INSIDE_END_CHARS = { "{": "}", "(": ")", "[": "]" };
5
-
6
- const fromStrToCorrectType = (token) => {
7
- if (token === "true") return true;
8
- if (token === "false") return false;
9
- if (NUMBER_REGEXP.test(token)) return token.includes(".") ? parseFloat(token) : parseInt(token, 10);
10
- return token;
11
- };
12
-
13
- const toLength = (q) => q.length;
14
-
15
- const reduceTokenize = (acc, char) => {
16
- if (acc.isInsideToken) {
17
- if (char !== acc.tokenEndChar) acc.array[acc.array.length - 1] += char;
18
- else {
19
- acc.array.push("");
20
- acc.isInsideToken = false;
21
- }
22
-
23
- return acc;
24
- }
25
-
26
- if (TOKEN_INSIDE_START_CHARS.includes(char)) {
27
- acc.array.push("");
28
- acc.isInsideToken = true;
29
- acc.tokenEndChar = TOKEN_INSIDE_END_CHARS[char] || char;
30
- return acc;
31
- }
32
-
33
- if (char === " ") acc.array.push("");
34
- else acc.array[acc.array.length - 1] += char;
35
-
36
- return acc;
37
- };
38
-
39
- export const tokenizeExecLine = (text) =>
40
- [...text]
41
- .reduce(reduceTokenize, { array: [""] })
42
- .array.filter(toLength)
43
- .map(fromStrToCorrectType);
package/tests.js DELETED
@@ -1,18 +0,0 @@
1
- import { tokenizeExecLine } from "./index.js";
2
-
3
- const tests = [
4
- "{Confusion} 500:500:500",
5
- "{Confusion} 500 500 500",
6
- "co change {co/hand_on_shoulder} 300",
7
- '"Открыть глаза" 50 50',
8
- `'Открыть глаза' "Открыть глаза" 50 50`,
9
- 'fl "Прикрой её."',
10
- `fl 'Прикрой её.'`,
11
- "{true} {123.45} abc",
12
- "a{b}c",
13
- " true false 42 ",
14
- ];
15
-
16
- tests.forEach((test) => {
17
- console.log(`${JSON.stringify(test)} ->`, tokenizeExecLine(test));
18
- });