Skip to content

Commit

Permalink
Merge pull request #31 from Lazy-work/core/rename_switch_to_match
Browse files Browse the repository at this point in the history
refactor(core): rename switch to match
abdullah-wn authored Jan 25, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 052be75 + a113490 commit 4482b34
Showing 7 changed files with 96 additions and 95 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-seas-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@unisonjs/core': patch
'@unisonjs/vue': patch
---

Rename $switch to $match
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import { $switch } from "../../src";
import { $match } from "../../src";

describe('$match test suite', () => {
it('should match primitive cases', () => {
let input;
let result;
input = 0;
result = $switch(input)
.case(0, 'zero')
.case(1, 'one')
.case(2, 'two')
result = $match(input)
.with(0, 'zero')
.with(1, 'one')
.with(2, 'two')
.default(null);


expect(result).toBe('zero');

input = 1;
result = $switch(input)
.case(0, 'zero')
.case(1, 'one')
.case(2, 'two')
result = $match(input)
.with(0, 'zero')
.with(1, 'one')
.with(2, 'two')
.default(null);


expect(result).toBe('one');

input = 2;
result = $switch(input)
.case(0, 'zero')
.case(1, 'one')
.case(2, 'two')
result = $match(input)
.with(0, 'zero')
.with(1, 'one')
.with(2, 'two')
.default(null);

expect(result).toBe('two');

input = -1;
result = $switch(input)
.case(0, 'zero')
.case(1, 'one')
.case(2, 'two')
result = $match(input)
.with(0, 'zero')
.with(1, 'one')
.with(2, 'two')
.default(null);


@@ -46,63 +46,63 @@ describe('$match test suite', () => {

it('should match grouped primitive cases', () => {
let result;
result = $switch(0)
.case(0, 'zero')
.case(1, 'one')
.case(2, 3, 4, 5, '2-5')
result = $match(0)
.with(0, 'zero')
.with(1, 'one')
.with(2, 3, 4, 5, '2-5')
.default(null);


expect(result).toBe('zero');

result = $switch(1)
.case(0, 'zero')
.case(1, 'one')
.case(2, 3, 4, 5, '2-5')
result = $match(1)
.with(0, 'zero')
.with(1, 'one')
.with(2, 3, 4, 5, '2-5')
.default(null);


expect(result).toBe('one');

result = $switch(2)
.case(0, 'zero')
.case(1, 'one')
.case(2, 3, 4, 5, '2-5')
result = $match(2)
.with(0, 'zero')
.with(1, 'one')
.with(2, 3, 4, 5, '2-5')
.default(null);

expect(result).toBe('2-5');

result = $switch(3)
.case(0, 'zero')
.case(1, 'one')
.case(2, 3, 4, 5, '2-5')
result = $match(3)
.with(0, 'zero')
.with(1, 'one')
.with(2, 3, 4, 5, '2-5')
.default(null);


expect(result).toBe('2-5');

result = $switch(4)
.case(0, 'zero')
.case(1, 'one')
.case(2, 3, 4, 5, '2-5')
result = $match(4)
.with(0, 'zero')
.with(1, 'one')
.with(2, 3, 4, 5, '2-5')
.default(null);


expect(result).toBe('2-5');

result = $switch(5)
.case(0, 'zero')
.case(1, 'one')
.case(2, 3, 4, 5, '2-5')
result = $match(5)
.with(0, 'zero')
.with(1, 'one')
.with(2, 3, 4, 5, '2-5')
.default(null);


expect(result).toBe('2-5');

result = $switch(6)
.case(0, 'zero')
.case(1, 'one')
.case(2, 3, 4, 5, '2-5')
result = $match(6)
.with(0, 'zero')
.with(1, 'one')
.with(2, 3, 4, 5, '2-5')
.default(null);

expect(result).toBe(null);
@@ -141,36 +141,36 @@ describe('$match test suite', () => {
LOADING,
ERROR
}
let result = $switch(pending)
.case({ status: { value: 'success' } }, Result.SUCCESS)
.case({ status: { value: "error" } }, Result.ERROR)
.case({ status: { value: 'pending' } }, Result.LOADING)
let result = $match(pending)
.with({ status: { value: 'success' } }, Result.SUCCESS)
.with({ status: { value: "error" } }, Result.ERROR)
.with({ status: { value: 'pending' } }, Result.LOADING)
.default(null);
expect(result).toBe(Result.LOADING);


result = $switch(error)
.case({ status: { value: 'success' } }, Result.SUCCESS)
.case({ status: { value: "error" } }, Result.ERROR)
.case({ status: { value: 'pending' } }, Result.LOADING)
result = $match(error)
.with({ status: { value: 'success' } }, Result.SUCCESS)
.with({ status: { value: "error" } }, Result.ERROR)
.with({ status: { value: 'pending' } }, Result.LOADING)
.default(null);


expect(result).toBe(Result.ERROR);

result = $switch(success)
.case({ status: { value: 'success' } }, Result.SUCCESS)
.case({ status: { value: "error" } }, Result.ERROR)
.case({ status: { value: 'pending' } }, Result.LOADING)
result = $match(success)
.with({ status: { value: 'success' } }, Result.SUCCESS)
.with({ status: { value: "error" } }, Result.ERROR)
.with({ status: { value: 'pending' } }, Result.LOADING)
.default(null);

expect(result).toBe(Result.SUCCESS);


result = $switch(nope)
.case({ status: { value: 'success' } }, Result.SUCCESS)
.case({ status: { value: "error" } }, Result.ERROR)
.case({ status: { value: 'pending' } }, Result.LOADING)
result = $match(nope)
.with({ status: { value: 'success' } }, Result.SUCCESS)
.with({ status: { value: "error" } }, Result.ERROR)
.with({ status: { value: 'pending' } }, Result.LOADING)
.default(null);


@@ -210,40 +210,39 @@ describe('$match test suite', () => {
LOADING,
ERROR
}
let result = $switch(pending)
.case({ status: 'success' }, Result.SUCCESS)
.case({ status: 'error' }, Result.ERROR)
.case({ status: 'pending' }, Result.LOADING)
let result = $match(pending)
.with({ status: 'success' }, Result.SUCCESS)
.with({ status: 'error' }, Result.ERROR)
.with({ status: 'pending' }, Result.LOADING)
.default(null);
expect(result).toBe(Result.LOADING);


result = $switch(error)
.case({ status: 'success' }, Result.SUCCESS)
.case({ status: 'error' }, Result.ERROR)
.case({ status: 'pending' }, Result.LOADING)
result = $match(error)
.with({ status: 'success' }, Result.SUCCESS)
.with({ status: 'error' }, Result.ERROR)
.with({ status: 'pending' }, Result.LOADING)
.default(null);


expect(result).toBe(Result.ERROR);

result = $switch(success)
.case({ status: 'success' }, Result.SUCCESS)
.case({ status: 'error' }, Result.ERROR)
.case({ status: 'pending' }, Result.LOADING)
result = $match(success)
.with({ status: 'success' }, Result.SUCCESS)
.with({ status: 'error' }, Result.ERROR)
.with({ status: 'pending' }, Result.LOADING)
.default(null);

expect(result).toBe(Result.SUCCESS);


result = $switch(nope)
.case({ status: 'success' }, Result.SUCCESS)
.case({ status: 'error' }, Result.ERROR)
.case({ status: 'pending' }, Result.LOADING)
result = $match(nope)
.with({ status: 'success' }, Result.SUCCESS)
.with({ status: 'error' }, Result.ERROR)
.with({ status: 'pending' }, Result.LOADING)
.default(null);


expect(result).toBe(null);
})

})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class LogicalEvaluation {
export default class IfEvaluation {
#state = false;
#onTrue: any;
#alternative: any;
@@ -31,6 +31,4 @@ class LogicalEvaluation {
if (typeof result === "function") return result();
return result;
}
}

export default LogicalEvaluation;
}
Original file line number Diff line number Diff line change
@@ -2,14 +2,14 @@ type DeepPartial<T> = T extends object ? {
[P in keyof T]?: DeepPartial<T[P]>;
} : T;
type CaseParams<T> = T extends object ? [...(DeepPartial<T> | boolean)[], any] : [...(T | boolean)[], any];
class SwitchEvaluation<T> {
export default class MatchEvaluation<T> {
#input: T;
#result: any;
constructor(input: T) {
this.#input = input;
}

case(...args: CaseParams<T>) {
with(...args: CaseParams<T>) {
const patterns = args.slice(0, -1);
const expression = args.at(-1);
if (this.#result !== undefined) return this;
@@ -57,6 +57,4 @@ class SwitchEvaluation<T> {
if (typeof result === "function") return result(this.#input);
return result;
}
}

export default SwitchEvaluation;
}
10 changes: 5 additions & 5 deletions packages/core/src/conditional/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import LogicalEvaluation from "./LogicalEvaluation";
import MatchingEvaluation from "./SwitchEvaluation";
import IfEvaluation from "./IfEvaluation";
import MatchingEvaluation from "./MatchEvaluation";

export function $if(bool: boolean) {
return new LogicalEvaluation(bool);
return new IfEvaluation(bool);
}

export function $switch<T>(input: T) {
export function $match<T>(input: T) {
return new MatchingEvaluation(input);
}

export function v<T>(value: T) {
return { value };
}

export type { LogicalEvaluation, MatchingEvaluation };
export type { IfEvaluation, MatchingEvaluation };
2 changes: 1 addition & 1 deletion packages/vue/src/index.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ export {
createReactHook,
useUnison,
$if,
$switch,
$match,
v,
nextTick,
getCurrentInstance,
4 changes: 2 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -36,8 +36,8 @@ export default defineConfig({
setupFiles: 'scripts/setup-vitest.ts',
environment: 'jsdom',
include: [
'packages/core/__tests__/*.spec.{js,jsx,ts,tsx}',
'packages/vue/__tests__/*.spec.{js,jsx,ts,tsx}',
'packages/core/__tests__/**/*.spec.{js,jsx,ts,tsx}',
'packages/vue/__tests__/**/*.spec.{js,jsx,ts,tsx}',
],
},
})

0 comments on commit 4482b34

Please sign in to comment.