Skip to content

Commit d520652

Browse files
authored
release v0.12.0
* update vitepress * add docs for combined converter and update documentation for converters * update documentation * update package files * rename stringify to unify
1 parent 982f132 commit d520652

File tree

35 files changed

+1026
-527
lines changed

35 files changed

+1026
-527
lines changed

adapters/in-memory/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "@loom-io/in-memory-adapter",
3-
"version": "0.11.0",
3+
"version": "0.12.0",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",
77
"description": "A file system wrapper for Node.js and Bun",
88
"keywords": [
9+
"loom-io",
910
"in-memory",
10-
"adapter",
11-
"@loom-io"
11+
"adapter"
1212
],
1313
"files": [
1414
"dist"

adapters/minio/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "@loom-io/minio-s3-adapter",
3-
"version": "0.11.0",
3+
"version": "0.12.0",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",
77
"description": "A file system wrapper for Node.js and Bun",
88
"keywords": [
9+
"loom-io",
910
"adapter",
1011
"minio",
1112
"s3",
12-
"file",
13-
"@loom-io"
13+
"file"
1414
],
1515
"files": [
1616
"dist"

adapters/node-fs/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "@loom-io/node-filesystem-adapter",
3-
"version": "0.11.0",
3+
"version": "0.12.0",
44
"main": "dist/exports/lib.js",
55
"types": "dist/exports/lib.d.ts",
66
"author": "Cotton Coding",
77
"description": "A file system wrapper for Node.js and Bun",
88
"keywords": [
9+
"loom-io",
910
"fs",
1011
"filesystem",
1112
"node",
1213
"bun",
1314
"nodejs",
14-
"adapter",
15-
"@loom-io"
15+
"adapter"
1616
],
1717
"files": [
1818
"dist"
Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
1-
import { defineConfig } from 'vitepress'
1+
import { defineConfig } from "vitepress";
22

33
// https://vitepress.dev/reference/site-config
44
export default defineConfig({
5-
title: "loom-io",
6-
description: "weave your data access",
7-
lastUpdated: true,
8-
themeConfig: {
9-
logo: "/loom-io.png",
10-
// https://vitepress.dev/reference/default-theme-config
11-
nav: [
12-
{ text: 'Home', link: '/' },
13-
{ text: 'Docs', link: '/core/intro' }
14-
],
15-
sidebar: [
16-
{
17-
text: 'Getting Started',
18-
items: [
19-
{ text: 'Introduction', link: '/core/intro' },
20-
{ text: 'Setup', link: '/core/setup' },
21-
{ text: 'Examples', link: '/core/examples' }
22-
]
23-
},
24-
{
25-
text: 'Source Adapter',
26-
items: [
27-
{ text: 'In-Memory', link: '/adapter/in-memory-adapter' },
28-
{ text: 'Filesystem', link: '/adapter/node-filesystem-adapter' },
29-
{ text: 'S3-Minio', link: '/adapter/minio-s3-adapter' }
30-
]
31-
},
32-
{
33-
text: 'Converter',
34-
items: [
35-
{ text: 'Front Matter', link: '/converter/front-matter-converter' },
36-
{ text: 'JSON', link: '/converter/json-converter' },
37-
{ text: 'YAML', link: '/converter/yaml-converter' }
38-
]
39-
},
40-
{
41-
text: 'Reference',
42-
items: [
43-
{ text: 'File', link: '/ref/file' },
44-
{ text: 'Directory', link: '/ref/directory' },
45-
{ text: 'List', link: '/ref/list'},
46-
{ text: 'Editor', link: '/ref/editor' }
47-
]
48-
}
49-
],
5+
title: "loom-io",
6+
description: "weave your data access",
7+
lastUpdated: true,
8+
themeConfig: {
9+
logo: "/loom-io.png",
10+
// https://vitepress.dev/reference/default-theme-config
11+
nav: [
12+
{ text: "Home", link: "/" },
13+
{ text: "Docs", link: "/core/intro" },
14+
],
15+
sidebar: [
16+
{
17+
text: "Getting Started",
18+
items: [
19+
{ text: "Introduction", link: "/core/intro" },
20+
{ text: "Setup", link: "/core/setup" },
21+
{ text: "Examples", link: "/core/examples" },
22+
],
23+
},
24+
{
25+
text: "Source Adapter",
26+
items: [
27+
{ text: "In-Memory", link: "/adapter/in-memory-adapter" },
28+
{ text: "Filesystem", link: "/adapter/node-filesystem-adapter" },
29+
{ text: "S3-Minio", link: "/adapter/minio-s3-adapter" },
30+
],
31+
},
32+
{
33+
text: "Converter",
34+
items: [
35+
{ text: "Combined", link: "/converter/combined-converter" },
36+
{ text: "Front Matter", link: "/converter/front-matter-converter" },
37+
{ text: "JSON", link: "/converter/json-converter" },
38+
{ text: "YAML", link: "/converter/yaml-converter" },
39+
],
40+
},
41+
{
42+
text: "Reference",
43+
items: [
44+
{ text: "File", link: "/ref/file" },
45+
{ text: "Directory", link: "/ref/directory" },
46+
{ text: "List", link: "/ref/list" },
47+
{ text: "Editor", link: "/ref/editor" },
48+
],
49+
},
50+
],
5051

51-
socialLinks: [
52-
{ icon: 'github', link: 'https://github.com/cotton-coding/loom-io' }
53-
]
54-
}
55-
})
52+
socialLinks: [
53+
{ icon: "github", link: "https://github.com/cotton-coding/loom-io" },
54+
],
55+
},
56+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Combining Converters
2+
3+
Sometimes files content could be written in different formats like `json` and `yaml`, but the information is the same, so you will note care about the format and just have a json like object. `CombinedConverter`is doing this task for you. Combined with a LoomFile it directly reads the data from the file and convert it to `json`if one of the registered converter can handle the file type.
4+
5+
::: code-group
6+
7+
```sh [npm]
8+
npm add @loom-io/converter
9+
```
10+
11+
```sh [pnpm]
12+
pnpm add @loom-io/converter
13+
```
14+
15+
```sh [bun]
16+
bun add @loom-io/converter
17+
```
18+
19+
:::
20+
21+
## Usage
22+
23+
At least it works still as all other converter, but you need to register other converter to do the convert task.
24+
25+
```ts
26+
import { createCombinedConverter, NoValidFileConverterException } from '@loom-io/converter';
27+
import { createJsonConverter } from '@loom-io/json-converter';
28+
import { createYamlConverter } from '@loom-io/yaml-converter';
29+
import { MemoryAdapter } from '@loom-io/memory-adapter';
30+
31+
const adapter = new MemoryAdapter();
32+
const converter = createCombinedConverter([createJsonConverter(), createYamlConverter()], {
33+
failOnNoConverter: true // this is the default value.
34+
})
35+
36+
const file = adapter.file('some/random/file.yaml')
37+
38+
try {
39+
const json = await converter.parse(file);
40+
// do some stuff with the data ....
41+
} catch ( error ) {
42+
if(error instanceOf NoValidFileConverterException){
43+
console.log('do something');
44+
}
45+
throw error;
46+
}
47+
```
48+
49+
## Build a compatible converter
50+
51+
Converters have a very simple base structure, to be compatible they just need to full fill the `FileConverter` interface. Take a look a the code of the [json](https://github.com/cotton-coding/loom-io/blob/main/plugins/json-converter/src/json-converter.ts) or [yaml](https://github.com/cotton-coding/loom-io/blob/main/plugins/yaml-converter/src/yaml-converter.ts) converter to see some easy implementations.

documentation/converter/front-matter-converter.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: This converter allows you to read files such as markdown files with
55

66
# Front Matter Converter
77

8-
Add this converter to read files with a front matter part as json.
8+
Use this converter to read files with a front matter part as json.
99

1010
::: code-group
1111

@@ -25,33 +25,36 @@ bun add @loom-io/front-matter-converter
2525

2626
## Usage
2727

28-
When reading, the converter returns a file with the attributes `data` and `content`. The first is the json converted header part. The second is a string containing the content.
29-
30-
But before using it, you need to register the yaml converter
28+
When reading, the converter returns a file with the attributes `data` and `content`. The first is the json converted header part. The second is a string containing the content. The content will not be converted by default. By default the converter only reads markdown files (md), but any other file type could be read as well. Setting the option will overwrite the default value.
3129

3230
```ts
33-
import Loom from "@loom-io/core";
34-
import matterConverter from "@loom-io/front-matter-converter";
31+
import { createFrontMatterConverter } from "@loom-io/front-matter-converter";
3532

36-
Loom.register(matterConverter());
37-
// Other file types
38-
Loom.register(matterConverter({ extensions: ["md", "nyk"] }));
39-
```
33+
// at default it reads markdown files (md)
34+
const matterConverter = createFrontMatterConverter();
35+
// but every other file type could be set as well
36+
const nykMatterConverter = createFrontMatterConverter({
37+
extensions: ["nyk"]
38+
})
4039

41-
As seen in the example above, you can specify which file extension it should try to convert. If no value is given, it will only react to markdown files. This will be overwritten by the given value.
42-
If there is no front matter part, `data` will be undefined and if there is no content, `content` will be an empty string. When writing the stringify will analyse the given function and convert it to a file. If the given value has more than just a `data` or `content` key it will only be translated to a header. You can also set only one of the two keys.
40+
```
41+
If there is no front matter part, `data` will be undefined and if there is no content, `content` will be an empty string. When writing the unify function will analyse the given function and convert it to a file. If the given value has more than just a `data` or `content` key it will only be translated to a header. You can also set only one of the two keys.
4342

4443
```ts
45-
const mdFile = await Loom.file("memory://some/yaml/file.md");
46-
const fileData = await mdFile.json();
44+
const adapter = new MemoryAdapter();
45+
const mdFile = adapter.file("some/yaml/file.md");
46+
const converter = createFrontMatterConverter();
47+
const fileData = await converter.parse(mdFile);
4748
fileData.data.someValue = "test";
4849
fileData.content = `
4950
#Headline
5051
5152
some random markdown
5253
5354
`;
54-
mdFile.stringify(data);
55+
56+
converter.unify(mdFile, fileData);
57+
5558
```
5659

5760
Be careful when writing. A `undefined` value for content or data will still overwrite the section!

documentation/converter/json-converter.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,14 @@ bun add @loom-io/json-converter
2525

2626
## Usage
2727

28-
Before using it you need to register the json converter
28+
Under the hood the json converter fits the standard `JSON.parse` and `JSON.stringify` and just reads the data from file as text. So the benefits are mainly existing in combination with the [CombinedConverter](/converter/combined-converter)
2929

3030
```ts
31-
import Loom from "@loom-io/core";
32-
import jsonConverter from "@loom-io/json-converter";
31+
import { createJsonConverter } from "@loom-io/json-converter";
3332

34-
Loom.register(jsonConverter());
35-
```
36-
37-
This allows you to read and write json files
38-
39-
```ts
40-
const jsonFile = await Loom.file("memory://some/json/file.json");
41-
const data = await jsonFile.json();
33+
const jsonFile = adapter.file("some/yaml/file.json");
34+
const converter = createJsonConverter();
35+
const data = await converter.parse(jsonFile);
4236
data.val = "test";
43-
jsonFile.stringify(data);
37+
await converter.unify(jsonFile, data);
4438
```

documentation/converter/yaml-converter.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,14 @@ bun add @loom-io/yaml-converter
2525

2626
## Usage
2727

28-
Before using, you need to register the yaml converter
28+
You can read and write yaml files with the suffixes `yaml` adn `yml`.
2929

3030
```ts
31-
import Loom from "@loom-io/core";
32-
import yamlConverter from "@loom-io/yaml-converter";
31+
import { createYamlConverter } from "@loom-io/yaml-converter";
3332

34-
Loom.register(yamlConverter());
35-
```
36-
37-
You can read and write yaml files with the suffixes `yaml` adn `yml`
38-
39-
```ts
40-
const yamlFile = await Loom.file("memory://some/yaml/file.yaml");
41-
const data = await yamlFile.json();
33+
const yamlFile = adapter.file("some/yaml/file.yaml");
34+
const converter = createJsonConverter();
35+
const data = await converter.parse(yamlFile);
4236
data.val = "test";
43-
yamlFile.stringify(data);
44-
```
37+
await converter.unify(yamlFile, data);
38+
```

documentation/core/examples.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ Have a look at the specific reference to get all the details of the functionalit
1414
As show in the chapter before, do not forget to setup and config loom-io before usage.
1515

1616
```ts
17-
import Loom from "@loom-io/core";
18-
import fsAdapter from "@loom-io/node-filesystem-adapter";
17+
import FsAdapter from "@loom-io/node-filesystem-adapter";
1918

20-
// Now we add the filesystem adapter.
19+
// Now we create a instance of the filesystem adapter.
2120
// By default the root of our filesystem is your project directory
22-
Loom.register(fsAdapter("file://"));
21+
const adapter = new FsAdapter();
2322
```
2423

2524
The following examples will not show registering an adapter every time, as you will probably only need to do this once.
@@ -100,17 +99,19 @@ It can be annoying to think about the file format all the time when reading. Whe
10099
import MemoryAdapter from "@loom-io/in-memory-adapter"
101100
import jsonConverter from '@loom-io/json-converter';
102101
import yamlConverter from '@loom-io/yaml-converter':
102+
import { createCombinedConverter } from '@loom-io/converter'
103+
104+
105+
const converter = createCombinedConverter([jsonConverter(), yamlConverter()])
103106

104-
Loom.register(jsonConverter());
105-
Loom.register(yamlConverter());
106107

107108
const adapter = new MemoryAdapter()
108109

109110
const filePaths = ['file://test.yml', 'file://test.json'];
110111

111112
for(let filePath of filePaths) {
112113
const file = adapter.file(filePath);
113-
console.log(await file.json());
114+
console.log(await converter.parse(file));
114115
}
115116

116117
```

0 commit comments

Comments
 (0)