Skip to content

Commit

Permalink
Improve repository structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zoontek committed Mar 21, 2022
1 parent a8ed996 commit 5fc635e
Show file tree
Hide file tree
Showing 34 changed files with 3,289 additions and 238 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
charset = utf-8
indent_size = 2
trim_trailing_whitespace = true
indent_style = space
insert_final_newline = true
22 changes: 16 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/dist

# misc
.DS_Store
.DS_Store?
node_modules/
src/**/*.js
src/**/*.d.ts
benchmark/**/*.js
benchmark/**/*.d.ts

npm-debug.log*
yarn-debug.log*
yarn-error.log*
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Swan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 0 additions & 7 deletions MIT-LICENSE

This file was deleted.

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# @swan-io/boxed

[![mit licence](https://img.shields.io/dub/l/vibe-d.svg?style=for-the-badge)](https://github.com/swan-io/boxed/blob/main/LICENSE)
[![npm version](https://img.shields.io/npm/v/@swan-io/boxed?style=for-the-badge)](https://www.npmjs.org/package/@swan-io/boxed)
[![bundlephobia](https://img.shields.io/bundlephobia/minzip/@swan-io/boxed?label=size&style=for-the-badge)](https://bundlephobia.com/result?p=@swan-io/boxed)

> Utility types for functional TypeScript
**Boxed** provides functional utility types and functions, while focusing on ease-of-use.
Expand All @@ -21,7 +25,7 @@
- `Lazy<Value>`
- Some utils like `Deferred`, `Dict` & `Array`

## Install
## Installation

```console
$ yarn add @swan-io/boxed
Expand All @@ -35,4 +39,4 @@ $ npm install --save @swan-io/boxed

## [Documentation](https://swan-io.github.io/boxed)

## [License](./MIT-LICENSE)
## [License](./LICENSE)
22 changes: 11 additions & 11 deletions src/__tests__/Array.test.ts → __tests__/Array.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import { test, expect } from "vitest";
import { binarySearchBy, getBy, getIndexBy, keepMap } from "../Array";
import { Option } from "../Option";
import { expect, test } from "vitest";
import { binarySearchBy, getBy, getIndexBy, keepMap } from "../src/Array";
import { Option } from "../src/Option";

test("Array.keepMap", () => {
expect(
keepMap([1, 2, 3, 4], (a: number) => (a % 2 === 0 ? a * 2 : null))
keepMap([1, 2, 3, 4], (a: number) => (a % 2 === 0 ? a * 2 : null)),
).toEqual([4, 8]);

expect(
keepMap([1, 2, 3, 4], (a: number) => (a % 2 === 0 ? a * 2 : undefined))
keepMap([1, 2, 3, 4], (a: number) => (a % 2 === 0 ? a * 2 : undefined)),
).toEqual([4, 8]);

expect(
keepMap([1, 2, 3, 4, undefined, null], (a) => (a != null ? a : null))
keepMap([1, 2, 3, 4, undefined, null], (a) => (a != null ? a : null)),
).toEqual([1, 2, 3, 4]);
});

test("Array.getBy", () => {
expect(getBy([1, undefined, 2], (a) => a === undefined)).toEqual(
Option.Some(undefined)
Option.Some(undefined),
);

expect(getBy([1, 2, 3], (a) => a > 4)).toEqual(Option.None());

expect(getBy([1, undefined, 2], (a) => a === 1)).toEqual(Option.Some(1));

expect(
getBy([Option.Some(1), Option.None(), Option.Some(2)], (x) => x.isNone())
getBy([Option.Some(1), Option.None(), Option.Some(2)], (x) => x.isNone()),
).toEqual(Option.Some(Option.None()));
});

test("Array.getIndexBy", () => {
expect(getIndexBy([1, undefined, 2], (a) => a === undefined)).toEqual(
Option.Some(1)
Option.Some(1),
);

expect(getIndexBy([1, undefined, 2], (a) => a === 1)).toEqual(Option.Some(0));

expect(
getIndexBy([Option.Some(1), Option.None(), Option.Some(2)], (x) =>
x.isNone()
)
x.isNone(),
),
).toEqual(Option.Some(1));
});

Expand Down
52 changes: 28 additions & 24 deletions src/__tests__/AsyncData.test.ts → __tests__/AsyncData.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from "vitest";
import { AsyncData } from "../AsyncData";
import { Option } from "../Option";
import { expect, test } from "vitest";
import { AsyncData } from "../src/AsyncData";
import { Option } from "../src/Option";

test("AsyncData.is{Done|Loading|NotAsked}", () => {
expect(AsyncData.Done(1).isDone()).toBeTruthy();
Expand All @@ -18,29 +18,29 @@ test("AsyncData.is{Done|Loading|NotAsked}", () => {

test("AsyncData.map", () => {
expect(AsyncData.NotAsked<number>().map((x) => x * 2)).toEqual(
AsyncData.NotAsked()
AsyncData.NotAsked(),
);
expect(AsyncData.Loading<number>().map((x) => x * 2)).toEqual(
AsyncData.Loading()
AsyncData.Loading(),
);
expect(AsyncData.Done(5).map((x) => x * 2)).toEqual(AsyncData.Done(10));
});

test("AsyncData.flatMap", () => {
expect(
AsyncData.NotAsked<number>().flatMap((x) => AsyncData.Done(x * 2))
AsyncData.NotAsked<number>().flatMap((x) => AsyncData.Done(x * 2)),
).toEqual(AsyncData.NotAsked());
expect(
AsyncData.Loading<number>().flatMap((x) => AsyncData.Done(x * 2))
AsyncData.Loading<number>().flatMap((x) => AsyncData.Done(x * 2)),
).toEqual(AsyncData.Loading());
expect(AsyncData.Done(5).flatMap((x) => AsyncData.Done(x * 2))).toEqual(
AsyncData.Done(10)
AsyncData.Done(10),
);
expect(AsyncData.Done(5).flatMap((x) => AsyncData.NotAsked())).toEqual(
AsyncData.NotAsked()
AsyncData.NotAsked(),
);
expect(AsyncData.Done(5).flatMap((x) => AsyncData.Loading())).toEqual(
AsyncData.Loading()
AsyncData.Loading(),
);
});

Expand Down Expand Up @@ -79,48 +79,52 @@ test("AsyncData.equals", () => {
AsyncData.equals(
AsyncData.NotAsked(),
AsyncData.NotAsked(),
(a, b) => a === b
)
(a, b) => a === b,
),
).toBe(true);
expect(
AsyncData.equals(
AsyncData.NotAsked(),
AsyncData.Loading(),
(a, b) => a === b
)
(a, b) => a === b,
),
).toBe(false);
expect(
AsyncData.equals(AsyncData.Done(1), AsyncData.Loading(), (a, b) => a === b)
AsyncData.equals(AsyncData.Done(1), AsyncData.Loading(), (a, b) => a === b),
).toBe(false);
expect(
AsyncData.equals(AsyncData.Done(1), AsyncData.NotAsked(), (a, b) => a === b)
AsyncData.equals(
AsyncData.Done(1),
AsyncData.NotAsked(),
(a, b) => a === b,
),
).toBe(false);
expect(
AsyncData.equals(
AsyncData.Loading(),
AsyncData.Loading(),
(a, b) => a === b
)
(a, b) => a === b,
),
).toBe(true);
expect(
AsyncData.equals(AsyncData.Done(1), AsyncData.Done(1), (a, b) => a === b)
AsyncData.equals(AsyncData.Done(1), AsyncData.Done(1), (a, b) => a === b),
).toBe(true);
expect(
AsyncData.equals(AsyncData.Done(1), AsyncData.Done(2), (a, b) => a === b)
AsyncData.equals(AsyncData.Done(1), AsyncData.Done(2), (a, b) => a === b),
).toBe(false);
expect(
AsyncData.equals(
AsyncData.Done(1),
AsyncData.Done(2),
(a, b) => Math.abs(a - b) < 2
)
(a, b) => Math.abs(a - b) < 2,
),
).toBe(true);
expect(
AsyncData.equals(
AsyncData.Done(1),
AsyncData.Done(6),
(a, b) => Math.abs(a - b) < 2
)
(a, b) => Math.abs(a - b) < 2,
),
).toBe(false);
});

Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/Deferred.test.ts → __tests__/Deferred.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "vitest";
import { Deferred } from "../Deferred";
import { expect, test } from "vitest";
import { Deferred } from "../src/Deferred";

test("Deferred", async () => {
const [future, resolve] = Deferred.make();
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/Dict.test.ts → __tests__/Dict.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "vitest";
import { entries, keys, values } from "../Dict";
import { expect, test } from "vitest";
import { entries, keys, values } from "../src/Dict";

test("Dict.entries", () => {
expect(entries({ foo: 1, bar: 2 })).toEqual([
Expand Down
24 changes: 12 additions & 12 deletions src/__tests__/Future.test.ts → __tests__/Future.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from "vitest";
import { Future } from "../Future";
import { Result } from "../Result";
import { expect, test } from "vitest";
import { Future } from "../src/Future";
import { Result } from "../src/Result";

test("Future make value", async () => {
const value = await Future.value(1);
Expand Down Expand Up @@ -81,7 +81,7 @@ test("Future mapOk error", async () => {

test("Future mapError", async () => {
const result = await Future.value(Result.Error("one")).mapError(
(x) => `${x}!`
(x) => `${x}!`,
);
expect(result).toEqual(Result.Error("one!"));
});
Expand All @@ -93,42 +93,42 @@ test("Future mapError ok", async () => {

test("Future mapResult", async () => {
const result = await Future.value(Result.Ok("one")).mapResult((x) =>
Result.Ok(`${x}!`)
Result.Ok(`${x}!`),
);
expect(result).toEqual(Result.Ok("one!"));
});

test("Future mapResult error", async () => {
const result = await Future.value(Result.Error("one")).mapResult((x) =>
Result.Ok(`${x}!`)
Result.Ok(`${x}!`),
);
expect(result).toEqual(Result.Error("one"));
});

test("Future flatMapOk", async () => {
const result = await Future.value(Result.Ok("one")).flatMapOk((x) =>
Future.value(Result.Ok(`${x}!`))
Future.value(Result.Ok(`${x}!`)),
);
expect(result).toEqual(Result.Ok("one!"));
});

test("Future flatMapOk error", async () => {
const result = await Future.value(Result.Error("one")).flatMapOk((x) =>
Future.value(Result.Ok(`${x}!`))
Future.value(Result.Ok(`${x}!`)),
);
expect(result).toEqual(Result.Error("one"));
});

test("Future flatMapError", async () => {
const result = await Future.value(Result.Error("one")).flatMapError((x) =>
Future.value(Result.Error(`${x}!`))
Future.value(Result.Error(`${x}!`)),
);
expect(result).toEqual(Result.Error("one!"));
});

test("Future flatMapError ok", async () => {
const result = await Future.value(Result.Ok("one")).flatMapError((x) =>
Future.value(Result.Ok(`${x}!`))
Future.value(Result.Ok(`${x}!`)),
);
expect(result).toEqual(Result.Ok("one"));
});
Expand Down Expand Up @@ -346,14 +346,14 @@ test("Future doesn't consider flatMap returned as dependents", async () => {

test("Future fromPromise", async () => {
const value = await Future.fromPromise(Promise.resolve("one")).mapOk(
(value) => `${value}!`
(value) => `${value}!`,
);
expect(value).toEqual(Result.Ok("one!"));
});

test("Future fromPromise", async () => {
const value = await Future.fromPromise(Promise.reject("one")).mapError(
(value) => `${value}!`
(value) => `${value}!`,
);
expect(value).toEqual(Result.Error("one!"));
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/Lazy.test.ts → __tests__/Lazy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test, expect } from "vitest";
import { Lazy } from "../Lazy";
import { expect, test } from "vitest";
import { Lazy } from "../src/Lazy";

test("Lazy doesn't compute until access", () => {
const lazy = Lazy(() => {
Expand Down
Loading

0 comments on commit 5fc635e

Please sign in to comment.