1
1
/* eslint no-magic-numbers: 0 */
2
2
3
- import { getLikelySubtagsMin } from "./subtags" ;
3
+ import { getLikelySubtagsMin } from "./subtags" ;
4
4
5
5
const languageCodeRe = "([a-z]{2,3}|\\*)" ;
6
6
const scriptCodeRe = "(?:-([a-z]{4}|\\*))" ;
@@ -22,9 +22,13 @@ const variantCodeRe = "(?:-(([0-9][a-z0-9]{3}|[a-z0-9]{5,8})|\\*))";
22
22
const localeRe = new RegExp (
23
23
`^${ languageCodeRe } ${ scriptCodeRe } ?${ regionCodeRe } ?${ variantCodeRe } ?$` , "i" ) ;
24
24
25
- export const localeParts = [ "language" , "script" , "region" , "variant" ] ;
25
+ export class Locale {
26
+ isWellFormed : boolean ;
27
+ language ?: string ;
28
+ script ?: string ;
29
+ region ?: string ;
30
+ variant ?: string ;
26
31
27
- export default class Locale {
28
32
/**
29
33
* Parses a locale id using the localeRe into an array with four elements.
30
34
*
@@ -34,7 +38,7 @@ export default class Locale {
34
38
* It also allows skipping the script section of the id, so `en-US` is
35
39
* properly parsed as `en-*-US-*`.
36
40
*/
37
- constructor ( locale ) {
41
+ constructor ( locale : string ) {
38
42
const result = localeRe . exec ( locale . replace ( / _ / g, "-" ) ) ;
39
43
if ( ! result ) {
40
44
this . isWellFormed = false ;
@@ -56,38 +60,49 @@ export default class Locale {
56
60
this . isWellFormed = true ;
57
61
}
58
62
59
- isEqual ( locale ) {
60
- return localeParts . every ( part => this [ part ] === locale [ part ] ) ;
63
+ isEqual ( other : Locale ) : boolean {
64
+ return this . language === other . language
65
+ && this . script === other . script
66
+ && this . region === other . region
67
+ && this . variant === other . variant ;
61
68
}
62
69
63
- matches ( locale , thisRange = false , otherRange = false ) {
64
- return localeParts . every ( part => {
65
- return ( ( thisRange && this [ part ] === undefined ) ||
66
- ( otherRange && locale [ part ] === undefined ) ||
67
- this [ part ] === locale [ part ] ) ;
68
- } ) ;
70
+ matches ( other : Locale , thisRange = false , otherRange = false ) : boolean {
71
+ return ( this . language === other . language
72
+ || thisRange && this . language === undefined
73
+ || otherRange && other . language === undefined )
74
+ && ( this . script === other . script
75
+ || thisRange && this . script === undefined
76
+ || otherRange && other . script === undefined )
77
+ && ( this . region === other . region
78
+ || thisRange && this . region === undefined
79
+ || otherRange && other . region === undefined )
80
+ && ( this . variant === other . variant
81
+ || thisRange && this . variant === undefined
82
+ || otherRange && other . variant === undefined ) ;
69
83
}
70
84
71
- toString ( ) {
72
- return localeParts
73
- . map ( part => this [ part ] )
85
+ toString ( ) : string {
86
+ return [ this . language , this . script , this . region , this . variant ]
74
87
. filter ( part => part !== undefined )
75
88
. join ( "-" ) ;
76
89
}
77
90
78
- clearVariants ( ) {
91
+ clearVariants ( ) : void {
79
92
this . variant = undefined ;
80
93
}
81
94
82
- clearRegion ( ) {
95
+ clearRegion ( ) : void {
83
96
this . region = undefined ;
84
97
}
85
98
86
- addLikelySubtags ( ) {
99
+ addLikelySubtags ( ) : boolean {
87
100
const newLocale = getLikelySubtagsMin ( this . toString ( ) . toLowerCase ( ) ) ;
88
-
89
101
if ( newLocale ) {
90
- localeParts . forEach ( part => this [ part ] = newLocale [ part ] ) ;
102
+ this . language = newLocale . language ;
103
+ this . script = newLocale . script ;
104
+ this . region = newLocale . region ;
105
+ this . variant = newLocale . variant ;
91
106
return true ;
92
107
}
93
108
return false ;
0 commit comments