Skip to content

Commit

Permalink
Add tslint and prettier.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmackrodt committed Jan 7, 2019
1 parent c1cee71 commit 9fb2b4f
Show file tree
Hide file tree
Showing 28 changed files with 304 additions and 133 deletions.
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "all",
"singleQuote": true,
"jsxSingleQuote": true
}
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"example": "yarn build && cd packages/example && yarn start",
"bootstrap": "lerna bootstrap",
"test": "jest --coverage",
"travis": "yarn test",
"coveralls": "yarn test && cat ./coverage/lcov.info | coveralls"
"lint": "tslint 'packages/**/*.{ts,tsx}' -c ./tslint.json -e \"**/node_modules/**\"",
"travis": "yarn lint && yarn test",
"coveralls": "yarn lint && yarn test && cat ./coverage/lcov.info | coveralls"
},
"author": "",
"license": "ISC",
Expand All @@ -22,7 +23,12 @@
"coveralls": "^3.0.2",
"jest": "^23.6.0",
"lerna": "^3.8.5",
"prettier": "^1.15.3",
"ts-jest": "^23.10.5",
"tslint": "^5.12.0",
"tslint-config-prettier": "^1.17.0",
"tslint-react": "^3.6.0",
"tslint-react-a11y": "^1.0.0",
"typescript": "^3.2.2"
},
"private": true,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/__tests__/state-container.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
describe("StateContainer", () => {
it("should test", () => {
describe('StateContainer', () => {
it('should test', () => {
expect(true).toBe(true);
});
});
34 changes: 17 additions & 17 deletions packages/core/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { setScope } from "../utils";
import { setScope } from '../utils';

describe("utils", () => {
describe("setScope", () => {
it("should build correct object", () => {
describe('utils', () => {
describe('setScope', () => {
it('should build correct object', () => {
const state = {
level1: {
name: "Test"
}
name: 'Test',
},
};

const result = setScope(state, { name: "Another" }, "level1");
const result = setScope(state, { name: 'Another' }, 'level1');
expect(result).toEqual({
level1: {
name: "Another"
}
name: 'Another',
},
});
expect(result).not.toBe(state);
});

it("should build correct object with multiple levels", () => {
it('should build correct object with multiple levels', () => {
const state = {
level1: {
level2: {
name: "Test"
}
}
name: 'Test',
},
},
};

const result = setScope(state, { name: "Another" }, "level1.level2");
const result = setScope(state, { name: 'Another' }, 'level1.level2');
expect(result).toEqual({
level1: {
level2: {
name: "Another"
}
}
name: 'Another',
},
},
});
expect(result).not.toBe(state);
});
Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import staat from "./staat";
import { isPromise, getScope } from "./utils";
import staat from './staat';
import { isPromise, getScope } from './utils';
export {
Transformers,
Subscription,
Staat,
IType,
StateContainerType
} from "./types";
export * from "./scoped-transformer";
StateContainerType,
} from './types';
export * from './scoped-transformer';
export const internals = {
isPromise,
getScope
getScope,
};

export default staat;
6 changes: 3 additions & 3 deletions packages/core/src/scoped-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPromise, getScope, setScope } from "./utils";
import { isPromise, getScope, setScope } from './utils';

export function scopedTransformer<
TState extends Record<keyof TState, unknown>,
Expand All @@ -8,14 +8,14 @@ export function scopedTransformer<
definition: (
currentScope: TScope,
...args: TArgs
) => TScope | Promise<TScope>
) => TScope | Promise<TScope>,
) {
return function(currentState: TState, ...args: TArgs) {
const s = getScope<TState, TScope>(currentState, scope);
const result = definition(s, ...args);
if (isPromise(result)) {
return result.then(promiseResult =>
setScope({ ...currentState }, promiseResult, scope)
setScope({ ...currentState }, promiseResult, scope),
);
}
return setScope({ ...currentState }, result, scope);
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/staat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@ function addTransformers<
>(
obj: Record<string, any>,
transformers: TTransformers,
container: StateContainer<TState>
container: StateContainer<TState>,
) {
return Object.keys(transformers).reduce((obj, key) => {
return Object.keys(transformers).reduce((acc, key) => {
const current = transformers[key];
if (isTransformer(current)) {
obj[key] = function(...args: any[]) {
acc[key] = function(...args: any[]) {
const state = container.getState();
const result = current(state, ...args);
if (isPromise(result)) {
return result.then(state => container.setState(state));
return result.then(s => container.setState(s));
}
return container.setState(result);
};
} else {
obj[key] = addTransformers({}, current, container);
acc[key] = addTransformers({}, current, container);
}

return obj;
return acc;
}, obj);
}

function initializeObject<TState>(
container: StateContainer<TState>
container: StateContainer<TState>,
): StateContainerType<TState> {
const obj: Partial<StateContainerType<TState>> = {};

Object.defineProperty(obj, 'currentState', {
get: () => container.getState()
get: () => container.getState(),
});
obj.subscribe = container.subscribe.bind(container);
obj.unsubscribe = container.unsubscribe.bind(container);
Expand All @@ -44,7 +44,7 @@ function initializeObject<TState>(

export default function staat<TState, TTransformers extends {}>(
transformers: TTransformers,
initialState: TState
initialState: TState,
): Staat<TState, TTransformers> {
const container = new StateContainer(initialState);
const obj = initializeObject(container);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/time-travel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { applyChange, diff } from "deep-diff";
import { StateContainer } from "./state-container";
import { applyChange, diff } from 'deep-diff';
import { StateContainer } from './state-container';

export class TimeTravelContainer<T> extends StateContainer<T> {
private pastDiffs: deepDiff.IDiff[][];
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export interface IType<T> extends Function {
new (...args: any[]): T;
}
export type IType<T> = new (...args: any[]) => T;

export type Transformers<TTransformers extends {}> = {
[TKey in keyof TTransformers]: TTransformers[TKey] extends (
Expand Down
24 changes: 12 additions & 12 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { TransformerOrObject, TransformerSignature } from "./types";
import { TransformerOrObject, TransformerSignature } from './types';

export function isPromise<T>(obj: T | Promise<T>): obj is Promise<T> {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof (obj as Promise<T>).then === "function" &&
typeof (obj as Promise<T>).catch === "function"
(typeof obj === 'object' || typeof obj === 'function') &&
typeof (obj as Promise<T>).then === 'function' &&
typeof (obj as Promise<T>).catch === 'function'
);
}

export function isTransformer<TState>(
input: TransformerOrObject<TState>
input: TransformerOrObject<TState>,
): input is TransformerSignature<TState> {
return typeof input === "function";
return typeof input === 'function';
}

export function setProperty<TScope, TState extends Record<string, any>>(
state: TState,
path: string[],
value: TScope
value: TScope,
): TState {
const key = path.shift();
if (!key || !state[key!]) {
Expand All @@ -42,15 +42,15 @@ export function setProperty<TScope, TState extends Record<string, any>>(
export function setScope<TScope, TState extends Record<string, any>>(
state: TState,
scope: TScope,
path: string
path: string,
): TState {
const parts = path.split(".");
const parts = path.split('.');
return setProperty(state, parts, scope);
}

export function getProperty<TScope, TState extends Record<string, any>>(
state: TState,
path: string[]
path: string[],
): TScope {
const key = path.shift();
if (path.length === 0) {
Expand All @@ -61,8 +61,8 @@ export function getProperty<TScope, TState extends Record<string, any>>(

export function getScope<TState extends Record<string, any>, TScope>(
state: TState,
path: string
path: string,
): TScope {
const parts = path.split(".");
const parts = path.split('.');
return getProperty(state, parts);
}
10 changes: 5 additions & 5 deletions packages/example/src/calculator-state-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import { CalculatorState, AppState } from './types';
const transformer = scopedTransformer<AppState, CalculatorState>('calculator');

export const initialState: CalculatorState = {
count: 0
count: 0,
};

export const add = transformer(
(currentState: CalculatorState, val: number): CalculatorState => {
return {
...currentState,
count: currentState.count + val
count: currentState.count + val,
};
}
},
);

export const subtract = transformer(
(currentState: CalculatorState, val: number): CalculatorState => {
return {
...currentState,
count: currentState.count - val
count: currentState.count - val,
};
}
},
);
24 changes: 12 additions & 12 deletions packages/example/src/calculator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { calculator, connect } from "./state";
import * as React from 'react';
import { calculator as calculatorTransformers, connect } from './state';

const Calculator: React.StatelessComponent<CalculatorProps> = props => {
return (
Expand All @@ -19,10 +19,10 @@ type StateProps = {
};

type TransformerProps = {
add: typeof calculator.add;
subtract: typeof calculator.subtract;
undo: typeof calculator.undo;
redo: typeof calculator.redo;
add: typeof calculatorTransformers.add;
subtract: typeof calculatorTransformers.subtract;
undo: typeof calculatorTransformers.undo;
redo: typeof calculatorTransformers.redo;
};

type OwnProps = {};
Expand All @@ -32,15 +32,15 @@ type CalculatorProps = TransformerProps & StateProps & OwnProps;
export default connect<OwnProps, StateProps, TransformerProps>(
({ calculator }) => {
return {
count: calculator.count
count: calculator.count,
};
},
() => {
return {
add: calculator.add,
subtract: calculator.subtract,
undo: calculator.undo,
redo: calculator.redo
add: calculatorTransformers.add,
subtract: calculatorTransformers.subtract,
undo: calculatorTransformers.undo,
redo: calculatorTransformers.redo,
};
}
},
)(Calculator);
2 changes: 1 addition & 1 deletion packages/example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ ReactDOM.render(
<Welcome />
<Calculator />
</Provider>,
document.getElementById('entry')
document.getElementById('entry'),
);
4 changes: 2 additions & 2 deletions packages/example/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ const {

const initialState = {
calculator: calcInitialState,
welcome: welcomeInitialState
welcome: welcomeInitialState,
};

const transformers = {
calculator: timeTravel(calcTransformers, 'calculator'),
welcome: welcomeTransformers
welcome: welcomeTransformers,
};

export const appState = staat(transformers, initialState);
Expand Down
6 changes: 3 additions & 3 deletions packages/example/src/welcome-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ type WelcomeProps = StateProps & TrasformerProps & OwnProps;
export default connect<OwnProps, StateProps, TrasformerProps>(
state => {
return {
name: state.welcome.name
name: state.welcome.name,
};
},
() => {
return {
setName: welcome.setName
setName: welcome.setName,
// undo: welcome.undo.bind(welcomeState),
// redo: welcomeState.redo.bind(welcomeState)
};
}
},
)(Welcome);
2 changes: 1 addition & 1 deletion packages/example/src/welcome-state-definition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ const transformer = scopedTransformer<AppState, WelcomeState>('welcome');
export const setName = transformer(
(currentState: WelcomeState, name: string) => {
return { ...currentState, name };
}
},
);

0 comments on commit 9fb2b4f

Please sign in to comment.