muggle-string 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-present Johnson Chu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ This library aims to provide codegen helpers and data structure for Vue language plugin API v1.x that does not depend on Volar runtime.
2
+
3
+ ## Why "Muggle"?
4
+
5
+ A better situation is Volar can use magic-string on the Vue plugin API, but currently can't do this easily.
6
+
7
+ This solution is just for Vue language plugin API v1.x and planned to be deprecate in v2.
8
+
9
+ ## Usage
10
+
11
+ The example is base-on [magic-string readme](https://github.com/rich-harris/magic-string#usage).
12
+
13
+ ```html
14
+ <script>problems = 99</script>
15
+
16
+ <more-script lang="js">console.log( answer )</more-script>
17
+ ```
18
+
19
+ ```ts
20
+ import {
21
+ toString,
22
+ overwrite, // not yet support
23
+ } from 'muggle-string';
24
+
25
+ /** @type {import('@volar/vue-language-core').VueLanguagePlugin} */
26
+ const plugin = () => {
27
+ return {
28
+ resolveEmbeddedFile(fileName, sfc, embeddedFile) {
29
+ if (embeddedFile.fileName.replace(fileName, '').match(/^\.(js|ts|jsx|tsx)$/)) {
30
+
31
+ const s = embeddedFile.content;
32
+ toString(s); // 'problems = 99'
33
+
34
+ overwrite(s, 0, 8, 'answer'); // not yet support
35
+ toString(s); // 'answer = 99'
36
+
37
+ overwrite(s, 11, 13, '42'); // not yet support
38
+ toString(s); // 'answer = 42'
39
+
40
+ // add string by Array method directly
41
+ s.unshift('var ');
42
+ s.push(';');
43
+ toString(s); // 'var answer = 42;'
44
+
45
+ for (const block of sfc.customBlocks) {
46
+ if (block.type === 'more-script') {
47
+ s.push([
48
+ block.content, // text to add
49
+ block.name, // source
50
+ 0, // content offset in source
51
+ {
52
+ // langauge capabilities to enable in this segment
53
+ hover: true,
54
+ references: true,
55
+ definition: true,
56
+ diagnostic: true,
57
+ rename: true,
58
+ completion: true,
59
+ semanticTokens: true,
60
+ },
61
+ ]);
62
+ toString(s); // 'var answer = 42;console.log( answer )'
63
+ }
64
+ }
65
+ }
66
+ }
67
+ };
68
+ };
69
+ module.exports = plugin;
70
+ ```
package/out/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ export declare type Segment<T = '--'> = T extends '--' ? SegmentWithoutData : SegmentWithData<T>;
2
+ export declare type SegmentWithoutData = [
3
+ string,
4
+ // text
5
+ string | undefined,
6
+ // source
7
+ number | [number, number]
8
+ ] | string;
9
+ export declare type SegmentWithData<T> = [
10
+ string,
11
+ // text
12
+ string | undefined,
13
+ // source
14
+ number | [number, number],
15
+ T
16
+ ] | string;
17
+ export declare function getLength(segments: Segment<any>[]): number;
18
+ export declare function toString(segments: Segment<any>[]): string;
package/out/index.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toString = exports.getLength = void 0;
4
+ function getLength(segments) {
5
+ let length = 0;
6
+ for (const segment of segments) {
7
+ length += typeof segment == 'string' ? segment.length : segment[0].length;
8
+ }
9
+ return length;
10
+ }
11
+ exports.getLength = getLength;
12
+ function toString(segments) {
13
+ return segments.map(s => typeof s === 'string' ? s : s[0]).join('');
14
+ }
15
+ exports.toString = toString;
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "muggle-string",
3
+ "version": "0.1.0",
4
+ "main": "out/index.js",
5
+ "license": "MIT",
6
+ "files": [
7
+ "out/**/*.js",
8
+ "out/**/*.d.ts"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/johnsoncodehk/muggle-string.git"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "latest"
16
+ }
17
+ }