Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-53: Refactor project src into modules #54

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
#!/usr/bin/env bash

npx jest

#npx jest ./test/alt.ts
#npx jest ./test/compose.ts
#npx jest ./test/container.ts
#npx jest ./test/curry.ts
#npx jest ./test/either.ts
#npx jest ./test/empty.ts
#npx jest ./test/getPropOrElse.ts
#npx jest ./test/io.ts
#npx jest ./test/maybe.ts
#npx jest ./test/partial.ts
#npx jest ./test/pipe.ts
#npx jest ./test/isEmpty.ts
#npx jest ./test/isBoolean.ts
#npx jest ./test/lift.ts
#npx jest ./test/seq.ts
#npx jest ./test/notEmpty.ts
#npx jest ./test/utils.ts
npx jest --watchAll=false --testPathPattern=.*.test.*$
19 changes: 19 additions & 0 deletions src/alt/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import alt from '.';

describe('alt', () => {
test('left path', () =>
expect(
alt(
(x: number) => (x += 1),
x => (x -= 1)
)(0)
).toBe(1));

test('right path', () =>
expect(
alt(
(x: number) => x,
x => (x += 1)
)(0)
).toBe(1));
});
File renamed without changes.
32 changes: 16 additions & 16 deletions test/camelCase.test.ts → src/camelCase/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import camelCase from '../src/camelCase';
import camelCase from './';

test.each([
[ 'abc def', 'abcDef'],
[ 'abc-def', 'abcDef'],
[ 'abc_def', 'abcDef'],
[ 'abc!def', 'abcDef'],
[ 'abc@def', 'abcDef'],
[ 'abc%def', 'abcDef'],
[ 'abc^def', 'abcDef'],
[ 'abc&def', 'abcDef'],
[ 'abc*def', 'abcDef'],
[ 'abc(def', 'abcDef'],
[ 'abc)def', 'abcDef'],
[ 'a品cd!e f', 'a品cdEF'],
[ 'ն !w**sss', 'նWSss'],
[ '0 a s w', '0ASW'],
[ '0 a 1 w', '0A1W'],
['abc def', 'abcDef'],
['abc-def', 'abcDef'],
['abc_def', 'abcDef'],
['abc!def', 'abcDef'],
['abc@def', 'abcDef'],
['abc%def', 'abcDef'],
['abc^def', 'abcDef'],
['abc&def', 'abcDef'],
['abc*def', 'abcDef'],
['abc(def', 'abcDef'],
['abc)def', 'abcDef'],
['a品cd!e f', 'a品cdEF'],
['ն !w**sss', 'նWSss'],
['0 a s w', '0ASW'],
['0 a 1 w', '0A1W'],
['1abcd', '1Abcd'],
['11Abcd', '11Abcd'],
['abcd1', 'abcd1'],
Expand Down
7 changes: 4 additions & 3 deletions src/camelCase.ts → src/camelCase/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { anyWhitespace, nonAlphaUnicode, oneOrMoreDigits } from './regex';
import { anyWhitespace, nonAlphaUnicode, oneOrMoreDigits } from '../regex';

const camelCase: (val: string) => string = (val: string): string =>
val
.toLowerCase()
.split(oneOrMoreDigits).join(' ')
.split(oneOrMoreDigits)
.join(' ')
.replace(nonAlphaUnicode, ' ')
.replace(anyWhitespace, (match, p1) => p1.toUpperCase().trimStart())
.replace(anyWhitespace, (_, p1) => p1.toUpperCase().trimStart())
.trim();

export default camelCase;
6 changes: 0 additions & 6 deletions src/chainC.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/chainC/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import chainC from '.';
import Maybe from '../maybe';
// import Either from '../either';

test('chain a Maybe Just', () => {
expect(chainC(x => (x += 1), Maybe.just(10))).toBe(11);
});

test('chain a Maybe Nothing', () => {
expect(chainC(x => (x += 1), Maybe.nothing(void 0)).value).toBe(void 0);
});

// test('chain a either left', () => {
// expect(chainc(x => (x += 1), either.left(10))).tobe(11);
// });

// test('chain a either right', () => {
// expect(chainc(x => (x += 1), either.right(void 0)).value).tobe(void 0);
// });
11 changes: 11 additions & 0 deletions src/chainC/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Curry, Either, FuncSpreadable, Monadical } from '../';
import Maybe from '../maybe/typings';
import curry from '../curry';

type ChainC = Monadical<Either<any, any> | Maybe<any, any>> | Either<any, any> | Maybe<any, any>;

const chainC: Curry<[FuncSpreadable, ChainC], any> = curry(<T, V>(func: T, container: Monadical<V>) =>
container.chain(func)
);

export default chainC;
6 changes: 6 additions & 0 deletions src/chainC/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Monadical } from '..';

export type ChainC = {
<T, V>(func: T): (container: Monadical<V>) => any;
<T, V>(func: T, container: Monadical<V>): any;
};
116 changes: 0 additions & 116 deletions src/maybe.ts

This file was deleted.

121 changes: 121 additions & 0 deletions src/maybe/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import Maybe, { Nothing } from './';
import identity from '../identity';

describe('Maybe', () => {
test('Maybe Just.value', () => {
const x = Maybe.just(10);
expect(x.value).toBe(10);
});

test('Maybe Nothing.value', () => {
const x = Maybe.nothing(void 0);
expect(x.value).toBe(void 0);
});

test('Maybe.just value', async () => {
const val: number = 10;
const x = Maybe.of(val);
expect(x.isJust()).toBe(true);
expect(x.isNothing()).toBe(false);
expect(x.value).toBe(10);
expect(x.getOrElse('A default value')).toBe(10);

const { data } = await Promise.resolve({ data: 'Resolved value' }).then(identity);
const y = Maybe.nullable(data);
expect(y.isJust()).toBe(true);
expect(y.isNothing()).toBe(false);
expect(y.getOrElse('An error occurred')).toBe('Resolved value');
});

test('Maybe.just is chainable', () => {
const xa = Maybe.of(10);
expect(
xa
.map(x => x * x)
.map(x => x - 10)
.chain(x => x * 2)
).toBe(180);
});

test('Maybe.just is filterable', () => {
const xa = Maybe.of(10);

expect(
xa
.map(x => x * x)
.filter(x => x >= 90)
.map(x => x)
.chain(identity)
).toBe(100);

expect(
xa
.map(x => x * x)
.filter(x => x >= 90)
.getOrElse('An error occurred')
).toBe(100);

expect(
xa
.map(x => x * x)
.filter(x => x >= 90)
.filter(x => x >= 90)
.chain(identity)
).toBe(100);
});

test('Maybe.nothing value', async () => {
const x = Maybe.nothing();
expect(x.isJust()).toBe(false);
expect(x.isNothing()).toBe(true);
expect(x.value).toBe(void 0);

const { data } = await Promise.reject({ data: void 0 }).catch(identity);
const y = Maybe.nullable(data);
expect(y.isJust()).toBe(false);
expect(y.isNothing()).toBe(true);
expect(y.value).toBe(void 0);
});

test('Maybe.nothing is filterable', () => {
const xa = Maybe.nothing<number, void>();
const xVal = xa
.map(x => x * x)
.map(x => x - 10)
.filter(x => x >= 90);

expect(xVal.isNothing()).toBe(true);

const ya = Maybe.nothing<number, void>();
const yVal = ya
.map(x => x * x)
.map(x => x - 10)
.filter(x => x >= 90)
.chain(x => x * 2);

expect(yVal).not.toBe('A test');
});

test('Maybe.nothing is chainable', () => {
const xa = Maybe.nothing<number, void>();

const xVal = xa
.map(x => x * x)
.map(x => x - 10)
.chain(x => x * 2);

expect(xVal instanceof Nothing).toBe(true);
});

test('Maybe.nullable value', async () => {
expect(Maybe.nullable(void 0).value).toBe(void 0);

let x: string = 'a value';
x = await Promise.reject(void 0).catch(identity);
expect(
Maybe.nullable<string, void>(x)
.map(x => x.length.toString())
.map(x => x.split('')[0]).value
).toBe(void 0);
});
});
Loading