@flighthq/textsegment 0.5.1-next.973.788d3d7 → 0.6.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/README.md +2 -2
- package/dist/.tsbuildinfo.contract +1 -0
- package/dist/.tsbuildinfo.runtime +1 -0
- package/dist/contract.d.ts +7 -3
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js +8 -3
- package/dist/contract.js.map +1 -1
- package/dist/contract.textsegment-types.d.ts +2 -0
- package/dist/contract.textsegment-types.d.ts.map +1 -0
- package/dist/contract.textsegment-types.js +4 -0
- package/dist/contract.textsegment-types.js.map +1 -0
- package/dist/enableTextSegmentGuards.d.ts +6 -0
- package/dist/enableTextSegmentGuards.d.ts.map +1 -0
- package/dist/enableTextSegmentGuards.js +16 -0
- package/dist/enableTextSegmentGuards.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/textSegment.d.ts +4 -4
- package/dist/textSegment.d.ts.map +1 -1
- package/dist/textSegment.js +21 -16
- package/dist/textSegment.js.map +1 -1
- package/dist/textSegmentBoundary.d.ts +8 -6
- package/dist/textSegmentBoundary.d.ts.map +1 -1
- package/dist/textSegmentBoundary.js +87 -28
- package/dist/textSegmentBoundary.js.map +1 -1
- package/dist/textSegmentGuards.d.ts +4 -0
- package/dist/textSegmentGuards.d.ts.map +1 -0
- package/dist/textSegmentGuards.js +8 -0
- package/dist/textSegmentGuards.js.map +1 -0
- package/dist/textSegmenterBackend.d.ts +4 -13
- package/dist/textSegmenterBackend.d.ts.map +1 -1
- package/dist/textSegmenterBackend.js +25 -25
- package/dist/textSegmenterBackend.js.map +1 -1
- package/dist/types.d.ts +38 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +30 -0
- package/dist/types.js.map +1 -0
- package/package.json +6 -4
- package/src/enableTextSegmentGuards.test.ts +51 -0
- package/src/textSegment.test.ts +41 -39
- package/src/textSegmentBoundary.test.ts +84 -44
- package/src/textSegmentConformance.test.ts +24 -0
- package/src/textSegmentGuards.test.ts +21 -0
- package/src/textSegmenterBackend.test.ts +83 -74
- package/src/types.test.ts +52 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { reportTextSegmenterUnavailable, setTextSegmentGuard } from './textSegmentGuards.ts';
|
|
2
|
+
|
|
3
|
+
afterEach(() => setTextSegmentGuard(null));
|
|
4
|
+
|
|
5
|
+
describe('reportTextSegmenterUnavailable', () => {
|
|
6
|
+
it('is silent when no guard is installed', () => {
|
|
7
|
+
expect(() => reportTextSegmenterUnavailable()).not.toThrow();
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('setTextSegmentGuard', () => {
|
|
12
|
+
it('replaces the active callback', () => {
|
|
13
|
+
let first = 0;
|
|
14
|
+
let second = 0;
|
|
15
|
+
setTextSegmentGuard(() => first++);
|
|
16
|
+
setTextSegmentGuard(() => second++);
|
|
17
|
+
reportTextSegmenterUnavailable();
|
|
18
|
+
expect(first).toBe(0);
|
|
19
|
+
expect(second).toBe(1);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
@@ -1,104 +1,92 @@
|
|
|
1
|
-
import { allocateEntity
|
|
2
|
-
import type { EntityRuntimeKey } from '@flighthq/
|
|
3
|
-
import
|
|
4
|
-
HasTextSegmenter,
|
|
5
|
-
TextSegment,
|
|
6
|
-
TextSegmentGranularity,
|
|
7
|
-
TextSegmenterBackend,
|
|
8
|
-
} from '@flighthq/types/contract';
|
|
1
|
+
import { allocateEntity } from '@flighthq/entity/contract';
|
|
2
|
+
import type { EntityRuntimeKey } from '@flighthq/entity/contract';
|
|
3
|
+
import { vi } from 'vitest';
|
|
9
4
|
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
getTextSegmenterBackend,
|
|
13
|
-
initializeWebTextSegmenterBackend,
|
|
14
|
-
setTextSegmenterBackend,
|
|
15
|
-
webTextSegmenterBackend,
|
|
16
|
-
} from './textSegmenterBackend';
|
|
5
|
+
import { explainTextSegmenterBackend, webTextSegmenterBackend } from './textSegmenterBackend.ts';
|
|
6
|
+
import type { HostTextSegmenterCapability, TextSegment, TextSegmentGranularity } from './types.ts';
|
|
17
7
|
|
|
18
|
-
interface RecordingBackend extends
|
|
8
|
+
interface RecordingBackend extends HostTextSegmenterCapability {
|
|
19
9
|
calls: Array<{ text: string; granularity: TextSegmentGranularity; locale: string | undefined }>;
|
|
20
10
|
}
|
|
21
11
|
|
|
22
12
|
function recordingBackend(): RecordingBackend {
|
|
23
13
|
const calls: RecordingBackend['calls'] = [];
|
|
24
|
-
const out = allocateEntity<any>();
|
|
14
|
+
const out = allocateEntity<any>() as any;
|
|
25
15
|
out.calls = calls;
|
|
26
|
-
out.segment = (text: string, granularity: TextSegmentGranularity, locale?: string):
|
|
16
|
+
out.segment = (text: string, granularity: TextSegmentGranularity, locale?: string): TextSegment[] => {
|
|
27
17
|
calls.push({ text, granularity, locale });
|
|
28
18
|
return [{ start: 0, end: text.length, text }];
|
|
29
19
|
};
|
|
30
|
-
return
|
|
20
|
+
return out;
|
|
31
21
|
}
|
|
32
22
|
|
|
33
|
-
afterEach(() =>
|
|
34
|
-
|
|
35
|
-
describe('createWebTextSegmenterBackend', () => {
|
|
36
|
-
it('segments a ZWJ family emoji as a single grapheme', () => {
|
|
37
|
-
const segments = createWebTextSegmenterBackend().segment('a👨👩👧b', 'grapheme');
|
|
38
|
-
expect(segments.map((s) => s.text)).toEqual(['a', '👨👩👧', 'b']);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('reports isWordLike for word granularity and omits it otherwise', () => {
|
|
42
|
-
const backend = createWebTextSegmenterBackend();
|
|
43
|
-
const words = backend.segment('Hi there', 'word');
|
|
44
|
-
expect(words.every((s) => typeof s.isWordLike === 'boolean')).toBe(true);
|
|
45
|
-
const graphemes = backend.segment('Hi', 'grapheme');
|
|
46
|
-
expect(graphemes.every((s) => s.isWordLike === undefined)).toBe(true);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('produces gap-free start/end offsets covering the whole string', () => {
|
|
50
|
-
const segments = createWebTextSegmenterBackend().segment('abc', 'grapheme');
|
|
51
|
-
expect(segments).toEqual([
|
|
52
|
-
{ start: 0, end: 1, text: 'a' },
|
|
53
|
-
{ start: 1, end: 2, text: 'b' },
|
|
54
|
-
{ start: 2, end: 3, text: 'c' },
|
|
55
|
-
]);
|
|
56
|
-
});
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
vi.unstubAllGlobals();
|
|
57
25
|
});
|
|
58
26
|
|
|
59
|
-
describe('
|
|
60
|
-
it('
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
27
|
+
describe('explainTextSegmenterBackend', () => {
|
|
28
|
+
it('reports the bundled Intl capability as available', () => {
|
|
29
|
+
expect(explainTextSegmenterBackend(webTextSegmenterBackend)).toEqual({
|
|
30
|
+
available: true,
|
|
31
|
+
backend: 'web-intl',
|
|
32
|
+
intlSegmenterAvailable: true,
|
|
33
|
+
});
|
|
66
34
|
});
|
|
67
35
|
|
|
68
|
-
it('
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
36
|
+
it('classifies a copied bundled capability by its segment function', () => {
|
|
37
|
+
const copied = { ...webTextSegmenterBackend };
|
|
38
|
+
const wrapped = {} as HostTextSegmenterCapability;
|
|
39
|
+
wrapped.segment = webTextSegmenterBackend.segment;
|
|
40
|
+
expect(explainTextSegmenterBackend(copied)).toEqual({
|
|
41
|
+
available: true,
|
|
42
|
+
backend: 'web-intl',
|
|
43
|
+
intlSegmenterAvailable: true,
|
|
44
|
+
});
|
|
45
|
+
expect(explainTextSegmenterBackend(wrapped)).toEqual({
|
|
46
|
+
available: true,
|
|
47
|
+
backend: 'web-intl',
|
|
48
|
+
intlSegmenterAvailable: true,
|
|
49
|
+
});
|
|
72
50
|
});
|
|
73
51
|
|
|
74
|
-
it('
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
52
|
+
it('identifies a capability with a distinct segment function as custom even when it delegates to the bundled one', () => {
|
|
53
|
+
const delegating = {} as HostTextSegmenterCapability;
|
|
54
|
+
delegating.segment = (text: string, granularity: TextSegmentGranularity, locale?: string): readonly TextSegment[] =>
|
|
55
|
+
webTextSegmenterBackend.segment(text, granularity, locale);
|
|
56
|
+
expect(explainTextSegmenterBackend(delegating)).toEqual({
|
|
57
|
+
available: true,
|
|
58
|
+
backend: 'custom',
|
|
59
|
+
intlSegmenterAvailable: true,
|
|
60
|
+
});
|
|
78
61
|
});
|
|
79
|
-
});
|
|
80
62
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
63
|
+
it('identifies a custom capability independently of Intl availability', () => {
|
|
64
|
+
expect(explainTextSegmenterBackend(recordingBackend())).toEqual({
|
|
65
|
+
available: true,
|
|
66
|
+
backend: 'custom',
|
|
67
|
+
intlSegmenterAvailable: true,
|
|
68
|
+
});
|
|
84
69
|
});
|
|
85
|
-
});
|
|
86
70
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
71
|
+
it('reports the bundled capability unavailable when the runtime primitive is absent', () => {
|
|
72
|
+
vi.stubGlobal('Intl', { Segmenter: undefined });
|
|
73
|
+
expect(explainTextSegmenterBackend(webTextSegmenterBackend)).toEqual({
|
|
74
|
+
available: false,
|
|
75
|
+
backend: 'web-intl',
|
|
76
|
+
intlSegmenterAvailable: false,
|
|
77
|
+
});
|
|
93
78
|
});
|
|
94
79
|
|
|
95
|
-
it('
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
80
|
+
it('reports a copied bundled capability unavailable when the runtime primitive is absent', () => {
|
|
81
|
+
vi.stubGlobal('Intl', { Segmenter: undefined });
|
|
82
|
+
expect(explainTextSegmenterBackend({ ...webTextSegmenterBackend })).toEqual({
|
|
83
|
+
available: false,
|
|
84
|
+
backend: 'web-intl',
|
|
85
|
+
intlSegmenterAvailable: false,
|
|
86
|
+
});
|
|
100
87
|
});
|
|
101
88
|
});
|
|
89
|
+
|
|
102
90
|
describe('webTextSegmenterBackend', () => {
|
|
103
91
|
it('provides the stable bundled Intl fallback', () => {
|
|
104
92
|
expect(webTextSegmenterBackend.segment('a👨👩👧b', 'grapheme').map((segment) => segment.text)).toEqual([
|
|
@@ -107,4 +95,25 @@ describe('webTextSegmenterBackend', () => {
|
|
|
107
95
|
'b',
|
|
108
96
|
]);
|
|
109
97
|
});
|
|
98
|
+
|
|
99
|
+
it('segments a ZWJ family emoji as a single grapheme', () => {
|
|
100
|
+
const segments = webTextSegmenterBackend.segment('a👨👩👧b', 'grapheme');
|
|
101
|
+
expect(segments.map((s: TextSegment) => s.text)).toEqual(['a', '👨👩👧', 'b']);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('reports isWordLike for word granularity and omits it otherwise', () => {
|
|
105
|
+
const words = webTextSegmenterBackend.segment('Hi there', 'word');
|
|
106
|
+
expect(words.every((s: TextSegment) => typeof s.isWordLike === 'boolean')).toBe(true);
|
|
107
|
+
const graphemes = webTextSegmenterBackend.segment('Hi', 'grapheme');
|
|
108
|
+
expect(graphemes.every((s: TextSegment) => s.isWordLike === undefined)).toBe(true);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('produces gap-free start/end offsets covering the whole string', () => {
|
|
112
|
+
const segments = webTextSegmenterBackend.segment('abc', 'grapheme');
|
|
113
|
+
expect(segments).toEqual([
|
|
114
|
+
{ start: 0, end: 1, text: 'a' },
|
|
115
|
+
{ start: 1, end: 2, text: 'b' },
|
|
116
|
+
{ start: 2, end: 3, text: 'c' },
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
110
119
|
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Generated from crates/flight-textsegment/tests/types_test.rs by flight-compiler.
|
|
2
|
+
// Rust is authoritative; run `npm run generate` after changing it.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
getTextSegmentGranularityString,
|
|
6
|
+
getTextSegmenterKindString,
|
|
7
|
+
parseTextSegmentGranularity,
|
|
8
|
+
parseTextSegmenterKind,
|
|
9
|
+
} from '@flighthq/textsegment/contract';
|
|
10
|
+
import { describe, expect, it } from 'vitest';
|
|
11
|
+
|
|
12
|
+
import { TextSegmentGranularity, TextSegmenterKind } from './contract.ts';
|
|
13
|
+
|
|
14
|
+
describe('getTextSegmenterKindString', () => {
|
|
15
|
+
it('returns the exact ts strings', () => {
|
|
16
|
+
expect(getTextSegmenterKindString(TextSegmenterKind.Custom)).toEqual('custom');
|
|
17
|
+
expect(getTextSegmenterKindString(TextSegmenterKind.WebIntl)).toEqual('web-intl');
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('getTextSegmentGranularityString', () => {
|
|
22
|
+
it('returns the exact ts strings', () => {
|
|
23
|
+
expect(getTextSegmentGranularityString(TextSegmentGranularity.Grapheme)).toEqual('grapheme');
|
|
24
|
+
expect(getTextSegmentGranularityString(TextSegmentGranularity.Word)).toEqual('word');
|
|
25
|
+
expect(getTextSegmentGranularityString(TextSegmentGranularity.Sentence)).toEqual('sentence');
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('parseTextSegmenterKind', () => {
|
|
30
|
+
it('parses the exact ts strings', () => {
|
|
31
|
+
expect(parseTextSegmenterKind('custom')).toEqual(TextSegmenterKind.Custom);
|
|
32
|
+
expect(parseTextSegmenterKind('web-intl')).toEqual(TextSegmenterKind.WebIntl);
|
|
33
|
+
});
|
|
34
|
+
it('rejects other text and wrong casing', () => {
|
|
35
|
+
expect(parseTextSegmenterKind('Custom')).toEqual(undefined);
|
|
36
|
+
expect(parseTextSegmenterKind('web_intl')).toEqual(undefined);
|
|
37
|
+
expect(parseTextSegmenterKind('')).toEqual(undefined);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
describe('parseTextSegmentGranularity', () => {
|
|
42
|
+
it('parses the exact ts strings', () => {
|
|
43
|
+
expect(parseTextSegmentGranularity('grapheme')).toEqual(TextSegmentGranularity.Grapheme);
|
|
44
|
+
expect(parseTextSegmentGranularity('word')).toEqual(TextSegmentGranularity.Word);
|
|
45
|
+
expect(parseTextSegmentGranularity('sentence')).toEqual(TextSegmentGranularity.Sentence);
|
|
46
|
+
});
|
|
47
|
+
it('rejects other text and wrong casing', () => {
|
|
48
|
+
expect(parseTextSegmentGranularity('Grapheme')).toEqual(undefined);
|
|
49
|
+
expect(parseTextSegmentGranularity('line')).toEqual(undefined);
|
|
50
|
+
expect(parseTextSegmentGranularity('')).toEqual(undefined);
|
|
51
|
+
});
|
|
52
|
+
});
|