Skip to content
This repository was archived by the owner on Jul 8, 2021. It is now read-only.

Commit 85ea4aa

Browse files
committed
refactor: address all issue in #14
1 parent 1410180 commit 85ea4aa

9 files changed

+46
-42
lines changed

package.json

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "ng-packagr -p package.json",
8-
"test": "ngc -p tsconfig.json && jasmine dist/*.spec.js",
8+
"test": "ngc -p tsconfig.spec.json && jasmine dist/spec/*.spec.js",
99
"publish": "npm publish dist",
1010
"precommit": "lint-staged",
1111
"lint": "tslint -c tslint.json 'src/**/*.ts'",
@@ -15,13 +15,7 @@
1515
"type": "git",
1616
"url": "git+https://[email protected]/amcdnl/ngrx-actions.git"
1717
},
18-
"keywords": [
19-
"ngrx",
20-
"redux",
21-
"state",
22-
"store",
23-
"state-mangement"
24-
],
18+
"keywords": ["ngrx", "redux", "state", "store", "state-mangement"],
2519
"author": "Austin McDaniel",
2620
"license": "MIT",
2721
"bugs": {
@@ -59,10 +53,7 @@
5953
}
6054
},
6155
"lint-staged": {
62-
"*.ts": [
63-
"npm run prettier",
64-
"git add"
65-
]
56+
"*.ts": ["npm run prettier", "git add"]
6657
},
6758
"prettier": {
6859
"singleQuote": true,

src/internals.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Action } from '@ngrx/store';
21
import { ActionType } from './symbols';
32

43
export interface ActionMeta {

src/of-action.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Action } from '@ngrx/store';
2-
import { Actions } from '@ngrx/effects';
32
import { filter } from 'rxjs/operators';
4-
import { Observable } from 'rxjs/Observable';
53
import { OperatorFunction } from 'rxjs/interfaces';
64
import { ActionType } from '.';
75

src/select.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { Injectable } from '@angular/core';
22
import { Store, Selector } from '@ngrx/store';
3-
import { Observable } from 'rxjs/Observable';
43

54
@Injectable()
65
export class NgrxSelect {
7-
static store: Store<any> = undefined;
6+
static store: Store<any> | undefined = undefined;
87
connect(store: Store<any>) {
98
NgrxSelect.store = store;
109
}
@@ -18,22 +17,23 @@ export function Select<TState = any, TValue = any>(
1817
...paths: string[]
1918
): (target: any, name: string) => void;
2019
export function Select<TState = any, TValue = any>(
21-
selectorOrFeature: string | Selector<TState, TValue>,
20+
selectorOrFeature?: string | Selector<TState, TValue>,
2221
...paths: string[]
2322
) {
2423
return function(target: any, name: string): void {
2524
let fn: Selector<TState, TValue>;
2625
// Nothing here? Use propery name as selector
27-
if (typeof selectorOrFeature === 'undefined') {
26+
if (!selectorOrFeature) {
2827
selectorOrFeature = name;
2928
}
3029
// Handle string vs Selector<TState, TValue>
3130
if (typeof selectorOrFeature === 'string') {
32-
if (paths.length) {
33-
selectorOrFeature = [selectorOrFeature, ...paths].join('.');
31+
let propsArray = [selectorOrFeature, ...paths];
32+
if (!paths.length) {
33+
propsArray = selectorOrFeature.split('.');
3434
}
35-
fn = fastPropGetter(selectorOrFeature);
36-
} else {
35+
fn = fastPropGetter(propsArray);
36+
} else if (typeof selectorOrFeature === 'function') {
3737
fn = selectorOrFeature;
3838
}
3939
// Redefine property
@@ -55,21 +55,21 @@ export function Select<TState = any, TValue = any>(
5555
}
5656

5757
/**
58-
* The generated function is faster then:
58+
* The generated function is faster than:
5959
* - pluck (Observable operator)
6060
* - memoize (old ngrx-actions implementation)
6161
* - MemoizedSelector (ngrx)
6262
* @param path
6363
*/
64-
function fastPropGetter(path: string): (x: any) => any {
65-
const segments = path.split('.');
64+
export function fastPropGetter(paths: string[]): (x: any) => any {
65+
const segments = paths;
6666
let seg = 'store.' + segments[0],
67-
i = 0,
68-
l = segments.length;
67+
i = 0;
68+
const l = segments.length;
6969
let expr = seg;
7070
while (++i < l) {
7171
expr = expr + ' && ' + (seg = seg + '.' + segments[i]);
7272
}
73-
const fn = new Function('store', 'return ' + expr + '');
73+
const fn = new Function('store', 'return ' + expr + ';');
7474
return <(x: any) => any>fn;
7575
}

src/index.spec.ts src/spec/index.spec.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Store, createReducer, Action, ofAction, Select, NgrxSelect } from './index';
2-
import { Action as NgRxAction, Store as NgRxStore, createFeatureSelector, createSelector } from '@ngrx/store';
1+
import { Store, createReducer, Action, ofAction, Select, NgrxSelect } from '../index';
2+
import { Action as NgRxAction, createFeatureSelector, createSelector, Store as NgRxStore } from '@ngrx/store';
33
import { Observable } from 'rxjs/Observable';
44
import { of } from 'rxjs/observable/of';
55

@@ -26,9 +26,9 @@ describe('actions', () => {
2626
}
2727
}
2828

29-
const reducer = createReducer<FooState>(Bar);
29+
const reducer = createReducer<FooState | undefined>(Bar);
3030
const res = reducer(undefined, new MyAction());
31-
expect(res.foo).toBe(true);
31+
expect(res && res.foo).toBe(true);
3232
});
3333

3434
it('adds defaults', () => {
@@ -146,7 +146,7 @@ describe('actions', () => {
146146
action2 = new MyAction2(),
147147
action3 = new MyAction3('a', 0);
148148
const actions = of<NgRxAction>(action, action2, action3);
149-
let tappedActions: NgRxAction[] = [];
149+
const tappedActions: NgRxAction[] = [];
150150
actions.pipe(ofAction<MyAction | MyAction2>(MyAction, MyAction2)).subscribe(a => {
151151
tappedActions.push(a);
152152
});
@@ -157,7 +157,7 @@ describe('actions', () => {
157157
});
158158

159159
it('selects sub state', () => {
160-
const state: {
160+
const featureState: {
161161
myFeature: FooState;
162162
} = {
163163
myFeature: {
@@ -183,7 +183,7 @@ describe('actions', () => {
183183
@Select(msBar) bar$: Observable<any>; // using MemoizedSelector
184184
}
185185

186-
const store = new NgRxStore(of(state), undefined, undefined);
186+
const store = new NgRxStore(of(featureState), undefined, undefined);
187187

188188
try {
189189
NgrxSelect.store = store;
@@ -195,11 +195,11 @@ describe('actions', () => {
195195
});
196196

197197
mss.myFeature.subscribe(n => {
198-
expect(n).toBe(state.myFeature);
198+
expect(n).toBe(featureState.myFeature);
199199
});
200200

201201
mss.bar$.subscribe(n => {
202-
expect(n).toBe(state.myFeature.bar);
202+
expect(n).toBe(featureState.myFeature.bar);
203203
});
204204
} finally {
205205
NgrxSelect.store = undefined;

src/spec/tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* This file is for IDE only */
2+
{
3+
"extends": "../../tsconfig.spec.json"
4+
}

src/store.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { INITIAL_STATE_KEY } from './keys';
2-
import { Action } from '@ngrx/store';
32

4-
export function Store<TState>(initialState?: TState): (target: Function) => void;
5-
export function Store(initialState?: any): (target: Function) => void;
6-
export function Store(initialState: any = {}) {
3+
export function Store<TState extends object>(initialState?: TState): (target: Function) => void;
4+
export function Store(initialState?: object): (target: Function) => void;
5+
export function Store(initialState: object = {}) {
76
return function(target: Function) {
87
Reflect.defineMetadata(INITIAL_STATE_KEY, initialState, target);
98
};

tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"rootDir": "src/",
1515
"skipDefaultLibCheck": true,
1616
"skipLibCheck": true,
17+
"noImplicitAny": false,
18+
"noUnusedLocals": true,
19+
"strict": true,
1720
"sourceMap": true,
1821
"target": "es5",
1922
"types": [
@@ -23,5 +26,8 @@
2326
},
2427
"include": [
2528
"src/**/*"
29+
],
30+
"exclude": [
31+
"src/spec/**/*"
2632
]
2733
}

tsconfig.spec.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"strictNullChecks": false
5+
},
6+
"files": ["src/spec/index.spec.ts"]
7+
}

0 commit comments

Comments
 (0)