UNPKG

5.66 kBTypeScriptView Raw
1/*! *****************************************************************************
2Copyright (c) Microsoft Corporation. All rights reserved.
3Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4this file except in compliance with the License. You may obtain a copy of the
5License at http://www.apache.org/licenses/LICENSE-2.0
6
7THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
9WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
10MERCHANTABLITY OR NON-INFRINGEMENT.
11
12See the Apache Version 2.0 License for specific language governing permissions
13and limitations under the License.
14***************************************************************************** */
15
16
17
18/// <reference no-default-lib="true"/>
19
20
21declare namespace Intl {
22
23 /**
24 * An object with some or all properties of the `Intl.Segmenter` constructor `options` parameter.
25 *
26 * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/Segmenter#parameters)
27 */
28 interface SegmenterOptions {
29 /** The locale matching algorithm to use. For information about this option, see [Intl page](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation). */
30 localeMatcher?: "best fit" | "lookup" | undefined;
31 /** The type of input to be split */
32 granularity?: "grapheme" | "word" | "sentence" | undefined;
33 }
34
35 interface Segmenter {
36 /**
37 * Returns `Segments` object containing the segments of the input string, using the segmenter's locale and granularity.
38 *
39 * @param input - The text to be segmented as a `string`.
40 *
41 * @returns A new iterable Segments object containing the segments of the input string, using the segmenter's locale and granularity.
42 */
43 segment(input: string): Segments;
44 resolvedOptions(): ResolvedSegmenterOptions;
45 }
46
47 interface ResolvedSegmenterOptions {
48 locale: string;
49 granularity: "grapheme" | "word" | "sentence";
50 }
51
52 interface Segments {
53 /**
54 * Returns an object describing the segment in the original string that includes the code unit at a specified index.
55 *
56 * @param codeUnitIndex - A number specifying the index of the code unit in the original input string. If the value is omitted, it defaults to `0`.
57 */
58 containing(codeUnitIndex?: number): SegmentData;
59
60 /** Returns an iterator to iterate over the segments. */
61 [Symbol.iterator](): IterableIterator<SegmentData>;
62 }
63
64 interface SegmentData {
65 /** A string containing the segment extracted from the original input string. */
66 segment: string;
67 /** The code unit index in the original input string at which the segment begins. */
68 index: number;
69 /** The complete input string that was segmented. */
70 input: string;
71 /**
72 * A boolean value only if granularity is "word"; otherwise, undefined.
73 * If granularity is "word", then isWordLike is true when the segment is word-like (i.e., consists of letters/numbers/ideographs/etc.); otherwise, false.
74 */
75 isWordLike?: boolean;
76 }
77
78 const Segmenter: {
79 prototype: Segmenter;
80
81 /**
82 * Creates a new `Intl.Segmenter` object.
83 *
84 * @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
85 * For the general form and interpretation of the `locales` argument,
86 * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
87 *
88 * @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/Segmenter#parameters)
89 * with some or all options of `SegmenterOptions`.
90 *
91 * @returns [Intl.Segmenter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segments) object.
92 *
93 * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter).
94 */
95 new(locales?: BCP47LanguageTag | BCP47LanguageTag[], options?: SegmenterOptions): Segmenter;
96
97 /**
98 * Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.
99 *
100 * @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
101 * For the general form and interpretation of the `locales` argument,
102 * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
103 *
104 * @param options An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/supportedLocalesOf#parameters).
105 * with some or all possible options.
106 *
107 * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Segmenter/supportedLocalesOf)
108 */
109 supportedLocalesOf(locales: BCP47LanguageTag | BCP47LanguageTag[], options?: Pick<SegmenterOptions, "localeMatcher">): BCP47LanguageTag[];
110 };
111}