Skip to main content

Contributing Code

Development workflow​

  1. Fork the repository on GitHub
git clone <https://github.com/YOUR_USERNAME/refract.git>
cd refract
  1. Create a feature branch from main
git checkout -b feature/my-awesome-feature
  1. Make your changes with clear, focused commits
git add .
git commit -m "feat: add awesome new feature"
  1. Add tests for new features

  2. Update documentation if needed

  3. Submit a pull request

git push origin feature/my-awesome-feature

Code style guidelines​

Follow these consistent coding standards to keep code quality:

  1. ✅ Good - clear function names and documentation
   /**
- Creates a new refraction with the given initial value
- @param {T} initialValue - The initial value for the refraction
- @returns {Refraction<T>} A new refraction instance
*/

export function createRefraction(initialValue) {
return new Refraction(initialValue);
}
  1. ✅ Good - consistent error handling
 export function validateComponent(component) {
if (typeof component !== 'function') {
throw new RefractError(
'Component must be a function',
'INVALID_COMPONENT_TYPE'
);
}
}
  1. ✅ Good - clear variable names
const isComponentMounted = checkMountStatus(component);
const hasValidProps = validateProps(props);
  1. ❌ Avoid - unclear names and missing documentation
function cr(iv) {
return new R(iv);
}

const x = check(c);

Testing guidelines​

All contributions should include appropriate tests, e.g:

Unit test example

describe("createRefraction", () => {
it("should create a refraction with initial value", () => {
const refraction = createRefraction(42);
expect(refraction.value).toBe(42);
});

it("should notify subscribers when value changes", () => {
const refraction = createRefraction(0);
const mockCallback = jest.fn();

refraction.subscribe(mockCallback);
refraction.set(10);

expect(mockCallback).toHaveBeenCalledWith(10);
});

it("should handle complex objects", () => {
const initialState = { count: 0, name: "test" };
const refraction = createRefraction(initialState);

expect(refraction.value).toEqual(initialState);
expect(refraction.value).not.toBe(initialState); // Should be immutable
});
});