@flighthq/textsegment 0.5.1-next.704.19c91ec → 0.5.1-next.729.2aeb707

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 CHANGED
@@ -13,5 +13,5 @@ Import the supported application-facing API from `@flighthq/textsegment`. The `@
13
13
  This package is part of the locked-version Flight SDK graph. Applications may instead install and import `@flighthq/sdk` when package-level tree shaking is sufficient.
14
14
 
15
15
  - [Flight project](https://github.com/flighthq/flight)
16
- - [Source for @flighthq/textsegment@0.5.1-next.704.19c91ec](https://github.com/flighthq/flight/tree/19c91ec3e569d6b3b471682726465a79fba9f8cb/packages/textsegment)
17
- - [License](https://github.com/flighthq/flight/blob/19c91ec3e569d6b3b471682726465a79fba9f8cb/LICENSE.md)
16
+ - [Source for @flighthq/textsegment@0.5.1-next.729.2aeb707](https://github.com/flighthq/flight/tree/2aeb707259007d61c5aaf2e31fe47447a260fe2a/packages/textsegment)
17
+ - [License](https://github.com/flighthq/flight/blob/2aeb707259007d61c5aaf2e31fe47447a260fe2a/LICENSE.md)
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { getNextGraphemeBoundary, getNextWordBoundary, getPreviousGraphemeBoundary, getPreviousWordBoundary, getWordRangeAt, segmentGraphemes, segmentSentences, segmentWords, } from './contract';
1
+ export { getNextGraphemeBoundary, getNextWordBoundary, getPreviousGraphemeBoundary, getPreviousWordBoundary, getWordRangeAt, segmentGraphemes, segmentSentences, segmentWords, webTextSegmenterBackend, } from './contract';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,EACvB,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,GACb,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,EACvB,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,uBAAuB,GACxB,MAAM,YAAY,CAAC"}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export { getNextGraphemeBoundary, getNextWordBoundary, getPreviousGraphemeBoundary, getPreviousWordBoundary, getWordRangeAt, segmentGraphemes, segmentSentences, segmentWords, } from './contract';
1
+ export { getNextGraphemeBoundary, getNextWordBoundary, getPreviousGraphemeBoundary, getPreviousWordBoundary, getWordRangeAt, segmentGraphemes, segmentSentences, segmentWords, webTextSegmenterBackend, } from './contract';
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,EACvB,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,GACb,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,EACvB,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,YAAY,EACZ,uBAAuB,GACxB,MAAM,YAAY,CAAC"}
@@ -1,5 +1,5 @@
1
- import type { TextSegment } from '@flighthq/types/contract';
2
- export declare function segmentGraphemes(text: string, locale?: string): readonly TextSegment[];
3
- export declare function segmentSentences(text: string, locale?: string): readonly TextSegment[];
4
- export declare function segmentWords(text: string, locale?: string): readonly TextSegment[];
1
+ import type { HasTextSegmenter, TextSegment } from '@flighthq/types/contract';
2
+ export declare function segmentGraphemes(text: string, locale?: string, host?: HasTextSegmenter): readonly TextSegment[];
3
+ export declare function segmentSentences(text: string, locale?: string, host?: HasTextSegmenter): readonly TextSegment[];
4
+ export declare function segmentWords(text: string, locale?: string, host?: HasTextSegmenter): readonly TextSegment[];
5
5
  //# sourceMappingURL=textSegment.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"textSegment.d.ts","sourceRoot":"","sources":["../src/textSegment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAQ5D,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,WAAW,EAAE,CAEtF;AAID,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,WAAW,EAAE,CAEtF;AAKD,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,WAAW,EAAE,CAElF"}
1
+ {"version":3,"file":"textSegment.d.ts","sourceRoot":"","sources":["../src/textSegment.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAQ9E,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,SAAS,WAAW,EAAE,CAE/G;AAID,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,SAAS,WAAW,EAAE,CAE/G;AAKD,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,SAAS,WAAW,EAAE,CAE3G"}
@@ -3,18 +3,18 @@ import { getTextSegmenterBackend } from './textSegmenterBackend';
3
3
  // user-perceived character: an emoji ZWJ sequence or a base-plus-combining-mark cluster is a single
4
4
  // segment, not its constituent code points. `locale` is threaded to the backend for the rare
5
5
  // locale-tailored cases. isWordLike is absent on grapheme segments.
6
- export function segmentGraphemes(text, locale) {
7
- return getTextSegmenterBackend().segment(text, 'grapheme', locale);
6
+ export function segmentGraphemes(text, locale, host) {
7
+ return getTextSegmenterBackend(host).segment(text, 'grapheme', locale);
8
8
  }
9
9
  // Enumerates the sentences of `text` (UAX #29) via the active backend, in order and covering the
10
10
  // whole string. isWordLike is absent on sentence segments.
11
- export function segmentSentences(text, locale) {
12
- return getTextSegmenterBackend().segment(text, 'sentence', locale);
11
+ export function segmentSentences(text, locale, host) {
12
+ return getTextSegmenterBackend(host).segment(text, 'sentence', locale);
13
13
  }
14
14
  // Enumerates the words of `text` (UAX #29) via the active backend. Word segments carry isWordLike:
15
15
  // true for letters/numbers (a real word), false for the punctuation and whitespace runs between
16
16
  // them. Filter on isWordLike to keep only words. `locale` is threaded to the backend.
17
- export function segmentWords(text, locale) {
18
- return getTextSegmenterBackend().segment(text, 'word', locale);
17
+ export function segmentWords(text, locale, host) {
18
+ return getTextSegmenterBackend(host).segment(text, 'word', locale);
19
19
  }
20
20
  //# sourceMappingURL=textSegment.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"textSegment.js","sourceRoot":"","sources":["../src/textSegment.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAEjE,mGAAmG;AACnG,oGAAoG;AACpG,6FAA6F;AAC7F,oEAAoE;AACpE,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,MAAe;IAC5D,OAAO,uBAAuB,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;AACrE,CAAC;AAED,iGAAiG;AACjG,2DAA2D;AAC3D,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,MAAe;IAC5D,OAAO,uBAAuB,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;AACrE,CAAC;AAED,mGAAmG;AACnG,gGAAgG;AAChG,sFAAsF;AACtF,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,MAAe;IACxD,OAAO,uBAAuB,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACjE,CAAC"}
1
+ {"version":3,"file":"textSegment.js","sourceRoot":"","sources":["../src/textSegment.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AAEjE,mGAAmG;AACnG,oGAAoG;AACpG,6FAA6F;AAC7F,oEAAoE;AACpE,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,MAAe,EAAE,IAAuB;IACrF,OAAO,uBAAuB,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;AACzE,CAAC;AAED,iGAAiG;AACjG,2DAA2D;AAC3D,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,MAAe,EAAE,IAAuB;IACrF,OAAO,uBAAuB,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,MAAM,CAAC,CAAC;AACzE,CAAC;AAED,mGAAmG;AACnG,gGAAgG;AAChG,sFAAsF;AACtF,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,MAAe,EAAE,IAAuB;IACjF,OAAO,uBAAuB,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AACrE,CAAC"}
@@ -1,7 +1,7 @@
1
- import type { TextSegmentRange } from '@flighthq/types/contract';
2
- export declare function getNextGraphemeBoundary(text: string, index: number, locale?: string): number;
3
- export declare function getNextWordBoundary(text: string, index: number, locale?: string): number;
4
- export declare function getPreviousGraphemeBoundary(text: string, index: number, locale?: string): number;
5
- export declare function getPreviousWordBoundary(text: string, index: number, locale?: string): number;
6
- export declare function getWordRangeAt(text: string, index: number, locale?: string): TextSegmentRange | null;
1
+ import type { HasTextSegmenter, TextSegmentRange } from '@flighthq/types/contract';
2
+ export declare function getNextGraphemeBoundary(text: string, index: number, locale?: string, host?: HasTextSegmenter): number;
3
+ export declare function getNextWordBoundary(text: string, index: number, locale?: string, host?: HasTextSegmenter): number;
4
+ export declare function getPreviousGraphemeBoundary(text: string, index: number, locale?: string, host?: HasTextSegmenter): number;
5
+ export declare function getPreviousWordBoundary(text: string, index: number, locale?: string, host?: HasTextSegmenter): number;
6
+ export declare function getWordRangeAt(text: string, index: number, locale?: string, host?: HasTextSegmenter): TextSegmentRange | null;
7
7
  //# sourceMappingURL=textSegmentBoundary.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"textSegmentBoundary.d.ts","sourceRoot":"","sources":["../src/textSegmentBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAe,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAO9E,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAE5F;AAKD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAExF;AAKD,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhG;AAID,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAE5F;AAMD,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAapG"}
1
+ {"version":3,"file":"textSegmentBoundary.d.ts","sourceRoot":"","sources":["../src/textSegmentBoundary.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAe,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAOhG,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,MAAM,CAErH;AAKD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,MAAM,CAEjH;AAKD,wBAAgB,2BAA2B,CACzC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,gBAAgB,GACtB,MAAM,CAER;AAID,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,GAAG,MAAM,CAErH;AAMD,wBAAgB,cAAc,CAC5B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,MAAM,EACf,IAAI,CAAC,EAAE,gBAAgB,GACtB,gBAAgB,GAAG,IAAI,CAazB"}
@@ -2,38 +2,38 @@ import { segmentGraphemes, segmentWords } from './textSegment';
2
2
  // Returns the next grapheme-cluster boundary at or after `index` — the offset a caret lands on when
3
3
  // stepping right by one user-perceived character, so an emoji or combining sequence is crossed in a
4
4
  // single step. `index` is clamped into [0, text.length]; at or past the end it returns text.length.
5
- export function getNextGraphemeBoundary(text, index, locale) {
6
- return nextSegmentBoundary(segmentGraphemes(text, locale), index, text.length);
5
+ export function getNextGraphemeBoundary(text, index, locale, host) {
6
+ return nextSegmentBoundary(segmentGraphemes(text, locale, host), index, text.length);
7
7
  }
8
8
  // Returns the next word boundary at or after `index` — the offset a caret lands on when jumping right
9
9
  // by one word (Ctrl/Alt-Right). `index` is clamped into [0, text.length]; at or past the end it
10
10
  // returns text.length.
11
- export function getNextWordBoundary(text, index, locale) {
12
- return nextSegmentBoundary(segmentWords(text, locale), index, text.length);
11
+ export function getNextWordBoundary(text, index, locale, host) {
12
+ return nextSegmentBoundary(segmentWords(text, locale, host), index, text.length);
13
13
  }
14
14
  // Returns the previous grapheme-cluster boundary at or before `index` — the offset a caret lands on
15
15
  // when stepping left by one user-perceived character. `index` is clamped into [0, text.length]; at or
16
16
  // before the start it returns 0.
17
- export function getPreviousGraphemeBoundary(text, index, locale) {
18
- return previousSegmentBoundary(segmentGraphemes(text, locale), index);
17
+ export function getPreviousGraphemeBoundary(text, index, locale, host) {
18
+ return previousSegmentBoundary(segmentGraphemes(text, locale, host), index);
19
19
  }
20
20
  // Returns the previous word boundary at or before `index` — the offset a caret lands on when jumping
21
21
  // left by one word. `index` is clamped into [0, text.length]; at or before the start it returns 0.
22
- export function getPreviousWordBoundary(text, index, locale) {
23
- return previousSegmentBoundary(segmentWords(text, locale), index);
22
+ export function getPreviousWordBoundary(text, index, locale, host) {
23
+ return previousSegmentBoundary(segmentWords(text, locale, host), index);
24
24
  }
25
25
  // Returns the word-like segment's range covering `index`, for double-click / word-select. `index` is
26
26
  // clamped into [0, text.length]; at the very end it resolves against the last character. Returns null
27
27
  // when the covered segment is not word-like (whitespace or punctuation) or the text is empty — the
28
28
  // caller should then select nothing, mirroring how a double-click on a space selects no word.
29
- export function getWordRangeAt(text, index, locale) {
29
+ export function getWordRangeAt(text, index, locale, host) {
30
30
  if (text.length === 0)
31
31
  return null;
32
32
  const clamped = clampIndex(index, text.length);
33
33
  // At the trailing boundary there is no segment starting at text.length; resolve against the last
34
34
  // character so a double-click at the end still selects the final word.
35
35
  const lookup = clamped === text.length ? text.length - 1 : clamped;
36
- const segments = segmentWords(text, locale);
36
+ const segments = segmentWords(text, locale, host);
37
37
  for (const segment of segments) {
38
38
  if (lookup >= segment.start && lookup < segment.end) {
39
39
  return segment.isWordLike === true ? { start: segment.start, end: segment.end } : null;
@@ -1 +1 @@
1
- {"version":3,"file":"textSegmentBoundary.js","sourceRoot":"","sources":["../src/textSegmentBoundary.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE/D,oGAAoG;AACpG,oGAAoG;AACpG,oGAAoG;AACpG,MAAM,UAAU,uBAAuB,CAAC,IAAY,EAAE,KAAa,EAAE,MAAe;IAClF,OAAO,mBAAmB,CAAC,gBAAgB,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;AACjF,CAAC;AAED,sGAAsG;AACtG,gGAAgG;AAChG,uBAAuB;AACvB,MAAM,UAAU,mBAAmB,CAAC,IAAY,EAAE,KAAa,EAAE,MAAe;IAC9E,OAAO,mBAAmB,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;AAC7E,CAAC;AAED,oGAAoG;AACpG,sGAAsG;AACtG,iCAAiC;AACjC,MAAM,UAAU,2BAA2B,CAAC,IAAY,EAAE,KAAa,EAAE,MAAe;IACtF,OAAO,uBAAuB,CAAC,gBAAgB,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,KAAK,CAAC,CAAC;AACxE,CAAC;AAED,qGAAqG;AACrG,mGAAmG;AACnG,MAAM,UAAU,uBAAuB,CAAC,IAAY,EAAE,KAAa,EAAE,MAAe;IAClF,OAAO,uBAAuB,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,KAAK,CAAC,CAAC;AACpE,CAAC;AAED,qGAAqG;AACrG,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AI,QAAQ,EAAE,CAAC;QAC/B,IAAI,OAAO,CAAC,KAAK,IAAI,IAAI;YAAE,MAAM;QACjC,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC;IAC3B,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
@@ -1,5 +1,13 @@
1
- import type { TextSegmenterBackend } from '@flighthq/types/contract';
1
+ import type { HasTextSegmenter, TextSegmenterBackend } from '@flighthq/types/contract';
2
2
  export declare function createWebTextSegmenterBackend(): TextSegmenterBackend;
3
- export declare function getTextSegmenterBackend(): TextSegmenterBackend;
3
+ export declare const webTextSegmenterBackend: TextSegmenterBackend;
4
+ export declare function getTextSegmenterBackend(host?: HasTextSegmenter): TextSegmenterBackend;
5
+ /**
6
+ * Installs the backend used by calls that omit an explicit host; pass null to restore the stable
7
+ * bundled web default.
8
+ *
9
+ * @deprecated Pass a HasTextSegmenter to the segmentation or boundary operation. Retained for
10
+ * source compatibility until the legacy global path is removed.
11
+ */
4
12
  export declare function setTextSegmenterBackend(backend: TextSegmenterBackend | null): void;
5
13
  //# sourceMappingURL=textSegmenterBackend.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"textSegmenterBackend.d.ts","sourceRoot":"","sources":["../src/textSegmenterBackend.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAuC,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAQ1G,wBAAgB,6BAA6B,IAAI,oBAAoB,CAEpE;AAKD,wBAAgB,uBAAuB,IAAI,oBAAoB,CAG9D;AAKD,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":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAGhB,oBAAoB,EACrB,MAAM,0BAA0B,CAAC;AAQlC,wBAAgB,6BAA6B,IAAI,oBAAoB,CAEpE;AAID,eAAO,MAAM,uBAAuB,EAAE,oBAAsD,CAAC;AAK7F,wBAAgB,uBAAuB,CAAC,IAAI,CAAC,EAAE,gBAAgB,GAAG,oBAAoB,CAErF;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,oBAAoB,GAAG,IAAI,GAAG,IAAI,CAElF"}
@@ -2,22 +2,27 @@
2
2
  // Unicode tables — the engine already carries them — so the common path costs nothing in bundle
3
3
  // weight. Intl.Segmenter instances are cached by (locale, granularity) because constructing one is
4
4
  // expensive relative to a single segment() call. Where Intl.Segmenter is absent (an old or headless
5
- // engine), segment() returns [] rather than throwing; install a from-scratch UAX #29 backend via
6
- // setTextSegmenterBackend for those hosts.
5
+ // engine), segment() returns [] rather than throwing; compose a from-scratch UAX #29 backend into
6
+ // the host for those environments.
7
7
  export function createWebTextSegmenterBackend() {
8
8
  return { segment: segmentWithIntlSegmenter };
9
9
  }
10
- // Returns the active segmenter backend, lazily creating the web default the first time so there is
11
- // always an answer. A native host or a from-scratch UAX #29 backend replaces it via
12
- // setTextSegmenterBackend; passing null there restores this lazy web default.
13
- export function getTextSegmenterBackend() {
14
- if (_backend === null)
15
- _backend = createWebTextSegmenterBackend();
16
- return _backend;
10
+ // Stable bundled web provider. Hosts can import this directly when composing their explicit
11
+ // capability object; callers that omit a host receive it as the final fallback.
12
+ export const webTextSegmenterBackend = createWebTextSegmenterBackend();
13
+ // Returns the explicit host's provider when supplied, then the legacy installed backend, and
14
+ // finally the bundled web provider. The explicit dependency always wins, so independent callers
15
+ // can interleave different hosts without mutating shared capability state.
16
+ export function getTextSegmenterBackend(host) {
17
+ return host?.text.segmenter ?? _backend ?? webTextSegmenterBackend;
17
18
  }
18
- // Installs a segmenter backend; pass null to fall back to the lazily-created web default. Last write
19
- // wins registering over an existing backend replaces it. Opt-in and side-effect-free at import:
20
- // nothing installs until a host calls this (or a segment*/boundary query lazily builds the web one).
19
+ /**
20
+ * Installs the backend used by calls that omit an explicit host; pass null to restore the stable
21
+ * bundled web default.
22
+ *
23
+ * @deprecated Pass a HasTextSegmenter to the segmentation or boundary operation. Retained for
24
+ * source compatibility until the legacy global path is removed.
25
+ */
21
26
  export function setTextSegmenterBackend(backend) {
22
27
  _backend = backend;
23
28
  }
@@ -1 +1 @@
1
- {"version":3,"file":"textSegmenterBackend.js","sourceRoot":"","sources":["../src/textSegmenterBackend.ts"],"names":[],"mappings":"AAEA,gGAAgG;AAChG,gGAAgG;AAChG,mGAAmG;AACnG,oGAAoG;AACpG,iGAAiG;AACjG,2CAA2C;AAC3C,MAAM,UAAU,6BAA6B;IAC3C,OAAO,EAAE,OAAO,EAAE,wBAAwB,EAAE,CAAC;AAC/C,CAAC;AAED,mGAAmG;AACnG,oFAAoF;AACpF,8EAA8E;AAC9E,MAAM,UAAU,uBAAuB;IACrC,IAAI,QAAQ,KAAK,IAAI;QAAE,QAAQ,GAAG,6BAA6B,EAAE,CAAC;IAClE,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,qGAAqG;AACrG,kGAAkG;AAClG,qGAAqG;AACrG,MAAM,UAAU,uBAAuB,CAAC,OAAoC;IAC1E,QAAQ,GAAG,OAAO,CAAC;AACrB,CAAC;AAED,IAAI,QAAQ,GAAgC,IAAI,CAAC;AAEjD,qGAAqG;AACrG,oGAAoG;AACpG,mDAAmD;AACnD,MAAM,eAAe,GAAG,IAAI,GAAG,EAA0B,CAAC;AAC1D,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAEnC,SAAS,kBAAkB,CAAC,MAA0B,EAAE,WAAmC;IACzF,IAAI,OAAO,IAAI,KAAK,WAAW,IAAI,OAAO,IAAI,CAAC,SAAS,KAAK,WAAW;QAAE,OAAO,IAAI,CAAC;IACtF,MAAM,GAAG,GAAG,GAAG,MAAM,IAAI,EAAE,IAAI,WAAW,EAAE,CAAC;IAC7C,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1C,IAAI,QAAQ,KAAK,S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IAAI,IAAI,uBAAuB,EAAE,CAAC;QACpD,MAAM,MAAM,GAAG,eAAe,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;QACnD,IAAI,MAAM,KAAK,SAAS;YAAE,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC3D,CAAC;IACD,eAAe,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAChC,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,wBAAwB,CAC/B,IAAY,EACZ,WAAmC,EACnC,MAAe;IAEf,MAAM,SAAS,GAAG,kBAAkB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC1D,IAAI,SAAS,KAAK,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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flighthq/textsegment",
3
- "version": "0.5.1-next.704.19c91ec",
3
+ "version": "0.5.1-next.729.2aeb707",
4
4
  "author": "Joshua Granick and other contributors",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -38,7 +38,7 @@
38
38
  "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
39
39
  },
40
40
  "dependencies": {
41
- "@flighthq/types": "0.5.1-next.704.19c91ec"
41
+ "@flighthq/types": "0.5.1-next.729.2aeb707"
42
42
  },
43
43
  "devDependencies": {
44
44
  "typescript": "^5.3.0"
@@ -1,4 +1,9 @@
1
- import type { TextSegment, TextSegmentGranularity, TextSegmenterBackend } from '@flighthq/types/contract';
1
+ import type {
2
+ HasTextSegmenter,
3
+ TextSegment,
4
+ TextSegmentGranularity,
5
+ TextSegmenterBackend,
6
+ } from '@flighthq/types/contract';
2
7
 
3
8
  import { segmentGraphemes, segmentSentences, segmentWords } from './textSegment';
4
9
  import { setTextSegmenterBackend } from './textSegmenterBackend';
@@ -6,6 +11,25 @@ import { setTextSegmenterBackend } from './textSegmenterBackend';
6
11
  afterEach(() => setTextSegmenterBackend(null));
7
12
 
8
13
  describe('segmentGraphemes', () => {
14
+ it('accepts an optional explicit segmenter host without breaking the existing signature', () => {
15
+ expectTypeOf(segmentGraphemes).toEqualTypeOf<
16
+ (text: string, locale?: string, host?: HasTextSegmenter) => readonly TextSegment[]
17
+ >();
18
+ });
19
+
20
+ it('gives an explicit host precedence over the legacy installed backend', () => {
21
+ setTextSegmenterBackend(taggingBackend('legacy'));
22
+ expect(segmentGraphemes('hi', undefined, segmenterHost(taggingBackend('explicit')))[0].text).toBe('explicit');
23
+ });
24
+
25
+ it('isolates callers that interleave different explicit hosts', () => {
26
+ const first = segmenterHost(taggingBackend('first'));
27
+ const second = segmenterHost(taggingBackend('second'));
28
+ expect(segmentGraphemes('hi', undefined, first)[0].text).toBe('first');
29
+ expect(segmentGraphemes('hi', undefined, second)[0].text).toBe('second');
30
+ expect(segmentGraphemes('hi', undefined, first)[0].text).toBe('first');
31
+ });
32
+
9
33
  it('treats a ZWJ family emoji as one grapheme', () => {
10
34
  const segments = segmentGraphemes('a👨‍👩‍👧b');
11
35
  expect(segments.map((s) => s.text)).toEqual(['a', '👨‍👩‍👧', 'b']);
@@ -33,6 +57,16 @@ describe('segmentGraphemes', () => {
33
57
  });
34
58
  });
35
59
 
60
+ function segmenterHost(segmenter: TextSegmenterBackend): HasTextSegmenter {
61
+ return { text: { segmenter } };
62
+ }
63
+
64
+ function taggingBackend(tag: string): TextSegmenterBackend {
65
+ return {
66
+ segment: () => [{ start: 0, end: tag.length, text: tag }],
67
+ };
68
+ }
69
+
36
70
  describe('segmentSentences', () => {
37
71
  it('splits into sentences', () => {
38
72
  const segments = segmentSentences('Hi. Bye.');
@@ -1,3 +1,5 @@
1
+ import type { HasTextSegmenter, TextSegmenterBackend } from '@flighthq/types/contract';
2
+
1
3
  import {
2
4
  getNextGraphemeBoundary,
3
5
  getNextWordBoundary,
@@ -68,6 +70,15 @@ describe('getPreviousWordBoundary', () => {
68
70
  });
69
71
 
70
72
  describe('getWordRangeAt', () => {
73
+ it('threads an explicit host through the boundary helper', () => {
74
+ const explicit: TextSegmenterBackend = {
75
+ segment: (text) => [{ start: 0, end: text.length, text, isWordLike: true }],
76
+ };
77
+ const host: HasTextSegmenter = { text: { segmenter: explicit } };
78
+ setTextSegmenterBackend({ segment: () => [] });
79
+ expect(getWordRangeAt('word', 1, undefined, host)).toEqual({ start: 0, end: 4 });
80
+ });
81
+
71
82
  it('returns the word range under an index', () => {
72
83
  expect(getWordRangeAt('foo bar', 1)).toEqual({ start: 0, end: 3 });
73
84
  });
@@ -1,9 +1,15 @@
1
- import type { TextSegment, TextSegmentGranularity, TextSegmenterBackend } from '@flighthq/types/contract';
1
+ import type {
2
+ HasTextSegmenter,
3
+ TextSegment,
4
+ TextSegmentGranularity,
5
+ TextSegmenterBackend,
6
+ } from '@flighthq/types/contract';
2
7
 
3
8
  import {
4
9
  createWebTextSegmenterBackend,
5
10
  getTextSegmenterBackend,
6
11
  setTextSegmenterBackend,
12
+ webTextSegmenterBackend,
7
13
  } from './textSegmenterBackend';
8
14
 
9
15
  interface RecordingBackend extends TextSegmenterBackend {
@@ -48,9 +54,17 @@ describe('createWebTextSegmenterBackend', () => {
48
54
  });
49
55
 
50
56
  describe('getTextSegmenterBackend', () => {
51
- it('lazily falls back to a web backend when none is registered', () => {
57
+ it('returns an explicit host provider ahead of the legacy backend', () => {
58
+ const legacy = recordingBackend();
59
+ const explicit = recordingBackend();
60
+ const host: HasTextSegmenter = { text: { segmenter: explicit } };
61
+ setTextSegmenterBackend(legacy);
62
+ expect(getTextSegmenterBackend(host)).toBe(explicit);
63
+ });
64
+
65
+ it('falls back to the stable bundled web backend when none is registered', () => {
52
66
  const backend = getTextSegmenterBackend();
53
- expect(backend).not.toBeNull();
67
+ expect(backend).toBe(webTextSegmenterBackend);
54
68
  expect(backend.segment('ab', 'grapheme').length).toBe(2);
55
69
  });
56
70
 
@@ -69,10 +83,20 @@ describe('setTextSegmenterBackend', () => {
69
83
  expect(backend.calls).toEqual([{ text: 'hello', granularity: 'sentence', locale: 'fr' }]);
70
84
  });
71
85
 
72
- it('clears back to the lazy web default when passed null', () => {
86
+ it('clears back to the stable bundled web default when passed null', () => {
73
87
  setTextSegmenterBackend(recordingBackend());
74
88
  setTextSegmenterBackend(null);
75
89
  // The web default segments a ZWJ cluster as one grapheme; a recording backend would not.
76
90
  expect(getTextSegmenterBackend().segment('a👨‍👩‍👧b', 'grapheme').length).toBe(3);
77
91
  });
78
92
  });
93
+
94
+ describe('webTextSegmenterBackend', () => {
95
+ it('provides the stable bundled Intl fallback', () => {
96
+ expect(webTextSegmenterBackend.segment('a👨‍👩‍👧b', 'grapheme').map((segment) => segment.text)).toEqual([
97
+ 'a',
98
+ '👨‍👩‍👧',
99
+ 'b',
100
+ ]);
101
+ });
102
+ });