Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0b88e4

Browse files
committed
fix(state): shorthand camelCase support for side effects
This repairs an issue where the on shorthand for side effects only supports single case lower case action. Repo step https://stackblitz.com/edit/stackblitz-starters-ltqcqj?file=src%2Fmain.ts,src%2Fdata%2Fstate.ts
1 parent cc6dc98 commit c0b88e4

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

libs/state/actions/src/lib/proxy.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ export function actionProxyHandler<T extends object, U extends object>({
7171

7272
// the user wants to get a single EventEmitter and trigger a side effect on event emission
7373
if (prop.toString().startsWith('on')) {
74-
const propName = prop.toString().slice(2).toLowerCase() as KeysOfT;
74+
// we need to first remove the 'on' from the the prop name
75+
const slicedPropName = prop.toString().slice(2);
76+
// now convert the slicedPropName to camelcase
77+
const propName = (slicedPropName.charAt(0).toLowerCase() +
78+
slicedPropName.slice(1)) as KeysOfT;
7579
return (
7680
behaviour: OperatorFunction<T[KeysOfT], T[KeysOfT]>,
7781
sf: (v: T[KeysOfT]) => void

libs/state/actions/src/lib/rx-actions.spec.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,17 @@ describe('actions fn', () => {
9090
const t = { se: () => void 0 };
9191
const dummyBehaviour = (o$) => o$;
9292
const spyT = jest.fn((_: any) => void 0);
93+
const spyF = jest.fn((_: any) => void 0);
9394

94-
const sub = component.actions.onProp(dummyBehaviour, spyT);
95+
component.actions.onProp(dummyBehaviour, spyT);
9596
component.actions.prop('p');
9697
expect(spyT).toBeCalledTimes(1);
9798
expect(spyT).toBeCalledWith('p');
99+
100+
component.actions.onLongPropName(dummyBehaviour, spyF);
101+
component.actions.longPropName('p');
102+
expect(spyF).toBeCalledTimes(1);
103+
expect(spyF).toBeCalledWith('p');
98104
});
99105

100106
it('should apply behaviour to trigger', () => {
@@ -200,7 +206,13 @@ describe('actions fn', () => {
200206
});
201207
});
202208

203-
type Actions = { prop: string; prop2: string; search: string; resize: number };
209+
type Actions = {
210+
prop: string;
211+
prop2: string;
212+
search: string;
213+
resize: number;
214+
longPropName: string;
215+
};
204216
function setupComponent<
205217
Actions extends object,
206218
Transforms extends ActionTransforms<Actions> = {}

0 commit comments

Comments
 (0)