@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.
Files changed (47) hide show
  1. package/README.md +2 -2
  2. package/dist/.tsbuildinfo.contract +1 -0
  3. package/dist/.tsbuildinfo.runtime +1 -0
  4. package/dist/contract.d.ts +7 -3
  5. package/dist/contract.d.ts.map +1 -1
  6. package/dist/contract.js +8 -3
  7. package/dist/contract.js.map +1 -1
  8. package/dist/contract.textsegment-types.d.ts +2 -0
  9. package/dist/contract.textsegment-types.d.ts.map +1 -0
  10. package/dist/contract.textsegment-types.js +4 -0
  11. package/dist/contract.textsegment-types.js.map +1 -0
  12. package/dist/enableTextSegmentGuards.d.ts +6 -0
  13. package/dist/enableTextSegmentGuards.d.ts.map +1 -0
  14. package/dist/enableTextSegmentGuards.js +16 -0
  15. package/dist/enableTextSegmentGuards.js.map +1 -0
  16. package/dist/index.d.ts +2 -1
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +3 -1
  19. package/dist/index.js.map +1 -1
  20. package/dist/textSegment.d.ts +4 -4
  21. package/dist/textSegment.d.ts.map +1 -1
  22. package/dist/textSegment.js +21 -16
  23. package/dist/textSegment.js.map +1 -1
  24. package/dist/textSegmentBoundary.d.ts +8 -6
  25. package/dist/textSegmentBoundary.d.ts.map +1 -1
  26. package/dist/textSegmentBoundary.js +87 -28
  27. package/dist/textSegmentBoundary.js.map +1 -1
  28. package/dist/textSegmentGuards.d.ts +4 -0
  29. package/dist/textSegmentGuards.d.ts.map +1 -0
  30. package/dist/textSegmentGuards.js +8 -0
  31. package/dist/textSegmentGuards.js.map +1 -0
  32. package/dist/textSegmenterBackend.d.ts +4 -13
  33. package/dist/textSegmenterBackend.d.ts.map +1 -1
  34. package/dist/textSegmenterBackend.js +25 -25
  35. package/dist/textSegmenterBackend.js.map +1 -1
  36. package/dist/types.d.ts +38 -0
  37. package/dist/types.d.ts.map +1 -0
  38. package/dist/types.js +30 -0
  39. package/dist/types.js.map +1 -0
  40. package/package.json +6 -4
  41. package/src/enableTextSegmentGuards.test.ts +51 -0
  42. package/src/textSegment.test.ts +41 -39
  43. package/src/textSegmentBoundary.test.ts +84 -44
  44. package/src/textSegmentConformance.test.ts +24 -0
  45. package/src/textSegmentGuards.test.ts +21 -0
  46. package/src/textSegmenterBackend.test.ts +83 -74
  47. package/src/types.test.ts +52 -0
@@ -1,14 +1,5 @@
1
- import type { EntityConstruction, HasTextSegmenter, TextSegmenterBackend } from '@flighthq/types/contract';
2
- export declare function createWebTextSegmenterBackend(): TextSegmenterBackend;
3
- export declare const webTextSegmenterBackend: TextSegmenterBackend;
4
- export declare function getTextSegmenterBackend(host?: HasTextSegmenter): TextSegmenterBackend;
5
- export declare function initializeWebTextSegmenterBackend(out: EntityConstruction<TextSegmenterBackend>): void;
6
- /**
7
- * Installs the backend used by calls that omit an explicit host; pass null to restore the stable
8
- * bundled web default.
9
- *
10
- * @deprecated Pass a HasTextSegmenter to the segmentation or boundary operation. Retained for
11
- * source compatibility until the legacy global path is removed.
12
- */
13
- export declare function setTextSegmenterBackend(backend: TextSegmenterBackend | null): void;
1
+ import type { HostTextSegmenterCapability, TextSegmenterExplanation } from './types.ts';
2
+ export declare const webTextSegmenterBackend: HostTextSegmenterCapability;
3
+ /** Describes which provider an operation would use and whether Intl.Segmenter is present. */
4
+ export declare function explainTextSegmenterBackend(textSegmenter: Readonly<HostTextSegmenterCapability>): TextSegmenterExplanation;
14
5
  //# sourceMappingURL=textSegmenterBackend.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"textSegmenterBackend.d.ts","sourceRoot":"","sources":["../src/textSegmenterBackend.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,kBAAkB,EAClB,gBAAgB,EAGhB,oBAAoB,EACrB,MAAM,0BAA0B,CAAC;AAQlC,wBAAgB,6BAA6B,IAAI,oBAAoB,CAIpE;AAID,eAAO,MAAM,uBAAuB,EAAE,oBAAsD,CAAC;AAK7F,wBAAgB,uBAAuB,CAAC,IAAI,CAAC,EAAE,gBAAgB,GAAG,oBAAoB,CAErF;AAED,wBAAgB,iCAAiC,CAAC,GAAG,EAAE,kBAAkB,CAAC,oBAAoB,CAAC,GAAG,IAAI,CAErG;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,oBAAoB,GAAG,IAAI,GAAG,IAAI,CAElF"}
1
+ {"version":3,"file":"textSegmenterBackend.d.ts","sourceRoot":"","sources":["../src/textSegmenterBackend.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,2BAA2B,EAG3B,wBAAwB,EACzB,MAAM,YAAY,CAAC;AAIpB,eAAO,MAAM,uBAAuB,EAAE,2BAA6D,CAAC;AAcpG,6FAA6F;AAC7F,wBAAgB,2BAA2B,CACzC,aAAa,EAAE,QAAQ,CAAC,2BAA2B,CAAC,GACnD,wBAAwB,CAU1B"}
@@ -1,46 +1,43 @@
1
- import { allocateEntity, finishEntity } from '@flighthq/entity/contract';
1
+ import { reportTextSegmenterUnavailable } from './textSegmentGuards.js';
2
+ // Stable bundled web provider. Hosts can import this directly when composing their explicit
3
+ // capability object.
4
+ export const webTextSegmenterBackend = createWebTextSegmenterBackend();
2
5
  // Builds the default web backend: a wrapper over the browser-native Intl.Segmenter. It ships no
3
6
  // Unicode tables — the engine already carries them — so the common path costs nothing in bundle
4
7
  // weight. Intl.Segmenter instances are cached by (locale, granularity) because constructing one is
5
8
  // expensive relative to a single segment() call. Where Intl.Segmenter is absent (an old or headless
6
9
  // engine), segment() returns [] rather than throwing; compose a from-scratch UAX #29 backend into
7
10
  // the host for those environments.
8
- export function createWebTextSegmenterBackend() {
9
- const out = allocateEntity();
11
+ function createWebTextSegmenterBackend() {
12
+ const out = {};
10
13
  initializeWebTextSegmenterBackend(out);
11
- return finishEntity(out);
14
+ return out;
12
15
  }
13
- // Stable bundled web provider. Hosts can import this directly when composing their explicit
14
- // capability object; callers that omit a host receive it as the final fallback.
15
- export const webTextSegmenterBackend = createWebTextSegmenterBackend();
16
- // Returns the explicit host's provider when supplied, then the legacy installed backend, and
17
- // finally the bundled web provider. The explicit dependency always wins, so independent callers
18
- // can interleave different hosts without mutating shared capability state.
19
- export function getTextSegmenterBackend(host) {
20
- return host?.text.segmenter ?? _backend ?? webTextSegmenterBackend;
16
+ /** Describes which provider an operation would use and whether Intl.Segmenter is present. */
17
+ export function explainTextSegmenterBackend(textSegmenter) {
18
+ // Backend identity is segment-function identity, as in Rust HostTextSegmenterCapability equality,
19
+ // so a copied or wrapping capability object that keeps the bundled function is still the web backend.
20
+ const web = textSegmenter.segment === webTextSegmenterBackend.segment;
21
+ const intlSegmenterAvailable = hasIntlSegmenter();
22
+ return {
23
+ available: !web || intlSegmenterAvailable,
24
+ backend: web ? 'web-intl' : 'custom',
25
+ intlSegmenterAvailable,
26
+ };
21
27
  }
22
- export function initializeWebTextSegmenterBackend(out) {
28
+ function initializeWebTextSegmenterBackend(out) {
23
29
  out.segment = segmentWithIntlSegmenter;
24
30
  }
25
- /**
26
- * Installs the backend used by calls that omit an explicit host; pass null to restore the stable
27
- * bundled web default.
28
- *
29
- * @deprecated Pass a HasTextSegmenter to the segmentation or boundary operation. Retained for
30
- * source compatibility until the legacy global path is removed.
31
- */
32
- export function setTextSegmenterBackend(backend) {
33
- _backend = backend;
34
- }
35
- let _backend = null;
36
31
  // Cached Intl.Segmenter instances keyed by `locale|granularity`. A Map preserves insertion order, so
37
32
  // the first key is the oldest and drives simple FIFO eviction once the cache is full. Instances are
38
33
  // immutable, so sharing them across calls is safe.
39
34
  const _segmenterCache = new Map();
40
35
  const _segmenterCacheCapacity = 64;
41
36
  function getCachedSegmenter(locale, granularity) {
42
- if (typeof Intl === 'undefined' || typeof Intl.Segmenter === 'undefined')
37
+ if (!hasIntlSegmenter()) {
38
+ reportTextSegmenterUnavailable();
43
39
  return null;
40
+ }
44
41
  const key = `${locale ?? ''}|${granularity}`;
45
42
  const existing = _segmenterCache.get(key);
46
43
  if (existing !== undefined)
@@ -54,6 +51,9 @@ function getCachedSegmenter(locale, granularity) {
54
51
  _segmenterCache.set(key, built);
55
52
  return built;
56
53
  }
54
+ function hasIntlSegmenter() {
55
+ return typeof Intl !== 'undefined' && typeof Intl.Segmenter !== 'undefined';
56
+ }
57
57
  function segmentWithIntlSegmenter(text, granularity, locale) {
58
58
  const segmenter = getCachedSegmenter(locale, granularity);
59
59
  if (segmenter === null)
@@ -1 +1 @@
1
- {"version":3,"file":"textSegmenterBackend.js","sourceRoot":"","sources":["../src/textSegmenterBackend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AASzE,gGAAgG;AAChG,gGAAgG;AAChG,mGAAmG;AACnG,oGAAoG;AACpG,kGAAkG;AAClG,mCAAmC;AACnC,MAAM,UAAU,6BAA6B;IAC3C,MAAM,GAAG,GAAG,cAAc,EAAwB,CAAC;IACnD,iCAAiC,CAAC,GAAG,CAAC,CAAC;IACvC,OAAO,YAAY,CAAC,GAAG,CAAC,CAAC;AAC3B,CAAC;AAED,4FAA4F;AAC5F,gFAAgF;AAChF,MAAM,CAAC,MAAM,uBAAuB,GAAyB,6BAA6B,EAAE,CAAC;AAE7F,6FAA6F;AAC7F,gGAAgG;AAChG,2EAA2E;AAC3E,MAAM,UAAU,uBAAuB,CAAC,IAAuB;IAC7D,OAAO,IAAI,EAAE,IAAI,CAAC,SAAS,IAAI,QAAQ,IAAI,uBAAuB,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,iCAAiC,CAAC,GAA6C;IAC7F,GAAG,CAAC,OAAO,GAAG,wBAAwB,CAAC;AACzC,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,uBAAuB,CAAC,OAAoC;IAC1E,QAAQ,GAAG,OAAO,CAAC;AACrB,CAAC;AAED,IAAI,QAAQ,GAAgC,IAAI,CAAC;AAEjD,qGAAqG;AACrG,oGAAoG;AACpG,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IAAI;QAAE,OAAO,EAAE,CAAC;IAElC,MAAM,GAAG,GAAkB,EAAE,CAAC;IAC9B,MAAM,iBAAiB,GAAG,WAAW,KAAK,MAAM,CAAC;IACjD,KAAK,MAAM,IAAI,IAAI,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACzB,MAAM,MAAM,GAAgB,EAAE,KAAK,EAAE,GAAG,EAAE,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC;QAC5F,4EAA4E;QAC5E,IAAI,iBAAiB;YAAE,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,KAAK,CAAC;QACpE,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACnB,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,38 @@
1
+ export type TextSegmentGranularity = 'grapheme' | 'word' | 'sentence';
2
+ export interface TextSegment {
3
+ start: number;
4
+ end: number;
5
+ text: string;
6
+ isWordLike?: boolean;
7
+ }
8
+ export interface TextSegmentRange {
9
+ start: number;
10
+ end: number;
11
+ }
12
+ export interface HostTextSegmentCapabilities {
13
+ readonly segmenter?: HostTextSegmenterCapability;
14
+ }
15
+ export interface HostTextSegmenterCapability {
16
+ segment(text: string, granularity: TextSegmentGranularity, locale?: string): readonly TextSegment[];
17
+ }
18
+ export type TextSegmenterKind = 'custom' | 'web-intl';
19
+ export interface TextSegmenterExplanation {
20
+ available: boolean;
21
+ backend: TextSegmenterKind;
22
+ intlSegmenterAvailable: boolean;
23
+ }
24
+ export type TextSegmentGuard = () => void;
25
+ export declare const TextSegmentGranularity: {
26
+ readonly Grapheme: 'grapheme';
27
+ readonly Word: 'word';
28
+ readonly Sentence: 'sentence';
29
+ };
30
+ export declare const TextSegmenterKind: {
31
+ readonly Custom: 'custom';
32
+ readonly WebIntl: 'web-intl';
33
+ };
34
+ export declare function getTextSegmenterKindString(value: TextSegmenterKind): string;
35
+ export declare function getTextSegmentGranularityString(value: TextSegmentGranularity): string;
36
+ export declare function parseTextSegmenterKind(value: string): TextSegmenterKind | undefined;
37
+ export declare function parseTextSegmentGranularity(value: string): TextSegmentGranularity | undefined;
38
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,sBAAsB,GAAG,UAAU,GAAG,MAAM,GAAG,UAAU,CAAC;AAEtE,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,SAAS,CAAC,EAAE,2BAA2B,CAAC;CAClD;AAED,MAAM,WAAW,2BAA2B;IAC1C,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,sBAAsB,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,WAAW,EAAE,CAAC;CACrG;AAED,MAAM,MAAM,iBAAiB,GAAG,QAAQ,GAAG,UAAU,CAAC;AAEtD,MAAM,WAAW,wBAAwB;IACvC,SAAS,EAAE,OAAO,CAAC;IACnB,OAAO,EAAE,iBAAiB,CAAC;IAC3B,sBAAsB,EAAE,OAAO,CAAC;CACjC;AAED,MAAM,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC;AAE1C,eAAO,MAAM,sBAAsB;aACjC,QAAQ,EAAE,UAAU;aACpB,IAAI,EAAE,MAAM;aACZ,QAAQ,EAAE,UAAU;CACZ,CAAC;AAEX,eAAO,MAAM,iBAAiB;aAC5B,MAAM,EAAE,QAAQ;aAChB,OAAO,EAAE,UAAU;CACX,CAAC;AAEX,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,iBAAiB,GAAG,MAAM,CAE3E;AAED,wBAAgB,+BAA+B,CAAC,KAAK,EAAE,sBAAsB,GAAG,MAAM,CAErF;AAED,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,iBAAiB,GAAG,SAAS,CAGnF;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,MAAM,GAAG,sBAAsB,GAAG,SAAS,CAG7F"}
package/dist/types.js ADDED
@@ -0,0 +1,30 @@
1
+ // Unicode text-segmentation seam (UAX #29). Operations accept an explicit
2
+ // HostTextSegmenterCapability. A from-scratch UAX #29 provider can be composed by headless/native
3
+ // hosts or a flight-rs table kernel. Line breaking (UAX #14) is a separate algorithm owned by
4
+ // @flighthq/textlayout.
5
+ export const TextSegmentGranularity = {
6
+ Grapheme: 'grapheme',
7
+ Word: 'word',
8
+ Sentence: 'sentence',
9
+ };
10
+ export const TextSegmenterKind = {
11
+ Custom: 'custom',
12
+ WebIntl: 'web-intl',
13
+ };
14
+ export function getTextSegmenterKindString(value) {
15
+ return value;
16
+ }
17
+ export function getTextSegmentGranularityString(value) {
18
+ return value;
19
+ }
20
+ export function parseTextSegmenterKind(value) {
21
+ if (value === 'custom' || value === 'web-intl')
22
+ return value;
23
+ return undefined;
24
+ }
25
+ export function parseTextSegmentGranularity(value) {
26
+ if (value === 'grapheme' || value === 'word' || value === 'sentence')
27
+ return value;
28
+ return undefined;
29
+ }
30
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,kGAAkG;AAClG,8FAA8F;AAC9F,wBAAwB;AAkCxB,MAAM,CAAC,MAAM,sBAAsB,GAAG;IACpC,QAAQ,EAAE,UAAU;IACpB,IAAI,EAAE,MAAM;IACZ,QAAQ,EAAE,UAAU;CACZ,CAAC;AAEX,MAAM,CAAC,MAAM,iBAAiB,GAAG;IAC/B,MAAM,EAAE,QAAQ;IAChB,OAAO,EAAE,UAAU;CACX,CAAC;AAEX,MAAM,UAAU,0BAA0B,CAAC,KAAwB;IACjE,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,+BAA+B,CAAC,KAA6B;IAC3E,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,KAAa;IAClD,IAAI,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,UAAU;QAAE,OAAO,KAAK,CAAC;IAC7D,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,2BAA2B,CAAC,KAAa;IACvD,IAAI,KAAK,KAAK,UAAU,IAAI,KAAK,KAAK,MAAM,IAAI,KAAK,KAAK,UAAU;QAAE,OAAO,KAAK,CAAC;IACnF,OAAO,SAAS,CAAC;AACnB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flighthq/textsegment",
3
- "version": "0.5.1-next.973.788d3d7",
3
+ "version": "0.6.0",
4
4
  "author": "Joshua Granick and other contributors",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -32,17 +32,19 @@
32
32
  "scripts": {
33
33
  "build": "tsc -b",
34
34
  "clean": "tsc -b --clean",
35
+ "generate": "tsx ../../scripts/generate-rust-package.ts --crate flight-textsegment --package textsegment --lenient",
36
+ "generate:check": "tsx ../../scripts/generate-rust-package.ts --crate flight-textsegment --package textsegment --lenient --check",
35
37
  "test": "vitest run --config vitest.config.ts",
36
38
  "test:watch": "vitest --watch --config vitest.config.ts",
37
39
  "prepack": "npm run clean && npm run clean:dist && npm run build",
38
40
  "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
39
41
  },
40
42
  "dependencies": {
41
- "@flighthq/entity": "0.5.1-next.973.788d3d7",
42
- "@flighthq/types": "0.5.1-next.973.788d3d7"
43
+ "@flighthq/entity": "0.6.0",
44
+ "@flighthq/log": "0.6.0"
43
45
  },
44
46
  "devDependencies": {
45
- "typescript": "^5.3.0"
47
+ "typescript": "^7.0.2"
46
48
  },
47
49
  "description": "Unicode text-segmentation seam (UAX #29 grapheme/word/sentence) over a swappable backend defaulting to the browser-native Intl.Segmenter",
48
50
  "sideEffects": false
@@ -0,0 +1,51 @@
1
+ import type { LogEntry, LogState } from '@flighthq/log/contract';
2
+ import { createLogState, setLogSink } from '@flighthq/log/contract';
3
+ import { vi } from 'vitest';
4
+
5
+ import { disableTextSegmentGuards, enableTextSegmentGuards } from './enableTextSegmentGuards.ts';
6
+ import { segmentGraphemes } from './textSegment.ts';
7
+ import { webTextSegmenterBackend } from './textSegmenterBackend.ts';
8
+
9
+ const backend = webTextSegmenterBackend;
10
+
11
+ let entries: LogEntry[];
12
+ let logState: LogState;
13
+
14
+ beforeEach(() => {
15
+ entries = [];
16
+ logState = createLogState();
17
+ setLogSink(logState, (entry) => entries.push(entry));
18
+ });
19
+
20
+ afterEach(() => {
21
+ disableTextSegmentGuards();
22
+ setLogSink(logState, null);
23
+ vi.unstubAllGlobals();
24
+ });
25
+
26
+ describe('disableTextSegmentGuards', () => {
27
+ it('restores silent missing-engine recovery', () => {
28
+ enableTextSegmentGuards(logState, () => 0);
29
+ disableTextSegmentGuards();
30
+ vi.stubGlobal('Intl', { Segmenter: undefined });
31
+ segmentGraphemes([], backend, 'text');
32
+ expect(entries).toEqual([]);
33
+ });
34
+ });
35
+
36
+ describe('enableTextSegmentGuards', () => {
37
+ it('warns once and names the provider type when Intl.Segmenter is absent', () => {
38
+ enableTextSegmentGuards(logState, () => 0);
39
+ vi.stubGlobal('Intl', { Segmenter: undefined });
40
+ segmentGraphemes([], backend, 'first');
41
+ segmentGraphemes([], backend, 'second');
42
+ expect(entries).toHaveLength(1);
43
+ expect(String((entries[0].data as { message?: unknown }).message)).toContain('HostTextSegmenterCapability');
44
+ });
45
+
46
+ it('stays silent when the bundled Intl provider is available', () => {
47
+ enableTextSegmentGuards(logState, () => 0);
48
+ segmentGraphemes([], backend, 'text');
49
+ expect(entries).toEqual([]);
50
+ });
51
+ });
@@ -1,83 +1,85 @@
1
- import { allocateEntity, finishEntity } from '@flighthq/entity/contract';
2
- import type { EntityRuntimeKey } from '@flighthq/types/contract';
1
+ import { segmentGraphemes, segmentSentences, segmentWords } from './textSegment.ts';
2
+ import { webTextSegmenterBackend } from './textSegmenterBackend.ts';
3
3
  import type {
4
- HasTextSegmenter,
4
+ HostTextSegmentCapabilities,
5
+ HostTextSegmenterCapability,
5
6
  TextSegment,
6
7
  TextSegmentGranularity,
7
- TextSegmenterBackend,
8
- } from '@flighthq/types/contract';
8
+ } from './types.ts';
9
9
 
10
- import { segmentGraphemes, segmentSentences, segmentWords } from './textSegment';
11
- import { setTextSegmenterBackend } from './textSegmenterBackend';
12
-
13
- afterEach(() => setTextSegmenterBackend(null));
10
+ const backend = webTextSegmenterBackend;
14
11
 
15
12
  describe('segmentGraphemes', () => {
16
- it('accepts an optional explicit segmenter host without breaking the existing signature', () => {
17
- expectTypeOf(segmentGraphemes).toEqualTypeOf<
18
- (text: string, locale?: string, host?: HasTextSegmenter) => readonly TextSegment[]
19
- >();
20
- });
13
+ it('accepts the text segmenter capability exposed by Host', () => {
14
+ const readonlySegments: readonly TextSegment[] = [{ start: 0, end: 5, text: 'hello' }];
15
+ const hostTextSegment: HostTextSegmentCapabilities = {
16
+ segmenter: { segment: () => readonlySegments },
17
+ };
18
+ const hostCapability = hostTextSegment.segmenter;
19
+ if (hostCapability === undefined) throw new Error('fixture must provide Host.textSegment.segmenter');
20
+ const segments: TextSegment[] = [];
21
21
 
22
- it('gives an explicit host precedence over the legacy installed backend', () => {
23
- setTextSegmenterBackend(taggingBackend('legacy'));
24
- expect(segmentGraphemes('hi', undefined, segmenterHost(taggingBackend('explicit')))[0].text).toBe('explicit');
22
+ segmentGraphemes(segments, hostCapability, 'hello');
23
+
24
+ expect(segments).toEqual(readonlySegments);
25
25
  });
26
26
 
27
27
  it('isolates callers that interleave different explicit hosts', () => {
28
- const first = segmenterHost(taggingBackend('first'));
29
- const second = segmenterHost(taggingBackend('second'));
30
- expect(segmentGraphemes('hi', undefined, first)[0].text).toBe('first');
31
- expect(segmentGraphemes('hi', undefined, second)[0].text).toBe('second');
32
- expect(segmentGraphemes('hi', undefined, first)[0].text).toBe('first');
28
+ const first = taggingBackend('first');
29
+ const second = taggingBackend('second');
30
+ const segments: TextSegment[] = [];
31
+ segmentGraphemes(segments, first, 'hi');
32
+ expect(segments[0].text).toBe('first');
33
+ segmentGraphemes(segments, second, 'hi');
34
+ expect(segments[0].text).toBe('second');
35
+ segmentGraphemes(segments, first, 'hi');
36
+ expect(segments[0].text).toBe('first');
33
37
  });
34
38
 
35
39
  it('treats a ZWJ family emoji as one grapheme', () => {
36
- const segments = segmentGraphemes('a👨‍👩‍👧b');
40
+ const segments: TextSegment[] = [];
41
+ segmentGraphemes(segments, backend, 'a👨‍👩‍👧b');
37
42
  expect(segments.map((s) => s.text)).toEqual(['a', '👨‍👩‍👧', 'b']);
38
43
  expect(segments[1].start).toBe(1);
39
44
  expect(segments[1].end).toBe('a👨‍👩‍👧'.length);
40
45
  });
41
46
 
42
47
  it('treats a base plus combining mark as one grapheme', () => {
43
- // 'e' + combining acute accent (U+0301) is one user-perceived character.
44
- const segments = segmentGraphemes('éx');
45
- expect(segments.map((s) => s.text)).toEqual(['é', 'x']);
48
+ const segments: TextSegment[] = [];
49
+ segmentGraphemes(segments, backend, 'éx');
50
+ expect(segments.map((s) => s.text)).toEqual(['é', 'x']);
46
51
  });
47
52
 
48
53
  it('threads the locale argument to the active backend', () => {
49
54
  let seenLocale: string | undefined = 'unset';
50
- const fake = allocateEntity<TextSegmenterBackend>();
51
- fake.segment = (text: string, _granularity: TextSegmentGranularity, locale?: string): readonly TextSegment[] => {
55
+ const fake = {} as HostTextSegmenterCapability;
56
+ fake.segment = (text: string, _granularity: TextSegmentGranularity, locale?: string): TextSegment[] => {
52
57
  seenLocale = locale;
53
58
  return [{ start: 0, end: text.length, text }];
54
59
  };
55
- setTextSegmenterBackend(finishEntity(fake));
56
- segmentGraphemes('hi', 'de-DE');
60
+ segmentGraphemes([], fake, 'hi', 'de-DE');
57
61
  expect(seenLocale).toBe('de-DE');
58
62
  });
59
63
  });
60
64
 
61
- function segmenterHost(segmenter: TextSegmenterBackend): HasTextSegmenter {
62
- return { text: { segmenter } };
63
- }
64
-
65
- function taggingBackend(tag: string): TextSegmenterBackend {
66
- const out = allocateEntity<TextSegmenterBackend>();
65
+ function taggingBackend(tag: string): HostTextSegmenterCapability {
66
+ const out = {} as HostTextSegmenterCapability;
67
67
  out.segment = () => [{ start: 0, end: tag.length, text: tag }];
68
- return finishEntity(out);
68
+ return out;
69
69
  }
70
70
 
71
71
  describe('segmentSentences', () => {
72
72
  it('splits into sentences', () => {
73
- const segments = segmentSentences('Hi. Bye.');
73
+ const segments: TextSegment[] = [];
74
+ segmentSentences(segments, backend, 'Hi. Bye.');
74
75
  expect(segments.map((s) => s.text.trim())).toEqual(['Hi.', 'Bye.']);
75
76
  });
76
77
  });
77
78
 
78
79
  describe('segmentWords', () => {
79
80
  it('marks words as word-like and punctuation/whitespace as not', () => {
80
- const segments = segmentWords('Hello, world.');
81
+ const segments: TextSegment[] = [];
82
+ segmentWords(segments, backend, 'Hello, world.');
81
83
  const words = segments.filter((s) => s.isWordLike === true).map((s) => s.text);
82
84
  expect(words).toEqual(['Hello', 'world']);
83
85
  const comma = segments.find((s) => s.text === ',');
@@ -1,17 +1,19 @@
1
- import { allocateEntity, finishEntity } from '@flighthq/entity/contract';
2
- import type { EntityRuntimeKey } from '@flighthq/types/contract';
3
- import type { HasTextSegmenter, TextSegmenterBackend } from '@flighthq/types/contract';
1
+ import { allocateEntity } from '@flighthq/entity/contract';
2
+ import { vi } from 'vitest';
4
3
 
5
4
  import {
6
5
  getNextGraphemeBoundary,
6
+ getNextSentenceBoundary,
7
7
  getNextWordBoundary,
8
8
  getPreviousGraphemeBoundary,
9
+ getPreviousSentenceBoundary,
9
10
  getPreviousWordBoundary,
10
11
  getWordRangeAt,
11
- } from './textSegmentBoundary';
12
- import { setTextSegmenterBackend } from './textSegmenterBackend';
12
+ } from './textSegmentBoundary.ts';
13
+ import { webTextSegmenterBackend } from './textSegmenterBackend.ts';
14
+ import type { HostTextSegmenterCapability } from './types.ts';
13
15
 
14
- afterEach(() => setTextSegmenterBackend(null));
16
+ const backend = webTextSegmenterBackend;
15
17
 
16
18
  // A ZWJ family emoji is a single grapheme cluster spanning several UTF-16 code units.
17
19
  const FAMILY = '👨‍👩‍👧';
@@ -22,99 +24,137 @@ const EMOJI_LEN = EMOJI_TEXT.length;
22
24
 
23
25
  describe('getNextGraphemeBoundary', () => {
24
26
  it('steps over an emoji cluster in a single move', () => {
25
- expect(getNextGraphemeBoundary(EMOJI_TEXT, FAMILY_START)).toBe(B_START);
27
+ expect(getNextGraphemeBoundary(backend, EMOJI_TEXT, FAMILY_START)).toBe(B_START);
26
28
  });
27
29
 
28
30
  it('advances one plain grapheme from the start', () => {
29
- expect(getNextGraphemeBoundary(EMOJI_TEXT, 0)).toBe(FAMILY_START);
31
+ expect(getNextGraphemeBoundary(backend, EMOJI_TEXT, 0)).toBe(FAMILY_START);
30
32
  });
31
33
 
32
34
  it('clamps at text.length', () => {
33
- expect(getNextGraphemeBoundary(EMOJI_TEXT, EMOJI_LEN)).toBe(EMOJI_LEN);
34
- expect(getNextGraphemeBoundary(EMOJI_TEXT, 999)).toBe(EMOJI_LEN);
35
+ expect(getNextGraphemeBoundary(backend, EMOJI_TEXT, EMOJI_LEN)).toBe(EMOJI_LEN);
36
+ expect(getNextGraphemeBoundary(backend, EMOJI_TEXT, 999)).toBe(EMOJI_LEN);
37
+ });
38
+ });
39
+
40
+ describe('getNextSentenceBoundary', () => {
41
+ it('advances to the next sentence start', () => {
42
+ expect(getNextSentenceBoundary(backend, 'First. Second!', 0)).toBe(7);
43
+ });
44
+
45
+ it('clamps at text.length', () => {
46
+ expect(getNextSentenceBoundary(backend, 'First.', 99)).toBe(6);
47
+ });
48
+
49
+ it('stops the bundled iterator without materializing backend TextSegment records', () => {
50
+ const segment = vi.spyOn(webTextSegmenterBackend, 'segment');
51
+ expect(getNextSentenceBoundary(backend, 'First. Second!', 0)).toBe(7);
52
+ expect(segment).not.toHaveBeenCalled();
53
+ segment.mockRestore();
54
+ });
55
+
56
+ it('routes a copied bundled capability through the bundled iterator', () => {
57
+ const segment = vi.spyOn(webTextSegmenterBackend, 'segment');
58
+ const copied = { ...webTextSegmenterBackend };
59
+ expect(getNextSentenceBoundary(copied, 'First. Second!', 0)).toBe(7);
60
+ expect(segment).not.toHaveBeenCalled();
61
+ segment.mockRestore();
35
62
  });
36
63
  });
37
64
 
38
65
  describe('getNextWordBoundary', () => {
39
66
  it('jumps to the end of the current word', () => {
40
- expect(getNextWordBoundary('foo bar', 0)).toBe(3);
67
+ expect(getNextWordBoundary(backend, 'foo bar', 0)).toBe(3);
41
68
  });
42
69
 
43
70
  it('clamps at text.length', () => {
44
- expect(getNextWordBoundary('foo bar', 7)).toBe(7);
45
- expect(getNextWordBoundary('foo bar', 42)).toBe(7);
71
+ expect(getNextWordBoundary(backend, 'foo bar', 7)).toBe(7);
72
+ expect(getNextWordBoundary(backend, 'foo bar', 42)).toBe(7);
46
73
  });
47
74
  });
48
75
 
49
76
  describe('getPreviousGraphemeBoundary', () => {
50
77
  it('inverts a step over an emoji cluster', () => {
51
- expect(getPreviousGraphemeBoundary(EMOJI_TEXT, B_START)).toBe(FAMILY_START);
78
+ expect(getPreviousGraphemeBoundary(backend, EMOJI_TEXT, B_START)).toBe(FAMILY_START);
52
79
  });
53
80
 
54
81
  it('clamps at 0', () => {
55
- expect(getPreviousGraphemeBoundary(EMOJI_TEXT, 0)).toBe(0);
56
- expect(getPreviousGraphemeBoundary(EMOJI_TEXT, -5)).toBe(0);
82
+ expect(getPreviousGraphemeBoundary(backend, EMOJI_TEXT, 0)).toBe(0);
83
+ expect(getPreviousGraphemeBoundary(backend, EMOJI_TEXT, -5)).toBe(0);
57
84
  });
58
85
 
59
86
  it('steps back from the end onto the last cluster start', () => {
60
- expect(getPreviousGraphemeBoundary(EMOJI_TEXT, EMOJI_LEN)).toBe(B_START);
87
+ expect(getPreviousGraphemeBoundary(backend, EMOJI_TEXT, EMOJI_LEN)).toBe(B_START);
88
+ });
89
+ });
90
+
91
+ describe('getPreviousSentenceBoundary', () => {
92
+ it('steps back to the current sentence start', () => {
93
+ expect(getPreviousSentenceBoundary(backend, 'First. Second!', 14)).toBe(7);
94
+ });
95
+
96
+ it('clamps at zero', () => {
97
+ expect(getPreviousSentenceBoundary(backend, 'First.', -1)).toBe(0);
98
+ });
99
+
100
+ it('routes a copied bundled capability through the bundled iterator', () => {
101
+ const segment = vi.spyOn(webTextSegmenterBackend, 'segment');
102
+ const copied = { ...webTextSegmenterBackend };
103
+ expect(getPreviousSentenceBoundary(copied, 'First. Second!', 10)).toBe(7);
104
+ expect(segment).not.toHaveBeenCalled();
105
+ segment.mockRestore();
61
106
  });
62
107
  });
63
108
 
64
109
  describe('getPreviousWordBoundary', () => {
65
110
  it('jumps to the start of the current word', () => {
66
- expect(getPreviousWordBoundary('foo bar', 7)).toBe(4);
111
+ expect(getPreviousWordBoundary(backend, 'foo bar', 7)).toBe(4);
67
112
  });
68
113
 
69
114
  it('clamps at 0', () => {
70
- expect(getPreviousWordBoundary('foo bar', 0)).toBe(0);
115
+ expect(getPreviousWordBoundary(backend, 'foo bar', 0)).toBe(0);
71
116
  });
72
117
  });
73
118
 
74
119
  describe('getWordRangeAt', () => {
75
120
  it('threads an explicit host through the boundary helper', () => {
76
- const explicit = allocateEntity<TextSegmenterBackend>();
121
+ const explicit = {} as HostTextSegmenterCapability;
77
122
  explicit.segment = (text: string) => [{ start: 0, end: text.length, text, isWordLike: true }];
78
- const host: HasTextSegmenter = { text: { segmenter: finishEntity(explicit) } };
79
- setTextSegmenterBackend(
80
- (() => {
81
- const out = allocateEntity<TextSegmenterBackend>();
82
- out.segment = () => [];
83
- return finishEntity(out);
84
- })(),
85
- );
86
- expect(getWordRangeAt('word', 1, undefined, host)).toEqual({ start: 0, end: 4 });
123
+ expect(getWordRangeAt(explicit, 'word', 1)).toEqual({ start: 0, end: 4 });
87
124
  });
88
125
 
89
126
  it('returns the word range under an index', () => {
90
- expect(getWordRangeAt('foo bar', 1)).toEqual({ start: 0, end: 3 });
127
+ expect(getWordRangeAt(backend, 'foo bar', 1)).toEqual({ start: 0, end: 3 });
91
128
  });
92
129
 
93
130
  it('returns null in whitespace (double-click on a space selects no word)', () => {
94
- expect(getWordRangeAt('foo bar', 3)).toBeNull();
131
+ expect(getWordRangeAt(backend, 'foo bar', 3)).toBeNull();
95
132
  });
96
133
 
97
134
  it('resolves the final word from the trailing boundary', () => {
98
- expect(getWordRangeAt('foo bar', 7)).toEqual({ start: 4, end: 7 });
135
+ expect(getWordRangeAt(backend, 'foo bar', 7)).toEqual({ start: 4, end: 7 });
99
136
  });
100
137
 
101
138
  it('returns null for empty text', () => {
102
- expect(getWordRangeAt('', 0)).toBeNull();
139
+ expect(getWordRangeAt(backend, '', 0)).toBeNull();
140
+ });
141
+
142
+ it('routes a copied bundled capability through the bundled iterator', () => {
143
+ const segment = vi.spyOn(webTextSegmenterBackend, 'segment');
144
+ const copied = { ...webTextSegmenterBackend };
145
+ expect(getWordRangeAt(copied, 'foo bar', 1)).toEqual({ start: 0, end: 3 });
146
+ expect(segment).not.toHaveBeenCalled();
147
+ segment.mockRestore();
103
148
  });
104
149
 
105
150
  it('threads the locale to the active backend', () => {
106
151
  let seenLocale: string | undefined = 'unset';
107
- setTextSegmenterBackend(
108
- (() => {
109
- const out = allocateEntity<TextSegmenterBackend>();
110
- out.segment = (text: string, _granularity: string, locale: string) => {
111
- seenLocale = locale;
112
- return [{ start: 0, end: text.length, text, isWordLike: true }];
113
- };
114
- return finishEntity(out);
115
- })(),
116
- );
117
- getWordRangeAt('word', 1, 'ja-JP');
152
+ const localeBackend = {} as HostTextSegmenterCapability;
153
+ localeBackend.segment = (text: string, _granularity: string, locale: string) => {
154
+ seenLocale = locale;
155
+ return [{ start: 0, end: text.length, text, isWordLike: true }];
156
+ };
157
+ getWordRangeAt(localeBackend, 'word', 1, 'ja-JP');
118
158
  expect(seenLocale).toBe('ja-JP');
119
159
  });
120
160
  });
@@ -0,0 +1,24 @@
1
+ import { describe, expect, it } from 'vitest';
2
+
3
+ import { segmentGraphemes } from './textSegment.ts';
4
+ import { webTextSegmenterBackend } from './textSegmenterBackend.ts';
5
+ import type { TextSegment } from './types.ts';
6
+
7
+ const backend = webTextSegmenterBackend;
8
+
9
+ const graphemeFixtures: ReadonlyArray<{ clusters: string[]; text: string }> = [
10
+ { text: '🇺🇸🇨🇦', clusters: ['🇺🇸', '🇨🇦'] },
11
+ { text: '👨‍👩‍👧', clusters: ['👨‍👩‍👧'] },
12
+ { text: '\r\n', clusters: ['\r\n'] },
13
+ { text: '각', clusters: ['각'] },
14
+ ];
15
+
16
+ describe('text segment conformance fixtures', () => {
17
+ it('covers regional indicators, ZWJ emoji, CRLF, and Hangul jamo', () => {
18
+ const segments: TextSegment[] = [];
19
+ for (const fixture of graphemeFixtures) {
20
+ segmentGraphemes(segments, backend, fixture.text);
21
+ expect(segments.map((segment) => segment.text)).toEqual(fixture.clusters);
22
+ }
23
+ });
24
+ });