1- import { Ability , Boolean as Checked , Boolean as Disabled } from "../lib" ;
1+ import { Ability , Boolean as Checked , Boolean as Disabled , NamedArrayState } from "../lib" ;
22
33export class Checkbox extends Ability {
44 private checked = new Checked ( ) ;
@@ -14,3 +14,51 @@ export class Checkbox extends Ability {
1414const checkbox = new Checkbox ( false ) ;
1515
1616console . log ( `Checkbox` , checkbox . disable ( ) ) ;
17+
18+
19+ // NamedArrayState
20+
21+ class AppConfiguration extends NamedArrayState < 'theme' | 'language' | 'notifications' , string | boolean > {
22+ constructor ( ) {
23+ super (
24+ [ 'theme' , 'language' , 'notifications' ] , // Names of the configuration settings
25+ [ 'dark' , 'en' , true ] // Default values
26+ ) ;
27+ }
28+
29+ /**
30+ * Updates the value of a specific configuration by name.
31+ * @param {string } name - The name of the configuration to update.
32+ * @param {string | boolean } value - The new value to set.
33+ */
34+ public updateConfiguration ( name : 'theme' | 'language' | 'notifications' , value : string | boolean ) {
35+ this . update ( this . indexOf ( name ) , value ) ;
36+ }
37+ }
38+
39+ const config = new AppConfiguration ( ) ;
40+
41+ console . group ( `NamedArrayState` ) ;
42+
43+ // View the current state as an object
44+ console . log ( config . toObject ( ) ) ; // Output: { theme: 'dark', language: 'en', notifications: true }
45+
46+ // Get the value of a specific setting
47+ console . log ( config . get ( 'theme' ) ) ; // Output: 'dark'
48+
49+ // Update a specific configuration setting
50+ config . updateConfiguration ( 'theme' , 'light' ) ;
51+ console . log ( config . get ( 'theme' ) ) ; // Output: 'light'
52+
53+ // Selecting multiple configuration options
54+ const selectedValues = config . select ( 'theme' , 'language' ) ;
55+ console . log ( selectedValues ) ; // Output: ['light', 'en']
56+
57+ // Retrieve state with names as tuples
58+ console . log ( config . stateWithNames ) ; // Output: [['theme', 'light'], ['language', 'en'], ['notifications', true]]
59+
60+ // Reset the configuration state to its initial values
61+ config . reset ( ) ;
62+ console . log ( config . toObject ( ) ) ; // Output: { theme: 'dark', language: 'en', notifications: true }
63+
64+ console . groupEnd ( ) ;
0 commit comments