1- import type { Hook , PrimitiveType } from "./types.ts" ;
1+ import type { Hook , HookContext , PrimitiveType } from "./types.ts" ;
22import { printArgs } from "./utils.ts" ;
33
44const VALID_MALE_VALUES = new Set < PrimitiveType > ( [
@@ -16,19 +16,21 @@ const VALID_FEMALE_VALUES = new Set<PrimitiveType>([
1616 "Female" ,
1717] ) ;
1818
19+ const contextMatchedMap = new WeakMap < HookContext , string > ( ) ;
20+
1921function createGenderHook ( gender : "male" | "female" ) : Hook {
20- return ( source , args , ctx ) => {
21- if ( ctx . metadata . matched ) {
22- return ctx . out ;
22+ return ( ctx , args ) => {
23+ if ( contextMatchedMap . has ( ctx ) ) {
24+ return contextMatchedMap . get ( ctx ) ! ;
2325 }
2426 if (
25- gender === "male" && VALID_MALE_VALUES . has ( source ) ||
26- gender === "female" && VALID_FEMALE_VALUES . has ( source )
27+ gender === "male" && VALID_MALE_VALUES . has ( ctx . original ) ||
28+ gender === "female" && VALID_FEMALE_VALUES . has ( ctx . original )
2729 ) {
28- ctx . metadata . matched = true ;
29- return printArgs ( args ) ;
30+ const result = printArgs ( args ) ;
31+ contextMatchedMap . set ( ctx , result ) ;
32+ return result ;
3033 }
31- return "" ;
3234 } ;
3335}
3436
@@ -40,28 +42,27 @@ function getPluralRules(locale: string) {
4042 return pluralRules . get ( locale ) ! ;
4143}
4244
43- const symPluralRule = Symbol ( "pluralRule" ) ;
45+ const contextPluralMap = new WeakMap < HookContext , Intl . LDMLPluralRule > ( ) ;
4446
4547function createPluralHook ( pluralRule : Intl . LDMLPluralRule ) : Hook {
46- return ( source , args , ctx , info ) => {
47- if ( ctx . metadata . matched ) {
48- return ctx . out ;
48+ return ( ctx , args ) => {
49+ if ( contextMatchedMap . has ( ctx ) ) {
50+ return contextMatchedMap . get ( ctx ) ! ;
4951 }
50- let selectedPluralRule = ctx . metadata [ symPluralRule ] as
51- | Intl . LDMLPluralRule
52- | undefined ;
53- if ( ! ctx . metadata [ symPluralRule ] ) {
54- selectedPluralRule = ctx . metadata [ symPluralRule ] = getPluralRules (
55- info . locale ?? "en" ,
56- ) . select ( + ( source ?? 0 ) ) ;
52+ let selectedPluralRule = contextPluralMap . get ( ctx ) ;
53+ if ( ! selectedPluralRule ) {
54+ selectedPluralRule = getPluralRules ( ctx . locale ?? "en" ) . select (
55+ + ( ctx . original ?? 0 ) ,
56+ ) ;
57+ contextPluralMap . set ( ctx , selectedPluralRule ) ;
5758 }
5859 if (
5960 selectedPluralRule === pluralRule
6061 ) {
61- ctx . metadata . matched = true ;
62- return printArgs ( args ) ;
62+ const result = printArgs ( args ) ;
63+ contextMatchedMap . set ( ctx , result ) ;
64+ return result ;
6365 }
64- return "" ;
6566 } ;
6667}
6768
@@ -79,9 +80,9 @@ export const defaultHooks: Record<string, Hook> = {
7980 other : createPluralHook ( "other" ) ,
8081
8182 // else
82- else : ( _ , args , ctx ) => {
83- if ( ctx . metadata . matched ) {
84- return ctx . out ;
83+ else : ( ctx , args ) => {
84+ if ( contextMatchedMap . has ( ctx ) ) {
85+ return contextMatchedMap . get ( ctx ) ! ;
8586 }
8687 return printArgs ( args ) ;
8788 } ,
0 commit comments