-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
got typescript working and some of the most basic types and tests runnin
- Loading branch information
Thomas Chen
committed
May 17, 2017
1 parent
6a61ec7
commit bfb5b79
Showing
15 changed files
with
304 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface PureFunction<A,B> { | ||
(a: A): B | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Arrow, ArrowInstance } from './arrow'; | ||
import { MonadInstance } from './monad'; | ||
import { PureFunction } from './function'; | ||
|
||
export interface Kleisli<A, B, M extends MonadInstance<B> > extends Arrow<A,B> { | ||
runKleisli(k: KleisliInstance<A, B, M>, a: A): M; | ||
|
||
/** | ||
* Equivalent to ^>> from haskell | ||
* This lifts a pure function to an arrow | ||
* then appends it to an arrow. For example: | ||
* | ||
* arrow >>^ pureFun | ||
* | ||
* would be equivalent to | ||
* | ||
* appendPure(arrow, pureFun) | ||
* | ||
* Of course, because we aren't haskell, we can infix operators. | ||
* | ||
* Furthermore, we'd like to constraing the MonadInstance, but we | ||
* can't because no haskell typeclass... so just don't switch | ||
* monads mid-way through and we should be fine (lol) | ||
*/ | ||
appendPure<C, MC extends MonadInstance<C>>(k: KleisliInstance<A, B, M>, fn: PureFunction<B, C>): KleisliInstance<B, C, MC> | ||
|
||
prependPure<C, MC extends MonadInstance<C>>(fn: PureFunction<A, B>, k: KleisliInstance<B, C, MC>): KleisliInstance<A, B, M> | ||
} | ||
|
||
export interface KleisliInstance<A, B, MB extends MonadInstance<B>> extends ArrowInstance<A,B> { | ||
runKleisli(a: A): MB; | ||
appendPure<C, MC extends MonadInstance<C>>(fn: PureFunction<B, C>): KleisliInstance<B, C, MC> | ||
prependPure<Z>(fn: PureFunction<Z, A>): KleisliInstance<Z, B, MB> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
export interface Return<A,B> { | ||
(a: A): MonadInstance<B> | ||
} | ||
|
||
/** | ||
* The static Monad interface. We have to separate this from | ||
* the instance variation because lol javascript | ||
* | ||
* We also have to append M to everything to avoid accidental | ||
* name collisons with native functions like `bind` and `return` | ||
*/ | ||
export interface Monad<A,B> { | ||
/** | ||
* >>= | ||
*/ | ||
bindM(m: MonadInstance<A>, fn: Return<A,B>): MonadInstance<B> | ||
/** | ||
* >> | ||
*/ | ||
thruM(m1: MonadInstance<A>, m2: MonadInstance<B>): MonadInstance<B> | ||
/** | ||
* return is a keyword in javascript, so we append M | ||
*/ | ||
returnM(a: A): MonadInstance<B> | ||
/** | ||
* For completion reasons, we put this here, but I won't use it | ||
*/ | ||
failM(str: String): MonadInstance<A> | ||
} | ||
|
||
export interface MonadInstance<A> { | ||
bindM<B>(fn: Return<A,B>): MonadInstance<B> | ||
thruM<B>(m: MonadInstance<B>): MonadInstance<B> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,54 @@ | ||
type Action = any; | ||
type State = any; | ||
interface ActionState { | ||
action: Action; | ||
state: State; | ||
import { Kleisli, KleisliInstance } from './kleisli'; | ||
import { ActionState, ReduxState } from './redux'; | ||
import { MonadInstance } from './monad' | ||
|
||
class DepKeyState implements MonadInstance<ReduxState> { | ||
reduxState: ReduxState | ||
changedKeys: string[] | ||
|
||
constructor(reduxState, changedKeys=[]) { | ||
this.reduxState = reduxState; | ||
this.changedKeys = changedKeys | ||
} | ||
|
||
bindM(fn) { | ||
const { reduxState, changedKeys } = fn(this.reduxState); | ||
return new DepKeyState(reduxState, changedKeys); | ||
} | ||
thruM(fn) { | ||
return fn(this.reduxState); | ||
} | ||
} | ||
|
||
|
||
|
||
interface Transformer { | ||
(ks: KeyState): KeyState | ||
} | ||
class ReducerArrow extends ExtensibleFunction { | ||
|
||
interface Updater { | ||
(s: ReduxState, k: KeyState): ReduxState | ||
} | ||
class StaticReducerArrow implements Arrow<ActionState, State> { | ||
|
||
function objectAssign(reduxState, keyState) { | ||
return Object.keys(keyState).reduce((state, key) => Object.assign({}, ), reduxState); | ||
} | ||
|
||
class ReducerArrow implements KleisliInstance<ActionState, ReduxState, DepKeyState> { | ||
readingKeys: string[] | ||
writingKeys: string[] | ||
transformer: Transformer | ||
updater: Updater | ||
constructor(transformer, updater=objectAssign, readingKeys=[], writingKeys=[]) { | ||
|
||
} | ||
runKleisli(actionState) { | ||
|
||
} | ||
appendPure() { | ||
|
||
} | ||
prependPure() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type Action = any; | ||
export type ReduxState = any; | ||
|
||
export interface ActionState { | ||
action: Action; | ||
state: ReduxState; | ||
} | ||
|
||
export interface Reducer { | ||
(state: ReduxState, action: Action): ReduxState | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
export interface Hash { | ||
[propName: string]: any | ||
} | ||
|
||
/** | ||
* Similar to ember set, except copies instead of | ||
* mutates | ||
* @param obj | ||
* @param key | ||
* @param val | ||
*/ | ||
export function set(obj: Hash, keyString: string, val: any): Hash { | ||
const keys = parseKeys(keyString); | ||
return setCore(keys, val, obj); | ||
} | ||
|
||
function setCore(keys: string[], val: any, obj?: Hash): Hash { | ||
const hash = obj || {}; | ||
|
||
if (keys.length === 1) { | ||
const [key] = keys; | ||
return Object.assign({}, hash, { [key]: val }); | ||
} else if (keys.length > 1) { | ||
const [key, ...tailKeys] = keys; | ||
return Object.assign({}, obj, { | ||
[key]: setCore(tailKeys, val, hash[key]) | ||
}) | ||
} else { | ||
return hash; | ||
} | ||
} | ||
|
||
export function parseKeys(keyStr: string): string[] { | ||
return keyStr.split('.'); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { set } from '../src/utils'; | ||
|
||
describe('utils', () => { | ||
it('should be propertly imported', () => { | ||
expect(set).toBeInstanceOf(Function) | ||
}) | ||
|
||
describe('set', () => { | ||
const obj = { dog: 1 }; | ||
let obj2; | ||
beforeAll(() => { | ||
obj2 = set(obj, 'cat', 2) | ||
}) | ||
it('should immutably set to the object', () => { | ||
expect(obj2).not.toBe(obj) | ||
}) | ||
it('should not affect obj1', () => { | ||
expect(obj).toMatchObject({ dog: 1 }) | ||
}) | ||
it('should properly change obj2', () => { | ||
expect(obj2).toMatchObject({ dog: 1, cat: 2 }) | ||
}) | ||
|
||
describe('deep set', () => { | ||
let obj3; | ||
beforeAll(() => { | ||
obj3 = set(obj2, 'bird.plane.man', 44) | ||
}) | ||
it('should not alter obj1', () => { | ||
expect(obj).toMatchObject({ dog: 1 }) | ||
}) | ||
it('should not alter obj2 either', () => { | ||
expect(obj2).toMatchObject({ dog: 1, cat: 2 }) | ||
}) | ||
it('should properly create a deep object', () => { | ||
expect(obj3).toMatchObject({ | ||
dog: 1, | ||
cat: 2, | ||
bird: { | ||
plane: { | ||
man: 44 | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist/", | ||
"sourceMap": true, | ||
"strictNullChecks": true, | ||
"lib": ["es2017"], | ||
"module": "es6", | ||
"jsx": "react", | ||
"target": "es5", | ||
"allowJs": true | ||
}, | ||
"include": [ | ||
"./src/" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"extends": [ | ||
"tslint:recommended" | ||
], | ||
"jsRules": {}, | ||
"rules": {}, | ||
"rulesDirectory": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
// change to .tsx if necessary | ||
entry: './src/index.ts', | ||
output: { | ||
filename: './dist/index.js' | ||
}, | ||
resolve: { | ||
// changed from extensions: [".js", ".jsx"] | ||
extensions: [".ts", ".tsx", ".js", ".jsx"] | ||
}, | ||
module: { | ||
rules: [ | ||
// changed from { test: /\.jsx?$/, use: { loader: 'babel-loader' } }, | ||
{ test: /\.(t|j)sx?$/, use: { loader: 'awesome-typescript-loader' } }, | ||
// newline - add source-map support | ||
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" } | ||
] | ||
}, | ||
externals: { | ||
"react": "React", | ||
"react-dom": "ReactDOM", | ||
}, | ||
// newline - add source-map support | ||
devtool: "source-map" | ||
} |