Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit e0f95ae

Browse files
author
Tomasz Gałkowski
committed
initial commit, v0.1.0
0 parents  commit e0f95ae

30 files changed

+601
-0
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*.ts]
4+
indent_style = space
5+
indent_size = 4
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# editors
2+
.vscode
3+
.idea
4+
5+
# secrets
6+
.env

CODE_OF_CONDUCT.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as
6+
contributors and maintainers pledge to making participation in our project and
7+
our community a harassment-free experience for everyone, regardless of age, body
8+
size, disability, ethnicity, sex characteristics, gender identity and expression,
9+
level of experience, education, socio-economic status, nationality, personal
10+
appearance, race, religion, or sexual identity and orientation.
11+
12+
## Our Standards
13+
14+
Examples of behavior that contributes to creating a positive environment
15+
include:
16+
17+
* Using welcoming and inclusive language
18+
* Being respectful of differing viewpoints and experiences
19+
* Gracefully accepting constructive criticism
20+
* Focusing on what is best for the community
21+
* Showing empathy towards other community members
22+
23+
Examples of unacceptable behavior by participants include:
24+
25+
* The use of sexualized language or imagery and unwelcome sexual attention or
26+
advances
27+
* Trolling, insulting/derogatory comments, and personal or political attacks
28+
* Public or private harassment
29+
* Publishing others' private information, such as a physical or electronic
30+
address, without explicit permission
31+
* Other conduct which could reasonably be considered inappropriate in a
32+
professional setting
33+
34+
## Our Responsibilities
35+
36+
Project maintainers are responsible for clarifying the standards of acceptable
37+
behavior and are expected to take appropriate and fair corrective action in
38+
response to any instances of unacceptable behavior.
39+
40+
Project maintainers have the right and responsibility to remove, edit, or
41+
reject comments, commits, code, wiki edits, issues, and other contributions
42+
that are not aligned to this Code of Conduct, or to ban temporarily or
43+
permanently any contributor for other behaviors that they deem inappropriate,
44+
threatening, offensive, or harmful.
45+
46+
## Scope
47+
48+
This Code of Conduct applies both within project spaces and in public spaces
49+
when an individual is representing the project or its community. Examples of
50+
representing a project or community include using an official project e-mail
51+
address, posting via an official social media account, or acting as an appointed
52+
representative at an online or offline event. Representation of a project may be
53+
further defined and clarified by project maintainers.
54+
55+
## Enforcement
56+
57+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
58+
reported by contacting the project team at galkowski.t@gmail.com. All
59+
complaints will be reviewed and investigated and will result in a response that
60+
is deemed necessary and appropriate to the circumstances. The project team is
61+
obligated to maintain confidentiality with regard to the reporter of an incident.
62+
Further details of specific enforcement policies may be posted separately.
63+
64+
Project maintainers who do not follow or enforce the Code of Conduct in good
65+
faith may face temporary or permanent repercussions as determined by other
66+
members of the project's leadership.
67+
68+
## Attribution
69+
70+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71+
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72+
73+
[homepage]: https://www.contributor-covenant.org
74+
75+
For answers to common questions about this code of conduct, see
76+
https://www.contributor-covenant.org/faq

CONTRIBUTING.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Contributing
2+
Thank you for thinking about contributing to denofun!
3+
When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method with the owners of this repository before making a change.
4+
5+
Please note we have a code of conduct, please follow it in all your interactions with the project.
6+
7+
## Pull Request Process
8+
1. You Pull Request has to be zero-dependency excluding deno_std.
9+
2. Update README.md with required documenation.
10+
3. Update CHANGELOG.md with your proposed changes.
11+
4. Increase the version numbers in any examples files and the README.md to the new version that this Pull Request would represent. The versioning scheme we use is SemVer.
12+
5. Your PR will be reviewed and merged by the repository's owner.
13+
14+
Thanks!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Tomasz Gałkowski
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test:
2+
deno --importmap=src/import_map.json src/test.ts

README.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# denofun
2+
Small utility library for [Deno](https://deno.land) containing functions, monads and other fun stuff.
3+
4+
<image src="https://media.giphy.com/media/XOXdQszYm4I3m/giphy.gif" width="480" height="360">
5+
6+
## Documentation
7+
Code samples assume [import map](https://deno.land/manual.html#importmaps) is configured like this (URL is still WIP, `denofun` like Deno is still under heavy development).
8+
9+
```
10+
{
11+
"imports": {
12+
"std/": "https://deno.land/std/",
13+
"denofun/": "https://raw.githubusercontent.com/galkowskit/denofun/master/"
14+
}
15+
}
16+
```
17+
18+
### compose
19+
20+
```typescript
21+
import compose from "denofun/lib/compose.ts";
22+
23+
function greet(name: string) {
24+
return `hello, ${name}!`;
25+
}
26+
27+
function makeLoud(x) {
28+
return x.toUpperCase();
29+
}
30+
31+
const greetLoudly = compose(makeLoud, greet);
32+
33+
greetLoudly('world'); // => HELLO, WORLD!
34+
```
35+
36+
### curry
37+
38+
```typescript
39+
import curry from "denofun/lib/curry.ts";
40+
41+
const greet = (name: string, age: number) => `hello, I'm Tomasz and I'm 26 years old`;
42+
greet("Tomasz", 26); // => hello, I'm Tomasz and I'm 26 years old
43+
44+
const curriedGreet = curry(greet);
45+
curriedGreet("Tomasz")(26) // => hello, I'm Tomasz and I'm 26 years old
46+
47+
// ===
48+
49+
const cars = [
50+
{ make: 'Alfa Romeo', model: 'Giulia' },
51+
{ make: 'Alfa Romeo', model: 'Stelvio' },
52+
{ make: 'Ford', model: 'Mustang '},
53+
{ make: 'Ford', model: 'Focus '},
54+
{ make: 'Toyota', model: 'Mirai' },
55+
{ make: 'Toyota', model: 'Yaris' },
56+
{ make: 'Toyota', model: 'Supra' },
57+
];
58+
59+
const filterCarsByMake = curry((make: string, car) => car.make === make);
60+
const filterAlfas = filterCarsByMake('Alfa Romeo');
61+
cars.filter(filterAlfas); // => [ { make: "Alfa Romeo", model: "Giulia" }, { make: "Alfa Romeo", model: "Stelvio" } ]
62+
```
63+
64+
### filter
65+
66+
```typescript
67+
import filter from "denofun/lib/filter.ts";
68+
69+
const cars = [
70+
{ make: 'Alfa Romeo', model: 'Giulia' },
71+
{ make: 'Alfa Romeo', model: 'Stelvio' },
72+
{ make: 'Ford', model: 'Mustang '},
73+
{ make: 'Ford', model: 'Focus '},
74+
{ make: 'Toyota', model: 'Mirai' },
75+
{ make: 'Toyota', model: 'Yaris' },
76+
{ make: 'Toyota', model: 'Supra' },
77+
];
78+
79+
filter(car => car.make === 'Ford', cars); // => [ { make: "Ford", model: "Mustang " }, { make: "Ford", model: "Focus " } ]
80+
```
81+
82+
### head
83+
84+
```typescript
85+
import head from "denofun/lib/head.ts";
86+
87+
const numbers = [1, 2, 3, 4, 5];
88+
head(numbers); // => 1
89+
90+
// ===
91+
92+
head("hello world!"); // => "h"
93+
```
94+
95+
### identity
96+
97+
```typescript
98+
import identity from "denofun/lib/identity.ts";
99+
100+
const x = 5;
101+
identity(x); // => 5
102+
```
103+
104+
### map
105+
106+
```typescript
107+
import map from "denofun/lib/map.ts";
108+
109+
const numbers = [1, 2, 3, 4, 5];
110+
111+
map(n => n * 2, numbers); // => [2, 4, 6, 8, 10]
112+
```
113+
114+
### pipe
115+
116+
```typescript
117+
import pipe from "denofun/lib/pipe.ts";
118+
119+
function greet(name: string) {
120+
return `hello, ${name}!`;
121+
}
122+
123+
function makeLoud(x) {
124+
return x.toUpperCase();
125+
}
126+
127+
const greetLoudly = pipe(greet, makeLoud);
128+
129+
greetLoudly('world'); // => HELLO, WORLD!
130+
```
131+
132+
### reduce
133+
134+
```typescript
135+
import reduce from "denofun/lib/reduce.ts";
136+
137+
const numbers = [1, 2, 3, 4, 5];
138+
139+
const add = (a, b) => a + b;
140+
reduce(add, 0, numbers); // => 15
141+
```
142+
143+
### reverse
144+
145+
```typescript
146+
import reverse from "denofun/lib/reverse.ts";
147+
148+
reverse([1, 2, 3, 4, 5]); // => [5, 4, 3, 2, 1]
149+
150+
// ===
151+
152+
reverse("hello world!"); // => ["!dlrow olleh"]
153+
```
154+
155+
### tail
156+
157+
```typescript
158+
import tail from "denofun/lib/tail.ts";
159+
160+
tail([1, 2, 3, 4, 5]); // => [2, 3, 4, 5]
161+
162+
// ===
163+
164+
tail("hello world!"); // => "ello world!"
165+
```
166+
167+
## Goals
168+
- provide a good replacement for libraries like Ramda or Lodash with "native" Deno feel
169+
- provide basic monads (Either, Maybe/Option)
170+
- good, out-of-the-box typings
171+
172+
## Non-Goals
173+
- get integrated into `deno_std` as `fun` module
174+
- provide more monads (Future, Stream)

lib/compose.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { test } from "https://deno.land/std/testing/mod.ts";
2+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3+
4+
import compose from './compose.ts';
5+
6+
function add5(n) {
7+
return n + 5;
8+
}
9+
10+
function mul2(n) {
11+
return n * 2;
12+
}
13+
14+
function sub(a, b) {
15+
return a - b;
16+
}
17+
18+
const add5mul2 = compose(mul2, add5);
19+
const mul2sub = compose(mul2, sub);
20+
21+
test({
22+
name: "compose",
23+
fn(): void {
24+
assertEquals(add5mul2(5), 20);
25+
assertEquals(add5mul2(2), 14);
26+
assertEquals(mul2sub(2, 5), -6);
27+
}
28+
})

lib/compose.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import curry from "./curry.ts";
2+
3+
function compose(...fns) {
4+
return fns.reduceRight((f, g) => (...args) => g(f(...args)));
5+
}
6+
7+
export default curry(compose);

lib/curry.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { test } from "https://deno.land/std/testing/mod.ts";
2+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
3+
4+
import curry from './curry.ts';
5+
6+
function add(a, b) {
7+
return a + b;
8+
}
9+
10+
const curriedAdd = curry(add);
11+
const add5 = curriedAdd(5);
12+
const add5and10 = curriedAdd(5, 10);
13+
14+
test({
15+
name: "curry",
16+
fn(): void {
17+
assertEquals(typeof curry(add), 'function');
18+
assertEquals(typeof curriedAdd(5), 'function');
19+
assertEquals(add5(2), 7);
20+
assertEquals(add5and10, 15);
21+
}
22+
})

0 commit comments

Comments
 (0)