File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -85,3 +85,15 @@ describe('sort text', () => {
85
85
expect ( TextUtils . sort ( 'A' , 'A' , false ) ) . toBe ( 0 ) ;
86
86
} ) ;
87
87
} ) ;
88
+
89
+ describe ( 'pluralize' , ( ) => {
90
+ it ( 'handles singular' , ( ) => {
91
+ expect ( TextUtils . pluralize ( 1 , 'item' ) ) . toBe ( 'item' ) ;
92
+ expect ( TextUtils . pluralize ( 1 , 'item' , 'pluralized' ) ) . toBe ( 'item' ) ;
93
+ } ) ;
94
+
95
+ it ( 'handles plural' , ( ) => {
96
+ expect ( TextUtils . pluralize ( 2 , 'item' ) ) . toBe ( 'items' ) ;
97
+ expect ( TextUtils . pluralize ( 2 , 'item' , 'pluralized' ) ) . toBe ( 'pluralized' ) ;
98
+ } ) ;
99
+ } ) ;
Original file line number Diff line number Diff line change @@ -60,6 +60,24 @@ export class TextUtils {
60
60
}
61
61
return 0 ;
62
62
}
63
+
64
+ /**
65
+ * Pluralize a string based on a value
66
+ * @param value The value to use for pluralization
67
+ * @param singular The singular form of the string
68
+ * @param pluralized The pluralized form of the string. If not provided, will append 's' to the singular form.
69
+ * @returns The pluralized string
70
+ */
71
+ static pluralize (
72
+ value : number ,
73
+ singular : string ,
74
+ pluralized ?: string
75
+ ) : string {
76
+ if ( value === 1 ) {
77
+ return singular ;
78
+ }
79
+ return pluralized ?? `${ singular } s` ;
80
+ }
63
81
}
64
82
65
83
export default TextUtils ;
You can’t perform that action at this time.
0 commit comments