Skip to content

Commit c216e09

Browse files
Implement some File System methods
0 parents  commit c216e09

File tree

6 files changed

+1207
-0
lines changed

6 files changed

+1207
-0
lines changed

LICENSE

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

README.md

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
# Functional Deno IO
2+
3+
Deno IO methods as valid Task monads perfect to write great point-free software in JavaScript that is compatible with most modern browsers and Deno.
4+
5+
[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno&labelColor=black)](https://deno.land/x/[email protected])
6+
[![deno version](https://img.shields.io/badge/deno-^1.3.2-lightgrey?logo=deno)](https://github.com/denoland/deno)
7+
[![GitHub release](https://img.shields.io/github/v/release/sebastienfilion/functional-deno-io)](https://github.com/sebastienfilion/functional-deno-io/releases)
8+
[![GitHub licence](https://img.shields.io/github/license/sebastienfilion/functional-deno-io)](https://github.com/sebastienfilion/functional-deno-io/blob/v0.1.0/LICENSE)
9+
10+
* [File System](#file-system)
11+
* [Network](#network)
12+
13+
# Usage
14+
15+
This example uses the [Ramda library](https://ramdajs.com) - for simplification - but you should be able to use any library that implements
16+
the [Fantasy-land specifications](https://github.com/fantasyland/fantasy-land).
17+
18+
```js
19+
import { compose, chain, curry } from "https://x.nest.land/[email protected]/source/index.js";
20+
import Either from "https://deno.land/x/[email protected]/Either.js";
21+
import Task from "https://deno.land/x/[email protected]/Task.js";
22+
import { File } from "https://deno.land/x/[email protected]/types.js";
23+
import { close, copyFile, create } from "https://deno.land/x/[email protected]/fs.js";
24+
25+
const copyToNewFile = curry(
26+
(sourceFile, destinationFile) =>
27+
File.isOrThrow(sourceFile)
28+
&& File.isOrThrow(destinationFile)
29+
&& compose(
30+
chain(close),
31+
chain(copyFile(sourceFile)),
32+
create
33+
)(destinationFile)
34+
);
35+
36+
// Calling `copyToNewFile` results in an instance of `Task` keeping the function pure.
37+
assert(
38+
Task.is(copyToNewFile(File.fromPath(`${Deno.cwd()}/hoge`), File.fromPath(`${Deno.cwd()}/piyo`)))
39+
);
40+
41+
// Finally, calling `Task#run` will call copy to the new file and return a promise
42+
copyToNewFile(File.fromPath(`${Deno.cwd()}/hoge`), File.fromPath(`${Deno.cwd()}/piyo`)).run()
43+
.then(container => {
44+
// The returned value should be an instance of `Either.Right` or `Either.Left`
45+
assert(Either.Right.is(container));
46+
// Forcing to coerce the container to string will show that the final value is our File representation.
47+
assert(container.toString(), `Either.Right(File("${Deno.cwd()}/piyo", ...))`);
48+
});
49+
50+
// await copyToNewFile(sourceFile, destinationFile).run() === Either.Right(File a)
51+
```
52+
53+
## File System
54+
55+
### `chdir` [📕](https://doc.deno.land/builtin/stable#Deno.chdir)
56+
57+
Change the current working directory to the specified path.
58+
59+
`chdir :: Directory a -> Task e Directory a`
60+
61+
```js
62+
import { chdir } from "https://deno.land/x/[email protected]/fs.js";
63+
import { Directory } from "https://deno.land/x/[email protected]/types.js";
64+
import Task from "https://deno.land/x/[email protected]/Task.js";
65+
66+
const container = chdir(Directory(".."));
67+
68+
assert(Task.is(container));
69+
```
70+
71+
### `chmod` [📕](https://doc.deno.land/builtin/stable#Deno.chmod)
72+
73+
Changes the permission of a specific file/directory of specified path. Ignores the process's umask.
74+
75+
`chmod :: Number -> File a -> Task e File a`
76+
77+
```js
78+
import { chmod } from "https://deno.land/x/[email protected]/fs.js";
79+
import { File } from "https://deno.land/x/[email protected]/types.js";
80+
import Task from "https://deno.land/x/[email protected]/Task.js";
81+
82+
const container = chmod(0o000, File.fromPath(`${Deno.cwd()}/hoge`));
83+
84+
assert(Task.is(container));
85+
```
86+
87+
### `chown` [📕](https://doc.deno.land/builtin/stable#Deno.chown)
88+
89+
Change owner of a regular file or directory. This functionality is not available on Windows.
90+
91+
`chown :: Number -> Number -> File a -> Task e File a`
92+
93+
```js
94+
import { chown } from "https://deno.land/x/[email protected]/fs.js";
95+
import { File } from "https://deno.land/x/[email protected]/types.js";
96+
import Task from "https://deno.land/x/[email protected]/Task.js";
97+
98+
const container = chown(null, null, File.fromPath(`${Deno.cwd()}/hoge`));
99+
100+
assert(Task.is(container));
101+
```
102+
103+
### `close` [📕](https://doc.deno.land/builtin/stable#Deno.close)
104+
105+
Close the given resource which has been previously opened, such as via opening or creating a file.
106+
Closing a file when you are finished with it is important to avoid leaking resources.
107+
108+
`copy :: File a -> Task e File a`
109+
110+
```js
111+
import { close } from "https://deno.land/x/[email protected]/fs.js";
112+
import { File } from "https://deno.land/x/[email protected]/types.js";
113+
import Task from "https://deno.land/x/[email protected]/Task.js";
114+
115+
const container = close(File(`${Deno.cwd()}/hoge`, new Uint8Array([]), 3));
116+
117+
assert(Task.is(container));
118+
```
119+
120+
### `copy` [📕](https://doc.deno.land/builtin/stable#Deno.copy)
121+
122+
Copies from a source to a destination until either EOF (null) is read from the source, or an error occurs.
123+
124+
`copy :: Options -> Buffer a -> Buffer b -> Task e Writer b`
125+
126+
```js
127+
import { copy } from "https://deno.land/x/[email protected]/fs.js";
128+
import { Buffer } from "https://deno.land/x/[email protected]/types.js";
129+
import Task from "https://deno.land/x/[email protected]/Task.js";
130+
131+
const container = copy({}, Buffer(new Uint8Array([ 65, 66, 67, 68, 69 ])), Buffer(new Uint8Array([])));
132+
133+
assert(Task.is(container));
134+
```
135+
136+
### `copyFile` [📕](https://doc.deno.land/builtin/stable#Deno.copyFile)
137+
138+
Copies the contents and permissions of one file to another specified file, by default creating a new file if needed,
139+
else overwriting. Fails if target path is a directory or is unwritable.
140+
141+
`copyFile :: File a -> File b -> Task e File b`
142+
143+
```js
144+
import { copyFile } from "https://deno.land/x/[email protected]/fs.js";
145+
import { File } from "https://deno.land/x/[email protected]/types.js";
146+
import Task from "https://deno.land/x/[email protected]/Task.js";
147+
148+
const container = copyFile(File.fromPath(`${Deno.cwd()}/hoge`), File.fromPath(`${Deno.cwd()}/piyo`));
149+
150+
assert(Task.is(container));
151+
```
152+
153+
### `create` [📕](https://doc.deno.land/builtin/stable#Deno.create)
154+
155+
Creates a file if none exists or truncates an existing file.
156+
157+
`create :: File a -> Task e File a`
158+
159+
```js
160+
import { create } from "https://deno.land/x/[email protected]/fs.js";
161+
import { File } from "https://deno.land/x/[email protected]/types.js";
162+
import Task from "https://deno.land/x/[email protected]/Task.js";
163+
164+
const container = create(File.fromPath(`${Deno.cwd()}/hoge`));
165+
166+
assert(Task.is(container));
167+
```
168+
169+
### `cwd` [📕](https://doc.deno.land/builtin/stable#Deno.cwd)
170+
171+
Return a Directory representation of the current working directory.
172+
173+
`cwd :: () -> Task e Directory a`
174+
175+
```js
176+
import { cwd } from "https://deno.land/x/[email protected]/fs.js";
177+
import Task from "https://deno.land/x/[email protected]/Task.js";
178+
179+
const container = cwd();
180+
181+
assert(Task.is(container));
182+
```
183+
184+
### `emptyDir` [📕](https://deno.land/[email protected]/fs#emptydir)
185+
186+
Ensures that a directory is empty. Deletes directory contents if the directory is not empty.
187+
If the directory does not exist, it is created. The directory itself is not deleted.
188+
189+
`emptyDir :: Directory a -> Task e Directory a`
190+
191+
```js
192+
import { emptyDir } from "https://deno.land/x/[email protected]/fs.js";
193+
import { Directory } from "https://deno.land/x/[email protected]/types.js";
194+
import Task from "https://deno.land/x/[email protected]/Task.js";
195+
196+
const container = emptyDir(Directory(`${Deno.cwd()}/hoge`));
197+
198+
assert(Task.is(container));
199+
```
200+
201+
### `ensureDir` [📕](https://deno.land/[email protected]/fs#ensuredir)
202+
203+
Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`.
204+
205+
`ensureDir :: Directory a -> Task e Directory a`
206+
207+
```js
208+
import { ensureDir } from "https://deno.land/x/[email protected]/fs.js";
209+
import { Directory } from "https://deno.land/x/[email protected]/types.js";
210+
import Task from "https://deno.land/x/[email protected]/Task.js";
211+
212+
const container = emptyDir(Directory(`${Deno.cwd()}/hoge`));
213+
214+
assert(Task.is(container));
215+
```
216+
217+
### `exists` [📕](https://deno.land/[email protected]/fs#exists)
218+
219+
Test whether the given path exists by checking with the file system.
220+
If the file or directory doesn't exist, it will resolve to `Either.Left(null)`.
221+
222+
`exists :: File a|Directory a -> Task null File a|Directory a`
223+
224+
```js
225+
import { exists } from "https://deno.land/x/[email protected]/fs.js";
226+
import { Directory } from "https://deno.land/x/[email protected]/types.js";
227+
import Task from "https://deno.land/x/[email protected]/Task.js";
228+
229+
const container = exists(Directory(`${Deno.cwd()}/hoge`));
230+
231+
assert(Task.is(container));
232+
```
233+
234+
## Deno
235+
236+
This codebase uses [Deno](https://deno.land/#installation).
237+
238+
### MIT License
239+
240+
Copyright © 2020 - Sebastien Filion
241+
242+
Permission is hereby granted, free of charge, to any person obtaining a copy
243+
of this software and associated documentation files (the "Software"), to deal
244+
in the Software without restriction, including without limitation the rights
245+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
246+
copies of the Software, and to permit persons to whom the Software is
247+
furnished to do so, subject to the following conditions:
248+
249+
The above copyright notice and this permission notice shall be included in all
250+
copies or substantial portions of the Software.
251+
252+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
253+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
254+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
255+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
256+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
257+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
258+
SOFTWARE.

0 commit comments

Comments
 (0)