Skip to content

Commit 7d789e9

Browse files
committed
chore: rename v3 types to remove Typed prefix
- Rename TypedRuleEngine to RuleEngine - Rename TypedEntity to Entity - Rename TypedRule to Rule - Rename TypedFilter to Filter - Rename TypedRuleSet to RuleSet - Rename TypedMatchingConfig to MatchingConfig - Update documentation and examples
1 parent 2ff59a0 commit 7d789e9

File tree

4 files changed

+63
-67
lines changed

4 files changed

+63
-67
lines changed

CHANGELOG.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { v3 } from '@phr3nzy/rulekit';
3333
const { TypedRuleEngine } = v3;
3434

3535
// After
36-
import { TypedRuleEngine } from '@phr3nzy/rulekit';
36+
import { RuleEngine } from '@phr3nzy/rulekit';
3737
```
3838

3939
Legacy v2 functionality remains available through the v2 namespace:
@@ -68,7 +68,7 @@ const { RuleEngine } = v2.ruleEngine;
6868

6969
### Breaking Changes
7070

71-
- New v3 API uses different import path (`import { v3 } from '@phr3nzy/rulekit'`)
71+
- New v3 API is now available at root level
7272
- Type-safe schemas require explicit type definitions
7373
- Array conditions now support both element and array-level matching
7474
- Rule evaluation now validates against schema types
@@ -89,15 +89,14 @@ const { RuleEngine } = v2.ruleEngine;
8989
import { RuleEngine } from '@phr3nzy/rulekit';
9090

9191
// After (v3)
92-
import { v3 } from '@phr3nzy/rulekit';
93-
const { TypedRuleEngine } = v3;
92+
import { RuleEngine, AttributeType } from '@phr3nzy/rulekit';
9493
```
9594

9695
#### 2. Define Type-Safe Schema
9796

9897
```typescript
99-
import { v3 } from '@phr3nzy/rulekit';
100-
const { AttributeType } = v3;
98+
import { AttributeType } from '@phr3nzy/rulekit';
99+
import type { AttributeSchema } from '@phr3nzy/rulekit';
101100

102101
// Define your schema with type safety
103102
type ProductSchema = {
@@ -117,7 +116,7 @@ type ProductSchema = {
117116
min: 0;
118117
};
119118
};
120-
} & v3.AttributeSchema;
119+
} & AttributeSchema;
121120
```
122121

123122
#### 3. Create Type-Safe Engine
@@ -144,14 +143,14 @@ const productSchema: ProductSchema = {
144143
};
145144

146145
// Create type-safe engine
147-
const engine = new TypedRuleEngine(productSchema);
146+
const engine = new RuleEngine(productSchema);
148147
```
149148

150149
#### 4. Use Type-Safe Rules
151150

152151
```typescript
153152
// Rules are now type-checked
154-
const rules: v3.TypedRule<ProductSchema>[] = [
153+
const rules: Rule<ProductSchema>[] = [
155154
{
156155
attributes: {
157156
category: { eq: 'electronics' }, // Type-safe: must be one of the enum values
@@ -161,7 +160,7 @@ const rules: v3.TypedRule<ProductSchema>[] = [
161160
];
162161

163162
// Entities are type-checked
164-
const entities: v3.TypedEntity<ProductSchema>[] = [
163+
const entities: Entity<ProductSchema>[] = [
165164
{
166165
id: '1',
167166
name: 'Laptop',
@@ -200,14 +199,15 @@ const rule2 = {
200199

201200
### Backward Compatibility
202201

203-
V2 exports are still available for backward compatibility:
202+
V2 exports are still available through the v2 namespace:
204203

205204
```typescript
206-
// V2 imports still work
205+
// Before
207206
import { RuleEngine } from '@phr3nzy/rulekit';
208207

209-
// V3 recommended imports
210-
import { v3 } from '@phr3nzy/rulekit';
208+
// After
209+
import { v2 } from '@phr3nzy/rulekit';
210+
const { RuleEngine } = v2.ruleEngine;
211211
```
212212

213213
## [2.0.2] - 2025-02-08

README.md

+14-15
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ pnpm add @phr3nzy/rulekit
3030
## Quick Start (v3)
3131

3232
```typescript
33-
import { v3 } from '@phr3nzy/rulekit';
34-
const { AttributeType, TypedRuleEngine } = v3;
33+
import { AttributeType, RuleEngine } from '@phr3nzy/rulekit';
3534

3635
// 1. Define your schema
3736
type ProductSchema = {
@@ -59,7 +58,7 @@ type ProductSchema = {
5958
required: false;
6059
};
6160
};
62-
} & v3.AttributeSchema;
61+
} & AttributeSchema;
6362

6463
// 2. Create schema instance
6564
const productSchema: ProductSchema = {
@@ -90,10 +89,10 @@ const productSchema: ProductSchema = {
9089
};
9190

9291
// 3. Create engine
93-
const engine = new TypedRuleEngine(productSchema);
92+
const engine = new RuleEngine(productSchema);
9493

9594
// 4. Define entities
96-
const entities: v3.TypedEntity<ProductSchema>[] = [
95+
const entities: Entity<ProductSchema>[] = [
9796
{
9897
id: '1',
9998
name: 'Gaming Laptop',
@@ -117,7 +116,7 @@ const entities: v3.TypedEntity<ProductSchema>[] = [
117116
];
118117

119118
// 5. Define rules
120-
const rules: v3.TypedRule<ProductSchema>[] = [
119+
const rules: Rule<ProductSchema>[] = [
121120
{
122121
and: [
123122
{
@@ -145,32 +144,32 @@ console.log(matches); // [{ id: '1', name: 'Gaming Laptop', ... }]
145144
### Type-Safe Schema Definition
146145

147146
```typescript
148-
import { v3 } from '@phr3nzy/rulekit';
147+
import { AttributeType } from '@phr3nzy/rulekit';
149148

150149
type UserSchema = {
151150
role: {
152-
type: typeof v3.AttributeType.ENUM;
151+
type: typeof AttributeType.ENUM;
153152
validation: {
154-
type: typeof v3.AttributeType.ENUM;
153+
type: typeof AttributeType.ENUM;
155154
required: true;
156155
enum: ['admin', 'user', 'guest'];
157156
};
158157
};
159158
permissions: {
160-
type: typeof v3.AttributeType.ARRAY;
159+
type: typeof AttributeType.ARRAY;
161160
validation: {
162-
type: typeof v3.AttributeType.ARRAY;
163-
arrayType: typeof v3.AttributeType.STRING;
161+
type: typeof AttributeType.ARRAY;
162+
arrayType: typeof AttributeType.STRING;
164163
required: true;
165164
};
166165
};
167-
} & v3.AttributeSchema;
166+
} & AttributeSchema;
168167
```
169168

170169
### Complex Rule Combinations
171170

172171
```typescript
173-
const rules: v3.TypedRule<UserSchema>[] = [
172+
const rules: Rule<UserSchema>[] = [
174173
{
175174
or: [
176175
{
@@ -216,7 +215,7 @@ const rule2 = {
216215

217216
```typescript
218217
// Engine automatically optimizes batch size based on rule complexity
219-
const engine = new v3.TypedRuleEngine(schema, {
218+
const engine = new RuleEngine(schema, {
220219
maxBatchSize: 1000, // Optional: customize batch size
221220
});
222221

src/v3/engine/rule-engine.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
* Provides type-safe rule evaluation and matching
44
*/
55

6-
import type { AttributeSchema, TypedEntity, TypedRule } from '../types/schema';
6+
import type { AttributeSchema, Entity, Rule } from '../types/schema';
77
import { isValidSchemaObject } from '../types/schema';
88

99
/**
10-
* Configuration interface for the TypedRuleEngine
10+
* Configuration interface for the RuleEngine
1111
*/
12-
interface TypedRuleEngineConfig {
12+
interface RuleEngineConfig {
1313
maxBatchSize?: number;
1414
}
1515

1616
/**
1717
* Enhanced rule engine with generic type support
1818
*/
19-
export class TypedRuleEngine<TSchema extends AttributeSchema> {
20-
private readonly config: Required<TypedRuleEngineConfig>;
19+
export class RuleEngine<TSchema extends AttributeSchema> {
20+
private readonly config: Required<RuleEngineConfig>;
2121

2222
constructor(
2323
private readonly schema: TSchema,
24-
config?: Partial<TypedRuleEngineConfig>,
24+
config?: Partial<RuleEngineConfig>,
2525
) {
2626
this.config = {
2727
maxBatchSize: 1000,
@@ -32,7 +32,7 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
3232
/**
3333
* Evaluates a single rule against an entity
3434
*/
35-
private evaluateRule(entity: TypedEntity<TSchema>, rule: TypedRule<TSchema>): boolean {
35+
private evaluateRule(entity: Entity<TSchema>, rule: Rule<TSchema>): boolean {
3636
// Validate entity against schema
3737
if (!isValidSchemaObject(entity.attributes, this.schema)) {
3838
return false;
@@ -94,15 +94,15 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
9494
/**
9595
* Splits entities into optimal batch sizes based on complexity
9696
*/
97-
private getBatchSize(entities: TypedEntity<TSchema>[], rules: TypedRule<TSchema>[]): number {
97+
private getBatchSize(entities: Entity<TSchema>[], rules: Rule<TSchema>[]): number {
9898
// Start with configured max batch size
9999
const entityCount = entities.length;
100100
let batchSize = this.config.maxBatchSize;
101101

102102
// Adjust based on rule complexity
103103
const ruleComplexity = rules.reduce((complexity, rule) => {
104104
// Count number of conditions in rule
105-
const countConditions = (r: TypedRule<TSchema>): number => {
105+
const countConditions = (r: Rule<TSchema>): number => {
106106
let count = 0;
107107
if (r.and) count += r.and.reduce((sum, subRule) => sum + countConditions(subRule), 0);
108108
if (r.or) count += r.or.reduce((sum, subRule) => sum + countConditions(subRule), 0);
@@ -123,7 +123,7 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
123123
/**
124124
* Processes entities in optimally-sized batches
125125
*/
126-
private processBatch(entities: TypedEntity<TSchema>[], rules: TypedRule<TSchema>[]): boolean[] {
126+
private processBatch(entities: Entity<TSchema>[], rules: Rule<TSchema>[]): boolean[] {
127127
const batchSize = this.getBatchSize(entities, rules);
128128
const results = new Array(entities.length);
129129
const batches = Math.ceil(entities.length / batchSize);
@@ -153,10 +153,7 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
153153
/**
154154
* Finds entities that satisfy all provided 'from' rules
155155
*/
156-
findMatchingFrom(
157-
entities: TypedEntity<TSchema>[],
158-
rules: TypedRule<TSchema>[],
159-
): TypedEntity<TSchema>[] {
156+
findMatchingFrom(entities: Entity<TSchema>[], rules: Rule<TSchema>[]): Entity<TSchema>[] {
160157
const results = this.processBatch(entities, rules);
161158
return entities.filter((_, index) => results[index]);
162159
}
@@ -165,10 +162,10 @@ export class TypedRuleEngine<TSchema extends AttributeSchema> {
165162
* Finds target entities that can be matched with source entities based on 'to' rules
166163
*/
167164
findMatchingTo(
168-
fromEntities: TypedEntity<TSchema>[],
169-
toRules: TypedRule<TSchema>[],
170-
allEntities: TypedEntity<TSchema>[],
171-
): TypedEntity<TSchema>[] {
165+
fromEntities: Entity<TSchema>[],
166+
toRules: Rule<TSchema>[],
167+
allEntities: Entity<TSchema>[],
168+
): Entity<TSchema>[] {
172169
// First, filter out 'from' entities from all entities
173170
const candidateEntities = allEntities.filter(
174171
entity => !fromEntities.some(from => from.id === entity.id),

0 commit comments

Comments
 (0)