Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/laisfrigerio/solid-principles-typescript
Implementing SOLID principles with TypeScript in a Password Generator
https://github.com/laisfrigerio/solid-principles-typescript
javascript jest jest-test password-generator solid solid-principles solid-principles-examples typescript
Last synced: 22 days ago
JSON representation
Implementing SOLID principles with TypeScript in a Password Generator
- Host: GitHub
- URL: https://github.com/laisfrigerio/solid-principles-typescript
- Owner: laisfrigerio
- License: mit
- Created: 2024-01-23T02:46:39.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-23T05:21:07.000Z (about 1 year ago)
- Last Synced: 2024-11-19T10:09:20.054Z (3 months ago)
- Topics: javascript, jest, jest-test, password-generator, solid, solid-principles, solid-principles-examples, typescript
- Language: TypeScript
- Homepage:
- Size: 72.3 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Solid Principles wih Typescript
Implementing SOLID principles with TypeScript in a Password Generator Project.
## SOLID
- **S**ingle Responsibility Principle (SRP)
- **O**pen/Closed Principle (OCP)
- **L**iskov Substitution Principle (LSP)
- **I**nterface Segregation Principle
- **D**ependency Inversion Principle### Single Responsibility Principle
Every class should have a single responsibility. A class should have one, and only one, reason to change.
### Open/Closed Principle
You should be able to extend a classes behavior, without modifying it. Software entities should be open for extension, but closed for modification.
### Liskov Substitution Principle
Derived classes must be substitutable for their base classes. Functions that use references to base classes must be able to use objects of derived classes without knowing it.
### Interface Segregation Principle
Make fine grained interfaces that are client specific. Clients should not be forced to depend on interfaces they do not use.
### Dependency Inversion Principle
Depend on abstractions, not on concretions. High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
## Solution
We can generate a password based on the following rules:
- Require a minimum of characters;
- Require at least one upper case character;
- Require at least one number character;
- Require at least one special character.### The usage of SOLID principles
1. We created a class for each rule under the `./src/domain/rules` folder to ensure the Single Responsibility Principle (SRP).
2. We created the `RuleFactory` class to be responsible for creating a list of rules based on user options, following again the SRP.
3. We created the `RuleFactory` class to be open for extension but closed for modification. We can easily add new rules by extending the `ruleClassMap` without modifying existing code (OCP).
4. Each rule under the `./src/domain/rules` folder implements the `IPasswordGenerationRule` interface, ensuring that they are substitutable for the base type without altering the correctness of the program (LSP).
5. The `IPasswordGenerationRule` interface follows the Interface Segregation Principle by providing a specific method (`applyRule`) relevant to the implementing rule classes. The Rule class (e.g., RequireNumberRule, RequireSpecialCharacterRule, RequireUpperCaseRule) are not forced to depend on unnecessary methods (ISP).
6. The `RuleFactory` class depends on the abstraction provided by the `IPasswordGenerationRule` interface, ensuring that high-level modules depend on abstractions rather than concrete implementations.## π οΈ Stack
- nodejs `18.12.1`
- typescript `5.3.3`
- jest `^29.7.0`
- yarn `1.22.19`## :gem: Run tests
```
yarn install
yarn test
```## Contributions
Feel free to contribute and share your ideas. Open an issue and/or a Pull Request. Let's talk about SOLID principles and implementing the best practices!
## π© Author
| [
@laisfrigerio](https://instagram.com/laisfrigerio/)
|
| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |## π License
This project is licensed under the MIT License - see the LICENSE.md file for details