pure-effect 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +120 -0
- package/index.js +32 -0
- package/package.json +16 -0
- package/test/all.js +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Aycan Gulez
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Pure Effect
|
|
2
|
+
|
|
3
|
+
**Pure Effect** is a tiny, zero-dependency effect system for writing pure, testable JavaScript without mocks.
|
|
4
|
+
|
|
5
|
+
It implements the "Functional Core, Imperative Shell" pattern, allowing you to decouple your business logic from external side effects like database calls or API requests. Instead of executing side effects immediately, your functions return Commands which are executed later by an interpreter.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install pure-effect
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
Here is a complete example of a User Registration flow.
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
import { Success, Failure, Command, effectPipe, runEffect } from 'pure-effect';
|
|
19
|
+
|
|
20
|
+
const validateRegistration = (input) => {
|
|
21
|
+
if (!input.email.includes('@')) return Failure('Invalid email.');
|
|
22
|
+
if (input.password.length < 8) return Failure('Password too short.');
|
|
23
|
+
return Success(input);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// These functions do NOT run the DB call. They return a Command object.
|
|
27
|
+
// The 'next' function defines what happens with the result of the async call.
|
|
28
|
+
const findUser = (email) => {
|
|
29
|
+
const cmd = () => db.findUser(email); // The work to do later
|
|
30
|
+
const next = (user) => Success(user); // Wrap result in Success
|
|
31
|
+
return Command(cmd, next);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const saveUser = (input) => {
|
|
35
|
+
const cmd = () => db.saveUser(input);
|
|
36
|
+
const next = (saved) => Success(saved);
|
|
37
|
+
return Command(cmd, next);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const ensureEmailAvailable = (user) => {
|
|
41
|
+
return user ? Failure('Email already in use.') : Success(true);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// The Pipeline uses arrow functions to capture 'input' from the scope where needed.
|
|
45
|
+
const registerUserFlow = (input) =>
|
|
46
|
+
effectPipe(
|
|
47
|
+
validateRegistration,
|
|
48
|
+
() => findUser(input.email),
|
|
49
|
+
ensureEmailAvailable,
|
|
50
|
+
() => saveUser(input)
|
|
51
|
+
)(input);
|
|
52
|
+
|
|
53
|
+
// The Imperative Shell
|
|
54
|
+
async function main() {
|
|
55
|
+
const input = { email: 'new@test.com', password: 'password123' };
|
|
56
|
+
|
|
57
|
+
// logic is just a data structure until we pass it to runEffect
|
|
58
|
+
const logic = registerUserFlow(input);
|
|
59
|
+
|
|
60
|
+
// runEffect performs the actual async work
|
|
61
|
+
const result = await runEffect(logic);
|
|
62
|
+
|
|
63
|
+
if (result.type === 'Success') {
|
|
64
|
+
console.log('User created:', result.value);
|
|
65
|
+
} else {
|
|
66
|
+
console.error('Error:', result.error);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
main();
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Testing Without Mocks
|
|
74
|
+
|
|
75
|
+
The biggest benefit of **Pure Effect** is testability. Because `registerUserFlow` returns a data structure (a tree of objects) instead of running a Promise, you can test your logic without mocking the database.
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
import assert from 'assert';
|
|
79
|
+
|
|
80
|
+
// 1. Test Validation Failure
|
|
81
|
+
const badInput = { email: 'bad-email', password: '123' };
|
|
82
|
+
const result = registerUserFlow(badInput);
|
|
83
|
+
|
|
84
|
+
assert.deepEqual(result, Failure('Invalid email.'));
|
|
85
|
+
// ✅ Logic tested instantly, no async needed.
|
|
86
|
+
|
|
87
|
+
// 2. Test Flow Intent (Introspection)
|
|
88
|
+
const goodInput = { email: 'test@test.com', password: 'password123' };
|
|
89
|
+
const step1 = registerUserFlow(goodInput);
|
|
90
|
+
|
|
91
|
+
// Check if the first thing the code does is try to find a user
|
|
92
|
+
assert.equal(step1.type, 'Command');
|
|
93
|
+
assert.equal(step1.cmd.name, 'findUser');
|
|
94
|
+
// ✅ We verified the *intent* of the code without touching a real DB.
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## API Reference
|
|
98
|
+
|
|
99
|
+
### `Success(value)`
|
|
100
|
+
|
|
101
|
+
Returns an object `{ type: 'Success', value }`. Represents a successful computation.
|
|
102
|
+
|
|
103
|
+
### `Failure(error)`
|
|
104
|
+
|
|
105
|
+
Returns an object `{ type: 'Failure', error }`. Represents a failed computation. Stops the pipeline immediately.
|
|
106
|
+
|
|
107
|
+
### `Command(cmdFn, nextFn)`
|
|
108
|
+
|
|
109
|
+
Returns an object `{ type: 'Command', cmd, next }`.
|
|
110
|
+
|
|
111
|
+
- `cmdFn`: A function (sync or async) that performs the side effect.
|
|
112
|
+
- `nextFn`: A function that receives the result of `cmdFn` and returns the next Effect (Success, Failure, or another Command).
|
|
113
|
+
|
|
114
|
+
### `effectPipe(...functions)`
|
|
115
|
+
|
|
116
|
+
A combinator that runs functions in sequence. It automatically handles unpacking `Success` values and passing them to the next function. If a `Failure` occurs, the pipe stops.
|
|
117
|
+
|
|
118
|
+
### `runEffect(effect)`
|
|
119
|
+
|
|
120
|
+
The interpreter. It takes an `effect` object, executes any nested Commands recursively using `async/await`, and returns the final `Success` or `Failure`.
|
package/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const Success = (value) => ({ type: 'Success', value });
|
|
2
|
+
const Failure = (error) => ({ type: 'Failure', error });
|
|
3
|
+
const Command = (cmd, next) => ({ type: 'Command', cmd, next });
|
|
4
|
+
|
|
5
|
+
const chain = (effect, fn) => {
|
|
6
|
+
switch (effect.type) {
|
|
7
|
+
case 'Success':
|
|
8
|
+
return fn(effect.value);
|
|
9
|
+
case 'Failure':
|
|
10
|
+
return effect;
|
|
11
|
+
case 'Command':
|
|
12
|
+
const next = (result) => chain(effect.next(result), fn);
|
|
13
|
+
return Command(effect.cmd, next);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const effectPipe = (...fns) => {
|
|
18
|
+
return (start) => fns.reduce(chain, Success(start));
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
async function runEffect(effect) {
|
|
22
|
+
while (effect.type === 'Command') {
|
|
23
|
+
try {
|
|
24
|
+
effect = effect.next(await effect.cmd());
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return Failure(e);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return effect;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { Success, Failure, Command, effectPipe, runEffect };
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pure-effect",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A tiny, zero-dependency effect system for writing pure, testable JavaScript without mocks.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": "./index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "mocha"
|
|
9
|
+
},
|
|
10
|
+
"author": "Aycan Gulez",
|
|
11
|
+
"homepage": "https://github.com/aycangulez/pure-effect",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"mocha": "^11.7.5"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/test/all.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { strict as assert } from 'assert';
|
|
2
|
+
import { Success, Failure, Command, effectPipe, runEffect } from '../index.js';
|
|
3
|
+
|
|
4
|
+
const db = {
|
|
5
|
+
users: new Map(),
|
|
6
|
+
async findUserByEmail(email) {
|
|
7
|
+
return this.users.get(email) || null;
|
|
8
|
+
},
|
|
9
|
+
async saveUser(user) {
|
|
10
|
+
const u = { id: Date.now(), ...user };
|
|
11
|
+
this.users.set(user.email, u);
|
|
12
|
+
return u;
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function validateRegistration(input) {
|
|
17
|
+
const { email, password } = input;
|
|
18
|
+
if (!email?.includes('@')) {
|
|
19
|
+
return Failure('Invalid email format.');
|
|
20
|
+
}
|
|
21
|
+
if (password?.length < 8) {
|
|
22
|
+
return Failure('Password must be at least 8 characters long.');
|
|
23
|
+
}
|
|
24
|
+
return Success(input);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function findUserByEmail(email) {
|
|
28
|
+
const cmdFindUser = () => db.findUserByEmail(email);
|
|
29
|
+
const next = (foundUser) => Success(foundUser);
|
|
30
|
+
return Command(cmdFindUser, next);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function ensureEmailIsAvailable(foundUser) {
|
|
34
|
+
return foundUser ? Failure('Email already in use.') : Success(true);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function saveUser(input) {
|
|
38
|
+
const { email, password } = input;
|
|
39
|
+
const hashedPassword = `hashed_${password}`;
|
|
40
|
+
const userToSave = { email, password: hashedPassword };
|
|
41
|
+
const cmdSaveUser = () => db.saveUser(userToSave);
|
|
42
|
+
const next = (savedUser) => Success(savedUser);
|
|
43
|
+
return Command(cmdSaveUser, next);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const registerUserFlow = (input) =>
|
|
47
|
+
effectPipe(
|
|
48
|
+
validateRegistration,
|
|
49
|
+
() => findUserByEmail(input.email),
|
|
50
|
+
ensureEmailIsAvailable,
|
|
51
|
+
() => saveUser(input)
|
|
52
|
+
)(input);
|
|
53
|
+
|
|
54
|
+
async function registerUser(input) {
|
|
55
|
+
return await runEffect(registerUserFlow(input));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
describe('Pure Effect', function () {
|
|
59
|
+
it('should return Failure when e-mail is invalid', async function () {
|
|
60
|
+
const input = { email: 'bad-email', password: '123' };
|
|
61
|
+
const effect = await registerUser(input);
|
|
62
|
+
assert.deepEqual(effect, Failure('Invalid email format.'));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should walk through the call tree', async function () {
|
|
66
|
+
const input = { email: 'test@test.com', password: 'password123' };
|
|
67
|
+
const step1 = registerUserFlow(input);
|
|
68
|
+
assert.equal(step1.type, 'Command');
|
|
69
|
+
assert.equal(step1.cmd.name, 'cmdFindUser');
|
|
70
|
+
|
|
71
|
+
const step2 = step1.next(null);
|
|
72
|
+
assert.equal(step2.type, 'Command');
|
|
73
|
+
assert.equal(step2.cmd.name, 'cmdSaveUser');
|
|
74
|
+
});
|
|
75
|
+
});
|