Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
1635e72
refactor(BooleanState): use the default state from the static propert…
sciborrudnicki Dec 10, 2024
d6c4df7
refactor(ImmutableState): differ state by using `#locked` name.
sciborrudnicki Dec 10, 2024
53dabfb
feat: Ability, Enabled, Disabled, Accessibility, Accessible, Inaccess…
sciborrudnicki Dec 10, 2024
cf260f7
refactor(Ability): use classes using the `as`.
sciborrudnicki Dec 11, 2024
05fc30e
refactor: remove in favor of using `as`.
sciborrudnicki Dec 11, 2024
a63027d
refactor(BooleanState): add `canChange` feature, move `on()` method t…
sciborrudnicki Dec 13, 2024
e4cfa88
refactor(State): add `canChange` feature to determine whether the `st…
sciborrudnicki Dec 13, 2024
234acba
feat(Boolean): add non `abstract` class `Boolean` to import with the …
sciborrudnicki Dec 14, 2024
6b1821b
feat(BooleanStates): add class to handle multiple `Boolean` states.
sciborrudnicki Dec 14, 2024
3460bee
refactor(State): simplify to use with the mixins or with a different …
sciborrudnicki Dec 15, 2024
2f450da
refactor(BooleanState): remove the `canChange` and set proper descrip…
sciborrudnicki Dec 15, 2024
5f2a76a
refactor(BooleanStates): remove the `canChange` and update description.
sciborrudnicki Dec 15, 2024
9ba0d2c
docs(NullState, NumberState, State): update.
sciborrudnicki Dec 15, 2024
e89ff46
refactor(EnumState): change generic type variable from `T` to `Type` …
sciborrudnicki Dec 15, 2024
2ea3d58
docs(Ability): remove `typedef`.
sciborrudnicki Dec 15, 2024
64bb501
docs(Boolean): remove `typedef`.
sciborrudnicki Dec 15, 2024
7fce13a
feat(Enum): add the `class`.
sciborrudnicki Dec 15, 2024
934793d
chore(index): update.
sciborrudnicki Dec 15, 2024
9feb8a4
refactor(Ability): use as `abstract`.
sciborrudnicki Dec 15, 2024
cdc6ae5
feat(ObjectState): add.
sciborrudnicki Dec 15, 2024
aade77b
refactor(State): check the locked state before the `set()`.
sciborrudnicki Dec 15, 2024
9b6b786
feat(ArrayState): add.
sciborrudnicki Dec 20, 2024
aaf1c74
feat(BooleanArrayState): add.
sciborrudnicki Dec 20, 2024
33b33a8
refactor(State): set state as `Readonly`.
sciborrudnicki Dec 20, 2024
287aae0
test(ArrayState): add.
sciborrudnicki Dec 20, 2024
302f5c4
refactor(ArrayState): add the `first()` and `last()` methods, and `ge…
sciborrudnicki Dec 20, 2024
6b39acc
refactor(BooleanStates): remove.
sciborrudnicki Dec 20, 2024
9c06cea
refactor(ArrayState): add `merge()`, `set()`, and `swap()` methods, c…
sciborrudnicki Dec 21, 2024
36e56a1
test(ArrayState): update.
sciborrudnicki Dec 21, 2024
26ce21e
feat(NamedArrayState): add.
sciborrudnicki Dec 22, 2024
2fb7f2b
refactor(ArrayState): add the `pick()` feature.
sciborrudnicki Dec 22, 2024
09f0285
test(NamedArrayState): add.
sciborrudnicki Dec 22, 2024
5be0ed0
testL remove.
sciborrudnicki Dec 22, 2024
6f8c1a3
feat(NamedBooleanArrayState): add.
sciborrudnicki Dec 22, 2024
ea9dcaf
refactor(NamedArrayState): add `values` to set initial state.
sciborrudnicki Dec 22, 2024
786560f
refactor(NamedBooleanArrayState): add `values` to set initial state.
sciborrudnicki Dec 22, 2024
e9d520f
chore(index): update.
sciborrudnicki Dec 22, 2024
292d2d3
test(NamedArrayState): update with initial values.
sciborrudnicki Dec 22, 2024
feeed95
test(NamedBooleanArrayState): add.
sciborrudnicki Dec 22, 2024
f8b7a51
refactor(NamedBooleanArrayState): initial default values.
sciborrudnicki Dec 22, 2024
3f8ab49
refactor(ObjectState): do not create the object in returned state, ad…
sciborrudnicki Dec 22, 2024
d3dfd14
feat(BooleanObjectState): add.
sciborrudnicki Dec 22, 2024
1a08d76
chore(array): move array state to `array` directory.
sciborrudnicki Dec 22, 2024
39beeaa
chore(boolean): move boolean state to `boolean` directory.
sciborrudnicki Dec 22, 2024
c91813e
test: update.
sciborrudnicki Dec 22, 2024
8087cfa
chore(boolean/index): add.
sciborrudnicki Dec 22, 2024
ffa22c4
chore(index): update.
sciborrudnicki Dec 22, 2024
d934907
chore(enum state): move to `enum` directory.
sciborrudnicki Dec 22, 2024
d0a72d2
chore(index): update.
sciborrudnicki Dec 22, 2024
c55f388
test(BooleanArrayState): add.
sciborrudnicki Dec 22, 2024
2396f95
test(EnumState): update the import.
sciborrudnicki Dec 22, 2024
918c25c
docs(ObjectState): update.
sciborrudnicki Dec 22, 2024
0e1d042
docs(BooleanObjectState): update.
sciborrudnicki Dec 22, 2024
4b1c62a
chore(object): move object state to the `object` directory.
sciborrudnicki Dec 22, 2024
7f36069
chore(Ability): change import.
sciborrudnicki Dec 22, 2024
07229a9
feat(BooleanActionObjectState): add.
sciborrudnicki Dec 22, 2024
1764ddb
test(BooleanActionObjectState): add.
sciborrudnicki Dec 22, 2024
097dd98
chore(object/index): add `BooleanActionObjectState`.
sciborrudnicki Dec 22, 2024
46a0ef6
refactor(BooleanObjectState): check the `names` length in the `update…
sciborrudnicki Dec 22, 2024
862d30d
test: update.
sciborrudnicki Dec 22, 2024
9c109f9
chore(index): add `BooleanActionObjectState`.
sciborrudnicki Dec 22, 2024
0bb87ef
chore(Ability): move to the `ability` directory.
sciborrudnicki Dec 22, 2024
962a5f2
refactor(Selected): remove.
sciborrudnicki Dec 28, 2024
dabbd30
refactor(Selectable): set as `abstract`, and use `Selected` of `Boole…
sciborrudnicki Dec 28, 2024
bce33d7
refactor(SelectableGroup): add to handle multiple selections.
sciborrudnicki Dec 28, 2024
b649ba2
refactor(ObjectState): change `initialState` to `state` param in the …
sciborrudnicki Dec 28, 2024
815f428
feat(ActionableAbility): add.
sciborrudnicki Dec 28, 2024
71d0230
feat(NamedObjectState): add.
sciborrudnicki Dec 28, 2024
194a883
refactor(BooleanObjectState): use `NamedObjectState` and pass `names`…
sciborrudnicki Dec 28, 2024
e1b0787
chore(index): update.
sciborrudnicki Dec 28, 2024
ee9e27d
test(Ability): add.
sciborrudnicki Dec 29, 2024
2a49552
refactor(Selectable): set to `false` in `deselect()` method.
sciborrudnicki Dec 29, 2024
a756bd1
refactor(ActionableAbility): remove.
sciborrudnicki Dec 29, 2024
922b1f0
refactor(array): move `updateState()` method to the `ArrayState` and …
sciborrudnicki Dec 29, 2024
ba882f2
test(array): add.
sciborrudnicki Dec 29, 2024
3ce2ac5
test(array): move to `array` directory.
sciborrudnicki Dec 29, 2024
c9d3ff2
refactor(BooleanActionObjectState): use as state names to actions, in…
sciborrudnicki Dec 29, 2024
ec7cd7a
refactor(BooleanObjectState): change the method name from `updateStat…
sciborrudnicki Dec 29, 2024
2ff838d
docs(README.md): update.
sciborrudnicki Dec 29, 2024
5de0501
docs(README.md): update.
sciborrudnicki Dec 29, 2024
bc983a2
test(array): update.
sciborrudnicki Dec 29, 2024
7e35518
docs(README.md): update.
sciborrudnicki Dec 29, 2024
2c958c2
test: update.
sciborrudnicki Dec 29, 2024
2c61eaa
refactor(BooleanActionObjectState): update the `dispatch()` method .
sciborrudnicki Dec 29, 2024
8536835
chore: update public api.
sciborrudnicki Dec 29, 2024
6b43ca8
docs(README.md): update.
sciborrudnicki Dec 29, 2024
7d84757
docs(README.md): update.
sciborrudnicki Dec 29, 2024
40e9953
test: update.
sciborrudnicki Dec 29, 2024
7ee54d4
docs(README.md): update.
sciborrudnicki Dec 29, 2024
3e38f35
2.0.0
sciborrudnicki Dec 29, 2024
8721fa5
docs(README.md): update.
sciborrudnicki Dec 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 270 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## typescript-package/state

Simple state management for TypeScript.
Simple state management for different types in TypeScript.

<!-- npm badge -->
[![npm version][typescript-package-npm-badge-svg]][typescript-package-npm-badge]
Expand All @@ -22,10 +22,23 @@ Simple state management for TypeScript.

* [Installation](#installation)
* [Api](#api)
* [`Ability`](#ability)
* [`ArrayState`](#arraystate)
* [`BooleanArrayState`](#booleanarraystate)
* [`NamedArrayState`](#namedarraystate)
* `NamedBooleanArrayState`
* `Boolean`
* [`BooleanState`](#booleanstate)
* `Enum`
* [`EnumState`](#enumstate)
* [`BooleanActionObjectState`](#booleanactionobjectstate)
* `BooleanObjectState`
* `NamedObjectState`
* `ObjectState`
* `ImmutableState`
* [`NullState`](#nullstate)
* [`NumberState`](#numberstate)
* `State`
* [Immutability](#immutability)
* [Sealed](#sealed)
* [Frozen](#frozen)
Expand All @@ -45,15 +58,240 @@ npm install @typescript-package/state

```typescript
import {
Ability,

// Array.
ArrayState,
BooleanArrayState,
NamedArrayState,
NamedBooleanArrayState,

// Boolean.
Boolean,
BooleanState,

// Enum.
Enum,
EnumState,

// Object.
BooleanActionObjectState,
BooleanObjectState,
NamedObjectState,
ObjectState,

NullState,
NumberState,
ImmutableState,
State
} from '@typescript-package/state';
```

### `Ability`

```typescript
import { Ability } from '@typescript-package/state';

// Extend the Ability class for a specific type
export class Connection extends Ability {}

// Initialize the connection.
const connection = new Connection();

// Disables the connection.
connection.disable();
connection.isDisabled();

// Enables the connection.
connection.enable();
connection.isEnabled();
```

### `ArrayState`

```typescript
import { ArrayState } from '@typescript-package/state';

// Extend the ArrayState class for a specific type
export class Numbers extends ArrayState<number> {
// Additional custom methods specific to Numbers can be added if needed
}

// Initialize `Numbers`.
const numbers = new Numbers([27, 28, 29]);

// Append a number to the array state
numbers.append(30);
console.log(numbers.state); // Output: [27, 28, 29, 30]

// Insert a number at a specific index
numbers.insert(2, 99);
console.log(numbers.state); // Output: [27, 28, 99, 29, 30]

// Remove a number by index
numbers.remove(1);
console.log(numbers.state); // Output: [27, 99, 29, 30]

// Pick specific indexes
const picked = numbers.pick(0, 2);
console.log(picked); // Output: [27, 29]

// Swap two elements
numbers.swap(1, 3);
console.log(numbers.state); // Output: [27, 30, 29, 99]

// Reset the state to its initial state
numbers.reset();
console.log(numbers.state); // Output: [27, 28, 29]
```

A simple queue implementation.

```typescript
import { ArrayState } from '@typescript-package/state';

// Extend the ArrayState class for a specific type
export class Queue extends ArrayState<number> {
/**
* Adds an item to the end of the queue (enqueue operation).
* @param {number} value - The number to add to the queue.
* @returns {this}
*/
public enqueue(value: number): this {
return this.append(value);
}

/**
* Removes and returns the item at the front of the queue (dequeue operation).
* @returns {number | undefined} - The dequeued number or undefined if the queue is empty.
*/
public dequeue(): number | undefined {
const front = this.first();
this.remove(0); // Remove the first element
return front;
}

/**
* Returns the item at the front of the queue without removing it.
* @returns {number | undefined} - The number at the front of the queue.
*/
public peek(): number | undefined {
return this.first();
}

/**
* Checks if the queue is empty.
* @returns {boolean} - True if the queue is empty, false otherwise.
*/
public isEmpty(): boolean {
return this.length === 0;
}
}

// Initialize `Queue`.
const queue = new Queue([27, 28, 29]);

// Enqueue a number
queue.enqueue(30);
console.log(queue.state); // Output: [27, 28, 29, 30]

// Dequeue a number
const dequeued = queue.dequeue();
console.log(dequeued); // Output: 27
console.log(queue.state); // Output: [28, 29, 30]

// Peek at the front of the queue
const front = queue.peek();
console.log(front); // Output: 28

// Check if the queue is empty
console.log(queue.isEmpty()); // Output: false

// Dequeue all items
queue.dequeue();
queue.dequeue();
queue.dequeue();
console.log(queue.isEmpty()); // Output: true
```

### `BooleanArrayState`

```typescript
import { BooleanArrayState as AbstractBooleanArrayState } from '@typescript-package/state';

// Extend the AbstractBooleanArrayState class for a specific type
export class BooleanArrayState extends AbstractBooleanArrayState {
// Additional custom methods specific to BooleanArrayState can be added if needed
}

let booleanArrayState = new BooleanArrayState(false, false, true, true);

// Sets all values to `false`.
booleanArrayState.false();
console.log(booleanArrayState.state); // Output: [false, false, false, false]

// Toggles all values to `true`.
booleanArrayState.toggle();
console.log(booleanArrayState.state); // Output: [true, true, true, true]

// Toggles all values to `false`.
booleanArrayState.toggle();
console.log(booleanArrayState.state); // Output: [false, false, false, false]

// Sets all values to `true`.
booleanArrayState.true();
console.log(booleanArrayState.state); // Output: [true, true, true, true]
```

### `NamedArrayState`

```typescript
import { NamedArrayState } from '@typescript-package/state';

// Extend the NamedArrayState class for a specific type
export class AppConfiguration extends NamedArrayState<'theme' | 'language' | 'notifications', string | boolean> {
constructor() {
super(
['theme', 'language', 'notifications'], // Names of the configuration settings
['dark', 'en', true] // Default values
);
}

/**
* Updates the value of a specific configuration by name.
* @param {string} name - The name of the configuration to update.
* @param {string | boolean} value - The new value to set.
*/
public updateConfiguration(name: 'theme' | 'language' | 'notifications', value: string | boolean) {
this.update(this.indexOf(name), value);
}
}

// Initialize.
const config = new AppConfiguration();

// View the current state as an object
console.log(config.toObject()); // Output: { theme: 'dark', language: 'en', notifications: true }

// Get the value of a specific setting
console.log(config.get('theme')); // Output: 'dark'

// Update a specific configuration setting
config.updateConfiguration('theme', 'light');
console.log(config.get('theme')); // Output: 'light'

// Selecting multiple configuration options
const selectedValues = config.select('theme', 'language');
console.log(selectedValues); // Output: ['light', 'en']

// Retrieve state with names as tuples
console.log(config.stateWithNames); // Output: [['theme', 'light'], ['language', 'en'], ['notifications', true]]

// Reset the configuration state to its initial value
config.reset();
console.log(config.toObject()); // Output: { theme: 'dark', language: 'en', notifications: true }
```

### `BooleanState`

```typescript
Expand Down Expand Up @@ -101,7 +339,6 @@ export class ActiveEnum extends EnumState<typeof Active, Active> {
constructor(state: Active) {
super(state, Active);
}

}

// Initialize.
Expand All @@ -111,6 +348,37 @@ const activeEnum = new ActiveEnum(Active.No);
activeEnum.is(Active.No)
```

### `BooleanActionObjectState`

```typescript
import { BooleanActionObjectState } from '@typescript-package/state';

// Extend the BooleanActionObjectState class for a specific type
export class Connection<Names extends PropertyKey, Actions extends string> extends BooleanActionObjectState<Names, Actions> {
/**
* Checks whether connection is connected.
* @returns {boolean}
*/
public isConnected() {
return this.is("connected" as any, true);
}
}
// Initialize the `Connection` with state `connected` and actions `connect` and `disconnect`.
const connection = new Connection(['connected'], ['connect', 'disconnect']);

// Dispatches the `connect` action.
connection.dispatch('connect');
console.log(connection.isConnected()); // Output: true

// Dispatches the `disconnect` action.
connection.dispatch('disconnect');
console.log(connection.isConnected()); // Output: false

// Dispatches the `connect` action.
connection.dispatch('connect');
console.log(connection.isConnected()); // Output: true
```

### `NullState`

```typescript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typescript-package/state",
"version": "1.0.0",
"version": "2.0.0",
"author": "wwwdev.io <[email protected]>",
"description": "State management for TypeScript.",
"license": "MIT",
Expand Down
Loading