Skip to content

Commit f43f98e

Browse files
committed
refactor: use constants for all property names and update TypeScript definitions
1 parent b41cb87 commit f43f98e

6 files changed

Lines changed: 94 additions & 74 deletions

File tree

AGENTS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ npm run benchmark # Run benchmarks
2727
- Keep functions small and focused
2828
- Use template literals for string concatenation
2929

30+
## Rules
31+
- No magic strings or magic numbers - always use constants from `src/constants.js`
32+
- All string literals must be defined as constants with descriptive names (e.g., `STRING_EMPTY`, `STRING_ID`)
33+
- All numeric literals (except 0 and 1 in simple operations) should use constants (e.g., `INT_0`, `CACHE_SIZE_DEFAULT`)
34+
- Constants follow naming convention: `TYPE_NAME` for strings, `TYPE_NAME` for numbers
35+
3036
## Testing
3137
- Tests use Node.js native test runner (`node --test`)
3238
- Test files are in `tests/unit/` directory

dist/haro.cjs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ const STRING_EMPTY = "";
1616
const STRING_PIPE = "|";
1717
const STRING_DOUBLE_PIPE = "||";
1818
const STRING_DOUBLE_AND = "&&";
19-
20-
// String constants - Operation and type names
21-
const STRING_ID = "id";
2219
const STRING_FUNCTION = "function";
20+
const STRING_ID = "id";
2321
const STRING_INDEXES = "indexes";
2422
const STRING_OBJECT = "object";
2523
const STRING_RECORDS = "records";
@@ -68,6 +66,16 @@ const STRING_ERROR_SORT_FN_TYPE = "sort: fn must be a function";
6866
const STRING_ERROR_WHERE_OP_TYPE = "where: op must be a string";
6967
const STRING_ERROR_WHERE_PREDICATE_TYPE = "where: predicate must be an object";
7068

69+
// String constants - Property names
70+
const PROP_DELIMITER = "delimiter";
71+
const PROP_ID = "id";
72+
const PROP_IMMUTABLE = "immutable";
73+
const PROP_INDEX = "index";
74+
const PROP_KEY = "key";
75+
const PROP_VERSIONING = "versioning";
76+
const PROP_VERSIONS = "versions";
77+
const PROP_WARN_ON_FULL_SCAN = "warnOnFullScan";
78+
7179
/**
7280
* Haro is an immutable DataStore with indexing, versioning, and batch operations.
7381
* Provides a Map-like interface with advanced querying capabilities.
@@ -138,35 +146,35 @@ class Haro {
138146
enumerable: true,
139147
get: () => this.#data.size,
140148
});
141-
Object.defineProperty(this, "key", {
149+
Object.defineProperty(this, PROP_KEY, {
142150
enumerable: true,
143151
get: () => this.#key,
144152
});
145-
Object.defineProperty(this, "index", {
153+
Object.defineProperty(this, PROP_INDEX, {
146154
enumerable: true,
147155
get: () => [...this.#index],
148156
});
149-
Object.defineProperty(this, "delimiter", {
157+
Object.defineProperty(this, PROP_DELIMITER, {
150158
enumerable: true,
151159
get: () => this.#delimiter,
152160
});
153-
Object.defineProperty(this, "immutable", {
161+
Object.defineProperty(this, PROP_IMMUTABLE, {
154162
enumerable: true,
155163
get: () => this.#immutable,
156164
});
157-
Object.defineProperty(this, "versioning", {
165+
Object.defineProperty(this, PROP_VERSIONING, {
158166
enumerable: true,
159167
get: () => this.#versioning,
160168
});
161-
Object.defineProperty(this, "warnOnFullScan", {
169+
Object.defineProperty(this, PROP_WARN_ON_FULL_SCAN, {
162170
enumerable: true,
163171
get: () => this.#warnOnFullScan,
164172
});
165-
Object.defineProperty(this, "versions", {
173+
Object.defineProperty(this, PROP_VERSIONS, {
166174
enumerable: true,
167175
get: () => this.#versions,
168176
});
169-
Object.defineProperty(this, "id", {
177+
Object.defineProperty(this, PROP_ID, {
170178
enumerable: true,
171179
get: () => this.#id,
172180
});

dist/haro.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ const STRING_EMPTY = "";
1111
const STRING_PIPE = "|";
1212
const STRING_DOUBLE_PIPE = "||";
1313
const STRING_DOUBLE_AND = "&&";
14-
15-
// String constants - Operation and type names
16-
const STRING_ID = "id";
1714
const STRING_FUNCTION = "function";
15+
const STRING_ID = "id";
1816
const STRING_INDEXES = "indexes";
1917
const STRING_OBJECT = "object";
2018
const STRING_RECORDS = "records";
@@ -61,7 +59,17 @@ const STRING_ERROR_SET_KEY_TYPE = "set: key must be a string or number";
6159
const STRING_ERROR_SET_DATA_TYPE = "set: data must be an object";
6260
const STRING_ERROR_SORT_FN_TYPE = "sort: fn must be a function";
6361
const STRING_ERROR_WHERE_OP_TYPE = "where: op must be a string";
64-
const STRING_ERROR_WHERE_PREDICATE_TYPE = "where: predicate must be an object";/**
62+
const STRING_ERROR_WHERE_PREDICATE_TYPE = "where: predicate must be an object";
63+
64+
// String constants - Property names
65+
const PROP_DELIMITER = "delimiter";
66+
const PROP_ID = "id";
67+
const PROP_IMMUTABLE = "immutable";
68+
const PROP_INDEX = "index";
69+
const PROP_KEY = "key";
70+
const PROP_VERSIONING = "versioning";
71+
const PROP_VERSIONS = "versions";
72+
const PROP_WARN_ON_FULL_SCAN = "warnOnFullScan";/**
6573
* Haro is an immutable DataStore with indexing, versioning, and batch operations.
6674
* Provides a Map-like interface with advanced querying capabilities.
6775
* @class
@@ -131,35 +139,35 @@ class Haro {
131139
enumerable: true,
132140
get: () => this.#data.size,
133141
});
134-
Object.defineProperty(this, "key", {
142+
Object.defineProperty(this, PROP_KEY, {
135143
enumerable: true,
136144
get: () => this.#key,
137145
});
138-
Object.defineProperty(this, "index", {
146+
Object.defineProperty(this, PROP_INDEX, {
139147
enumerable: true,
140148
get: () => [...this.#index],
141149
});
142-
Object.defineProperty(this, "delimiter", {
150+
Object.defineProperty(this, PROP_DELIMITER, {
143151
enumerable: true,
144152
get: () => this.#delimiter,
145153
});
146-
Object.defineProperty(this, "immutable", {
154+
Object.defineProperty(this, PROP_IMMUTABLE, {
147155
enumerable: true,
148156
get: () => this.#immutable,
149157
});
150-
Object.defineProperty(this, "versioning", {
158+
Object.defineProperty(this, PROP_VERSIONING, {
151159
enumerable: true,
152160
get: () => this.#versioning,
153161
});
154-
Object.defineProperty(this, "warnOnFullScan", {
162+
Object.defineProperty(this, PROP_WARN_ON_FULL_SCAN, {
155163
enumerable: true,
156164
get: () => this.#warnOnFullScan,
157165
});
158-
Object.defineProperty(this, "versions", {
166+
Object.defineProperty(this, PROP_VERSIONS, {
159167
enumerable: true,
160168
get: () => this.#versions,
161169
});
162-
Object.defineProperty(this, "id", {
170+
Object.defineProperty(this, PROP_ID, {
163171
enumerable: true,
164172
get: () => this.#id,
165173
});

src/constants.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ export const STRING_DOUBLE_PIPE = "||";
66
export const STRING_DOUBLE_AND = "&&";
77

88
// String constants - Operation and type names
9-
export const STRING_ID = "id";
109
export const STRING_DEL = "del";
1110
export const STRING_FUNCTION = "function";
11+
export const STRING_ID = "id";
12+
export const STRING_INDEX = "index";
1213
export const STRING_INDEXES = "indexes";
14+
export const STRING_KEY = "key";
1315
export const STRING_OBJECT = "object";
1416
export const STRING_RECORDS = "records";
1517
export const STRING_REGISTRY = "registry";
@@ -57,3 +59,13 @@ export const STRING_ERROR_SET_DATA_TYPE = "set: data must be an object";
5759
export const STRING_ERROR_SORT_FN_TYPE = "sort: fn must be a function";
5860
export const STRING_ERROR_WHERE_OP_TYPE = "where: op must be a string";
5961
export const STRING_ERROR_WHERE_PREDICATE_TYPE = "where: predicate must be an object";
62+
63+
// String constants - Property names
64+
export const PROP_DELIMITER = "delimiter";
65+
export const PROP_ID = "id";
66+
export const PROP_IMMUTABLE = "immutable";
67+
export const PROP_INDEX = "index";
68+
export const PROP_KEY = "key";
69+
export const PROP_VERSIONING = "versioning";
70+
export const PROP_VERSIONS = "versions";
71+
export const PROP_WARN_ON_FULL_SCAN = "warnOnFullScan";

src/haro.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import {
44
CACHE_SIZE_DEFAULT,
55
INT_0,
66
INT_2,
7+
PROP_DELIMITER,
8+
PROP_ID,
9+
PROP_IMMUTABLE,
10+
PROP_INDEX,
11+
PROP_KEY,
12+
PROP_VERSIONING,
13+
PROP_VERSIONS,
14+
PROP_WARN_ON_FULL_SCAN,
715
STRING_CACHE_DOMAIN_SEARCH,
816
STRING_CACHE_DOMAIN_WHERE,
917
STRING_COMMA,
@@ -114,35 +122,35 @@ export class Haro {
114122
enumerable: true,
115123
get: () => this.#data.size,
116124
});
117-
Object.defineProperty(this, "key", {
125+
Object.defineProperty(this, PROP_KEY, {
118126
enumerable: true,
119127
get: () => this.#key,
120128
});
121-
Object.defineProperty(this, "index", {
129+
Object.defineProperty(this, PROP_INDEX, {
122130
enumerable: true,
123131
get: () => [...this.#index],
124132
});
125-
Object.defineProperty(this, "delimiter", {
133+
Object.defineProperty(this, PROP_DELIMITER, {
126134
enumerable: true,
127135
get: () => this.#delimiter,
128136
});
129-
Object.defineProperty(this, "immutable", {
137+
Object.defineProperty(this, PROP_IMMUTABLE, {
130138
enumerable: true,
131139
get: () => this.#immutable,
132140
});
133-
Object.defineProperty(this, "versioning", {
141+
Object.defineProperty(this, PROP_VERSIONING, {
134142
enumerable: true,
135143
get: () => this.#versioning,
136144
});
137-
Object.defineProperty(this, "warnOnFullScan", {
145+
Object.defineProperty(this, PROP_WARN_ON_FULL_SCAN, {
138146
enumerable: true,
139147
get: () => this.#warnOnFullScan,
140148
});
141-
Object.defineProperty(this, "versions", {
149+
Object.defineProperty(this, PROP_VERSIONS, {
142150
enumerable: true,
143151
get: () => this.#versions,
144152
});
145-
Object.defineProperty(this, "id", {
153+
Object.defineProperty(this, PROP_ID, {
146154
enumerable: true,
147155
get: () => this.#id,
148156
});

0 commit comments

Comments
 (0)