mrkr 0.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/LICENSE +21 -0
- package/README.md +111 -0
- package/dist/index.cjs +162 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +122 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Xvezda <xvezda@naver.com>
|
|
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,111 @@
|
|
|
1
|
+
# mrkr
|
|
2
|
+
|
|
3
|
+
`mrkr` is a lightweight TypeScript library providing a `markable` template tag. It allows you to create strings with embedded markers to easily track positions, compose strings, and split them at marked locations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Create strings with markers using a simple template literal syntax.
|
|
8
|
+
- Automatically tracks the `start` and `end` positions of markers.
|
|
9
|
+
- Compose `markable` strings together, preserving marker information.
|
|
10
|
+
- Use `markRef` to create references to specific markers and find their offsets.
|
|
11
|
+
- Split strings into parts using the `cut()` method.
|
|
12
|
+
- Fully typed with TypeScript.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
Install the dependencies:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pnpm install
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Basic Marking
|
|
25
|
+
|
|
26
|
+
Import `markable` and `mark` to create your first marked string. The `mark` symbol indicates where a marker should be placed.
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { markable, mark as v } from 'mrkr';
|
|
30
|
+
|
|
31
|
+
const str = markable`Hello, ${v}world!`;
|
|
32
|
+
|
|
33
|
+
console.log(str.toString()); // "Hello, world!"
|
|
34
|
+
|
|
35
|
+
// Get the position of the first and last marks
|
|
36
|
+
console.log(str.start); // 7
|
|
37
|
+
console.log(str.end); // 7
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Multiple Markers and `cut()`
|
|
41
|
+
|
|
42
|
+
You can place multiple marks. The `cut()` method will split the string at each marker.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { markable, mark as v } from 'mrkr';
|
|
46
|
+
|
|
47
|
+
const str = markable`foo${v}bar${v}baz`;
|
|
48
|
+
|
|
49
|
+
console.log(str.cut()); // ['foo', 'bar', 'baz']
|
|
50
|
+
console.log(str.start); // 3
|
|
51
|
+
console.log(str.end); // 6
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Composition
|
|
55
|
+
|
|
56
|
+
`markable` strings can be nested and composed seamlessly.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { markable, mark as v } from 'mrkr';
|
|
60
|
+
|
|
61
|
+
const foo = markable`fo${v}o`;
|
|
62
|
+
const bar = markable`b${v}ar`;
|
|
63
|
+
const foobar = markable`${foo}${v}${bar}`;
|
|
64
|
+
|
|
65
|
+
console.log(foobar.toString()); // "foobar"
|
|
66
|
+
console.log(foobar.start); // 2 (from foo)
|
|
67
|
+
console.log(foobar.end); // 4 (from bar)
|
|
68
|
+
|
|
69
|
+
// .cut() splits at all nested and new marks
|
|
70
|
+
console.log(foobar.cut()); // ['fo', 'o', 'b', 'ar']
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### `markRef` for Tagged Markers
|
|
74
|
+
|
|
75
|
+
For more advanced control, `markRef` allows you to create a reference to a marker. This reference can be used to find the offset of that specific marker, even in composed strings.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { markable, markRef } from 'mrkr';
|
|
79
|
+
|
|
80
|
+
const nameRef = markRef();
|
|
81
|
+
const greeting = markable`Hello, ${nameRef}John Doe!`;
|
|
82
|
+
|
|
83
|
+
// Find the position of the marker using the reference
|
|
84
|
+
const namePosition = nameRef.offsetFrom(greeting);
|
|
85
|
+
// You can also get it from the string instance
|
|
86
|
+
// const namePosition = greeting.offsetOf(nameRef);
|
|
87
|
+
|
|
88
|
+
console.log(namePosition); // 7
|
|
89
|
+
console.log(greeting.substring(greeting.start)); // "John Doe!"
|
|
90
|
+
|
|
91
|
+
// The reference works across compositions
|
|
92
|
+
const announcement = markable`Attention! ${greeting}. Welcome.`;
|
|
93
|
+
const namePositionInAnnouncement = nameRef.offsetFrom(announcement);
|
|
94
|
+
|
|
95
|
+
console.log(namePositionInAnnouncement); // 18
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Development
|
|
99
|
+
|
|
100
|
+
Build the library:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
pnpm run build
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Build the library in watch mode:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
pnpm run dev
|
|
110
|
+
```
|
|
111
|
+
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: definition[key]
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
})();
|
|
11
|
+
(()=>{
|
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
13
|
+
})();
|
|
14
|
+
(()=>{
|
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
|
16
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
17
|
+
value: 'Module'
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
20
|
+
value: true
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
})();
|
|
24
|
+
var __webpack_exports__ = {};
|
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
mark: ()=>mark,
|
|
28
|
+
markRef: ()=>markRef,
|
|
29
|
+
markable: ()=>markable
|
|
30
|
+
});
|
|
31
|
+
const mark = Symbol('mark');
|
|
32
|
+
class MarkRef {
|
|
33
|
+
references = new Set();
|
|
34
|
+
addReference(markable) {
|
|
35
|
+
this.references.add(markable);
|
|
36
|
+
}
|
|
37
|
+
offsetFrom(markable, from = 0) {
|
|
38
|
+
return markable.offsetOf(this, from);
|
|
39
|
+
}
|
|
40
|
+
lastOffsetFrom(markable, from) {
|
|
41
|
+
return markable.lastOffsetOf(this, from);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function markRef() {
|
|
45
|
+
return new MarkRef();
|
|
46
|
+
}
|
|
47
|
+
class Markable extends String {
|
|
48
|
+
start = -1;
|
|
49
|
+
end = -1;
|
|
50
|
+
marks = [];
|
|
51
|
+
constructor(value){
|
|
52
|
+
super(value);
|
|
53
|
+
}
|
|
54
|
+
cut() {
|
|
55
|
+
if (0 === this.marks.length) return [
|
|
56
|
+
this.toString()
|
|
57
|
+
];
|
|
58
|
+
const parts = [];
|
|
59
|
+
let lastIndex = 0;
|
|
60
|
+
const addPart = (part)=>{
|
|
61
|
+
if (part) parts.push(part);
|
|
62
|
+
};
|
|
63
|
+
for (const mark of this.marks){
|
|
64
|
+
addPart(this.toString().slice(lastIndex, mark.offset));
|
|
65
|
+
lastIndex = mark.offset;
|
|
66
|
+
}
|
|
67
|
+
addPart(this.toString().slice(lastIndex));
|
|
68
|
+
return parts;
|
|
69
|
+
}
|
|
70
|
+
offsetOf(ref, from = 0) {
|
|
71
|
+
for (const mark of this.marks)if (mark.ref === ref && mark.offset >= from) return mark.offset;
|
|
72
|
+
return -1;
|
|
73
|
+
}
|
|
74
|
+
lastOffsetOf(ref, from) {
|
|
75
|
+
let lastOffset = -1;
|
|
76
|
+
for (const mark of this.marks)if (mark.ref === ref && (void 0 === from || mark.offset <= from)) lastOffset = mark.offset;
|
|
77
|
+
return lastOffset;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
class MarkableBuilder {
|
|
81
|
+
start = -1;
|
|
82
|
+
end = -1;
|
|
83
|
+
marks = [];
|
|
84
|
+
withStart(position) {
|
|
85
|
+
this.start = position;
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
withEnd(position) {
|
|
89
|
+
this.end = position;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
withMarks(marks) {
|
|
93
|
+
this.marks = marks;
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
build(value) {
|
|
97
|
+
const markable = new Markable(value);
|
|
98
|
+
markable.start = this.start;
|
|
99
|
+
markable.end = this.end;
|
|
100
|
+
markable.marks = this.marks;
|
|
101
|
+
return markable;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const markable = (strings, ...values)=>{
|
|
105
|
+
let string = '';
|
|
106
|
+
let start = -1;
|
|
107
|
+
let end = -1;
|
|
108
|
+
const marks = [];
|
|
109
|
+
const refs = [];
|
|
110
|
+
for(let i = 0; i < strings.length; i++){
|
|
111
|
+
string += strings[i];
|
|
112
|
+
if (i < values.length) if (values[i] === mark || values[i] instanceof MarkRef) {
|
|
113
|
+
if (-1 === start) start = string.length;
|
|
114
|
+
end = string.length;
|
|
115
|
+
if (values[i] instanceof MarkRef) {
|
|
116
|
+
refs.push(values[i]);
|
|
117
|
+
marks.push({
|
|
118
|
+
offset: string.length,
|
|
119
|
+
ref: values[i]
|
|
120
|
+
});
|
|
121
|
+
} else marks.push({
|
|
122
|
+
offset: string.length
|
|
123
|
+
});
|
|
124
|
+
} else if (values[i] instanceof Markable) {
|
|
125
|
+
const markableValue = values[i];
|
|
126
|
+
const offset = string.length;
|
|
127
|
+
string += markableValue.toString();
|
|
128
|
+
for (const m of markableValue.marks){
|
|
129
|
+
const markInfo = {
|
|
130
|
+
offset: m.offset + offset
|
|
131
|
+
};
|
|
132
|
+
if (m.ref) {
|
|
133
|
+
markInfo.ref = m.ref;
|
|
134
|
+
refs.push(m.ref);
|
|
135
|
+
}
|
|
136
|
+
marks.push(markInfo);
|
|
137
|
+
}
|
|
138
|
+
if (-1 !== markableValue.start) {
|
|
139
|
+
const absoluteStart = markableValue.start + offset;
|
|
140
|
+
if (-1 === start || absoluteStart < start) start = absoluteStart;
|
|
141
|
+
}
|
|
142
|
+
if (-1 !== markableValue.end) {
|
|
143
|
+
const absoluteEnd = markableValue.end + offset;
|
|
144
|
+
if (absoluteEnd > end) end = absoluteEnd;
|
|
145
|
+
}
|
|
146
|
+
} else string += String(values[i]);
|
|
147
|
+
}
|
|
148
|
+
const result = new MarkableBuilder().withStart(start).withEnd(end).withMarks(marks).build(string);
|
|
149
|
+
for (const ref of refs)ref.addReference(result);
|
|
150
|
+
return result;
|
|
151
|
+
};
|
|
152
|
+
exports.mark = __webpack_exports__.mark;
|
|
153
|
+
exports.markRef = __webpack_exports__.markRef;
|
|
154
|
+
exports.markable = __webpack_exports__.markable;
|
|
155
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
156
|
+
"mark",
|
|
157
|
+
"markRef",
|
|
158
|
+
"markable"
|
|
159
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
160
|
+
Object.defineProperty(exports, '__esModule', {
|
|
161
|
+
value: true
|
|
162
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export declare const mark: unique symbol;
|
|
2
|
+
export type Mark = typeof mark;
|
|
3
|
+
export type MarkInfo = {
|
|
4
|
+
offset: number;
|
|
5
|
+
ref?: MarkRef;
|
|
6
|
+
};
|
|
7
|
+
declare class MarkRef {
|
|
8
|
+
references: Set<Markable>;
|
|
9
|
+
addReference(markable: Markable): void;
|
|
10
|
+
offsetFrom(markable: Markable, from?: number): number;
|
|
11
|
+
lastOffsetFrom(markable: Markable, from?: number): number;
|
|
12
|
+
}
|
|
13
|
+
export declare function markRef(): MarkRef;
|
|
14
|
+
declare class Markable extends String {
|
|
15
|
+
start: number;
|
|
16
|
+
end: number;
|
|
17
|
+
marks: readonly MarkInfo[];
|
|
18
|
+
constructor(value: string);
|
|
19
|
+
cut(): string[];
|
|
20
|
+
offsetOf(ref: MarkRef, from?: number): number;
|
|
21
|
+
lastOffsetOf(ref: MarkRef, from?: number): number;
|
|
22
|
+
}
|
|
23
|
+
export declare const markable: (strings: TemplateStringsArray, ...values: any[]) => Markable;
|
|
24
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
const src_mark = Symbol('mark');
|
|
2
|
+
class MarkRef {
|
|
3
|
+
references = new Set();
|
|
4
|
+
addReference(markable) {
|
|
5
|
+
this.references.add(markable);
|
|
6
|
+
}
|
|
7
|
+
offsetFrom(markable, from = 0) {
|
|
8
|
+
return markable.offsetOf(this, from);
|
|
9
|
+
}
|
|
10
|
+
lastOffsetFrom(markable, from) {
|
|
11
|
+
return markable.lastOffsetOf(this, from);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function markRef() {
|
|
15
|
+
return new MarkRef();
|
|
16
|
+
}
|
|
17
|
+
class Markable extends String {
|
|
18
|
+
start = -1;
|
|
19
|
+
end = -1;
|
|
20
|
+
marks = [];
|
|
21
|
+
constructor(value){
|
|
22
|
+
super(value);
|
|
23
|
+
}
|
|
24
|
+
cut() {
|
|
25
|
+
if (0 === this.marks.length) return [
|
|
26
|
+
this.toString()
|
|
27
|
+
];
|
|
28
|
+
const parts = [];
|
|
29
|
+
let lastIndex = 0;
|
|
30
|
+
const addPart = (part)=>{
|
|
31
|
+
if (part) parts.push(part);
|
|
32
|
+
};
|
|
33
|
+
for (const mark of this.marks){
|
|
34
|
+
addPart(this.toString().slice(lastIndex, mark.offset));
|
|
35
|
+
lastIndex = mark.offset;
|
|
36
|
+
}
|
|
37
|
+
addPart(this.toString().slice(lastIndex));
|
|
38
|
+
return parts;
|
|
39
|
+
}
|
|
40
|
+
offsetOf(ref, from = 0) {
|
|
41
|
+
for (const mark of this.marks)if (mark.ref === ref && mark.offset >= from) return mark.offset;
|
|
42
|
+
return -1;
|
|
43
|
+
}
|
|
44
|
+
lastOffsetOf(ref, from) {
|
|
45
|
+
let lastOffset = -1;
|
|
46
|
+
for (const mark of this.marks)if (mark.ref === ref && (void 0 === from || mark.offset <= from)) lastOffset = mark.offset;
|
|
47
|
+
return lastOffset;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class MarkableBuilder {
|
|
51
|
+
start = -1;
|
|
52
|
+
end = -1;
|
|
53
|
+
marks = [];
|
|
54
|
+
withStart(position) {
|
|
55
|
+
this.start = position;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
withEnd(position) {
|
|
59
|
+
this.end = position;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
withMarks(marks) {
|
|
63
|
+
this.marks = marks;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
build(value) {
|
|
67
|
+
const markable = new Markable(value);
|
|
68
|
+
markable.start = this.start;
|
|
69
|
+
markable.end = this.end;
|
|
70
|
+
markable.marks = this.marks;
|
|
71
|
+
return markable;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const src_markable = (strings, ...values)=>{
|
|
75
|
+
let string = '';
|
|
76
|
+
let start = -1;
|
|
77
|
+
let end = -1;
|
|
78
|
+
const marks = [];
|
|
79
|
+
const refs = [];
|
|
80
|
+
for(let i = 0; i < strings.length; i++){
|
|
81
|
+
string += strings[i];
|
|
82
|
+
if (i < values.length) if (values[i] === src_mark || values[i] instanceof MarkRef) {
|
|
83
|
+
if (-1 === start) start = string.length;
|
|
84
|
+
end = string.length;
|
|
85
|
+
if (values[i] instanceof MarkRef) {
|
|
86
|
+
refs.push(values[i]);
|
|
87
|
+
marks.push({
|
|
88
|
+
offset: string.length,
|
|
89
|
+
ref: values[i]
|
|
90
|
+
});
|
|
91
|
+
} else marks.push({
|
|
92
|
+
offset: string.length
|
|
93
|
+
});
|
|
94
|
+
} else if (values[i] instanceof Markable) {
|
|
95
|
+
const markableValue = values[i];
|
|
96
|
+
const offset = string.length;
|
|
97
|
+
string += markableValue.toString();
|
|
98
|
+
for (const m of markableValue.marks){
|
|
99
|
+
const markInfo = {
|
|
100
|
+
offset: m.offset + offset
|
|
101
|
+
};
|
|
102
|
+
if (m.ref) {
|
|
103
|
+
markInfo.ref = m.ref;
|
|
104
|
+
refs.push(m.ref);
|
|
105
|
+
}
|
|
106
|
+
marks.push(markInfo);
|
|
107
|
+
}
|
|
108
|
+
if (-1 !== markableValue.start) {
|
|
109
|
+
const absoluteStart = markableValue.start + offset;
|
|
110
|
+
if (-1 === start || absoluteStart < start) start = absoluteStart;
|
|
111
|
+
}
|
|
112
|
+
if (-1 !== markableValue.end) {
|
|
113
|
+
const absoluteEnd = markableValue.end + offset;
|
|
114
|
+
if (absoluteEnd > end) end = absoluteEnd;
|
|
115
|
+
}
|
|
116
|
+
} else string += String(values[i]);
|
|
117
|
+
}
|
|
118
|
+
const result = new MarkableBuilder().withStart(start).withEnd(end).withMarks(marks).build(string);
|
|
119
|
+
for (const ref of refs)ref.addReference(result);
|
|
120
|
+
return result;
|
|
121
|
+
};
|
|
122
|
+
export { markRef, src_mark as mark, src_markable as markable };
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mrkr",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"author": "Xvezda <xvezda@naver.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/index.cjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@rslib/core": "^0.19.3",
|
|
21
|
+
"@rstest/core": "^0.8.0",
|
|
22
|
+
"@types/node": "^24.10.9",
|
|
23
|
+
"typescript": "^5.9.3"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rslib build",
|
|
27
|
+
"dev": "rslib build --watch",
|
|
28
|
+
"test": "rstest"
|
|
29
|
+
}
|
|
30
|
+
}
|